commit 56a8b34710e573ad006b3b9c0f9d5c13ea511075 Author: SeCherkasov Date: Thu Feb 19 06:57:09 2026 +0300 Initial commit: Rust calendar utility diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..7c88934 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,1335 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + +[[package]] +name = "assert_cmd" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c5bcfa8749ac45dd12cb11055aeeb6b27a3895560d60d71e3c23bf979e60514" +dependencies = [ + "anstyle", + "bstr", + "libc", + "predicates", + "predicates-core", + "predicates-tree", + "wait-timeout", +] + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "bitflags" +version = "2.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843867be96c8daad0d758b57df9392b6d8d271134fce549de6ce169ff98a92af" + +[[package]] +name = "bstr" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63044e1ae8e69f3b5a92c736ca6269b8d12fa7efe39bf34ddb06d102cf0e2cab" +dependencies = [ + "memchr", + "regex-automata", + "serde", +] + +[[package]] +name = "bumpalo" +version = "3.19.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510" + +[[package]] +name = "cal" +version = "0.1.0" +dependencies = [ + "assert_cmd", + "chrono", + "clap", + "libc", + "libloading", + "predicates", + "shellexpand", + "terminal_size", + "unicode-width", +] + +[[package]] +name = "cc" +version = "1.2.56" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aebf35691d1bfb0ac386a69bac2fde4dd276fb618cf8bf4f5318fe285e821bb2" +dependencies = [ + "find-msvc-tools", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "chrono" +version = "0.4.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fac4744fb15ae8337dc853fee7fb3f4e48c0fbaa23d0afe49c447b4fab126118" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-traits", + "pure-rust-locales", + "wasm-bindgen", + "windows-link", +] + +[[package]] +name = "clap" +version = "4.5.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c5caf74d17c3aec5495110c34cc3f78644bfa89af6c8993ed4de2790e49b6499" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.59" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "370daa45065b80218950227371916a1633217ae42b2715b2287b606dcd618e24" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a92793da1a46a5f2a02a6f4c46c6496b28c43638adea8306fcb0caa1634f24e5" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a822ea5bc7590f9d40f1ba12c0dc3c2760f3482c6984db1573ad11031420831" + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "crc32fast" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "difflib" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" + +[[package]] +name = "dirs" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e8aa94d75141228480295a7d0e7feb620b1a5ad9f12bc40be62411e38cce4e" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys 0.61.2", +] + +[[package]] +name = "displaydoc" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "find-msvc-tools" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" + +[[package]] +name = "flate2" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843fba2746e448b37e26a819579957415c8cef339bf08564fe8b7ddbd959573c" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + +[[package]] +name = "float-cmp" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b09cf3155332e944990140d967ff5eceb70df778b34f77d8075db46e4704e6d8" +dependencies = [ + "num-traits", +] + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "holiday_highlighter" +version = "0.1.0" +dependencies = [ + "chrono", + "libc", + "ureq", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "icu_collections" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c6b649701667bbe825c3b7e6388cb521c23d88644678e83c0c4d0a621a34b43" +dependencies = [ + "displaydoc", + "potential_utf", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edba7861004dd3714265b4db54a3c390e880ab658fec5f7db895fae2046b5bb6" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f6c8828b67bf8908d82127b2054ea1b4427ff0230ee9141c54251934ab1b599" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7aedcccd01fc5fe81e6b489c15b247b8b0690feb23304303a9e560f37efc560a" + +[[package]] +name = "icu_properties" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "020bfc02fe870ec3a66d93e677ccca0562506e5872c650f893269e08615d74ec" +dependencies = [ + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "616c294cf8d725c6afcd8f55abc17c56464ef6211f9ed59cccffe534129c77af" + +[[package]] +name = "icu_provider" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85962cf0ce02e1e0a629cc34e7ca3e373ce20dda4c4d7294bbd0bf1fdb59e614" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acae9609540aa318d1bc588455225fb2085b9ed0c4f6bd0d9d5bcd86f1a0344" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "js-sys" +version = "0.3.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c942ebf8e95485ca0d52d97da7c5a2c387d0e7f0ba4c35e93bfcaee045955b3" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.182" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112" + +[[package]] +name = "libloading" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55" +dependencies = [ + "cfg-if", + "windows-link", +] + +[[package]] +name = "libredox" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d0b95e02c851351f877147b7deea7b1afb1df71b63aa5f8270716e0c5720616" +dependencies = [ + "bitflags", + "libc", +] + +[[package]] +name = "linux-raw-sys" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039" + +[[package]] +name = "litemap" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6373607a59f0be73a39b6fe456b8192fcc3585f602af20751600e974dd455e77" + +[[package]] +name = "log" +version = "0.4.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" + +[[package]] +name = "memchr" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" + +[[package]] +name = "miniz_oxide" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "normalize-line-endings" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "potential_utf" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b73949432f5e2a09657003c25bca5e19a0e9c84f8058ca374f49e0ebe605af77" +dependencies = [ + "zerovec", +] + +[[package]] +name = "predicates" +version = "3.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ada8f2932f28a27ee7b70dd6c1c39ea0675c55a36879ab92f3a715eaa1e63cfe" +dependencies = [ + "anstyle", + "difflib", + "float-cmp", + "normalize-line-endings", + "predicates-core", + "regex", +] + +[[package]] +name = "predicates-core" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cad38746f3166b4031b1a0d39ad9f954dd291e7854fcc0eed52ee41a0b50d144" + +[[package]] +name = "predicates-tree" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0de1b847b39c8131db0467e9df1ff60e6d0562ab8e9a16e568ad0fdb372e2f2" +dependencies = [ + "predicates-core", + "termtree", +] + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "pure-rust-locales" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "869675ad2d7541aea90c6d88c81f46a7f4ea9af8cd0395d38f11a95126998a0d" + +[[package]] +name = "quote" +version = "1.0.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "redox_users" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4e608c6638b9c18977b00b475ac1f28d14e84b27d8d42f70e0bf1e3dec127ac" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + +[[package]] +name = "regex" +version = "1.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a96887878f22d7bad8a3b6dc5b7440e0ada9a245242924394987b21cf2210a4c" + +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rustix" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "146c9e247ccc180c1f61615433868c99f3de3ae256a30a43b49f67c2d9171f34" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.61.2", +] + +[[package]] +name = "rustls" +version = "0.23.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c665f33d38cea657d9614f766881e4d510e0eda4239891eea56b4cadcf01801b" +dependencies = [ + "log", + "once_cell", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pki-types" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be040f8b0a225e40375822a563fa9524378b9d63112f53e19ffff34df5d33fdd" +dependencies = [ + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7df23109aa6c1567d1c575b9952556388da57401e4ace1d15f79eedad0d8f53" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + +[[package]] +name = "rustversion" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "shellexpand" +version = "3.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b1fdf65dd6331831494dd616b30351c38e96e45921a27745cf98490458b90bb" +dependencies = [ + "dirs", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "simd-adler32" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e320a6c5ad31d271ad523dcf3ad13e2767ad8b1cb8f047f75a8aeaf8da139da2" + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "2.0.116" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3df424c70518695237746f84cede799c9c58fcb37450d7b23716568cc8bc69cb" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "terminal_size" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b8cb979cb11c32ce1603f8137b22262a9d131aaa5c37b5678025f22b8becd0" +dependencies = [ + "rustix", + "windows-sys 0.60.2", +] + +[[package]] +name = "termtree" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f50febec83f5ee1df3015341d8bd429f2d1cc62bcba7ea2076759d315084683" + +[[package]] +name = "thiserror" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinystr" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42d3e9c45c09de15d06dd8acf5f4e0e399e85927b7f00711024eb7ae10fa4869" +dependencies = [ + "displaydoc", + "zerovec", +] + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" + +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "ureq" +version = "2.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02d1a66277ed75f640d608235660df48c8e3c19f3b4edb6a263315626cc3c01d" +dependencies = [ + "base64", + "flate2", + "log", + "once_cell", + "rustls", + "rustls-pki-types", + "url", + "webpki-roots 0.26.11", +] + +[[package]] +name = "url" +version = "2.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "wait-timeout" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ac3b126d3914f9849036f826e054cbabdc8519970b8998ddaf3b5bd3c65f11" +dependencies = [ + "libc", +] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasm-bindgen" +version = "0.2.108" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64024a30ec1e37399cf85a7ffefebdb72205ca1c972291c51512360d90bd8566" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.108" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "008b239d9c740232e71bd39e8ef6429d27097518b6b30bdf9086833bd5b6d608" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.108" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5256bae2d58f54820e6490f9839c49780dff84c65aeab9e772f15d5f0e913a55" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.108" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f01b580c9ac74c8d8f0c0e4afb04eeef2acf145458e52c03845ee9cd23e3d12" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "webpki-roots" +version = "0.26.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "521bc38abb08001b01866da9f51eb7c5d647a19260e00054a8c7fd5f9e57f7a9" +dependencies = [ + "webpki-roots 1.0.6", +] + +[[package]] +name = "webpki-roots" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22cfaf3c063993ff62e73cb4311efde4db1efb31ab78a3e5c457939ad5cc0bed" +dependencies = [ + "rustls-pki-types", +] + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.6", +] + +[[package]] +name = "windows-sys" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" +dependencies = [ + "windows-targets 0.53.5", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm 0.52.6", + "windows_aarch64_msvc 0.52.6", + "windows_i686_gnu 0.52.6", + "windows_i686_gnullvm 0.52.6", + "windows_i686_msvc 0.52.6", + "windows_x86_64_gnu 0.52.6", + "windows_x86_64_gnullvm 0.52.6", + "windows_x86_64_msvc 0.52.6", +] + +[[package]] +name = "windows-targets" +version = "0.53.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3" +dependencies = [ + "windows-link", + "windows_aarch64_gnullvm 0.53.1", + "windows_aarch64_msvc 0.53.1", + "windows_i686_gnu 0.53.1", + "windows_i686_gnullvm 0.53.1", + "windows_i686_msvc 0.53.1", + "windows_x86_64_gnu 0.53.1", + "windows_x86_64_gnullvm 0.53.1", + "windows_x86_64_msvc 0.53.1", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_i686_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650" + +[[package]] +name = "writeable" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9edde0db4769d2dc68579893f2306b26c6ecfbe0ef499b013d731b7b9247e0b9" + +[[package]] +name = "yoke" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72d6e5c6afb84d73944e5cedb052c4680d5657337201555f9f2a16b7406d4954" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b659052874eb698efe5b9e8cf382204678a0086ebf46982b79d6ca3182927e5d" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zerofrom" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zeroize" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0" + +[[package]] +name = "zerotrie" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a59c17a5562d507e4b54960e8569ebee33bee890c70aa3fe7b97e85a9fd7851" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", +] + +[[package]] +name = "zerovec" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c28719294829477f525be0186d13efa9a3c602f7ec202ca9e353d310fb9a002" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eadce39539ca5cb3985590102671f2567e659fca9666581ad3411d59207951f3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a379b87 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "cal" +version = "0.1.0" +edition = "2024" +description = "Calendar display utility" +authors = ["Se.Cherkasov@yahoo.com"] + + +[dependencies] +chrono = { version = "0.4.43", features = ["unstable-locales"] } +clap = { version = "4", features = ["derive"] } +unicode-width = "0.1" +terminal_size = "0.4" +libloading = { version = "0.8", optional = true } +shellexpand = { version = "3.1", optional = true } +libc = { version = "0.2", optional = true } + +[features] +plugins = ["dep:libloading", "dep:shellexpand", "dep:libc"] +default = ["plugins"] + +[dev-dependencies] +assert_cmd = "2.0" +predicates = "3.0" + +[workspace] +members = [ + "plugins/holiday_highlighter", +] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..b238e7a --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 SeCherkasov + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7511eee --- /dev/null +++ b/README.md @@ -0,0 +1,187 @@ +# cal + +Terminal calendar — a Rust rewrite of the `cal` utility from util-linux. + +## Features + +- **Flexible display**: single month, three months, year, arbitrary number of months +- **Week start**: Monday `-m` (ISO) or Sunday `-s` (US) +- **Week numbers**: `-w` with numbering system choice (`--week-type iso` or `us`) +- **Julian days**: `-j` shows day of year instead of date +- **Vertical mode**: `-v` for compact day-by-column layout +- **Custom reform**: `--reform 1752|gregorian|iso|julian` for different calendar systems +- **Today highlight**: inverse color for current day +- **Weekend and holiday highlight**: colors for Saturday, Sunday, and official holidays +- **Plugins**: dynamic loading of holiday highlighter plugins via API + +## Installation + +```bash +cargo build --release +``` + +Binary will be in `target/release/cal`. + +### Building with plugins + +The holiday highlighter plugin is built in the workspace: + +```bash +cargo build --release --workspace +``` + +Plugin `libholiday_highlighter.so` will be in `target/release/`. + +## Commands + +### Basic usage + +| Command | Description | +|---------|----------| +| `cal` | Current month | +| `cal 2026` | Entire year 2026 | +| `cal 2 2026` | February 2026 | +| `cal 15 9 2026` | September 2026 with 15th highlighted | +| `cal december 2025` | December 2025 (month names supported) | + +### Display modes + +| Command | Description | +|---------|----------| +| `cal -y` | Entire year (12 months) | +| `cal -Y` | Next 12 months from current | +| `cal -3` | Three months: previous, current, next | +| `cal -n 6` | Show 6 months | +| `cal --span -n 12` | 12 months centered on current month | + +### Output format + +| Command | Description | +|---------|----------| +| `cal -v` | Vertical mode (days in columns) | +| `cal -j` | Julian days (day of year 1-365/366) | +| `cal -w` | With week numbers | +| `cal --week-type us` | Weeks by US standard (starting Sunday) | +| `cal -c 2` | Force 2 columns for multi-month mode | +| `cal -c auto` | Auto-detect columns by terminal width | + +### Calendar options + +| Command | Description | +|---------|----------| +| `cal -m` | Week starts Monday (ISO, default) | +| `cal -s` | Week starts Sunday (US style) | +| `cal --reform 1752` | 1752 reform (skip Sep 3-13 in Great Britain) | +| `cal --reform gregorian` | Always Gregorian calendar | +| `cal --reform julian` | Always Julian calendar | +| `cal --iso` | ISO 8601 (alias for `--reform iso`) | + +### Output and colors + +| Command | Description | +|---------|----------| +| `cal --color` | Disable colors (monochrome output) | +| `cal -H` | Holiday highlight via isdayoff.ru API (requires plugin) | + +### Combined examples + +```bash +# Year with week numbers and holidays +cal -y -w -H + +# Three months vertically with holidays +cal -3 -v -H + +# 6 months in 2 columns +cal -n 6 -c 2 + +# February 2026 with Julian days +cal -j 2 2026 + +# Next 12 months centered +cal --span -Y +``` + +## Plugins + +### Holiday Highlighter + +Plugin for highlighting official holidays via isdayoff.ru API. + +#### Supported countries + +| Code | Country | Locales | +|------|---------|---------| +| RU | Russia | ru_RU, ru_BY, ru_KZ, ru_UZ, ru_LV | +| BY | Belarus | be_BY, ru_BY | +| KZ | Kazakhstan | kk_KZ, ru_KZ | +| US | USA | en_US, en | +| UZ | Uzbekistan | uz_UZ, ru_UZ | +| TR | Turkey | tr_TR | +| LV | Latvia | lv_LV, ru_LV | + +Country is auto-detected from `LC_ALL`, `LC_TIME`, or `LANG`. + +#### Day types + +| Code | Meaning | Color | +|------|---------|-------| +| 0 | Working day | — | +| 1 | Weekend | Red | +| 2 | Shortened day | Teal | +| 8 | Public holiday | Red | + +#### Installing the plugin + +After building the workspace, the plugin is in `target/release/libholiday_highlighter.so`. + +For system installation: + +```bash +# User-local +mkdir -p ~/.local/lib/cal/plugins +cp target/release/libholiday_highlighter.so ~/.local/lib/cal/plugins/ + +# System-wide (requires root) +sudo mkdir -p /usr/lib/cal/plugins +sudo cp target/release/libholiday_highlighter.so /usr/lib/cal/plugins/ +``` + +#### API + +The plugin uses [isdayoff.ru API](https://isdayoff.ru/): +- `GET /api/getdata?year=YYYY&month=MM&pre=1` — monthly data +- `GET /api/getdata?year=YYYY&pre=1` — yearly data + +Parameter `pre=1` includes pre-holiday shortened days information. + +## Environment variables + +| Variable | Description | +|----------|-------------| +| `LC_ALL` | Priority locale for month and day names | +| `LC_TIME` | Locale for date formatting | +| `LANG` | Fallback locale | +| `CAL_TEST_TIME` | Fixed date for testing (format YYYY-MM-DD) | + +## Localization + +Month names are supported in Russian and English: + +```bash +cal январь 2026 +cal February 2026 +cal 1 2026 +``` + +Weekdays are abbreviated to 2 characters according to locale: +- **ru_RU**: Пн, Вт, Ср, Чт, Пт, Сб, Вс +- **en_US**: Mo, Tu, We, Th, Fr, Sa, Su + +## Differences from util-linux cal + +- Vertical mode support (`-v`) +- Dynamic plugins +- Automatic locale detection +- Flexible calendar reform configuration +- Color output with today/weekend/holiday highlights diff --git a/README_ru.md b/README_ru.md new file mode 100644 index 0000000..6d6accc --- /dev/null +++ b/README_ru.md @@ -0,0 +1,187 @@ +# cal + +Календарь для терминала — переписанная на Rust версия утилиты `cal` из util-linux. + +## Возможности + +- **Гибкое отображение**: один месяц, три месяца, год, произвольное количество месяцев +- **Неделя с понедельника или воскресенья**: `-m` (ISO) или `-s` (US) +- **Номера недель**: `-w` с выбором системы нумерации (`--week-type iso` или `us`) +- **Юлианские дни**: `-j` показывает день года вместо даты +- **Вертикальный режим**: `-v` для компактного отображения дней по колонкам +- **Кастомизация реформы**: `--reform 1752|gregorian|iso|julian` для разных календарных систем +- **Подсветка сегодня**: инверсия цвета для текущего дня +- **Подсветка выходных и праздников**: цвета для субботы, воскресенья и официальных праздников +- **Плагины**: динамическая загрузка плагинов для подсветки праздников через API + +## Установка + +```bash +cargo build --release +``` + +Бинарный файл появится в `target/release/cal`. + +### Сборка с плагинами + +Плагин подсветки праздников собирается в workspace: + +```bash +cargo build --release --workspace +``` + +Плагин `libholiday_highlighter.so` будет в `target/release/`. + +## Команды + +### Базовое использование + +| Команда | Описание | +|---------|----------| +| `cal` | Текущий месяц | +| `cal 2026` | Весь 2026 год | +| `cal 2 2026` | Февраль 2026 | +| `cal 15 9 2026` | Сентябрь 2026 с выделением 15 числа | +| `cal декабрь 2025` | Декабрь 2025 (поддержка названий месяцев) | + +### Режимы отображения + +| Команда | Описание | +|---------|----------| +| `cal -y` | Весь год (12 месяцев) | +| `cal -Y` | Следующие 12 месяцев от текущего | +| `cal -3` | Три месяца: предыдущий, текущий, следующий | +| `cal -n 6` | Показать 6 месяцев | +| `cal --span -n 12` | 12 месяцев с центрированием на текущем | + +### Формат вывода + +| Команда | Описание | +|---------|----------| +| `cal -v` | Вертикальный режим (дни в колонках) | +| `cal -j` | Юлианские дни (день года 1-365/366) | +| `cal -w` | С номерами недель | +| `cal --week-type us` | Недели по US стандарту (с воскресенья) | +| `cal -c 2` | Принудительно 2 колонки для мульти-месячного режима | +| `cal -c auto` | Автоподбор колонок по ширине терминала | + +### Календарные опции + +| Команда | Описание | +|---------|----------| +| `cal -m` | Неделя с понедельника (ISO, по умолчанию) | +| `cal -s` | Неделя с воскресенья (US стиль) | +| `cal --reform 1752` | Реформа 1752 года (пропуск 3-13 сентября в Великобритании) | +| `cal --reform gregorian` | Всегда григорианский календарь | +| `cal --reform julian` | Всегда юлианский календарь | +| `cal --iso` | ISO 8601 (алиас для `--reform iso`) | + +### Вывод и цвета + +| Команда | Описание | +|---------|----------| +| `cal --color` | Отключить цвета (монохромный вывод) | +| `cal -H` | Подсветка праздников через isdayoff.ru API (требует плагин) | + +### Комбинированные примеры + +```bash +# Год с номерами недель и праздниками +cal -y -w -H + +# Три месяца вертикально с праздниками +cal -3 -v -H + +# 6 месяцев в 2 колонки +cal -n 6 -c 2 + +# Февраль 2026 с юлианскими днями +cal -j 2 2026 + +# Следующие 12 месяцев с центрированием +cal --span -Y +``` + +## Плагины + +### Holiday Highlighter + +Плагин для подсветки официальных праздников через API isdayoff.ru. + +#### Поддерживаемые страны + +| Код | Страна | Локали | +|-----|--------|--------| +| RU | Россия | ru_RU, ru_BY, ru_KZ, ru_UZ, ru_LV | +| BY | Беларусь | be_BY, ru_BY | +| KZ | Казахстан | kk_KZ, ru_KZ | +| US | США | en_US, en | +| UZ | Узбекистан | uz_UZ, ru_UZ | +| TR | Турция | tr_TR | +| LV | Латвия | lv_LV, ru_LV | + +Страна определяется автоматически по `LC_ALL`, `LC_TIME` или `LANG`. + +#### Типы дней + +| Код | Значение | Цвет | +|-----|----------|------| +| 0 | Рабочий день | — | +| 1 | Выходной | Красный | +| 2 | Сокращённый день | Бирюзовый | +| 8 | Официальный праздник | Красный | + +#### Установка плагина + +После сборки workspace плагин находится в `target/release/libholiday_highlighter.so`. + +Для системной установки: + +```bash +# Локально для пользователя +mkdir -p ~/.local/lib/cal/plugins +cp target/release/libholiday_highlighter.so ~/.local/lib/cal/plugins/ + +# Системно (требует root) +sudo mkdir -p /usr/lib/cal/plugins +sudo cp target/release/libholiday_highlighter.so /usr/lib/cal/plugins/ +``` + +#### API + +Плагин использует [isdayoff.ru API](https://isdayoff.ru/): +- `GET /api/getdata?year=YYYY&month=MM&pre=1` — данные за месяц +- `GET /api/getdata?year=YYYY&pre=1` — данные за год + +Параметр `pre=1` включает информацию о предпраздничных сокращённых днях. + +## Переменные окружения + +| Переменная | Описание | +|------------|----------| +| `LC_ALL` | Приоритетная локаль для названий месяцев и дней | +| `LC_TIME` | Локаль для форматирования дат | +| `LANG` | Резервная локаль | +| `CAL_TEST_TIME` | Фиксированная дата для тестирования (формат YYYY-MM-DD) | + +## Локализация + +Поддерживаются названия месяцев на русском и английском: + +```bash +cal январь 2026 +cal February 2026 +cal 1 2026 +``` + +Дни недели сокращаются до 2 символов согласно локали: +- **ru_RU**: Пн, Вт, Ср, Чт, Пт, Сб, Вс +- **en_US**: Mo, Tu, We, Th, Fr, Sa, Su + +## Отличия от util-linux cal + +- Поддержка вертикального режима (`-v`) +- Динамические плагины +- Автоматическое определение локали +- Гибкая настройка календарной реформы +- Цветовой вывод с подсветкой сегодня/выходных/праздников diff --git a/plugins/holiday_highlighter/Cargo.toml b/plugins/holiday_highlighter/Cargo.toml new file mode 100644 index 0000000..6b21736 --- /dev/null +++ b/plugins/holiday_highlighter/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "holiday_highlighter" +version = "0.1.0" +edition = "2024" +description = "Holiday highlighting plugin for cal using isdayoff.ru API" + +[lib] +crate-type = ["cdylib", "rlib"] +name = "holiday_highlighter" + +[dependencies] +chrono = { version = "0.4.43", features = ["unstable-locales"] } +ureq = "2.12" +libc = "0.2" diff --git a/plugins/holiday_highlighter/README.md b/plugins/holiday_highlighter/README.md new file mode 100644 index 0000000..31367a9 --- /dev/null +++ b/plugins/holiday_highlighter/README.md @@ -0,0 +1,98 @@ +# Holiday Highlighter Plugin + +Plugin for `cal` that adds holiday highlighting via isdayoff.ru API. + +## Features + +- **Automatic country detection** from system locale +- **Data caching** to reduce API requests +- **Multiple country support**: Russia, Belarus, Kazakhstan, USA, Uzbekistan, Turkey, Latvia +- **Day type data**: working days, weekends, shortened days, official holidays + +## Building + +```bash +# Build entire workspace +cargo build --release --workspace + +# Plugin only +cargo build --release -p holiday_highlighter +``` + +## Installation + +### User-local + +```bash +mkdir -p ~/.local/lib/cal/plugins +cp target/release/libholiday_highlighter.so ~/.local/lib/cal/plugins/ +``` + +### System-wide + +```bash +sudo mkdir -p /usr/lib/cal/plugins +sudo cp target/release/libholiday_highlighter.so /usr/lib/cal/plugins/ +``` + +## Usage + +```bash +# Highlight holidays for current country +cal -H + +# Year with holidays +cal -y -H + +# Three months with holidays +cal -3 -H +``` + +## Supported countries + +| Code | Country | Auto-detect locales | +|------|---------|---------------------| +| RU | Russia | ru_RU, ru_BY, ru_KZ, ru_UZ, ru_LV | +| BY | Belarus | be_BY, ru_BY | +| KZ | Kazakhstan | kk_KZ, ru_KZ | +| US | USA | en_US, en | +| UZ | Uzbekistan | uz_UZ, ru_UZ | +| TR | Turkey | tr_TR | +| LV | Latvia | lv_LV, ru_LV | + +## API + +The plugin uses [isdayoff.ru API](https://isdayoff.ru/): + +### Monthly request + +``` +GET https://isdayoff.ru/api/getdata?year=2026&month=01&pre=1 +``` + +### Yearly request + +``` +GET https://isdayoff.ru/api/getdata?year=2026&pre=1 +``` + +Parameter `pre=1` includes pre-holiday shortened days information. + +## Data format + +Each character in the response represents a day of the month: + +| Character | Day type | Description | +|-----------|----------|-------------| +| `0` | Working | Regular working day | +| `1` | Weekend | Saturday or Sunday | +| `2` | Shortened | Pre-holiday shortened day | +| `8` | Holiday | Official public holiday | + +## Environment variables + +| Variable | Description | +|----------|-------------| +| `LC_ALL` | Priority locale for country detection | +| `LC_TIME` | Locale for country detection | +| `LANG` | Fallback locale for country detection | diff --git a/plugins/holiday_highlighter/README_ru.md b/plugins/holiday_highlighter/README_ru.md new file mode 100644 index 0000000..109a47a --- /dev/null +++ b/plugins/holiday_highlighter/README_ru.md @@ -0,0 +1,98 @@ +# Holiday Highlighter Plugin + +Плагин для `cal`, добавляющий подсветку официальных праздников через API isdayoff.ru. + +## Возможности + +- **Автоматическое определение страны** по системной локали +- **Кэширование данных** для уменьшения количества запросов к API +- **Поддержка нескольких стран**: Россия, Беларусь, Казахстан, США, Узбекистан, Турция, Латвия +- **Данные о типах дней**: рабочие, выходные, сокращённые, официальные праздники + +## Сборка + +```bash +# Сборка всего workspace +cargo build --release --workspace + +# Только плагин +cargo build --release -p holiday_highlighter +``` + +## Установка + +### Локально для пользователя + +```bash +mkdir -p ~/.local/lib/cal/plugins +cp target/release/libholiday_highlighter.so ~/.local/lib/cal/plugins/ +``` + +### Системно + +```bash +sudo mkdir -p /usr/lib/cal/plugins +sudo cp target/release/libholiday_highlighter.so /usr/lib/cal/plugins/ +``` + +## Использование + +```bash +# Подсветка праздников для текущей страны +cal -H + +# Год с праздниками +cal -y -H + +# Три месяца с праздниками +cal -3 -H +``` + +## Поддерживаемые страны + +| Код | Страна | Локали для автоопределения | +|-----|--------|----------------------------| +| RU | Россия | ru_RU, ru_BY, ru_KZ, ru_UZ, ru_LV | +| BY | Беларусь | be_BY, ru_BY | +| KZ | Казахстан | kk_KZ, ru_KZ | +| US | США | en_US, en | +| UZ | Узбекистан | uz_UZ, ru_UZ | +| TR | Турция | tr_TR | +| LV | Латвия | lv_LV, ru_LV | + +## API + +Плагин использует [isdayoff.ru API](https://isdayoff.ru/): + +### Запрос за месяц + +``` +GET https://isdayoff.ru/api/getdata?year=2026&month=01&pre=1 +``` + +### Запрос за год + +``` +GET https://isdayoff.ru/api/getdata?year=2026&pre=1 +``` + +Параметр `pre=1` включает информацию о предпраздничных сокращённых днях. + +## Формат данных + +Каждый символ в ответе представляет день месяца: + +| Символ | Тип дня | Описание | +|--------|---------|----------| +| `0` | Рабочий | Обычный рабочий день | +| `1` | Выходной | Суббота или воскресенье | +| `2` | Сокращённый | Предпраздничный день | +| `8` | Праздник | Официальный государственный праздник | + +## Переменные окружения + +| Переменная | Описание | +|------------|----------| +| `LC_ALL` | Приоритетная локаль для определения страны | +| `LC_TIME` | Локаль для определения страны | +| `LANG` | Резервная локаль для определения страны | diff --git a/plugins/holiday_highlighter/src/lib.rs b/plugins/holiday_highlighter/src/lib.rs new file mode 100644 index 0000000..abfdc6d --- /dev/null +++ b/plugins/holiday_highlighter/src/lib.rs @@ -0,0 +1,214 @@ +//! Holiday Highlighter Plugin for cal. +//! +//! Fetches holiday data from isdayoff.ru API for multiple countries. + +use libc::{c_char, c_int}; +use std::collections::HashMap; +use std::ffi::{CStr, CString}; +use std::sync::{LazyLock, Mutex}; + +pub const PLUGIN_NAME: &str = env!("CARGO_PKG_NAME"); +pub const PLUGIN_VERSION: &str = env!("CARGO_PKG_VERSION"); + +const API_URL_YEAR: &str = "https://isdayoff.ru/api/getdata"; +const API_URL_MONTH: &str = "https://isdayoff.ru/api/getdata"; + +/// Supported countries with their locale mappings. +const SUPPORTED_COUNTRIES: &[(&str, &[&str])] = &[ + ("RU", &["ru_RU", "ru_BY", "ru_KZ", "ru_UZ", "ru_LV"]), + ("BY", &["be_BY", "ru_BY"]), + ("KZ", &["kk_KZ", "ru_KZ"]), + ("US", &["en_US", "en"]), + ("UZ", &["uz_UZ", "ru_UZ"]), + ("TR", &["tr_TR"]), + ("LV", &["lv_LV", "ru_LV"]), +]; + +type CacheKey = (i32, u32, String); +static CACHE: LazyLock>> = + LazyLock::new(|| Mutex::new(HashMap::new())); + +/// Initialize the plugin (optional, cache is lazily initialized). +#[unsafe(no_mangle)] +pub extern "C" fn plugin_init() {} + +/// Get plugin name (do not free returned pointer). +#[unsafe(no_mangle)] +pub extern "C" fn plugin_get_name() -> *const c_char { + static NAME: LazyLock = LazyLock::new(|| CString::new(PLUGIN_NAME).unwrap()); + NAME.as_ptr() +} + +/// Get plugin version (do not free returned pointer). +#[unsafe(no_mangle)] +pub extern "C" fn plugin_get_version() -> *const c_char { + static VERSION: LazyLock = LazyLock::new(|| CString::new(PLUGIN_VERSION).unwrap()); + VERSION.as_ptr() +} + +/// Get holiday data for a specific month. +/// +/// Returns string where each character represents a day: +/// - '0' = working day, '1' = weekend, '2' = shortened, '8' = public holiday +/// +/// # Safety +/// `country_code` must be a valid null-terminated C string. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn plugin_get_holidays( + year: c_int, + month: c_int, + country_code: *const c_char, +) -> *mut c_char { + let country = unsafe { parse_country(country_code) }; + let key = (year, month as u32, country.clone()); + + if let Some(data) = CACHE.lock().unwrap().get(&key) { + return CString::new(data.as_str()).unwrap().into_raw(); + } + + let data = fetch_holidays(year, month as u32, &country).unwrap_or_default(); + CACHE.lock().unwrap().insert(key, data.clone()); + + CString::new(data).unwrap().into_raw() +} + +unsafe fn parse_country(country_code: *const c_char) -> String { + unsafe { + CStr::from_ptr(country_code) + .to_str() + .unwrap_or("RU") + .to_string() + } +} + +/// Free memory allocated by plugin_get_holidays. +/// +/// # Safety +/// `ptr` must be returned by `plugin_get_holidays`. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn plugin_free_holidays(ptr: *mut c_char) { + if !ptr.is_null() { + let _ = unsafe { CString::from_raw(ptr) }; + } +} + +/// Get country code from system locale. +#[unsafe(no_mangle)] +pub extern "C" fn plugin_get_country_from_locale() -> *mut c_char { + let country = get_country_from_locale(); + CString::new(country).unwrap().into_raw() +} + +/// Free memory allocated by plugin_get_country_from_locale. +/// +/// # Safety +/// `ptr` must be returned by `plugin_get_country_from_locale`. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn plugin_free_country(ptr: *mut c_char) { + if !ptr.is_null() { + let _ = unsafe { CString::from_raw(ptr) }; + } +} + +/// Check if a specific day is a holiday. +/// +/// Returns: 0=working, 1=weekend, 2=shortened, 8=public, -1=error +/// +/// # Safety +/// `country_code` must be a valid null-terminated C string. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn plugin_is_holiday( + year: c_int, + month: c_int, + day: c_int, + country_code: *const c_char, +) -> c_int { + let country = unsafe { parse_country(country_code) }; + let holidays = fetch_holidays(year, month as u32, &country); + + match holidays { + Some(data) => { + let day_idx = (day - 1) as usize; + if day_idx < data.len() { + data.chars() + .nth(day_idx) + .and_then(|c| c.to_digit(10).map(|d| d as c_int)) + .unwrap_or(-1) + } else { + -1 + } + } + None => -1, + } +} + +/// Get holiday data for entire year. +/// +/// # Safety +/// `country_code` must be a valid null-terminated C string. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn plugin_get_year_holidays( + year: c_int, + country_code: *const c_char, +) -> *mut c_char { + let country = unsafe { parse_country(country_code) }; + let data = fetch_holidays_year(year, &country).unwrap_or_default(); + CString::new(data).unwrap().into_raw() +} + +/// Fetch holiday data for a month from isdayoff.ru API. +fn fetch_holidays(year: i32, month: u32, _country: &str) -> Option { + let url = format!("{}?year={}&month={:02}&pre=1", API_URL_MONTH, year, month); + + match ureq::get(&url).call() { + Ok(response) => response.into_string().ok(), + Err(_) => None, + } +} + +/// Fetch holiday data for entire year from isdayoff.ru API. +pub fn fetch_holidays_year(year: i32, _country: &str) -> Option { + let url = format!("{}?year={}&pre=1", API_URL_YEAR, year); + + match ureq::get(&url).call() { + Ok(response) => response.into_string().ok(), + Err(_) => None, + } +} + +/// Determine country code from system locale. +pub fn get_country_from_locale() -> String { + let locale = std::env::var("LC_ALL") + .or_else(|_| std::env::var("LC_TIME")) + .or_else(|_| std::env::var("LANG")) + .unwrap_or_else(|_| "en_US.UTF-8".to_string()); + + let locale_name = locale + .split('.') + .next() + .unwrap_or(&locale) + .split('@') + .next() + .unwrap_or(&locale); + + // Match against supported countries + for (country, locales) in SUPPORTED_COUNTRIES { + for &supported_locale in *locales { + if locale_name == supported_locale { + return country.to_string(); + } + } + } + + // Extract country code from locale (e.g., "en_US" -> "US") + if let Some(underscore_pos) = locale_name.find('_') { + let country_code = &locale_name[underscore_pos + 1..]; + for (country, _) in SUPPORTED_COUNTRIES { + if *country == country_code { + return country_code.to_string(); + } + } + } + + "RU".to_string() +} diff --git a/plugins/holiday_highlighter/tests/integration_tests.rs b/plugins/holiday_highlighter/tests/integration_tests.rs new file mode 100644 index 0000000..0107b00 --- /dev/null +++ b/plugins/holiday_highlighter/tests/integration_tests.rs @@ -0,0 +1,56 @@ +//! Integration tests for holiday_highlighter plugin. + +use holiday_highlighter::{ + PLUGIN_NAME, PLUGIN_VERSION, get_country_from_locale, plugin_get_name, plugin_get_version, +}; +use std::ffi::CStr; + +#[test] +fn test_get_country_from_locale_ru() { + unsafe { + std::env::set_var("LC_ALL", "ru_RU.UTF-8"); + } + assert_eq!(get_country_from_locale(), "RU"); +} + +#[test] +fn test_get_country_from_locale_us() { + unsafe { + std::env::set_var("LC_ALL", "en_US.UTF-8"); + } + assert_eq!(get_country_from_locale(), "US"); +} + +#[test] +fn test_get_country_from_locale_by() { + unsafe { + std::env::set_var("LC_ALL", "be_BY.UTF-8"); + } + assert_eq!(get_country_from_locale(), "BY"); +} + +#[test] +fn test_get_country_from_locale_fallback() { + unsafe { + std::env::set_var("LC_ALL", ""); + std::env::set_var("LC_TIME", ""); + std::env::set_var("LANG", ""); + } + assert_eq!(get_country_from_locale(), "RU"); +} + +#[test] +fn test_plugin_metadata_from_cargo() { + assert_eq!(PLUGIN_NAME, "holiday_highlighter"); + assert_eq!(PLUGIN_VERSION, "0.1.0"); +} + +#[test] +fn test_plugin_get_name_version() { + unsafe { + let name = CStr::from_ptr(plugin_get_name()).to_str().unwrap(); + let version = CStr::from_ptr(plugin_get_version()).to_str().unwrap(); + assert_eq!(name, "holiday_highlighter"); + assert_eq!(version, "0.1.0"); + } +} diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 0000000..61846bd --- /dev/null +++ b/src/args.rs @@ -0,0 +1,313 @@ +//! Command-line argument parsing using clap. +//! +//! Arguments follow util-linux cal convention: `[[day] month] year` + +use chrono::Datelike; +use clap::{Parser, ValueHint}; +use std::io::IsTerminal; + +use crate::types::{ + COLOR_ENABLED_BY_DEFAULT, CalContext, ColumnsMode, GUTTER_WIDTH_REGULAR, ReformType, WeekType, +}; + +#[derive(Parser, Debug)] +#[command(name = "cal")] +#[command(about = "Displays calendar for specified month or year", long_about = None)] +#[command(version = "1.0.0")] +#[command(after_help = HELP_MESSAGE)] +pub struct Args { + /// Week starts on Sunday (default is Monday). + #[arg(short = 's', long, help_heading = "Calendar options")] + pub sunday: bool, + + /// Week starts on Monday (default). + #[arg(short = 'm', long, help_heading = "Calendar options")] + pub monday: bool, + + /// Display Julian days (day number in year). + #[arg(short = 'j', long, help_heading = "Calendar options")] + pub julian: bool, + + /// Display week numbers. + #[arg(short = 'w', long, help_heading = "Calendar options")] + pub week_numbers: bool, + + /// Week numbering system (iso or us). + #[arg( + long, + default_value = "iso", + help_heading = "Calendar options", + value_name = "system" + )] + pub week_type: WeekType, + + /// Display whole year. + #[arg(short = 'y', long, help_heading = "Display options")] + pub year: bool, + + /// Display the next twelve months. + #[arg(short = 'Y', long = "twelve", help_heading = "Display options")] + pub twelve_months: bool, + + /// Display three months (previous, current, next). + #[arg(short = '3', long = "three", help_heading = "Display options")] + pub three_months: bool, + + /// Number of months to display. + #[arg( + short = 'n', + long = "months", + help_heading = "Display options", + value_name = "num" + )] + pub months_count: Option, + + /// Show only a single month (default). + #[arg(short = '1', long = "one", help_heading = "Display options")] + pub one_month: bool, + + /// Span the date when displaying multiple months (center around current month). + #[arg(short = 'S', long = "span", help_heading = "Display options")] + pub span: bool, + + /// Gregorian reform date (1752|gregorian|iso|julian). + #[arg( + long, + default_value = "1752", + help_heading = "Calendar options", + value_name = "val" + )] + pub reform: ReformType, + + /// Use ISO 8601 reform (same as --reform iso). + #[arg(long, help_heading = "Calendar options")] + pub iso: bool, + + /// Day (1-31) - optional, used with month and year. + #[arg(index = 1, default_value = None, value_name = "day", value_hint = ValueHint::Other)] + pub day_arg: Option, + + /// Month (1-12 or name) - optional, used with year. + #[arg(index = 2, default_value = None, value_name = "month", value_hint = ValueHint::Other)] + pub month_arg: Option, + + /// Year (1-9999). + #[arg(index = 3, default_value = None, value_name = "year", value_hint = ValueHint::Other)] + pub year_arg: Option, + + /// Disable colorized output. + #[arg(long, help_heading = "Output options")] + pub color: bool, + + /// Number of columns for multiple months (or "auto" for terminal width). + #[arg( + short = 'c', + long = "columns", + help_heading = "Output options", + value_name = "width" + )] + pub columns: Option, + + /// Show days vertically (days in columns instead of rows). + #[arg(short = 'v', long, help_heading = "Output options")] + pub vertical: bool, + + /// Highlight holidays using isdayoff.ru API (requires plugin). + /// + /// **Note:** Build the workspace to include the plugin: + /// ```bash + /// cargo build --release --workspace + /// ``` + /// The plugin file (`libholiday_highlighter.so`) must be in one of: + /// - `./target/release/` (after building) + /// - `~/.local/lib/cal/plugins/` + /// - `/usr/lib/cal/plugins/` + #[arg(short = 'H', long = "holidays", help_heading = "Output options")] + pub holidays: bool, +} + +/// Help message displayed with --help. +const HELP_MESSAGE: &str = "Display a calendar, or some part of it. + +Without any arguments, display the current month. + +Examples: + cal Display current month + cal -3 Display three months (prev, current, next) + cal -y Display the whole year + cal -Y Display next twelve months + cal 2 2026 Display February 2026 + cal 2026 Display year 2026 + cal --span -n 12 Display 12 months centered on current month + cal --color Disable colorized output + cal -H Highlight holidays (requires plugin, see --help)"; + +impl Args { + pub fn parse() -> Self { + Parser::parse() + } +} + +impl CalContext { + pub fn new(args: &Args) -> Result { + let today = get_today_date(); + + let color = !args.color && COLOR_ENABLED_BY_DEFAULT && std::io::stdout().is_terminal(); + + let columns = match args.columns.as_deref() { + Some("auto") | None => ColumnsMode::Auto, + Some(s) => { + let n = s + .parse::() + .map_err(|_| format!("Invalid columns value: {}", s))?; + if n == 0 { + return Err("Columns must be positive".to_string()); + } + ColumnsMode::Fixed(n) + } + }; + + // Prevent conflicting display modes + let mode_count = [args.year, args.twelve_months, args.months_count.is_some()] + .iter() + .filter(|&&x| x) + .count(); + + if mode_count > 1 { + return Err("Options -y, -Y, and -n are mutually exclusive".to_string()); + } + + if let Some(year_str) = &args.year_arg { + let year: i32 = year_str + .parse() + .map_err(|_| format!("Invalid year value: {}", year_str))?; + if !(1..=9999).contains(&year) { + return Err(format!("Invalid year value: {} (must be 1-9999)", year)); + } + } + + // Vertical mode uses narrower gutter for compact layout + let gutter_width = if args.vertical { + 1 + } else { + GUTTER_WIDTH_REGULAR + }; + + // --iso overrides --reform + let reform_year = if args.iso { + ReformType::Iso.reform_year() + } else { + args.reform.reform_year() + }; + + Ok(CalContext { + reform_year, + week_start: if args.sunday { + chrono::Weekday::Sun + } else { + chrono::Weekday::Mon + }, + julian: args.julian, + week_numbers: args.week_numbers, + week_type: args.week_type, + color, + vertical: args.vertical, + today, + show_year_in_header: true, + gutter_width, + columns, + span: args.span, + #[cfg(feature = "plugins")] + holidays: args.holidays, + }) + } +} + +/// Get today's date, respecting CAL_TEST_TIME environment variable for testing. +pub fn get_today_date() -> chrono::NaiveDate { + if let Ok(test_time) = std::env::var("CAL_TEST_TIME") + && let Ok(date) = chrono::NaiveDate::parse_from_str(&test_time, "%Y-%m-%d") + { + return date; + } + chrono::Local::now().date_naive() +} + +/// Calculate display date from positional arguments. +/// +/// Argument patterns: +/// - 1 arg: year (4 digits) or month (1-2 digits) +/// - 2 args: month year +/// - 3 args: day month year +pub fn get_display_date(args: &Args) -> Result<(i32, u32, Option), String> { + let today = get_today_date(); + + let day_provided = args.day_arg.is_some(); + let month_provided = args.month_arg.is_some(); + let year_provided = args.year_arg.is_some(); + + match (day_provided, month_provided, year_provided) { + // One argument: could be year (4 digits) or month (1-2 digits) + (true, false, false) => { + let val = args.day_arg.as_ref().unwrap(); + if let Ok(num) = val.parse::() { + // 4 digits = year + if (1000..=9999).contains(&num) { + return Ok((num, today.month(), None)); + } + // 1-2 digits = month + if (1..=12).contains(&num) { + return Ok((today.year(), num as u32, None)); + } + } + // Try parsing as month name + if let Some(month) = crate::formatter::parse_month(val) { + return Ok((today.year(), month, None)); + } + Err(format!("Invalid argument: {}", val)) + } + // Two arguments: month year (e.g., cal 2 2026) + (true, true, false) => { + let month = crate::formatter::parse_month(args.day_arg.as_ref().unwrap()) + .ok_or_else(|| format!("Invalid month: {}", args.day_arg.as_ref().unwrap()))?; + let year = args + .month_arg + .as_ref() + .unwrap() + .parse::() + .map_err(|_| format!("Invalid year: {}", args.month_arg.as_ref().unwrap()))?; + if !(1..=9999).contains(&year) { + return Err(format!("Invalid year: {} (must be 1-9999)", year)); + } + Ok((year, month, None)) + } + // Three arguments: day month year + (true, true, true) => { + let day = args + .day_arg + .as_ref() + .unwrap() + .parse::() + .map_err(|_| format!("Invalid day: {}", args.day_arg.as_ref().unwrap()))?; + if !(1..=31).contains(&day) { + return Err(format!("Invalid day: {} (must be 1-31)", day)); + } + let month = crate::formatter::parse_month(args.month_arg.as_ref().unwrap()) + .ok_or_else(|| format!("Invalid month: {}", args.month_arg.as_ref().unwrap()))?; + let year = args + .year_arg + .as_ref() + .unwrap() + .parse::() + .map_err(|_| format!("Invalid year: {}", args.year_arg.as_ref().unwrap()))?; + if !(1..=9999).contains(&year) { + return Err(format!("Invalid year: {} (must be 1-9999)", year)); + } + Ok((year, month, Some(day))) + } + // No arguments: current month + (false, false, false) => Ok((today.year(), today.month(), None)), + // Invalid combinations + _ => Err("Invalid argument combination".to_string()), + } +} diff --git a/src/calendar.rs b/src/calendar.rs new file mode 100644 index 0000000..52a65bb --- /dev/null +++ b/src/calendar.rs @@ -0,0 +1,186 @@ +//! Calendar calculation logic using Zeller's algorithm and custom reform handling. + +use chrono::{Datelike, NaiveDate, Weekday}; + +use crate::types::{ + CELLS_PER_MONTH, CalContext, ColumnsMode, MonthData, REFORM_FIRST_DAY, REFORM_LAST_DAY, + REFORM_MONTH, REFORM_YEAR_GB, WeekType, +}; + +impl CalContext { + /// Check if a year is a leap year according to the calendar rules. + pub fn is_leap_year(&self, year: i32) -> bool { + if year < self.reform_year { + // Julian: every 4 years + year % 4 == 0 + } else { + // Gregorian: divisible by 4, except centuries unless divisible by 400 + (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) + } + } + + pub fn days_in_month(&self, year: i32, month: u32) -> u32 { + match month { + 1 | 3 | 5 | 7 | 8 | 10 | 12 => 31, + 4 | 6 | 9 | 11 => 30, + 2 if self.is_leap_year(year) => 29, + 2 => 28, + _ => 30, + } + } + + /// Check if a date falls within the reform gap (September 3-13, 1752). + pub fn is_reform_gap(&self, year: i32, month: u32, day: u32) -> bool { + if self.reform_year != REFORM_YEAR_GB { + return false; + } + year == REFORM_YEAR_GB + && month == REFORM_MONTH + && (REFORM_FIRST_DAY..=REFORM_LAST_DAY).contains(&day) + } + + /// Calculate weekday using Zeller's congruence algorithm. + pub fn first_day_of_month(&self, year: i32, month: u32) -> Weekday { + let m = if month < 3 { month + 12 } else { month }; + let q: i32 = 1; + let year_i = if month < 3 { year - 1 } else { year }; + let k: i32 = year_i % 100; + let j: i32 = year_i / 100; + + let h = (q + (13 * (m as i32 + 1)) / 5 + k + k / 4 + j / 4 - 2 * j) % 7; + // h: 0=Sat, 1=Sun, 2=Mon, 3=Tue, 4=Wed, 5=Thu, 6=Fri + match h { + 0 => Weekday::Sat, + 1 => Weekday::Sun, + 2 => Weekday::Mon, + 3 => Weekday::Tue, + 4 => Weekday::Wed, + 5 => Weekday::Thu, + 6 => Weekday::Fri, + _ => unreachable!(), + } + } + + /// Calculate day of year (Julian day number within the year). + pub fn day_of_year(&self, year: i32, month: u32, day: u32) -> u32 { + const DAYS_BEFORE_MONTH: [u32; 12] = + [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]; + let mut doy = DAYS_BEFORE_MONTH[(month - 1) as usize] + day; + + if month > 2 && self.is_leap_year(year) { + doy += 1; + } + + // Adjust for reform gap (11 days removed in September 1752) + if year == REFORM_YEAR_GB && month >= REFORM_MONTH { + doy = doy.saturating_sub(REFORM_LAST_DAY - REFORM_FIRST_DAY + 1); + } + doy + } + + pub fn week_number(&self, year: i32, month: u32, day: u32) -> u32 { + match self.week_type { + WeekType::Iso => { + // ISO 8601: week starts Monday, week 1 contains first Thursday + let date = NaiveDate::from_ymd_opt(year, month, day).unwrap(); + date.iso_week().week() + } + WeekType::Us => { + // US: week starts Sunday, week 1 contains January 1 + let date = NaiveDate::from_ymd_opt(year, month, day).unwrap(); + let jan1 = NaiveDate::from_ymd_opt(year, 1, 1).unwrap(); + let days_since_jan1 = date.signed_duration_since(jan1).num_days() as u32; + let jan1_weekday = jan1.weekday().num_days_from_sunday(); + ((days_since_jan1 + jan1_weekday) / 7) + 1 + } + } + } + + pub fn is_weekend(&self, weekday: Weekday) -> bool { + matches!(weekday, Weekday::Sat | Weekday::Sun) + } + + pub fn months_per_row(&self) -> u32 { + match self.columns { + ColumnsMode::Fixed(n) => n, + ColumnsMode::Auto => { + // ~20 chars per month + gutter, clamp to 1-3 for readability + let month_width = 20 + self.gutter_width; + if let Some(term_width) = get_terminal_width() { + (term_width / month_width as u32).clamp(1, 3) + } else { + 3 + } + } + } + } +} + +impl MonthData { + /// Build calendar data for a specific month. + pub fn new(ctx: &CalContext, year: i32, month: u32) -> Self { + let days_in_month = ctx.days_in_month(year, month); + let first_day = ctx.first_day_of_month(year, month); + + // Calculate offset based on week start day + let offset = match ctx.week_start { + Weekday::Mon if first_day == Weekday::Sun => 6, + Weekday::Mon => first_day.num_days_from_monday() as usize, + Weekday::Sun => first_day.num_days_from_sunday() as usize, + _ => unreachable!(), + }; + + let mut days: Vec> = Vec::with_capacity(CELLS_PER_MONTH); + let mut week_numbers: Vec> = Vec::with_capacity(CELLS_PER_MONTH); + let mut weekdays: Vec> = Vec::with_capacity(CELLS_PER_MONTH); + + // Empty cells before first day + for _ in 0..offset { + days.push(None); + week_numbers.push(None); + weekdays.push(None); + } + + // Fill days, skipping reform gap + let mut current_weekday = first_day; + let mut day = 1; + while day <= days_in_month { + if ctx.is_reform_gap(year, month, day) { + // Skip reform gap (3-13 September 1752) + for _ in REFORM_FIRST_DAY..=REFORM_LAST_DAY { + days.push(None); + week_numbers.push(None); + weekdays.push(None); + current_weekday = current_weekday.succ(); + } + day = REFORM_LAST_DAY + 1; + } else { + days.push(Some(day)); + week_numbers.push(ctx.week_numbers.then(|| ctx.week_number(year, month, day))); + weekdays.push(Some(current_weekday)); + current_weekday = current_weekday.succ(); + day += 1; + } + } + + // Pad to 42 cells (6 weeks) + while days.len() < CELLS_PER_MONTH { + days.push(None); + week_numbers.push(None); + weekdays.push(None); + } + + MonthData { + year, + month, + days, + week_numbers, + weekdays, + } + } +} + +/// Get terminal width using terminal_size crate. +fn get_terminal_width() -> Option { + terminal_size::terminal_size().map(|(w, _)| w.0 as u32) +} diff --git a/src/formatter.rs b/src/formatter.rs new file mode 100644 index 0000000..ea9c436 --- /dev/null +++ b/src/formatter.rs @@ -0,0 +1,970 @@ +//! Calendar formatting and display with localization and color support. + +use chrono::{Datelike, Locale, NaiveDate, Weekday}; +use unicode_width::UnicodeWidthStr; + +use crate::types::{ + COLOR_RED, COLOR_RESET, COLOR_REVERSE, COLOR_SAND_YELLOW, COLOR_TEAL, CalContext, + GUTTER_WIDTH_YEAR, MonthData, +}; + +#[cfg(feature = "plugins")] +use std::sync::Mutex; + +#[cfg(feature = "plugins")] +static PLUGIN: Mutex> = Mutex::new(None); + +#[cfg(feature = "plugins")] +static COUNTRY: Mutex> = Mutex::new(None); + +/// Cache for holiday data: (year, month, data) or (year, 0, full_year_data) +#[cfg(feature = "plugins")] +static HOLIDAY_CACHE: Mutex> = Mutex::new(None); + +/// Preload holiday data for entire year (called when -y flag is used) +#[cfg(feature = "plugins")] +pub fn preload_year_holidays(ctx: &CalContext, year: i32) { + if !ctx.holidays { + return; + } + + // Check if already cached + { + let cache_guard = HOLIDAY_CACHE.lock().unwrap(); + if let Some((cached_year, cached_month, _)) = &*cache_guard + && *cached_year == year + && *cached_month == 0 + { + return; + } + } + + if !init_plugin() { + return; + } + + let plugin_guard = PLUGIN.lock().unwrap(); + let country_guard = COUNTRY.lock().unwrap(); + + if let (Some(plugin), Some(country)) = (&*plugin_guard, &*country_guard) + && let Some(data) = plugin.get_year_holidays(year, country) + { + let mut cache_guard = HOLIDAY_CACHE.lock().unwrap(); + *cache_guard = Some((year, 0, data)); + } +} + +#[cfg(not(feature = "plugins"))] +pub fn preload_year_holidays(_ctx: &CalContext, _year: i32) {} + +#[cfg(feature = "plugins")] +fn init_plugin() -> bool { + let mut plugin_guard = PLUGIN.lock().unwrap(); + if plugin_guard.is_some() { + return true; + } + + if let Some(plugin) = crate::plugin_api::try_load_plugin() { + let country = plugin.get_country_from_locale(); + let mut country_guard = COUNTRY.lock().unwrap(); + *country_guard = Some(country.clone()); + *plugin_guard = Some(plugin); + true + } else { + false + } +} + +#[cfg(feature = "plugins")] +fn get_holiday_code(ctx: &CalContext, year: i32, month: u32, day: u32) -> i32 { + if !ctx.holidays { + return 0; + } + + // Check cache (including full year cache with month=0) + { + let cache_guard = HOLIDAY_CACHE.lock().unwrap(); + if let Some((cached_year, cached_month, data)) = &*cache_guard + && *cached_year == year + { + let day_idx = if *cached_month == 0 { + // Full year data - calculate day of year + let date = chrono::NaiveDate::from_ymd_opt(year, month, day); + if let Some(d) = date { + d.ordinal() as usize - 1 + } else { + return 0; + } + } else if *cached_month == month { + // Month data + (day - 1) as usize + } else { + return 0; + }; + + if day_idx < data.len() { + return data + .chars() + .nth(day_idx) + .and_then(|c| c.to_digit(10).map(|d| d as i32)) + .unwrap_or(0); + } + } + } + + // Cache miss - fetch data for the month + if !init_plugin() { + return 0; + } + + let plugin_guard = PLUGIN.lock().unwrap(); + let country_guard = COUNTRY.lock().unwrap(); + + if let (Some(plugin), Some(country)) = (&*plugin_guard, &*country_guard) + && let Some(data) = plugin.get_holidays(year, month, country) + { + // Don't overwrite full year cache with month data + let mut cache_guard = HOLIDAY_CACHE.lock().unwrap(); + let should_update = match &*cache_guard { + Some((cached_year, cached_month, _)) => !(*cached_year == year && *cached_month == 0), + None => true, + }; + + if should_update { + *cache_guard = Some((year, month, data.clone())); + } + + let day_idx = (day - 1) as usize; + if day_idx < data.len() { + return data + .chars() + .nth(day_idx) + .and_then(|c| c.to_digit(10).map(|d| d as i32)) + .unwrap_or(0); + } + } + + 0 +} + +#[cfg(not(feature = "plugins"))] +fn get_holiday_code(_ctx: &CalContext, _year: i32, _month: u32, _day: u32) -> i32 { + 0 +} + +#[cfg(feature = "plugins")] +pub fn preload_holidays(ctx: &CalContext, year: i32, month: u32) { + if !ctx.holidays { + return; + } + + { + let cache_guard = HOLIDAY_CACHE.lock().unwrap(); + if let Some((cached_year, cached_month, _)) = &*cache_guard + && *cached_year == year + && (*cached_month == month || *cached_month == 0) + { + return; + } + } + + let _ = get_holiday_code(ctx, year, month, 1); +} + +#[cfg(not(feature = "plugins"))] +pub fn preload_holidays(_ctx: &CalContext, _year: i32, _month: u32) {} + +/// Get system locale from environment (LC_ALL > LC_TIME > LANG > en_US). +pub fn get_system_locale() -> Locale { + std::env::var("LC_ALL") + .or_else(|_| std::env::var("LC_TIME")) + .or_else(|_| std::env::var("LANG")) + .unwrap_or_else(|_| "en_US.UTF-8".to_string()) + .split('.') + .next() + .unwrap_or("en_US") + .split('@') + .next() + .unwrap_or("en_US") + .parse() + .unwrap_or(Locale::en_US) +} + +/// Get month name in nominative case for current locale. +pub fn get_month_name(month: u32) -> String { + let locale = get_system_locale(); + + match locale { + Locale::ru_RU => [ + "Январь", + "Февраль", + "Март", + "Апрель", + "Май", + "Июнь", + "Июль", + "Август", + "Сентябрь", + "Октябрь", + "Ноябрь", + "Декабрь", + ][(month - 1) as usize] + .to_string(), + Locale::uk_UA => [ + "Січень", + "Лютий", + "Березень", + "Квітень", + "Травень", + "Червень", + "Липень", + "Серпень", + "Вересень", + "Жовтень", + "Листопад", + "Грудень", + ][(month - 1) as usize] + .to_string(), + Locale::be_BY => [ + "Студзень", + "Люты", + "Сакавік", + "Красавік", + "Май", + "Чэрвень", + "Ліпень", + "Жнівень", + "Верасень", + "Кастрычнік", + "Лістапад", + "Снежань", + ][(month - 1) as usize] + .to_string(), + _ => { + let date = NaiveDate::from_ymd_opt(2000, month, 1).unwrap(); + date.format_localized("%B", locale).to_string() + } + } +} + +/// Parse month from string (numeric 1-12 or name in English/Russian). +pub fn parse_month(s: &str) -> Option { + if let Ok(n) = s.parse::() + && (1..=12).contains(&n) + { + return Some(n); + } + + let s_lower = s.to_lowercase(); + let month_names: [(&str, u32); 35] = [ + // English full names + ("january", 1), + ("february", 2), + ("march", 3), + ("april", 4), + ("may", 5), + ("june", 6), + ("july", 7), + ("august", 8), + ("september", 9), + ("october", 10), + ("november", 11), + ("december", 12), + // Russian full names + ("январь", 1), + ("февраль", 2), + ("март", 3), + ("апрель", 4), + ("май", 5), + ("июнь", 6), + ("июль", 7), + ("август", 8), + ("сентябрь", 9), + ("октябрь", 10), + ("ноябрь", 11), + ("декабрь", 12), + // English short forms + ("jan", 1), + ("feb", 2), + ("mar", 3), + ("apr", 4), + ("jun", 6), + ("jul", 7), + ("aug", 8), + ("sep", 9), + ("oct", 10), + ("nov", 11), + ("dec", 12), + ]; + month_names + .iter() + .find(|(name, _)| *name == s_lower) + .map(|(_, num)| *num) +} + +/// Format month header with optional year and color. +pub fn format_month_header( + year: i32, + month: u32, + width: usize, + show_year: bool, + color: bool, +) -> String { + let month_name = get_month_name(month); + let header = if show_year { + format!("{} {}", month_name, year) + } else { + month_name + }; + let centered = center_text(&header, width); + if color { + format!("{}{}{}", COLOR_TEAL, centered, COLOR_RESET) + } else { + centered + } +} + +/// Center text within a specified width, accounting for Unicode character widths. +fn center_text(text: &str, width: usize) -> String { + let text_width = text.width(); + if text_width >= width { + return text.to_string(); + } + let total_padding = width - text_width; + let left_padding = total_padding.div_ceil(2); + let right_padding = total_padding - left_padding; + format!( + "{}{}{}", + " ".repeat(left_padding), + text, + " ".repeat(right_padding) + ) +} + +/// Get weekday order based on week start day. +pub fn get_weekday_order(week_start: Weekday) -> [Weekday; 7] { + match week_start { + Weekday::Mon => [ + Weekday::Mon, + Weekday::Tue, + Weekday::Wed, + Weekday::Thu, + Weekday::Fri, + Weekday::Sat, + Weekday::Sun, + ], + Weekday::Sun => [ + Weekday::Sun, + Weekday::Mon, + Weekday::Tue, + Weekday::Wed, + Weekday::Thu, + Weekday::Fri, + Weekday::Sat, + ], + _ => unreachable!(), + } +} + +/// Get 2-character weekday abbreviation for current locale. +pub fn get_weekday_short_name(weekday: Weekday, locale: Locale) -> String { + let base_date = NaiveDate::from_ymd_opt(2000, 1, 3).unwrap(); + let offset = weekday.num_days_from_monday() as i64; + let date = base_date + chrono::Duration::days(offset); + let day_name = date.format_localized("%a", locale).to_string(); + day_name.chars().take(2).collect() +} + +/// Format weekday header row with optional week numbers and color. +pub fn format_weekday_headers(ctx: &CalContext, week_numbers: bool) -> String { + let locale = get_system_locale(); + let mut result = String::new(); + + if week_numbers { + result.push_str(" "); + } + + if ctx.julian { + result.push(' '); + } + + let weekday_order = get_weekday_order(ctx.week_start); + + if ctx.color { + result.push_str(COLOR_SAND_YELLOW); + } + + for (i, &weekday) in weekday_order.iter().enumerate() { + let short_name = get_weekday_short_name(weekday, locale); + + if ctx.julian { + if i < 6 { + result.push_str(&format!("{} ", short_name)); + } else { + result.push_str(&format!(" {}", short_name)); + } + } else if i < 6 { + result.push_str(&format!("{} ", short_name)); + } else { + result.push_str(&short_name); + } + } + + if ctx.color { + result.push_str(COLOR_RESET); + } + + result +} + +/// Format day cell with color highlighting. +/// +/// Color priority: today > shortened day > weekend/holiday > regular +fn format_day( + ctx: &CalContext, + day: u32, + month: u32, + year: i32, + weekday: Weekday, + is_last: bool, +) -> String { + let is_today = ctx.color + && ctx.today.day() == day + && ctx.today.month() == month + && ctx.today.year() == year; + + let is_weekend = ctx.color && ctx.is_weekend(weekday); + let holiday_code = if ctx.color { + get_holiday_code(ctx, year, month, day) + } else { + 0 + }; + let day_str = format!("{:>2}", day); + + let formatted = if is_today { + format!("{}{}{}", COLOR_REVERSE, day_str, COLOR_RESET) + } else if holiday_code == 2 { + format!("{}{}{}", COLOR_TEAL, day_str, COLOR_RESET) + } else if is_weekend || holiday_code == 1 || holiday_code == 8 { + format!("{}{}{}", COLOR_RED, day_str, COLOR_RESET) + } else { + day_str + }; + + if is_last { + formatted + } else { + format!("{} ", formatted) + } +} + +/// Format month as grid of lines (horizontal layout). +pub fn format_month_grid(ctx: &CalContext, month: &MonthData) -> Vec { + let mut lines = Vec::with_capacity(8); + + let header_width = if ctx.julian { + 27 + } else if ctx.week_numbers { + 23 + } else { + 20 + }; + + let month_header = format_month_header( + month.year, + month.month, + header_width, + ctx.show_year_in_header, + ctx.color, + ); + lines.push(month_header); + + let weekday_header = format_weekday_headers(ctx, ctx.week_numbers); + lines.push(weekday_header); + + let mut day_idx = 0; + let total_days = month.days.len(); + + // Generate 6 weeks of calendar + for _week in 0..6 { + let mut line = String::new(); + + if ctx.week_numbers { + let week_wn = (0..7) + .filter_map(|d| { + let idx = day_idx + d; + if idx < total_days { + month.week_numbers.get(idx).copied().flatten() + } else { + None + } + }) + .next(); + + if let Some(wn) = week_wn { + line.push_str(&format!("{:>2} ", wn)); + } else { + line.push_str(" "); + } + } + + for day_in_week in 0..7 { + if day_idx >= total_days { + break; + } + let is_last = (day_in_week + 1) % 7 == 0; + + if let Some(day) = month.days[day_idx] { + if ctx.julian { + let doy = ctx.day_of_year(month.year, month.month, day); + let doy_str = format!("{:>3}", doy); + if is_last { + line.push_str(&doy_str); + } else { + line.push_str(&format!("{} ", doy_str)); + } + } else { + let weekday = month.weekdays[day_idx].unwrap(); + line.push_str(&format_day( + ctx, + day, + month.month, + month.year, + weekday, + is_last, + )); + } + } else if ctx.julian { + if is_last { + line.push_str(" "); + } else { + line.push_str(" "); + } + } else if is_last { + line.push_str(" "); + } else { + line.push_str(" "); + } + day_idx += 1; + } + + lines.push(line); + + if day_idx >= total_days { + break; + } + } + + lines +} + +/// Print single month in horizontal (default) or vertical layout. +pub fn print_month(ctx: &CalContext, year: i32, month: u32) { + preload_holidays(ctx, year, month); + + let month_data = MonthData::new(ctx, year, month); + if ctx.vertical { + print_month_vertical(ctx, &month_data, true); + } else { + let lines = format_month_grid(ctx, &month_data); + for line in lines { + println!("{}", line); + } + } +} + +/// Print single month in vertical layout (days in columns). +pub fn print_month_vertical(ctx: &CalContext, month: &MonthData, is_first: bool) { + let month_name = get_month_name(month.month); + let header = if ctx.show_year_in_header { + format!("{} {}", month_name, month.year) + } else { + month_name.to_string() + }; + let month_width = 18; + + let padded_header = if is_first { + format!( + " {: = weekday_order + .iter() + .map(|&w| get_weekday_short_name(w, locale)) + .collect(); + + for (row, weekday) in weekday_order.iter().enumerate() { + let day_short = &weekday_names[row]; + if ctx.color { + print!("{}{}{}", COLOR_SAND_YELLOW, day_short, COLOR_RESET); + } else { + print!("{}", day_short); + } + + for week in 0..6 { + let day_idx = (*weekday as usize) + 7 * week; + if day_idx < month.days.len() { + if let Some(day) = month.days[day_idx] { + print_day_vertical(ctx, day, month, *weekday); + } else { + print!(" "); + } + } + } + println!(); + } +} + +/// Print day cell in vertical layout with color highlighting. +fn print_day_vertical(ctx: &CalContext, day: u32, month: &MonthData, weekday: Weekday) { + let is_today = ctx.color + && ctx.today.day() == day + && ctx.today.month() == month.month + && ctx.today.year() == month.year; + + let is_weekend = ctx.color && ctx.is_weekend(weekday); + let holiday_code = if ctx.color { + get_holiday_code(ctx, month.year, month.month, day) + } else { + 0 + }; + let day_str = day.to_string(); + let padding = 3 - day_str.len(); + + let formatted = if is_today { + format!( + "{}{}{}{}", + " ".repeat(padding), + COLOR_REVERSE, + day, + COLOR_RESET + ) + } else if holiday_code == 2 { + format!( + "{}{}{}{}", + " ".repeat(padding), + COLOR_TEAL, + day, + COLOR_RESET + ) + } else if is_weekend || holiday_code == 1 || holiday_code == 8 { + format!("{}{}{}{}", " ".repeat(padding), COLOR_RED, day, COLOR_RESET) + } else { + format!("{:>3}", day) + }; + print!("{}", formatted); +} + +/// Print three months side by side (prev, current, next). +pub fn print_three_months(ctx: &CalContext, year: i32, month: u32) { + let prev_month = if month == 1 { 12 } else { month - 1 }; + let prev_year = if month == 1 { year - 1 } else { year }; + let next_month = if month == 12 { 1 } else { month + 1 }; + let next_year = if month == 12 { year + 1 } else { year }; + + preload_holidays(ctx, prev_year, prev_month); + preload_holidays(ctx, year, month); + preload_holidays(ctx, next_year, next_month); + + let months = vec![ + MonthData::new(ctx, prev_year, prev_month), + MonthData::new(ctx, year, month), + MonthData::new(ctx, next_year, next_month), + ]; + + if ctx.vertical { + print_three_months_vertical(ctx, &months); + } else { + print_months_side_by_side(ctx, &months); + } +} + +/// Print multiple months side by side in horizontal layout. +pub fn print_months_side_by_side(ctx: &CalContext, months: &[MonthData]) { + let grids: Vec> = months.iter().map(|m| format_month_grid(ctx, m)).collect(); + let max_height = grids.iter().map(|g| g.len()).max().unwrap_or(0); + + let month_width: usize = if ctx.julian { + 27 + } else if ctx.week_numbers { + 23 + } else { + 20 + }; + + for row in 0..max_height { + let mut line = String::new(); + for (i, grid) in grids.iter().enumerate() { + if row < grid.len() { + let text = &grid[row]; + let text_width = text.width(); + line.push_str(text); + let padding = month_width.saturating_sub(text_width); + for _ in 0..padding { + line.push(' '); + } + if i < grids.len() - 1 { + for _ in 0..ctx.gutter_width { + line.push(' '); + } + } + } else { + let width = if i < grids.len() - 1 { + month_width + ctx.gutter_width + } else { + month_width + }; + for _ in 0..width { + line.push(' '); + } + } + } + println!("{}", line); + } +} + +/// Print all 12 months of a year. +pub fn print_year(ctx: &CalContext, year: i32) { + if ctx.vertical { + println!("{}", center_text(&year.to_string(), 62)); + } else { + println!("{}", center_text(&year.to_string(), 66)); + } + println!(); + + if ctx.holidays { + preload_year_holidays(ctx, year); + } + + let mut month_ctx = ctx.clone(); + month_ctx.show_year_in_header = false; + month_ctx.gutter_width = if ctx.vertical { 1 } else { GUTTER_WIDTH_YEAR }; + + // Group months into rows of 3 + let mut month_rows = Vec::new(); + for month_row in 0..4 { + let mut months = Vec::new(); + for col in 0..3 { + let month = (month_row * 3 + col + 1) as u32; + if month <= 12 { + months.push(MonthData::new(&month_ctx, year, month)); + } + } + if !months.is_empty() { + month_rows.push(months); + } + } + + if ctx.vertical { + for months in month_rows.iter() { + print_three_months_vertical(&month_ctx, months); + } + } else { + for months in month_rows.iter() { + print_months_side_by_side(&month_ctx, months); + } + } +} + +/// Print three months in vertical layout. +pub fn print_three_months_vertical(ctx: &CalContext, months: &[MonthData]) { + let month_width = 18; + + // Print headers + for (i, month) in months.iter().enumerate() { + let month_name = get_month_name(month.month); + let header = if ctx.show_year_in_header { + format!("{} {}", month_name, month.year) + } else { + month_name.to_string() + }; + let padded_header = if i == 0 { + format!( + " {: = weekday_order + .iter() + .map(|&w| get_weekday_short_name(w, locale)) + .collect(); + + for (row, &weekday) in weekday_order.iter().enumerate() { + let day_short = &weekday_names[row]; + if ctx.color { + print!("{}{}{}", COLOR_SAND_YELLOW, day_short, COLOR_RESET); + } else { + print!("{}", day_short); + } + + for (month_idx, month) in months.iter().enumerate() { + if month_idx > 0 { + for _ in 0..ctx.gutter_width { + print!(" "); + } + } + + for week in 0..6 { + let day_idx = (weekday as usize) + 7 * week; + if day_idx < month.days.len() { + if let Some(day) = month.days[day_idx] { + print_day_vertical(ctx, day, month, weekday); + } else { + print!(" "); + } + } + } + } + println!(); + } + println!(); +} + +/// Print 12 months starting from a given month (--twelve mode). +pub fn print_twelve_months(ctx: &CalContext, start_year: i32, start_month: u32) { + // Preload holiday data for all 12 months + if ctx.holidays { + for i in 0..12 { + let mut month = start_month + i; + let mut year = start_year; + while month > 12 { + month -= 12; + year += 1; + } + preload_holidays(ctx, year, month); + } + } + + let mut month_ctx = ctx.clone(); + month_ctx.show_year_in_header = true; + month_ctx.gutter_width = GUTTER_WIDTH_YEAR; + + let months = (0..12) + .map(|i| { + let mut month = start_month + i; + let mut year = start_year; + while month > 12 { + month -= 12; + year += 1; + } + MonthData::new(&month_ctx, year, month) + }) + .collect::>(); + + if ctx.vertical { + for month_data in &months { + print_month_vertical(&month_ctx, month_data, true); + println!(); + } + } else { + for chunk in months.chunks(3) { + print_months_side_by_side(&month_ctx, chunk); + } + } +} + +/// Print a specified number of months (-n mode). +pub fn print_months_count( + ctx: &CalContext, + start_year: i32, + start_month: u32, + count: u32, +) -> Result<(), String> { + let months_per_row = ctx.months_per_row(); + + // Calculate start month for span mode (center around current month) + let (actual_start_year, actual_start_month) = if ctx.span && count > 1 { + let total_months = start_year * 12 + (start_month - 1) as i32; + let half = (count as i32 - 1) / 2; + let start = total_months - half; + let year = start.div_euclid(12); + let month = (start.rem_euclid(12) + 1) as u32; + (year, month) + } else { + (start_year, start_month) + }; + + // Preload holiday data for all months + if ctx.holidays { + for i in 0..count { + let mut month = actual_start_month + i; + let mut year = actual_start_year; + while month > 12 { + month -= 12; + year += 1; + } + while month < 1 { + month += 12; + year -= 1; + } + preload_holidays(ctx, year, month); + } + } + + let months = (0..count) + .map(|i| { + let mut month = actual_start_month + i; + let mut year = actual_start_year; + while month > 12 { + month -= 12; + year += 1; + } + while month < 1 { + month += 12; + year -= 1; + } + MonthData::new(ctx, year, month) + }) + .collect::>(); + + if ctx.vertical { + for month_data in &months { + print_month_vertical(ctx, month_data, true); + println!(); + } + } else { + for chunk in months.chunks(months_per_row as usize) { + print_months_side_by_side(ctx, chunk); + } + } + + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..67cbe9d --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,15 @@ +//! Calendar display utility with support for multiple calendar systems. +//! +//! Features: +//! - Gregorian and Julian calendar support +//! - Customizable week start (Monday/Sunday) +//! - Week numbers and Julian day display +//! - Plugin system for holiday highlighting + +pub mod args; +pub mod calendar; +pub mod formatter; +pub mod types; + +#[cfg(feature = "plugins")] +pub mod plugin_api; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..b5a79d9 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,45 @@ +//! Calendar CLI application. +//! +//! # Usage +//! ```ignore +//! cal // Current month +//! cal 2026 // Year 2026 +//! cal 2 2026 // February 2026 +//! cal -3 // Three months +//! cal -y // Whole year +//! ``` + +use cal::args::{Args, get_display_date}; +use cal::formatter::{ + print_month, print_months_count, print_three_months, print_twelve_months, print_year, +}; +use cal::types::CalContext; + +fn main() { + let args = Args::parse(); + + if let Err(e) = run(&args) { + eprintln!("cal: {}", e); + std::process::exit(1); + } +} + +fn run(args: &Args) -> Result<(), String> { + let ctx = CalContext::new(args)?; + let (year, month, _day) = get_display_date(args)?; + + // Display mode priority: year > twelve_months > three_months > months_count > single + if args.year { + print_year(&ctx, year); + } else if args.twelve_months { + print_twelve_months(&ctx, year, month); + } else if args.three_months { + print_three_months(&ctx, year, month); + } else if let Some(count) = args.months_count { + print_months_count(&ctx, year, month, count)?; + } else { + print_month(&ctx, year, month); + } + + Ok(()) +} diff --git a/src/plugin_api.rs b/src/plugin_api.rs new file mode 100644 index 0000000..a1d39c3 --- /dev/null +++ b/src/plugin_api.rs @@ -0,0 +1,176 @@ +//! Plugin API for dynamic loading of holiday highlighter. + +use libc::{c_char, c_int}; +use std::ffi::{CStr, CString}; +use std::path::Path; + +/// Handle to a loaded plugin. +pub struct PluginHandle { + #[allow(dead_code)] + lib: libloading::Library, + get_holidays_fn: libloading::Symbol< + 'static, + unsafe extern "C" fn(c_int, c_int, *const c_char) -> *mut c_char, + >, + free_holidays_fn: libloading::Symbol<'static, unsafe extern "C" fn(*mut c_char)>, + get_country_fn: libloading::Symbol<'static, unsafe extern "C" fn() -> *mut c_char>, + free_country_fn: libloading::Symbol<'static, unsafe extern "C" fn(*mut c_char)>, + is_holiday_fn: libloading::Symbol< + 'static, + unsafe extern "C" fn(c_int, c_int, c_int, *const c_char) -> c_int, + >, + get_year_holidays_fn: + libloading::Symbol<'static, unsafe extern "C" fn(c_int, *const c_char) -> *mut c_char>, +} + +impl PluginHandle { + /// Load plugin from path. + pub fn load>(path: P) -> Result { + let lib = unsafe { libloading::Library::new(path.as_ref())? }; + + unsafe { + let get_holidays_fn: libloading::Symbol< + unsafe extern "C" fn(c_int, c_int, *const c_char) -> *mut c_char, + > = lib.get(b"plugin_get_holidays")?; + + let free_holidays_fn: libloading::Symbol = + lib.get(b"plugin_free_holidays")?; + + let get_country_fn: libloading::Symbol *mut c_char> = + lib.get(b"plugin_get_country_from_locale")?; + + let free_country_fn: libloading::Symbol = + lib.get(b"plugin_free_country")?; + + let is_holiday_fn: libloading::Symbol< + unsafe extern "C" fn(c_int, c_int, c_int, *const c_char) -> c_int, + > = lib.get(b"plugin_is_holiday")?; + + let get_year_holidays_fn: libloading::Symbol< + unsafe extern "C" fn(c_int, *const c_char) -> *mut c_char, + > = lib.get(b"plugin_get_year_holidays")?; + + // Extend lifetime to match struct + let get_holidays_fn: libloading::Symbol< + 'static, + unsafe extern "C" fn(c_int, c_int, *const c_char) -> *mut c_char, + > = std::mem::transmute(get_holidays_fn); + let free_holidays_fn: libloading::Symbol<'static, unsafe extern "C" fn(*mut c_char)> = + std::mem::transmute(free_holidays_fn); + let get_country_fn: libloading::Symbol<'static, unsafe extern "C" fn() -> *mut c_char> = + std::mem::transmute(get_country_fn); + let free_country_fn: libloading::Symbol<'static, unsafe extern "C" fn(*mut c_char)> = + std::mem::transmute(free_country_fn); + let is_holiday_fn: libloading::Symbol< + 'static, + unsafe extern "C" fn(c_int, c_int, c_int, *const c_char) -> c_int, + > = std::mem::transmute(is_holiday_fn); + let get_year_holidays_fn: libloading::Symbol< + 'static, + unsafe extern "C" fn(c_int, *const c_char) -> *mut c_char, + > = std::mem::transmute(get_year_holidays_fn); + + Ok(PluginHandle { + lib, + get_holidays_fn, + free_holidays_fn, + get_country_fn, + free_country_fn, + is_holiday_fn, + get_year_holidays_fn, + }) + } + } + + /// Get holiday data for a specific month. + /// + /// Returns string where each character represents a day: + /// - '0' = working day, '1' = weekend, '2' = shortened, '8' = public holiday + pub fn get_holidays(&self, year: i32, month: u32, country: &str) -> Option { + let country_cstr = CString::new(country).ok()?; + + unsafe { + let result = + (self.get_holidays_fn)(year as c_int, month as c_int, country_cstr.as_ptr()); + if result.is_null() { + return None; + } + + let rust_str = CStr::from_ptr(result).to_str().ok()?.to_string(); + (self.free_holidays_fn)(result); + Some(rust_str) + } + } + + /// Get holiday data for entire year. + pub fn get_year_holidays(&self, year: i32, country: &str) -> Option { + let country_cstr = CString::new(country).ok()?; + + unsafe { + let result = (self.get_year_holidays_fn)(year as c_int, country_cstr.as_ptr()); + if result.is_null() { + return None; + } + + let rust_str = CStr::from_ptr(result).to_str().ok()?.to_string(); + (self.free_holidays_fn)(result); + Some(rust_str) + } + } + + /// Get country code from system locale. + pub fn get_country_from_locale(&self) -> String { + unsafe { + let result = (self.get_country_fn)(); + if result.is_null() { + return "RU".to_string(); + } + + let country = CStr::from_ptr(result).to_str().unwrap_or("RU").to_string(); + (self.free_country_fn)(result); + country + } + } + + /// Check if a specific day is a holiday. + /// + /// Returns: 0=working, 1=weekend, 2=shortened, 8=public, -1=error + pub fn is_holiday(&self, year: i32, month: u32, day: u32, country: &str) -> i32 { + let country_cstr = CString::new(country).unwrap(); + + unsafe { + (self.is_holiday_fn)( + year as c_int, + month as c_int, + day as c_int, + country_cstr.as_ptr(), + ) + } + } +} + +/// Try to load the holiday plugin from standard locations. +pub fn try_load_plugin() -> Option { + let search_paths = [ + // Build directory (development) + "./target/debug/libholiday_highlighter.so", + "./target/release/libholiday_highlighter.so", + // User local directory + "~/.local/lib/cal/plugins/libholiday_highlighter.so", + // System directory + "/usr/lib/cal/plugins/libholiday_highlighter.so", + "/usr/local/lib/cal/plugins/libholiday_highlighter.so", + // Relative to executable + "./plugins/libholiday_highlighter.so", + "./libholiday_highlighter.so", + ]; + + for path in &search_paths { + let expanded = shellexpand::tilde(path); + if let Ok(handle) = PluginHandle::load(expanded.as_ref()) { + return Some(handle); + } + } + + None +} diff --git a/src/types.rs b/src/types.rs new file mode 100644 index 0000000..efcf7ba --- /dev/null +++ b/src/types.rs @@ -0,0 +1,110 @@ +//! Type definitions and constants for calendar formatting. + +use chrono::Weekday; +use clap::ValueEnum; + +/// Calendar reform type determining which calendar system to use. +#[derive(Debug, Clone, Copy, PartialEq, ValueEnum)] +#[value(rename_all = "lowercase")] +pub enum ReformType { + /// Gregorian calendar (always). + Gregorian, + /// ISO 8601 calendar (same as Gregorian). + Iso, + /// Julian calendar (always). + Julian, + /// Great Britain reform year 1752 (September 3-13 were skipped). + #[value(name = "1752")] + Year1752, +} + +impl ReformType { + /// Return the reform year for this calendar type. + pub fn reform_year(self) -> i32 { + match self { + ReformType::Gregorian | ReformType::Iso => i32::MIN, + ReformType::Julian => i32::MAX, + ReformType::Year1752 => REFORM_YEAR_GB, + } + } +} + +/// Week numbering system for calendar display. +#[derive(Debug, Clone, Copy, PartialEq, ValueEnum)] +pub enum WeekType { + /// ISO 8601: week starts on Monday, week 1 contains the first Thursday. + Iso, + /// US style: week starts on Sunday, week 1 contains January 1. + Us, +} + +/// Column display mode for multi-month layouts. +#[derive(Debug, Clone, Copy)] +pub enum ColumnsMode { + /// Fixed number of columns. + Fixed(u32), + /// Auto-detect from terminal width. + Auto, +} + +/// Calendar formatting context containing all display options. +#[derive(Clone, Debug)] +pub struct CalContext { + /// Year when calendar reform occurred (i32::MIN = always Gregorian, i32::MAX = always Julian). + pub reform_year: i32, + /// First day of the week (Monday or Sunday). + pub week_start: Weekday, + /// Whether to display Julian day numbers (day of year). + pub julian: bool, + /// Whether to display ISO week numbers. + pub week_numbers: bool, + /// Week numbering system (ISO or US). + pub week_type: WeekType, + /// Whether to use ANSI color codes in output. + pub color: bool, + /// Whether to display days vertically (days in columns instead of rows). + pub vertical: bool, + /// Today's date for highlighting. + pub today: chrono::NaiveDate, + /// Whether to show year in month headers. + pub show_year_in_header: bool, + /// Width of gutter between months in multi-month display. + pub gutter_width: usize, + /// Column display mode. + pub columns: ColumnsMode, + /// Whether to center the date range when displaying multiple months. + pub span: bool, + /// Whether to highlight holidays using isdayoff.ru API. + #[cfg(feature = "plugins")] + pub holidays: bool, +} + +/// Calendar data for a single month. +pub struct MonthData { + pub year: i32, + pub month: u32, + pub days: Vec>, + pub week_numbers: Vec>, + pub weekdays: Vec>, +} + +// Constants for calendar formatting +pub const CELLS_PER_MONTH: usize = 42; // 6 weeks × 7 days +pub const GUTTER_WIDTH_REGULAR: usize = 2; +pub const GUTTER_WIDTH_YEAR: usize = 3; + +// Color is enabled by default for better user experience +pub const COLOR_ENABLED_BY_DEFAULT: bool = true; + +// Reform year for September 1752 (missing days 3-13 in Great Britain) +pub const REFORM_YEAR_GB: i32 = 1752; +pub const REFORM_MONTH: u32 = 9; +pub const REFORM_FIRST_DAY: u32 = 3; +pub const REFORM_LAST_DAY: u32 = 13; + +// ANSI color codes +pub const COLOR_RESET: &str = "\x1b[0m"; +pub const COLOR_REVERSE: &str = "\x1b[7m"; +pub const COLOR_RED: &str = "\x1b[91m"; +pub const COLOR_TEAL: &str = "\x1b[96m"; +pub const COLOR_SAND_YELLOW: &str = "\x1b[93m"; diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs new file mode 100644 index 0000000..4576d2b --- /dev/null +++ b/tests/integration_tests.rs @@ -0,0 +1,700 @@ +//! Integration tests for calendar calculation logic. + +use chrono::Weekday; +use unicode_width::UnicodeWidthStr; + +use cal::formatter::parse_month; +use cal::types::{CalContext, ColumnsMode, MonthData, ReformType, WeekType}; + +fn test_context() -> CalContext { + CalContext { + reform_year: ReformType::Year1752.reform_year(), + week_start: Weekday::Mon, + julian: false, + week_numbers: false, + week_type: WeekType::Iso, + color: false, + vertical: false, + today: chrono::NaiveDate::from_ymd_opt(2026, 2, 18).unwrap(), + show_year_in_header: true, + gutter_width: 2, + columns: ColumnsMode::Auto, + span: false, + #[cfg(feature = "plugins")] + holidays: false, + } +} + +fn julian_context() -> CalContext { + CalContext { + reform_year: ReformType::Julian.reform_year(), + ..test_context() + } +} + +fn gregorian_context() -> CalContext { + CalContext { + reform_year: ReformType::Gregorian.reform_year(), + ..test_context() + } +} + +mod leap_year_tests { + use super::*; + + #[test] + fn test_gregorian_leap_year_divisible_by_400() { + let ctx = gregorian_context(); + assert!(ctx.is_leap_year(2000)); + assert!(ctx.is_leap_year(2400)); + } + + #[test] + fn test_gregorian_leap_year_divisible_by_4_not_100() { + let ctx = gregorian_context(); + assert!(ctx.is_leap_year(2024)); + assert!(ctx.is_leap_year(2028)); + assert!(!ctx.is_leap_year(2023)); + assert!(!ctx.is_leap_year(2025)); + } + + #[test] + fn test_gregorian_not_leap_year_divisible_by_100() { + let ctx = gregorian_context(); + assert!(!ctx.is_leap_year(1900)); + assert!(!ctx.is_leap_year(2100)); + } + + #[test] + fn test_julian_leap_year() { + let ctx = julian_context(); + assert!(ctx.is_leap_year(2024)); + assert!(ctx.is_leap_year(2028)); + assert!(ctx.is_leap_year(1900)); + assert!(!ctx.is_leap_year(2023)); + } + + #[test] + fn test_year_1752_leap_year() { + let ctx = test_context(); + assert!(ctx.is_leap_year(1752)); + } +} + +mod days_in_month_tests { + use super::*; + + #[test] + fn test_31_day_months() { + let ctx = test_context(); + for month in [1, 3, 5, 7, 8, 10, 12] { + assert_eq!(ctx.days_in_month(2024, month), 31); + } + } + + #[test] + fn test_30_day_months() { + let ctx = test_context(); + for month in [4, 6, 9, 11] { + assert_eq!(ctx.days_in_month(2024, month), 30); + } + } + + #[test] + fn test_february_leap_year() { + let ctx = test_context(); + assert_eq!(ctx.days_in_month(2024, 2), 29); + assert_eq!(ctx.days_in_month(2028, 2), 29); + } + + #[test] + fn test_february_non_leap_year() { + let ctx = test_context(); + assert_eq!(ctx.days_in_month(2023, 2), 28); + assert_eq!(ctx.days_in_month(2025, 2), 28); + } +} + +mod first_day_tests { + use super::*; + + #[test] + fn test_first_day_known_dates() { + let ctx = test_context(); + + assert_eq!(ctx.first_day_of_month(2024, 1), Weekday::Mon); + assert_eq!(ctx.first_day_of_month(2025, 1), Weekday::Wed); + assert_eq!(ctx.first_day_of_month(2024, 2), Weekday::Thu); + } + + #[test] + fn test_first_day_september_1752() { + let ctx = test_context(); + assert_eq!(ctx.first_day_of_month(1752, 9), Weekday::Fri); + } +} + +mod reform_gap_tests { + use super::*; + + #[test] + fn test_reform_gap_detection() { + let ctx = test_context(); + + assert!(ctx.is_reform_gap(1752, 9, 3)); + assert!(ctx.is_reform_gap(1752, 9, 13)); + assert!(ctx.is_reform_gap(1752, 9, 8)); + + assert!(!ctx.is_reform_gap(1752, 9, 2)); + assert!(!ctx.is_reform_gap(1752, 9, 14)); + + assert!(!ctx.is_reform_gap(1752, 8, 5)); + assert!(!ctx.is_reform_gap(1752, 10, 5)); + assert!(!ctx.is_reform_gap(1751, 9, 5)); + } + + #[test] + fn test_no_reform_gap_gregorian() { + let ctx = gregorian_context(); + assert!(!ctx.is_reform_gap(1752, 9, 5)); + } + + #[test] + fn test_no_reform_gap_julian() { + let ctx = julian_context(); + assert!(!ctx.is_reform_gap(1752, 9, 5)); + } +} + +mod day_of_year_tests { + use super::*; + + #[test] + fn test_day_of_year_non_leap() { + let ctx = test_context(); + + assert_eq!(ctx.day_of_year(2023, 1, 1), 1); + assert_eq!(ctx.day_of_year(2023, 1, 31), 31); + assert_eq!(ctx.day_of_year(2023, 2, 1), 32); + assert_eq!(ctx.day_of_year(2023, 12, 31), 365); + } + + #[test] + fn test_day_of_year_leap() { + let ctx = test_context(); + + assert_eq!(ctx.day_of_year(2024, 1, 1), 1); + assert_eq!(ctx.day_of_year(2024, 2, 29), 60); + assert_eq!(ctx.day_of_year(2024, 3, 1), 61); + assert_eq!(ctx.day_of_year(2024, 12, 31), 366); + } + + #[test] + fn test_day_of_year_reform_gap() { + let ctx = test_context(); + + assert_eq!(ctx.day_of_year(1752, 9, 2), 235); + assert_eq!(ctx.day_of_year(1752, 9, 14), 247); + } +} + +mod month_data_tests { + use super::*; + + #[test] + fn test_month_data_january_2024() { + let ctx = test_context(); + let month = MonthData::new(&ctx, 2024, 1); + + assert_eq!(month.year, 2024); + assert_eq!(month.month, 1); + assert_eq!(month.days.len(), 42); + + // January 2024 starts on Monday + assert_eq!(month.days[0], Some(1)); + assert_eq!(month.days[30], Some(31)); + assert_eq!(month.days[31], None); + } + + #[test] + fn test_month_data_february_2024_leap() { + let ctx = test_context(); + let month = MonthData::new(&ctx, 2024, 2); + + // February 2024 starts on Thursday + assert_eq!(month.days[0], None); + assert_eq!(month.days[1], None); + assert_eq!(month.days[2], None); + assert_eq!(month.days[3], Some(1)); + assert_eq!(month.days[31], Some(29)); + } + + #[test] + fn test_month_data_september_1752_reform() { + let ctx = test_context(); + let month = MonthData::new(&ctx, 1752, 9); + + assert!(month.days.contains(&Some(2))); + assert!(!month.days.contains(&Some(3))); + assert!(!month.days.contains(&Some(13))); + assert!(month.days.contains(&Some(14))); + } + + #[test] + fn test_month_data_weekday_alignment() { + let ctx = test_context(); + + for month in 1..=12 { + let month_data = MonthData::new(&ctx, 2024, month); + + for (i, day) in month_data.days.iter().enumerate() { + if day.is_some() { + assert!(month_data.weekdays[i].is_some()); + } else { + assert!(month_data.weekdays[i].is_none()); + } + } + } + } +} + +mod weekend_tests { + use super::*; + + #[test] + fn test_is_weekend() { + let ctx = test_context(); + + assert!(ctx.is_weekend(Weekday::Sat)); + assert!(ctx.is_weekend(Weekday::Sun)); + assert!(!ctx.is_weekend(Weekday::Mon)); + assert!(!ctx.is_weekend(Weekday::Tue)); + assert!(!ctx.is_weekend(Weekday::Wed)); + assert!(!ctx.is_weekend(Weekday::Thu)); + assert!(!ctx.is_weekend(Weekday::Fri)); + } +} + +mod week_number_tests { + use super::*; + + #[test] + fn test_iso_week_number() { + let mut ctx = test_context(); + ctx.week_type = WeekType::Iso; + + assert_eq!(ctx.week_number(2024, 1, 1), 1); + + let week = ctx.week_number(2024, 12, 30); + assert!(week == 1 || week == 53); + } + + #[test] + fn test_us_week_number() { + let mut ctx = test_context(); + ctx.week_type = WeekType::Us; + + assert_eq!(ctx.week_number(2024, 1, 1), 1); + } +} + +mod context_validation_tests { + use super::*; + use cal::args::Args; + use clap::Parser; + + #[test] + fn test_context_creation_default() { + let args = Args::parse_from(["cal"]); + let ctx = CalContext::new(&args); + assert!(ctx.is_ok()); + } + + #[test] + fn test_context_creation_with_options() { + let args = Args::parse_from(["cal", "-y", "-j", "-w"]); + let ctx = CalContext::new(&args); + assert!(ctx.is_ok()); + let ctx = ctx.unwrap(); + assert!(ctx.julian); + assert!(ctx.week_numbers); + } + + #[test] + fn test_context_mutually_exclusive_options() { + let args = Args::parse_from(["cal", "-y", "-n", "5"]); + let result = CalContext::new(&args); + assert!(result.is_err()); + let err = result.unwrap_err(); + assert!(err.contains("mutually exclusive")); + } + + #[test] + fn test_context_invalid_columns() { + let args = Args::parse_from(["cal", "-c", "0"]); + assert!(CalContext::new(&args).is_err()); + + let args = Args::parse_from(["cal", "-c", "abc"]); + assert!(CalContext::new(&args).is_err()); + } + + #[test] + fn test_context_sunday_start() { + let args = Args::parse_from(["cal", "-s"]); + let ctx = CalContext::new(&args).unwrap(); + assert_eq!(ctx.week_start, Weekday::Sun); + } + + #[test] + fn test_context_color_settings() { + let args = Args::parse_from(["cal"]); + let ctx = CalContext::new(&args).unwrap(); + assert!(!ctx.color); + + let args = Args::parse_from(["cal", "--color"]); + let ctx = CalContext::new(&args).unwrap(); + assert!(!ctx.color); + } + + #[test] + fn test_context_reform_types() { + let args = Args::parse_from(["cal", "--reform", "gregorian"]); + let ctx = CalContext::new(&args).unwrap(); + assert_eq!(ctx.reform_year, i32::MIN); + + let args = Args::parse_from(["cal", "--reform", "julian"]); + let ctx = CalContext::new(&args).unwrap(); + assert_eq!(ctx.reform_year, i32::MAX); + + let args = Args::parse_from(["cal", "--iso"]); + let ctx = CalContext::new(&args).unwrap(); + assert_eq!(ctx.reform_year, i32::MIN); + } +} + +mod parse_month_tests { + use super::*; + + #[test] + fn test_parse_month_numeric() { + for (input, expected) in [ + ("1", Some(1)), + ("2", Some(2)), + ("12", Some(12)), + ("0", None), + ("13", None), + ("abc", None), + ] { + assert_eq!(parse_month(input), expected, "Failed for input: {}", input); + } + } + + #[test] + fn test_parse_month_english_names() { + assert_eq!(parse_month("january"), Some(1)); + assert_eq!(parse_month("January"), Some(1)); + assert_eq!(parse_month("JANUARY"), Some(1)); + assert_eq!(parse_month("february"), Some(2)); + assert_eq!(parse_month("december"), Some(12)); + } + + #[test] + fn test_parse_month_english_short() { + assert_eq!(parse_month("jan"), Some(1)); + assert_eq!(parse_month("feb"), Some(2)); + assert_eq!(parse_month("mar"), Some(3)); + assert_eq!(parse_month("apr"), Some(4)); + assert_eq!(parse_month("jun"), Some(6)); + assert_eq!(parse_month("jul"), Some(7)); + assert_eq!(parse_month("aug"), Some(8)); + assert_eq!(parse_month("sep"), Some(9)); + assert_eq!(parse_month("oct"), Some(10)); + assert_eq!(parse_month("nov"), Some(11)); + assert_eq!(parse_month("dec"), Some(12)); + } + + #[test] + fn test_parse_month_russian() { + assert_eq!(parse_month("январь"), Some(1)); + assert_eq!(parse_month("февраль"), Some(2)); + assert_eq!(parse_month("декабрь"), Some(12)); + } +} + +mod layout_tests { + use super::*; + use cal::formatter::{ + format_month_grid, format_month_header, format_weekday_headers, get_weekday_order, + }; + + #[test] + fn test_month_header_format() { + let header = format_month_header(2026, 2, 20, true, false); + assert!(header.contains("Февраль")); + assert!(header.contains("2026")); + assert!(header.width() >= 20); + } + + #[test] + fn test_month_header_without_year() { + let header = format_month_header(2026, 2, 20, false, false); + assert!(header.contains("Февраль")); + assert!(!header.contains("2026")); + } + + #[test] + fn test_month_header_with_color() { + let header = format_month_header(2026, 2, 20, true, true); + assert!(header.contains("\x1b[96m")); + assert!(header.contains("\x1b[0m")); + } + + #[test] + fn test_weekday_header_structure_monday_start() { + let ctx = test_context(); + let header = format_weekday_headers(&ctx, false); + + assert!(header.contains("Пн")); + assert!(header.contains("Вт")); + assert!(header.contains("Ср")); + assert!(header.contains("Чт")); + assert!(header.contains("Пт")); + assert!(header.contains("Сб")); + assert!(header.contains("Вс")); + + let mon_pos = header.find("Пн").unwrap(); + let tue_pos = header.find("Вт").unwrap(); + assert!(mon_pos < tue_pos); + } + + #[test] + fn test_weekday_header_structure_sunday_start() { + let mut ctx = test_context(); + ctx.week_start = chrono::Weekday::Sun; + let header = format_weekday_headers(&ctx, false); + + let sun_pos = header.find("Вс").unwrap(); + let mon_pos = header.find("Пн").unwrap(); + assert!(sun_pos < mon_pos); + } + + #[test] + fn test_weekday_header_with_week_numbers() { + let mut ctx = test_context(); + ctx.week_numbers = true; + let header = format_weekday_headers(&ctx, false); + assert!(header.len() > 20); + } + + #[test] + fn test_weekday_header_with_julian() { + let mut ctx = test_context(); + ctx.julian = true; + let header = format_weekday_headers(&ctx, false); + assert!(header.starts_with(" ")); + } + + #[test] + fn test_month_grid_line_count() { + let ctx = test_context(); + let month = MonthData::new(&ctx, 2024, 1); + let grid = format_month_grid(&ctx, &month); + + assert!(grid.len() >= 8); + assert!(grid.len() <= 9); + } + + #[test] + fn test_month_grid_first_line_is_header() { + let ctx = test_context(); + let month = MonthData::new(&ctx, 2024, 1); + let grid = format_month_grid(&ctx, &month); + + assert!(grid[0].contains("Январь")); + assert!(grid[0].contains("2024")); + } + + #[test] + fn test_month_grid_second_line_is_weekdays() { + let ctx = test_context(); + let month = MonthData::new(&ctx, 2024, 1); + let grid = format_month_grid(&ctx, &month); + + assert!(grid[1].contains("Пн")); + } + + #[test] + fn test_month_grid_contains_day_1() { + let ctx = test_context(); + let month = MonthData::new(&ctx, 2024, 1); + let grid = format_month_grid(&ctx, &month); + + let days_area: String = grid[2..].join("\n"); + assert!(days_area.contains(" 1")); + } + + #[test] + fn test_month_grid_contains_last_day() { + let ctx = test_context(); + let month = MonthData::new(&ctx, 2024, 1); + let grid = format_month_grid(&ctx, &month); + + let days_area: String = grid[2..].join("\n"); + assert!(days_area.contains("31")); + } + + #[test] + fn test_month_grid_february_leap_year() { + let ctx = test_context(); + let month = MonthData::new(&ctx, 2024, 2); + let grid = format_month_grid(&ctx, &month); + + let days_area: String = grid[2..].join("\n"); + assert!(days_area.contains("29")); + } + + #[test] + fn test_month_grid_february_non_leap_year() { + let ctx = test_context(); + let month = MonthData::new(&ctx, 2023, 2); + let grid = format_month_grid(&ctx, &month); + + let days_area: String = grid[2..].join("\n"); + assert!(days_area.contains("28")); + assert!(!days_area.contains("29")); + } + + #[test] + fn test_vertical_layout_weekday_order() { + let ctx = test_context(); + let weekday_order = get_weekday_order(ctx.week_start); + + assert_eq!(weekday_order[0], chrono::Weekday::Mon); + assert_eq!(weekday_order[6], chrono::Weekday::Sun); + } + + #[test] + fn test_vertical_layout_days_in_columns() { + let ctx = test_context(); + let month = MonthData::new(&ctx, 2024, 1); + + let day1_pos = month.days.iter().position(|&d| d == Some(1)).unwrap(); + let day8_pos = month.days.iter().position(|&d| d == Some(8)).unwrap(); + + assert_eq!(day8_pos - day1_pos, 7); + } + + #[test] + fn test_three_months_structure() { + let ctx = test_context(); + let prev = MonthData::new(&ctx, 2024, 1); + let curr = MonthData::new(&ctx, 2024, 2); + let next = MonthData::new(&ctx, 2024, 3); + + assert_eq!(prev.month, 1); + assert_eq!(curr.month, 2); + assert_eq!(next.month, 3); + } + + #[test] + fn test_three_months_year_boundary() { + let ctx = test_context(); + + let prev = MonthData::new(&ctx, 2023, 12); + let curr = MonthData::new(&ctx, 2024, 1); + let next = MonthData::new(&ctx, 2024, 2); + + assert_eq!(prev.year, 2023); + assert_eq!(curr.year, 2024); + assert_eq!(next.year, 2024); + } + + #[test] + fn test_color_codes_in_header() { + let header = format_month_header(2024, 1, 20, true, true); + assert!(header.starts_with("\x1b[96m")); + assert!(header.ends_with("\x1b[0m")); + } + + #[test] + fn test_no_color_codes_when_disabled() { + let header = format_month_header(2024, 1, 20, true, false); + assert!(!header.contains("\x1b[96m")); + assert!(!header.contains("\x1b[0m")); + } + + #[test] + fn test_weekday_header_color_placement() { + let mut ctx = test_context(); + ctx.color = true; + let header = format_weekday_headers(&ctx, false); + + assert!(header.starts_with("\x1b[93m")); + assert!(header.ends_with("\x1b[0m")); + } + + #[test] + fn test_weekday_header_no_color_when_disabled() { + let mut ctx = test_context(); + ctx.color = false; + let header = format_weekday_headers(&ctx, false); + + assert!(!header.contains("\x1b[93m")); + assert!(!header.contains("\x1b[0m")); + } + + #[test] + fn test_header_width_consistency() { + let width = 20; + let header1 = format_month_header(2024, 1, width, true, false); + let header2 = format_month_header(2024, 12, width, true, false); + + assert_eq!(header1.width(), width); + assert_eq!(header2.width(), width); + } + + #[test] + fn test_day_alignment_in_grid() { + let ctx = test_context(); + let month = MonthData::new(&ctx, 2024, 1); + let grid = format_month_grid(&ctx, &month); + + let expected_width = grid[2].width(); + for (i, line) in grid.iter().enumerate().skip(2) { + assert_eq!( + line.width(), + expected_width, + "Line {} has inconsistent width", + i + ); + } + } +} + +mod get_display_date_tests { + use cal::args::{Args, get_display_date}; + use clap::Parser; + + #[test] + fn test_single_year_argument() { + let args = Args::parse_from(["cal", "2026"]); + let (year, _month, day) = get_display_date(&args).unwrap(); + assert_eq!(year, 2026); + assert_eq!(day, None); + } + + #[test] + fn test_single_month_argument() { + let args = Args::parse_from(["cal", "2"]); + let (_year, month, _day) = get_display_date(&args).unwrap(); + assert_eq!(month, 2); + } + + #[test] + fn test_month_year_arguments() { + let args = Args::parse_from(["cal", "2", "2026"]); + let (year, month, _day) = get_display_date(&args).unwrap(); + assert_eq!(year, 2026); + assert_eq!(month, 2); + } +}