From f0dc66a3e8d229384706277e05f70917f8e1134a Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sun, 16 Mar 2025 21:13:24 +0200 Subject: [PATCH] wip: properly trimmed text, add some libs --- Cargo.lock | 435 +++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 26 +-- src/main.rs | 1 + src/view.rs | 72 +------- src/view/column.rs | 77 ++++++++ src/view/string.rs | 49 +++++ 6 files changed, 569 insertions(+), 91 deletions(-) create mode 100644 src/view/column.rs create mode 100644 src/view/string.rs diff --git a/Cargo.lock b/Cargo.lock index 2a9d7bf..51833d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,17 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "ahash" +version = "0.7.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + [[package]] name = "allocator-api2" version = "0.2.21" @@ -82,6 +93,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "arrayvec" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" + [[package]] name = "atomic_float" version = "1.1.0" @@ -125,6 +142,41 @@ version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + +[[package]] +name = "borsh" +version = "1.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5430e3be710b68d984d1391c854eb431a9d548640711faa54eecb1df93db91cc" +dependencies = [ + "borsh-derive", + "cfg_aliases", +] + +[[package]] +name = "borsh-derive" +version = "1.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8b668d39970baad5356d7c83a86fee3a539e6f93bf6764c97368243e17a0487" +dependencies = [ + "once_cell", + "proc-macro-crate", + "proc-macro2", + "quote", + "syn 2.0.100", +] + [[package]] name = "bstr" version = "1.11.3" @@ -148,12 +200,51 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "64fa3c856b712db6612c019f14756e64e4bcea13337a6b33b696333a9eaa2d06" +[[package]] +name = "byte-unit" +version = "5.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cd29c3c585209b0cbc7309bfe3ed7efd8c84c21b7af29c8bfae908f8777174" +dependencies = [ + "rust_decimal", + "serde", + "utf8-width", +] + +[[package]] +name = "bytecheck" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23cdc57ce23ac53c931e88a43d06d070a6fd142f2617be5855eb75efc9beb1c2" +dependencies = [ + "bytecheck_derive", + "ptr_meta", + "simdutf8", +] + +[[package]] +name = "bytecheck_derive" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3db406d29fbcd95542e92559bed4d8ad92636d1ca8b3b72ede10b4bcc010e659" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "byteorder" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" +[[package]] +name = "bytes" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" + [[package]] name = "cassowary" version = "0.3.0" @@ -184,6 +275,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + [[package]] name = "clap" version = "4.5.32" @@ -310,7 +407,7 @@ dependencies = [ "proc-macro2", "quote", "strsim", - "syn", + "syn 2.0.100", ] [[package]] @@ -321,7 +418,7 @@ checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" dependencies = [ "darling_core", "quote", - "syn", + "syn 2.0.100", ] [[package]] @@ -411,6 +508,12 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0d2fde1f7b3d48b8395d5f2de76c18a528bd6a9cdde438df747bfcba3e05d6f" +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + [[package]] name = "getrandom" version = "0.2.15" @@ -428,6 +531,15 @@ version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +dependencies = [ + "ahash", +] + [[package]] name = "hashbrown" version = "0.15.2" @@ -468,6 +580,16 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" +[[package]] +name = "indexmap" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3954d50fe15b02142bf25d3b8bdadb634ec3948f103d04ffe3031bc8fe9d7058" +dependencies = [ + "equivalent", + "hashbrown 0.15.2", +] + [[package]] name = "indoc" version = "2.0.6" @@ -484,7 +606,7 @@ dependencies = [ "indoc", "proc-macro2", "quote", - "syn", + "syn 2.0.100", ] [[package]] @@ -600,7 +722,7 @@ checksum = "ed9983e64b2358522f745c1251924e3ab7252d55637e80f6a0a3de642d6a9efc" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.100", ] [[package]] @@ -615,7 +737,7 @@ version = "0.12.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "234cf4f4a04dc1f57e24b96cc0cd600cf2af460d4161ac5ecdd0af8e1f3b2a38" dependencies = [ - "hashbrown", + "hashbrown 0.15.2", ] [[package]] @@ -654,6 +776,24 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -736,7 +876,7 @@ dependencies = [ "by_address", "proc-macro2", "quote", - "syn", + "syn 2.0.100", ] [[package]] @@ -798,7 +938,7 @@ dependencies = [ "phf_shared", "proc-macro2", "quote", - "syn", + "syn 2.0.100", ] [[package]] @@ -825,6 +965,24 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "primal-check" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc0d895b311e3af9902528fbb8f928688abbd95872819320517cc24ca6b2bd08" +dependencies = [ + "num-integer", +] + +[[package]] +name = "proc-macro-crate" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edce586971a4dfaa28950c6f18ed55e0406c1ab88bbce2c6f6293a7aaba73d35" +dependencies = [ + "toml_edit", +] + [[package]] name = "proc-macro2" version = "1.0.94" @@ -834,6 +992,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "ptr_meta" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0738ccf7ea06b608c10564b31debd4f5bc5e197fc8bfe088f68ae5ce81e7a4f1" +dependencies = [ + "ptr_meta_derive", +] + +[[package]] +name = "ptr_meta_derive" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16b845dbfca988fa33db069c0e230574d15a3088f147a87b64c7589eb662c9ac" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "quanta" version = "0.12.5" @@ -858,6 +1036,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + [[package]] name = "rand" version = "0.8.5" @@ -918,6 +1102,15 @@ dependencies = [ "bitflags", ] +[[package]] +name = "realfft" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "390252372b7f2aac8360fc5e72eba10136b166d6faeed97e6d0c8324eb99b2b1" +dependencies = [ + "rustfft", +] + [[package]] name = "redox_syscall" version = "0.5.10" @@ -933,12 +1126,93 @@ version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +[[package]] +name = "rend" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71fe3824f5629716b1589be05dacd749f6aa084c87e00e016714a8cdfccc997c" +dependencies = [ + "bytecheck", +] + +[[package]] +name = "rkyv" +version = "0.7.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9008cd6385b9e161d8229e1f6549dd23c3d022f132a2ea37ac3a10ac4935779b" +dependencies = [ + "bitvec", + "bytecheck", + "bytes", + "hashbrown 0.12.3", + "ptr_meta", + "rend", + "rkyv_derive", + "seahash", + "tinyvec", + "uuid", +] + +[[package]] +name = "rkyv_derive" +version = "0.7.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "503d1d27590a2b0a3a4ca4c94755aa2875657196ecbf401a42eff41d7de532c0" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "rubato" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdd96992d7e24b3d7f35fdfe02af037a356ac90d41b466945cf3333525a86eea" +dependencies = [ + "num-complex", + "num-integer", + "num-traits", + "realfft", +] + +[[package]] +name = "rust_decimal" +version = "1.36.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b082d80e3e3cc52b2ed634388d436fe1f4de6af5786cc2de9ba9737527bdf555" +dependencies = [ + "arrayvec", + "borsh", + "bytes", + "num-traits", + "rand", + "rkyv", + "serde", + "serde_json", +] + [[package]] name = "rustc-demangle" version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" +[[package]] +name = "rustfft" +version = "6.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43806561bc506d0c5d160643ad742e3161049ac01027b5e6d7524091fd401d86" +dependencies = [ + "num-complex", + "num-integer", + "num-traits", + "primal-check", + "strength_reduce", + "transpose", + "version_check", +] + [[package]] name = "rustix" version = "0.38.44" @@ -958,6 +1232,16 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2" +[[package]] +name = "rusty-chromaprint" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59e4234523e38d9c12201955f8216e1a60313e64c5077f4e1cf49b0db77bd7e8" +dependencies = [ + "rubato", + "rustfft", +] + [[package]] name = "ryu" version = "1.0.20" @@ -979,6 +1263,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "seahash" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c107b6f4780854c8b126e228ea8869f4d7b71260f962fefb57b996b8959ba6b" + [[package]] name = "serde" version = "1.0.219" @@ -996,7 +1286,19 @@ checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.100", +] + +[[package]] +name = "serde_json" +version = "1.0.140" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", ] [[package]] @@ -1035,6 +1337,12 @@ dependencies = [ "libc", ] +[[package]] +name = "simdutf8" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" + [[package]] name = "siphasher" version = "1.0.1" @@ -1053,6 +1361,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strength_reduce" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe895eb47f22e2ddd4dabc02bce419d2e643c8e3b585c78158b349195bc24d82" + [[package]] name = "strsim" version = "0.11.1" @@ -1078,7 +1392,18 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn", + "syn 2.0.100", +] + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", ] [[package]] @@ -1096,6 +1421,7 @@ dependencies = [ name = "taggart" version = "0.1.0" dependencies = [ + "byte-unit", "clap", "enolib", "file_type", @@ -1105,12 +1431,19 @@ dependencies = [ "opener", "opus_headers", "pad", + "rusty-chromaprint", "tengri", "unicode-width 0.2.0", "walkdir", "xxhash-rust", ] +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + [[package]] name = "tengri" version = "0.1.0" @@ -1148,6 +1481,48 @@ dependencies = [ "tengri_output", ] +[[package]] +name = "tinyvec" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09b3661f17e86524eccd4371ab0429194e0d7c008abb45f7a7495b1719463c71" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "toml_datetime" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41" + +[[package]] +name = "toml_edit" +version = "0.22.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17b4795ff5edd201c7cd6dca065ae59972ce77d1b80fa0a84d94950ece7d1474" +dependencies = [ + "indexmap", + "toml_datetime", + "winnow", +] + +[[package]] +name = "transpose" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad61aed86bc3faea4300c7aee358b4c6d0c8d6ccc36524c96e4c92ccf26e77e" +dependencies = [ + "num-integer", + "strength_reduce", +] + [[package]] name = "typewit" version = "1.11.0" @@ -1198,12 +1573,30 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd" +[[package]] +name = "utf8-width" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86bd8d4e895da8537e5315b8254664e6b769c4ff3db18321b297a1e7004392e3" + [[package]] name = "utf8parse" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "uuid" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "458f7a779bf54acc9f347480ac654f68407d3aab21269a6e3c9f922acd9e2da9" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + [[package]] name = "walkdir" version = "2.5.0" @@ -1241,7 +1634,7 @@ dependencies = [ "log", "proc-macro2", "quote", - "syn", + "syn 2.0.100", "wasm-bindgen-shared", ] @@ -1263,7 +1656,7 @@ checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.100", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -1400,6 +1793,24 @@ version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" +[[package]] +name = "winnow" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e97b544156e9bebe1a0ffbc03484fc1ffe3100cbce3ffb17eac35f7cdd7ab36" +dependencies = [ + "memchr", +] + +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] + [[package]] name = "xxhash-rust" version = "0.8.15" @@ -1423,5 +1834,5 @@ checksum = "6352c01d0edd5db859a63e2605f4ea3183ddbd15e2c4a9e7d32184df75e4f154" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.100", ] diff --git a/Cargo.toml b/Cargo.toml index 03438d2..7ba3f33 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,18 +6,20 @@ edition = "2024" [dependencies] tengri = { git = "https://codeberg.org/unspeaker/tengri", rev = "877b344765" } -clap = { version = "4.5.4", features = [ "cargo" ] } -enolib = { git = "https://codeberg.org/simonrepp/enolib-rs", tag = "0.5.0" } -file_type = "0.7" -hex = "0.4" -id3 = "1.16" -lofty = "0.22.2" -opener = "0.7" -opus_headers = "0.1.2" -pad = "0.1" -unicode-width = "0.2" -walkdir = "2" -xxhash-rust = { version = "0.8.5", features = ["xxh3"] } +clap = { version = "4.5.4", features = [ "cargo" ] } +enolib = { git = "https://codeberg.org/simonrepp/enolib-rs", tag = "0.5.0" } +byte-unit = "5.1.6" +file_type = "0.7" +hex = "0.4" +id3 = "1.16" +lofty = "0.22.2" +opener = "0.7" +opus_headers = "0.1.2" +pad = "0.1" +rusty-chromaprint = "0.3.0" +unicode-width = "0.2" +walkdir = "2" +xxhash-rust = { version = "0.8.5", features = ["xxh3"] } #base64 = "0.22" #moku = "0.2" diff --git a/src/main.rs b/src/main.rs index 245296a..57f5125 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ #![allow(stable_features)] #![feature(os_str_display)] +#![feature(str_as_str)] use std::sync::{Arc, RwLock}; use std::path::{Path, PathBuf}; diff --git a/src/view.rs b/src/view.rs index 23307fd..4e81c4e 100644 --- a/src/view.rs +++ b/src/view.rs @@ -1,72 +1,9 @@ use crate::*; use tengri::tui::ratatui::{style::{Color, Style}, prelude::Stylize}; -use pad::PadStr; +pub(crate) use pad::PadStr; -pub struct Column { - pub title: Arc, - pub width: usize, - pub value: BoxOption> + Send + Sync>, -} - -impl Column { - pub fn new ( - title: &impl AsRef, - width: usize, - value: impl Fn(&T)->Option> + Send + Sync + 'static - ) -> Self { - Self { width, value: Box::new(value), title: title.as_ref().into() } - } -} - -pub struct Columns(pub Vec>); - -impl Default for Columns { - fn default () -> Self { - Self(vec![ - Column::new(&"HASH", 16, |entry: &Entry|entry.hash()), - Column::new(&"FILE", 80, |entry: &Entry|entry.name()), - Column::new(&"ARTIST", 30, |entry: &Entry|entry.artist()), - Column::new(&"RELEASE", 30, |entry: &Entry|entry.album()), - Column::new(&"TRACK", 5, |entry: &Entry|entry.track()), - Column::new(&"TITLE", 80, |entry: &Entry|entry.title()), - ]) - } -} - -impl Columns { - pub fn header (&self) -> Arc { - let mut output = String::new(); - for Column { width, title, .. } in self.0.iter() { - let cell = title.pad_to_width(*width); - output = format!("{output}{cell}│"); - } - output.into() - } - pub fn row (&self, entry: &T) -> Arc { - let mut output = String::new(); - for Column { width, value, .. } in self.0.iter() { - let cell = value(entry).unwrap_or_default().pad_to_width(*width); - output = format!("{output}{cell}│"); - } - output.into() - } - pub fn xw (&self, column: usize) -> (u16, u16) { - let mut x: u16 = 0; - for (index, Column { width, .. }) in self.0.iter().enumerate() { - let w = *width as u16 + 1; - if index == column { - if x > 0 { - return (x - 1, w + 1) - } else { - return (x, w) - } - } else { - x += w; - } - } - (0, 0) - } -} +mod column; pub use self::column::*; +mod string; pub use self::string::*; impl Content for Taggart { fn content (&self) -> impl Render { @@ -93,7 +30,8 @@ impl<'a> Content for TreeTable<'a> { for (index, _fragment) in entry.path.iter().enumerate() { if index == entry.depth - 1 { let _cursor = if selected { ">" } else { " " }; - let label = self.0.columns.row(&entry); + let label = self.0.columns.row_content(&entry); + Content::render(&label) to.blit(&label, area.x(), y, entry.style()); if selected { let fill = [area.x(), y, area.w(), 1]; diff --git a/src/view/column.rs b/src/view/column.rs new file mode 100644 index 0000000..2e7964e --- /dev/null +++ b/src/view/column.rs @@ -0,0 +1,77 @@ +use crate::*; + +pub struct Column { + pub title: Arc, + pub width: usize, + pub value: BoxOption> + Send + Sync>, +} + +impl Column { + pub fn new ( + title: &impl AsRef, + width: usize, + value: impl Fn(&T)->Option> + Send + Sync + 'static + ) -> Self { + Self { width, value: Box::new(value), title: title.as_ref().into() } + } +} + +pub struct Columns(pub Vec>); + +impl Default for Columns { + fn default () -> Self { + Self(vec![ + Column::new(&"HASH", 16, |entry: &Entry|entry.hash()), + Column::new(&"FILE", 80, |entry: &Entry|entry.name()), + Column::new(&"ARTIST", 30, |entry: &Entry|entry.artist()), + Column::new(&"RELEASE", 30, |entry: &Entry|entry.album()), + Column::new(&"TRACK", 5, |entry: &Entry|entry.track()), + Column::new(&"TITLE", 80, |entry: &Entry|entry.title()), + ]) + } +} + +impl Columns { + pub fn header (&self) -> Arc { + let mut output = String::new(); + for Column { width, title, .. } in self.0.iter() { + let cell = title.pad_to_width(*width); + output = format!("{output}{cell}│"); + } + output.into() + } + pub fn row (&self, entry: &T) -> Arc { + let mut output = String::new(); + for Column { width, value, .. } in self.0.iter() { + let cell = value(entry).unwrap_or_default().pad_to_width(*width); + output = format!("{output}{cell}│"); + } + output.into() + } + pub fn row_content (&self, entry: &T) -> impl Content where T: Send + Sync { + Map::new( + ||self.0.iter(), + |Column { width, value, .. }, index|map_east( + 0u16, + *width as u16, + value(entry).map(|x|TrimString(*width as u16, x)) + ) + ) + } + pub fn xw (&self, column: usize) -> (u16, u16) { + let mut x: u16 = 0; + for (index, Column { width, .. }) in self.0.iter().enumerate() { + let w = *width as u16 + 1; + if index == column { + if x > 0 { + return (x - 1, w + 1) + } else { + return (x, w) + } + } else { + x += w; + } + } + (0, 0) + } +} diff --git a/src/view/string.rs b/src/view/string.rs new file mode 100644 index 0000000..a870d1c --- /dev/null +++ b/src/view/string.rs @@ -0,0 +1,49 @@ +use crate::*; +use crate::ratatui::prelude::Position; +use unicode_width::{UnicodeWidthStr, UnicodeWidthChar}; + +/// Displays an owned [str]-like with fixed maximum width. +/// +/// Width is computed using [unicode_width]. +pub struct TrimString>(pub u16, pub T); + +impl<'a, T: AsRef> TrimString { + fn as_ref (&self) -> TrimStringRef<'_, T> { + TrimStringRef(self.0, &self.1) + } +} +impl<'a, T: AsRef> Content for TrimString { + fn layout (&self, to: [u16; 4]) -> [u16;4] { + Content::layout(&self.as_ref(), to) + } + fn render (&self, to: &mut TuiOut) { + Content::render(&self.as_ref(), to) + } +} +/// Displays a borrowed [str]-like with fixed maximum width +/// +/// Width is computed using [unicode_width]. +pub struct TrimStringRef<'a, T: AsRef>(pub u16, pub &'a T); + +impl> Content for TrimStringRef<'_, T> { + fn layout (&self, to: [u16; 4]) -> [u16;4] { + [to.x(), to.y(), to.w().min(self.0).min(self.1.as_ref().width() as u16), to.h()] + } + fn render (&self, target: &mut TuiOut) { + let area = target.area(); + let mut width: u16 = 0; + let mut chars = self.1.as_ref().chars(); + while let Some(c) = chars.next() { + width += c.width().unwrap_or(0) as u16; + if width > self.0 || width > area.w() { + break + } + if let Some(cell) = target.buffer.cell_mut(Position { + x: area.x() + width, + y: area.y() + }) { + cell.set_char(c); + } + } + } +}