Compare commits
29 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aea2c39215 | ||
|
|
4c2464ab30 | ||
|
|
26053a5fd8 | ||
|
|
589220a2ba | ||
|
|
2cda54633f | ||
|
|
3eea6b9e88 | ||
|
|
db9bb7e168 | ||
|
|
3ccd094a22 | ||
|
|
032f21edaa | ||
|
|
42eb087363 | ||
|
|
d0ff449e3b | ||
|
|
858f5137d8 | ||
|
|
80d5dd0761 | ||
|
|
49b31c6e92 | ||
|
|
8ed2fbbe34 | ||
|
|
5ae8d13719 | ||
|
|
7bf2b81229 | ||
|
|
0215f2824a | ||
|
|
3fdb7e4e37 | ||
|
|
a3f578ebac | ||
|
|
f0bc7abaad | ||
|
|
f9d9231d50 | ||
|
|
465db82bd9 | ||
|
|
885a48bdd8 | ||
|
|
c915b3287b | ||
|
|
7b2f021509 | ||
|
|
5f1bdfefca | ||
|
|
3273a43e20 | ||
|
|
61e439a1d4 |
215
Cargo.lock
generated
215
Cargo.lock
generated
@@ -324,6 +324,27 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bit-set"
|
||||||
|
version = "0.5.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1"
|
||||||
|
dependencies = [
|
||||||
|
"bit-vec",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bit-vec"
|
||||||
|
version = "0.6.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitflags"
|
||||||
|
version = "1.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "2.9.1"
|
version = "2.9.1"
|
||||||
@@ -476,12 +497,14 @@ version = "0.4.2"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"common",
|
|
||||||
"crossterm",
|
"crossterm",
|
||||||
|
"once_cell",
|
||||||
"ratatui",
|
"ratatui",
|
||||||
"regex",
|
"regex",
|
||||||
|
"ropey",
|
||||||
"serde",
|
"serde",
|
||||||
"thiserror",
|
"syntect",
|
||||||
|
"thiserror 2.0.12",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-test",
|
"tokio-test",
|
||||||
"toml",
|
"toml",
|
||||||
@@ -771,7 +794,7 @@ version = "0.28.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "829d955a0bb380ef178a640b91779e3987da38c9aea133b20614cfed8cdea9c6"
|
checksum = "829d955a0bb380ef178a640b91779e3987da38c9aea133b20614cfed8cdea9c6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 2.9.1",
|
||||||
"crossterm_winapi",
|
"crossterm_winapi",
|
||||||
"mio",
|
"mio",
|
||||||
"parking_lot",
|
"parking_lot",
|
||||||
@@ -995,7 +1018,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "778e2ac28f6c47af28e4907f13ffd1e1ddbd400980a9abd7c8df189bf578a5ad"
|
checksum = "778e2ac28f6c47af28e4907f13ffd1e1ddbd400980a9abd7c8df189bf578a5ad"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.60.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1020,6 +1043,16 @@ dependencies = [
|
|||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "fancy-regex"
|
||||||
|
version = "0.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b95f7c0680e4142284cf8b22c14a476e87d61b004a3a0861872b32ef7ead40a2"
|
||||||
|
dependencies = [
|
||||||
|
"bit-set",
|
||||||
|
"regex",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fastdivide"
|
name = "fastdivide"
|
||||||
version = "0.4.2"
|
version = "0.4.2"
|
||||||
@@ -1038,6 +1071,16 @@ version = "0.5.7"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99"
|
checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "flate2"
|
||||||
|
version = "1.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4a3d7db9596fecd151c5f638c0ee5d5bd487b6e0ea232e5dc96d5250f6f94b1d"
|
||||||
|
dependencies = [
|
||||||
|
"crc32fast",
|
||||||
|
"miniz_oxide",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "flume"
|
name = "flume"
|
||||||
version = "0.11.1"
|
version = "0.11.1"
|
||||||
@@ -1724,7 +1767,7 @@ version = "0.7.9"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d93587f37623a1a17d94ef2bc9ada592f5465fe7732084ab7beefabe5c77c0c4"
|
checksum = "d93587f37623a1a17d94ef2bc9ada592f5465fe7732084ab7beefabe5c77c0c4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 2.9.1",
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
@@ -1843,7 +1886,7 @@ version = "0.1.6"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4488594b9328dee448adb906d8b126d9b7deb7cf5c22161ee591610bb1be83c0"
|
checksum = "4488594b9328dee448adb906d8b126d9b7deb7cf5c22161ee591610bb1be83c0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 2.9.1",
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -1857,6 +1900,12 @@ dependencies = [
|
|||||||
"vcpkg",
|
"vcpkg",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "linked-hash-map"
|
||||||
|
version = "0.5.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "linux-raw-sys"
|
name = "linux-raw-sys"
|
||||||
version = "0.4.15"
|
version = "0.4.15"
|
||||||
@@ -2139,7 +2188,7 @@ version = "0.10.73"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "8505734d46c8ab1e19a1dce3aef597ad87dcb4c37e7188231769bd6bd51cebf8"
|
checksum = "8505734d46c8ab1e19a1dce3aef597ad87dcb4c37e7188231769bd6bd51cebf8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 2.9.1",
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"foreign-types",
|
"foreign-types",
|
||||||
"libc",
|
"libc",
|
||||||
@@ -2327,6 +2376,19 @@ version = "0.3.32"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
|
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "plist"
|
||||||
|
version = "1.7.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3af6b589e163c5a788fab00ce0c0366f6efbb9959c2f9874b224936af7fce7e1"
|
||||||
|
dependencies = [
|
||||||
|
"base64",
|
||||||
|
"indexmap 2.10.0",
|
||||||
|
"quick-xml",
|
||||||
|
"serde",
|
||||||
|
"time",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "polling"
|
name = "polling"
|
||||||
version = "3.9.0"
|
version = "3.9.0"
|
||||||
@@ -2498,6 +2560,15 @@ dependencies = [
|
|||||||
"syn 1.0.109",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quick-xml"
|
||||||
|
version = "0.38.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9845d9dccf565065824e69f9f235fafba1587031eda353c1f1561cd6a6be78f4"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quickscope"
|
name = "quickscope"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
@@ -2613,7 +2684,7 @@ version = "0.29.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "eabd94c2f37801c20583fc49dd5cd6b0ba68c716787c2dd6ed18571e1e63117b"
|
checksum = "eabd94c2f37801c20583fc49dd5cd6b0ba68c716787c2dd6ed18571e1e63117b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 2.9.1",
|
||||||
"cassowary",
|
"cassowary",
|
||||||
"compact_str",
|
"compact_str",
|
||||||
"crossterm",
|
"crossterm",
|
||||||
@@ -2654,7 +2725,7 @@ version = "0.5.15"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7e8af0dde094006011e6a740d4879319439489813bd0bcdc7d821beaeeff48ec"
|
checksum = "7e8af0dde094006011e6a740d4879319439489813bd0bcdc7d821beaeeff48ec"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 2.9.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2665,7 +2736,7 @@ checksum = "dd6f9d3d47bdd2ad6945c5015a226ec6155d0bcdfd8f7cd29f86b71f8de99d2b"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"getrandom 0.2.16",
|
"getrandom 0.2.16",
|
||||||
"libredox",
|
"libredox",
|
||||||
"thiserror",
|
"thiserror 2.0.12",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2764,6 +2835,16 @@ dependencies = [
|
|||||||
"syn 1.0.109",
|
"syn 1.0.109",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ropey"
|
||||||
|
version = "1.6.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "93411e420bcd1a75ddd1dc3caf18c23155eda2c090631a85af21ba19e97093b5"
|
||||||
|
dependencies = [
|
||||||
|
"smallvec",
|
||||||
|
"str_indices",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rsa"
|
name = "rsa"
|
||||||
version = "0.9.8"
|
version = "0.9.8"
|
||||||
@@ -2877,11 +2958,11 @@ version = "0.38.44"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154"
|
checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 2.9.1",
|
||||||
"errno",
|
"errno",
|
||||||
"libc",
|
"libc",
|
||||||
"linux-raw-sys 0.4.15",
|
"linux-raw-sys 0.4.15",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2890,11 +2971,11 @@ version = "1.0.8"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "11181fbabf243db407ef8df94a6ce0b2f9a733bd8be4ad02b4eda9602296cac8"
|
checksum = "11181fbabf243db407ef8df94a6ce0b2f9a733bd8be4ad02b4eda9602296cac8"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 2.9.1",
|
||||||
"errno",
|
"errno",
|
||||||
"libc",
|
"libc",
|
||||||
"linux-raw-sys 0.9.4",
|
"linux-raw-sys 0.9.4",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.60.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2909,6 +2990,15 @@ version = "1.0.20"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
|
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "same-file"
|
||||||
|
version = "1.0.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
|
||||||
|
dependencies = [
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "schannel"
|
name = "schannel"
|
||||||
version = "0.1.27"
|
version = "0.1.27"
|
||||||
@@ -2953,7 +3043,7 @@ version = "2.11.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02"
|
checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 2.9.1",
|
||||||
"core-foundation",
|
"core-foundation",
|
||||||
"core-foundation-sys",
|
"core-foundation-sys",
|
||||||
"libc",
|
"libc",
|
||||||
@@ -3058,7 +3148,7 @@ dependencies = [
|
|||||||
"steel-decimal",
|
"steel-decimal",
|
||||||
"steel-derive",
|
"steel-derive",
|
||||||
"tantivy",
|
"tantivy",
|
||||||
"thiserror",
|
"thiserror 2.0.12",
|
||||||
"time",
|
"time",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-test",
|
"tokio-test",
|
||||||
@@ -3160,7 +3250,7 @@ checksum = "297f631f50729c8c99b84667867963997ec0b50f32b2a7dbcab828ef0541e8bb"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"num-bigint",
|
"num-bigint",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"thiserror",
|
"thiserror 2.0.12",
|
||||||
"time",
|
"time",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -3280,7 +3370,7 @@ dependencies = [
|
|||||||
"serde_json",
|
"serde_json",
|
||||||
"sha2",
|
"sha2",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"thiserror",
|
"thiserror 2.0.12",
|
||||||
"time",
|
"time",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-stream",
|
"tokio-stream",
|
||||||
@@ -3335,7 +3425,7 @@ checksum = "aa003f0038df784eb8fecbbac13affe3da23b45194bd57dba231c8f48199c526"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"atoi",
|
"atoi",
|
||||||
"base64",
|
"base64",
|
||||||
"bitflags",
|
"bitflags 2.9.1",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"bytes",
|
"bytes",
|
||||||
"chrono",
|
"chrono",
|
||||||
@@ -3366,7 +3456,7 @@ dependencies = [
|
|||||||
"smallvec",
|
"smallvec",
|
||||||
"sqlx-core",
|
"sqlx-core",
|
||||||
"stringprep",
|
"stringprep",
|
||||||
"thiserror",
|
"thiserror 2.0.12",
|
||||||
"time",
|
"time",
|
||||||
"tracing",
|
"tracing",
|
||||||
"uuid",
|
"uuid",
|
||||||
@@ -3381,7 +3471,7 @@ checksum = "db58fcd5a53cf07c184b154801ff91347e4c30d17a3562a635ff028ad5deda46"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"atoi",
|
"atoi",
|
||||||
"base64",
|
"base64",
|
||||||
"bitflags",
|
"bitflags 2.9.1",
|
||||||
"byteorder",
|
"byteorder",
|
||||||
"chrono",
|
"chrono",
|
||||||
"crc",
|
"crc",
|
||||||
@@ -3407,7 +3497,7 @@ dependencies = [
|
|||||||
"smallvec",
|
"smallvec",
|
||||||
"sqlx-core",
|
"sqlx-core",
|
||||||
"stringprep",
|
"stringprep",
|
||||||
"thiserror",
|
"thiserror 2.0.12",
|
||||||
"time",
|
"time",
|
||||||
"tracing",
|
"tracing",
|
||||||
"uuid",
|
"uuid",
|
||||||
@@ -3434,7 +3524,7 @@ dependencies = [
|
|||||||
"serde",
|
"serde",
|
||||||
"serde_urlencoded",
|
"serde_urlencoded",
|
||||||
"sqlx-core",
|
"sqlx-core",
|
||||||
"thiserror",
|
"thiserror 2.0.12",
|
||||||
"time",
|
"time",
|
||||||
"tracing",
|
"tracing",
|
||||||
"url",
|
"url",
|
||||||
@@ -3516,7 +3606,7 @@ dependencies = [
|
|||||||
"rust_decimal_macros",
|
"rust_decimal_macros",
|
||||||
"steel-core",
|
"steel-core",
|
||||||
"steel-derive",
|
"steel-derive",
|
||||||
"thiserror",
|
"thiserror 2.0.12",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -3558,6 +3648,12 @@ dependencies = [
|
|||||||
"smallvec",
|
"smallvec",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "str_indices"
|
||||||
|
version = "0.4.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d08889ec5408683408db66ad89e0e1f93dff55c73a4ccc71c427d5b277ee47e6"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "stringprep"
|
name = "stringprep"
|
||||||
version = "0.1.5"
|
version = "0.1.5"
|
||||||
@@ -3642,6 +3738,28 @@ dependencies = [
|
|||||||
"syn 2.0.104",
|
"syn 2.0.104",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syntect"
|
||||||
|
version = "5.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "874dcfa363995604333cf947ae9f751ca3af4522c60886774c4963943b4746b1"
|
||||||
|
dependencies = [
|
||||||
|
"bincode",
|
||||||
|
"bitflags 1.3.2",
|
||||||
|
"fancy-regex",
|
||||||
|
"flate2",
|
||||||
|
"fnv",
|
||||||
|
"once_cell",
|
||||||
|
"plist",
|
||||||
|
"regex-syntax",
|
||||||
|
"serde",
|
||||||
|
"serde_derive",
|
||||||
|
"serde_json",
|
||||||
|
"thiserror 1.0.69",
|
||||||
|
"walkdir",
|
||||||
|
"yaml-rust",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tantivy"
|
name = "tantivy"
|
||||||
version = "0.24.2"
|
version = "0.24.2"
|
||||||
@@ -3688,7 +3806,7 @@ dependencies = [
|
|||||||
"tantivy-stacker",
|
"tantivy-stacker",
|
||||||
"tantivy-tokenizer-api",
|
"tantivy-tokenizer-api",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"thiserror",
|
"thiserror 2.0.12",
|
||||||
"time",
|
"time",
|
||||||
"uuid",
|
"uuid",
|
||||||
"winapi",
|
"winapi",
|
||||||
@@ -3804,7 +3922,7 @@ dependencies = [
|
|||||||
"getrandom 0.3.3",
|
"getrandom 0.3.3",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"rustix 1.0.8",
|
"rustix 1.0.8",
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -3816,13 +3934,33 @@ dependencies = [
|
|||||||
"winapi-util",
|
"winapi-util",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror"
|
||||||
|
version = "1.0.69"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
|
||||||
|
dependencies = [
|
||||||
|
"thiserror-impl 1.0.69",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror"
|
name = "thiserror"
|
||||||
version = "2.0.12"
|
version = "2.0.12"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
|
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"thiserror-impl",
|
"thiserror-impl 2.0.12",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror-impl"
|
||||||
|
version = "1.0.69"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.104",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -4347,6 +4485,16 @@ version = "0.9.5"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "walkdir"
|
||||||
|
version = "2.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
|
||||||
|
dependencies = [
|
||||||
|
"same-file",
|
||||||
|
"winapi-util",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "want"
|
name = "want"
|
||||||
version = "0.3.1"
|
version = "0.3.1"
|
||||||
@@ -4485,7 +4633,7 @@ version = "0.1.9"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
|
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"windows-sys 0.48.0",
|
"windows-sys 0.59.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -4795,7 +4943,7 @@ version = "0.39.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
|
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags 2.9.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -4819,6 +4967,15 @@ version = "3.0.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2fb433233f2df9344722454bc7e96465c9d03bff9d77c248f9e7523fe79585b5"
|
checksum = "2fb433233f2df9344722454bc7e96465c9d03bff9d77c248f9e7523fe79585b5"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "yaml-rust"
|
||||||
|
version = "0.4.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
|
||||||
|
dependencies = [
|
||||||
|
"linked-hash-map",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "yoke"
|
name = "yoke"
|
||||||
version = "0.8.0"
|
version = "0.8.0"
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ use canvas::canvas::CanvasState;
|
|||||||
use canvas::canvas::CanvasAction;
|
use canvas::canvas::CanvasAction;
|
||||||
use canvas::canvas::ActionContext;
|
use canvas::canvas::ActionContext;
|
||||||
use canvas::canvas::HighlightState;
|
use canvas::canvas::HighlightState;
|
||||||
use canvas::canvas::CanvasTheme;
|
use canvas::CanvasTheme;
|
||||||
use canvas::dispatcher::ActionDispatcher;
|
use canvas::dispatcher::ActionDispatcher;
|
||||||
use canvas::canvas::ActionResult;
|
use canvas::canvas::ActionResult;
|
||||||
```
|
```
|
||||||
@@ -153,7 +153,7 @@ if editor.is_suggestions_active() {
|
|||||||
**New rendering:**
|
**New rendering:**
|
||||||
```rust
|
```rust
|
||||||
// Canvas handles everything
|
// Canvas handles everything
|
||||||
use canvas::canvas::render_canvas;
|
use canvas::render_canvas_default;
|
||||||
|
|
||||||
let active_field_rect = render_canvas(f, area, form_state, theme, edit_mode, highlight_state);
|
let active_field_rect = render_canvas(f, area, form_state, theme, edit_mode, highlight_state);
|
||||||
|
|
||||||
|
|||||||
@@ -2,15 +2,14 @@
|
|||||||
name = "canvas"
|
name = "canvas"
|
||||||
version.workspace = true
|
version.workspace = true
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
license.workspace = true
|
license = "MIT OR Apache-2.0"
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
description.workspace = true
|
description = "Form/textarea for TUI"
|
||||||
readme.workspace = true
|
readme.workspace = true
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
categories.workspace = true
|
categories.workspace = true
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
common = { path = "../common" }
|
|
||||||
ratatui = { workspace = true, optional = true }
|
ratatui = { workspace = true, optional = true }
|
||||||
crossterm = { workspace = true, optional = true }
|
crossterm = { workspace = true, optional = true }
|
||||||
anyhow.workspace = true
|
anyhow.workspace = true
|
||||||
@@ -24,6 +23,9 @@ tracing = "0.1.41"
|
|||||||
tracing-subscriber = "0.3.19"
|
tracing-subscriber = "0.3.19"
|
||||||
async-trait.workspace = true
|
async-trait.workspace = true
|
||||||
regex = { workspace = true, optional = true }
|
regex = { workspace = true, optional = true }
|
||||||
|
ropey = { version = "1.6.1", optional = true }
|
||||||
|
once_cell = "1.21.3"
|
||||||
|
syntect = { version = "5.2.0", optional = true, default-features = false, features = ["default-fancy"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio-test = "0.4.4"
|
tokio-test = "0.4.4"
|
||||||
@@ -35,7 +37,8 @@ suggestions = ["tokio"]
|
|||||||
cursor-style = ["crossterm"]
|
cursor-style = ["crossterm"]
|
||||||
validation = ["regex"]
|
validation = ["regex"]
|
||||||
computed = []
|
computed = []
|
||||||
textarea = ["gui"]
|
textarea = ["dep:ropey","gui"]
|
||||||
|
syntect = ["dep:syntect", "gui", "textarea"]
|
||||||
|
|
||||||
# text modes (mutually exclusive; default to vim)
|
# text modes (mutually exclusive; default to vim)
|
||||||
textmode-vim = []
|
textmode-vim = []
|
||||||
@@ -98,3 +101,8 @@ path = "examples/textarea_vim.rs"
|
|||||||
name = "textarea_normal"
|
name = "textarea_normal"
|
||||||
required-features = ["gui", "cursor-style", "textarea", "textmode-normal"]
|
required-features = ["gui", "cursor-style", "textarea", "textmode-normal"]
|
||||||
path = "examples/textarea_normal.rs"
|
path = "examples/textarea_normal.rs"
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "textarea_syntax"
|
||||||
|
required-features = ["gui", "cursor-style", "textarea", "textmode-normal", "syntect"]
|
||||||
|
path = "examples/textarea_syntax.rs"
|
||||||
|
|||||||
440
canvas/README.md
440
canvas/README.md
@@ -1,337 +1,113 @@
|
|||||||
# Canvas 🎨
|
# Canvas
|
||||||
|
|
||||||
A reusable, type-safe canvas system for building form-based TUI applications with vim-like modal editing.
|
Canvas is a Rust library for building form‑based and textarea‑driven terminal user interfaces.
|
||||||
|
It provides the core logic for text editing, validation, suggestions, and cursor management.
|
||||||
|
|
||||||
## ✨ Features
|
The library does not enforce a specific terminal UI framework:
|
||||||
|
- Core functionality works without any rendering backend.
|
||||||
- **Type-Safe Actions**: No more string-based action names - everything is compile-time checked
|
- Terminal rendering support is available through the `gui` feature, which enables integration with `ratatui` and `crossterm`.
|
||||||
- **Generic Design**: Implement `CanvasState` once, get navigation, editing, and suggestions for free
|
- Applications may also integrate Canvas with other backends by handling input and rendering independently.
|
||||||
- **Vim-Like Experience**: Modal editing with familiar keybindings
|
|
||||||
- **Suggestion System**: Built-in suggestions dropdown support
|
|
||||||
- **Framework Agnostic**: Works with any TUI framework or raw terminal handling
|
|
||||||
- **Async Ready**: Full async/await support for modern Rust applications
|
|
||||||
- **Batch Operations**: Execute multiple actions atomically
|
|
||||||
- **Extensible**: Custom actions and feature-specific handling
|
|
||||||
|
|
||||||
## 🚀 Quick Start
|
|
||||||
|
|
||||||
Add to your `Cargo.toml`:
|
|
||||||
|
|
||||||
```toml
|
|
||||||
cargo add canvas
|
|
||||||
```
|
|
||||||
|
|
||||||
Implement the `CanvasState` trait:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
use canvas::prelude::*;
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct LoginForm {
|
|
||||||
current_field: usize,
|
|
||||||
cursor_pos: usize,
|
|
||||||
username: String,
|
|
||||||
password: String,
|
|
||||||
has_changes: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CanvasState for LoginForm {
|
|
||||||
fn current_field(&self) -> usize { self.current_field }
|
|
||||||
fn current_cursor_pos(&self) -> usize { self.cursor_pos }
|
|
||||||
fn set_current_field(&mut self, index: usize) { self.current_field = index; }
|
|
||||||
fn set_current_cursor_pos(&mut self, pos: usize) { self.cursor_pos = pos; }
|
|
||||||
|
|
||||||
fn get_current_input(&self) -> &str {
|
|
||||||
match self.current_field {
|
|
||||||
0 => &self.username,
|
|
||||||
1 => &self.password,
|
|
||||||
_ => "",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_current_input_mut(&mut self) -> &mut String {
|
|
||||||
match self.current_field {
|
|
||||||
0 => &mut self.username,
|
|
||||||
1 => &mut self.password,
|
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn inputs(&self) -> Vec<&String> { vec![&self.username, &self.password] }
|
|
||||||
fn fields(&self) -> Vec<&str> { vec!["Username", "Password"] }
|
|
||||||
fn has_unsaved_changes(&self) -> bool { self.has_changes }
|
|
||||||
fn set_has_unsaved_changes(&mut self, changed: bool) { self.has_changes = changed; }
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Use the type-safe action dispatcher:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
#[tokio::main]
|
|
||||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
let mut form = LoginForm::new();
|
|
||||||
let mut ideal_cursor = 0;
|
|
||||||
|
|
||||||
// Type a character - compile-time safe!
|
|
||||||
ActionDispatcher::dispatch(
|
|
||||||
CanvasAction::InsertChar('h'),
|
|
||||||
&mut form,
|
|
||||||
&mut ideal_cursor,
|
|
||||||
).await?;
|
|
||||||
|
|
||||||
// Move to next field
|
|
||||||
ActionDispatcher::dispatch(
|
|
||||||
CanvasAction::NextField,
|
|
||||||
&mut form,
|
|
||||||
&mut ideal_cursor,
|
|
||||||
).await?;
|
|
||||||
|
|
||||||
// Batch operations
|
|
||||||
let actions = vec![
|
|
||||||
CanvasAction::InsertChar('p'),
|
|
||||||
CanvasAction::InsertChar('a'),
|
|
||||||
CanvasAction::InsertChar('s'),
|
|
||||||
CanvasAction::InsertChar('s'),
|
|
||||||
];
|
|
||||||
|
|
||||||
ActionDispatcher::dispatch_batch(actions, &mut form, &mut ideal_cursor).await?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## 🎯 Type-Safe Actions
|
|
||||||
|
|
||||||
The Canvas system uses strongly-typed actions instead of error-prone strings:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
// ✅ Type-safe - impossible to make typos
|
|
||||||
ActionDispatcher::dispatch(CanvasAction::MoveLeft, &mut form, &mut cursor).await?;
|
|
||||||
|
|
||||||
// ❌ Old way - runtime errors waiting to happen
|
|
||||||
execute_edit_action("move_left", key, &mut form, &mut cursor).await?;
|
|
||||||
execute_edit_action("move_leftt", key, &mut form, &mut cursor).await?; // Oops!
|
|
||||||
```
|
|
||||||
|
|
||||||
### Available Actions
|
|
||||||
|
|
||||||
```rust
|
|
||||||
pub enum CanvasAction {
|
|
||||||
// Character input
|
|
||||||
InsertChar(char),
|
|
||||||
|
|
||||||
// Deletion
|
|
||||||
DeleteBackward,
|
|
||||||
DeleteForward,
|
|
||||||
|
|
||||||
// Movement
|
|
||||||
MoveLeft, MoveRight, MoveUp, MoveDown,
|
|
||||||
MoveLineStart, MoveLineEnd,
|
|
||||||
MoveWordNext, MoveWordPrev,
|
|
||||||
|
|
||||||
// Navigation
|
|
||||||
NextField, PrevField,
|
|
||||||
MoveFirstLine, MoveLastLine,
|
|
||||||
|
|
||||||
// Suggestions
|
|
||||||
SuggestionUp, SuggestionDown,
|
|
||||||
SelectSuggestion, ExitSuggestions,
|
|
||||||
|
|
||||||
// Extensibility
|
|
||||||
Custom(String),
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## 🔧 Advanced Features
|
|
||||||
|
|
||||||
### Suggestions Dropdown (not inline autocomplete)
|
|
||||||
|
|
||||||
```rust
|
|
||||||
impl CanvasState for MyForm {
|
|
||||||
fn get_suggestions(&self) -> Option<&[String]> {
|
|
||||||
if self.suggestions.is_active {
|
|
||||||
Some(&self.suggestions.suggestions)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_feature_action(&mut self, action: &CanvasAction, _context: &ActionContext) -> Option<String> {
|
|
||||||
match action {
|
|
||||||
CanvasAction::InsertChar('@') => {
|
|
||||||
// Trigger email suggestions
|
|
||||||
let suggestions = vec![
|
|
||||||
format!("{}@gmail.com", self.username),
|
|
||||||
format!("{}@company.com", self.username),
|
|
||||||
];
|
|
||||||
self.activate_suggestions(suggestions);
|
|
||||||
None // Let generic handler insert the '@'
|
|
||||||
}
|
|
||||||
CanvasAction::SelectSuggestion => {
|
|
||||||
if let Some(suggestion) = self.suggestions.get_selected() {
|
|
||||||
*self.get_current_input_mut() = suggestion.clone();
|
|
||||||
self.deactivate_suggestions();
|
|
||||||
Some("Applied suggestion".to_string())
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Custom Actions
|
|
||||||
|
|
||||||
```rust
|
|
||||||
fn handle_feature_action(&mut self, action: &CanvasAction, _context: &ActionContext) -> Option<String> {
|
|
||||||
match action {
|
|
||||||
CanvasAction::Custom(cmd) => match cmd.as_str() {
|
|
||||||
"uppercase" => {
|
|
||||||
*self.get_current_input_mut() = self.get_current_input().to_uppercase();
|
|
||||||
Some("Converted to uppercase".to_string())
|
|
||||||
}
|
|
||||||
"validate_email" => {
|
|
||||||
if self.get_current_input().contains('@') {
|
|
||||||
Some("Email is valid".to_string())
|
|
||||||
} else {
|
|
||||||
Some("Invalid email format".to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
},
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Integration with TUI Frameworks
|
|
||||||
|
|
||||||
Canvas is framework-agnostic and works with any TUI library:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
// Works with crossterm (see examples)
|
|
||||||
// Works with termion
|
|
||||||
// Works with ratatui/tui-rs
|
|
||||||
// Works with cursive
|
|
||||||
// Works with raw terminal I/O
|
|
||||||
```
|
|
||||||
|
|
||||||
## 🏗️ Architecture
|
|
||||||
|
|
||||||
Canvas follows a clean, layered architecture:
|
|
||||||
|
|
||||||
```
|
|
||||||
┌─────────────────────────────────────┐
|
|
||||||
│ Your Application │
|
|
||||||
├─────────────────────────────────────┤
|
|
||||||
│ ActionDispatcher │ ← High-level API
|
|
||||||
├─────────────────────────────────────┤
|
|
||||||
│ CanvasAction (Type-Safe) │ ← Type safety layer
|
|
||||||
├─────────────────────────────────────┤
|
|
||||||
│ Action Handlers │ ← Core logic
|
|
||||||
├─────────────────────────────────────┤
|
|
||||||
│ CanvasState Trait │ ← Your implementation
|
|
||||||
└─────────────────────────────────────┘
|
|
||||||
```
|
|
||||||
|
|
||||||
## 🤝 Why Canvas?
|
|
||||||
|
|
||||||
### Before Canvas
|
|
||||||
```rust
|
|
||||||
// ❌ Error-prone string actions
|
|
||||||
execute_action("move_left", key, state)?;
|
|
||||||
execute_action("move_leftt", key, state)?; // Runtime error!
|
|
||||||
|
|
||||||
// ❌ Duplicate navigation logic everywhere
|
|
||||||
impl MyLoginForm { /* navigation code */ }
|
|
||||||
impl MyConfigForm { /* same navigation code */ }
|
|
||||||
impl MyDataForm { /* same navigation code again */ }
|
|
||||||
|
|
||||||
// ❌ Manual cursor and field management
|
|
||||||
if key == Key::Tab {
|
|
||||||
current_field = (current_field + 1) % fields.len();
|
|
||||||
cursor_pos = cursor_pos.min(current_input.len());
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### With Canvas
|
|
||||||
```rust
|
|
||||||
// ✅ Type-safe actions
|
|
||||||
ActionDispatcher::dispatch(CanvasAction::MoveLeft, state, cursor)?;
|
|
||||||
// Typos are impossible - won't compile!
|
|
||||||
|
|
||||||
// ✅ Implement once, use everywhere
|
|
||||||
impl CanvasState for MyForm { /* minimal implementation */ }
|
|
||||||
// All navigation, editing, suggestions work automatically!
|
|
||||||
|
|
||||||
// ✅ High-level operations
|
|
||||||
ActionDispatcher::dispatch_batch(actions, state, cursor)?;
|
|
||||||
```
|
|
||||||
|
|
||||||
## 📖 Documentation
|
|
||||||
|
|
||||||
- **API Docs**: `cargo doc --open`
|
|
||||||
- **Examples**: See `examples/` directory
|
|
||||||
- **Migration Guide**: See `CANVAS_MIGRATION.md`
|
|
||||||
|
|
||||||
## 🔄 Migration from String-Based Actions
|
|
||||||
|
|
||||||
Canvas provides backwards compatibility during migration:
|
|
||||||
|
|
||||||
```rust
|
|
||||||
// Legacy support (deprecated)
|
|
||||||
execute_edit_action("move_left", key, state, cursor).await?;
|
|
||||||
|
|
||||||
// New type-safe way
|
|
||||||
ActionDispatcher::dispatch(CanvasAction::MoveLeft, state, cursor).await?;
|
|
||||||
```
|
|
||||||
|
|
||||||
## 🧪 Testing
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Run all tests
|
|
||||||
cargo test
|
|
||||||
|
|
||||||
# Run specific example
|
|
||||||
cargo run --example simple_login
|
|
||||||
|
|
||||||
# Check type safety
|
|
||||||
cargo check
|
|
||||||
```
|
|
||||||
|
|
||||||
## 📋 Requirements
|
|
||||||
|
|
||||||
- Rust 1.70+
|
|
||||||
- Terminal with cursor support
|
|
||||||
- Optional: async runtime (tokio) for examples
|
|
||||||
|
|
||||||
## 🤔 FAQ
|
|
||||||
|
|
||||||
**Q: Does Canvas work with [my TUI framework]?**
|
|
||||||
A: Yes! Canvas is framework-agnostic. Just implement `CanvasState` and handle the key events.
|
|
||||||
|
|
||||||
**Q: Can I extend Canvas with custom actions?**
|
|
||||||
A: Absolutely! Use `CanvasAction::Custom("my_action")` or implement `handle_feature_action`.
|
|
||||||
|
|
||||||
**Q: Is Canvas suitable for complex forms?**
|
|
||||||
A: Yes! See the `config_screen` example for validation, suggestions, and multi-field forms.
|
|
||||||
|
|
||||||
**Q: How do I migrate from string-based actions?**
|
|
||||||
A: Canvas provides backwards compatibility. Migrate incrementally using the type-safe APIs.
|
|
||||||
|
|
||||||
## 📄 License
|
|
||||||
|
|
||||||
Licensed under either of:
|
|
||||||
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE))
|
|
||||||
- MIT License ([LICENSE-MIT](LICENSE-MIT))
|
|
||||||
|
|
||||||
at your option.
|
|
||||||
|
|
||||||
## 🙏 Contributing
|
|
||||||
|
|
||||||
Will write here something later on, too busy rn
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
Built with ❤️ for the Rust TUI community
|
## Overview
|
||||||
|
|
||||||
|
Canvas is designed for applications that require structured text input in a terminal environment.
|
||||||
|
It provides:
|
||||||
|
|
||||||
|
- Text editing modes (Vim‑like or normal)
|
||||||
|
- Validation (regex, masks, limits, formatting)
|
||||||
|
- Suggestions (asynchronous dropdowns)
|
||||||
|
- Computed fields (derived values)
|
||||||
|
- Textarea widget with cursor management
|
||||||
|
- Syntax highlighting (via syntect)
|
||||||
|
- Extensible architecture for custom behaviors
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Add the dependency to your `Cargo.toml`:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[dependencies]
|
||||||
|
canvas = { version = "0.x", features = ["gui", "cursor-style", "textarea", "validation"] }
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
The library is feature‑gated. Enable only what you need:
|
||||||
|
|
||||||
|
- `gui` – terminal rendering support (ratatui + crossterm)
|
||||||
|
- `cursor-style` – styled cursor support
|
||||||
|
- `validation` – regex, masks, limits, formatting
|
||||||
|
- `suggestions` – asynchronous suggestions dropdowns
|
||||||
|
- `computed` – derived fields
|
||||||
|
- `textarea` – textarea widget
|
||||||
|
- `syntect` – syntax highlighting support
|
||||||
|
- `textmode-vim` – Vim‑like editing (default)
|
||||||
|
- `textmode-normal` – normal editing mode
|
||||||
|
|
||||||
|
**Note:** `textmode-vim` and `textmode-normal` are mutually exclusive. Enable exactly one.
|
||||||
|
|
||||||
|
The default feature set is `["textmode-vim"]`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Running Examples
|
||||||
|
|
||||||
|
The repository includes several examples. Each requires specific feature flags.
|
||||||
|
Use the following commands to run them:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Textarea with Vim mode
|
||||||
|
cargo run --example textarea_vim --features "gui cursor-style textarea textmode-vim"
|
||||||
|
|
||||||
|
# Textarea with Normal mode
|
||||||
|
cargo run --example textarea_normal --features "gui cursor-style textarea textmode-normal"
|
||||||
|
|
||||||
|
# Textarea with syntax highlighting
|
||||||
|
cargo run --example textarea_syntax --features "gui cursor-style textarea syntect textmode-normal"
|
||||||
|
|
||||||
|
# Validation examples
|
||||||
|
cargo run --example validation_1 --features "gui validation cursor-style"
|
||||||
|
cargo run --example validation_2 --features "gui validation cursor-style"
|
||||||
|
cargo run --example validation_3 --features "gui validation cursor-style"
|
||||||
|
cargo run --example validation_4 --features "gui validation cursor-style"
|
||||||
|
cargo run --example validation_5 --features "gui validation cursor-style"
|
||||||
|
|
||||||
|
# Suggestions
|
||||||
|
cargo run --example suggestions --features "suggestions gui cursor-style"
|
||||||
|
cargo run --example suggestions2 --features "suggestions gui cursor-style"
|
||||||
|
|
||||||
|
# Cursor auto movement
|
||||||
|
cargo run --example canvas_cursor_auto --features "gui cursor-style"
|
||||||
|
|
||||||
|
# Computed fields
|
||||||
|
cargo run --example computed_fields --features "gui computed"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
- API documentation: `cargo doc --open`
|
||||||
|
- Migration notes: `CANVAS_MIGRATION.md`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
Licensed under either of:
|
||||||
|
- Apache License, Version 2.0
|
||||||
|
- MIT License
|
||||||
|
|
||||||
|
at your option.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Contributions are welcome. Please follow the existing code structure and feature‑gating conventions.
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ use ratatui::{
|
|||||||
use canvas::{
|
use canvas::{
|
||||||
canvas::{
|
canvas::{
|
||||||
gui::render_canvas_default,
|
gui::render_canvas_default,
|
||||||
modes::{AppMode, ModeManager, HighlightState},
|
modes::AppMode,
|
||||||
CursorManager, // This import only exists when cursor-style feature is enabled
|
CursorManager, // This import only exists when cursor-style feature is enabled
|
||||||
},
|
},
|
||||||
DataProvider, FormEditor,
|
DataProvider, FormEditor,
|
||||||
@@ -205,7 +205,7 @@ impl<D: DataProvider> AutoCursorFormEditor<D> {
|
|||||||
self.has_unsaved_changes = true;
|
self.has_unsaved_changes = true;
|
||||||
self.debug_message = "⌫ Deleted character backward".to_string();
|
self.debug_message = "⌫ Deleted character backward".to_string();
|
||||||
}
|
}
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delete_forward(&mut self) -> anyhow::Result<()> {
|
fn delete_forward(&mut self) -> anyhow::Result<()> {
|
||||||
@@ -214,7 +214,7 @@ impl<D: DataProvider> AutoCursorFormEditor<D> {
|
|||||||
self.has_unsaved_changes = true;
|
self.has_unsaved_changes = true;
|
||||||
self.debug_message = "⌦ Deleted character forward".to_string();
|
self.debug_message = "⌦ Deleted character forward".to_string();
|
||||||
}
|
}
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
// === MODE TRANSITIONS WITH AUTOMATIC CURSOR MANAGEMENT ===
|
// === MODE TRANSITIONS WITH AUTOMATIC CURSOR MANAGEMENT ===
|
||||||
@@ -240,7 +240,7 @@ impl<D: DataProvider> AutoCursorFormEditor<D> {
|
|||||||
if result.is_ok() {
|
if result.is_ok() {
|
||||||
self.has_unsaved_changes = true;
|
self.has_unsaved_changes = true;
|
||||||
}
|
}
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
// === MANUAL CURSOR OVERRIDE DEMONSTRATION ===
|
// === MANUAL CURSOR OVERRIDE DEMONSTRATION ===
|
||||||
@@ -429,13 +429,13 @@ fn handle_key_press(
|
|||||||
|
|
||||||
(AppMode::ReadOnly, KeyCode::Char('o'), _) => {
|
(AppMode::ReadOnly, KeyCode::Char('o'), _) => {
|
||||||
if let Err(e) = editor.open_line_below() {
|
if let Err(e) = editor.open_line_below() {
|
||||||
editor.set_debug_message(format!("Error opening line below: {}", e));
|
editor.set_debug_message(format!("Error opening line below: {e}"));
|
||||||
}
|
}
|
||||||
editor.clear_command_buffer();
|
editor.clear_command_buffer();
|
||||||
}
|
}
|
||||||
(AppMode::ReadOnly, KeyCode::Char('O'), _) => {
|
(AppMode::ReadOnly, KeyCode::Char('O'), _) => {
|
||||||
if let Err(e) = editor.open_line_above() {
|
if let Err(e) = editor.open_line_above() {
|
||||||
editor.set_debug_message(format!("Error opening line above: {}", e));
|
editor.set_debug_message(format!("Error opening line above: {e}"));
|
||||||
}
|
}
|
||||||
editor.clear_command_buffer();
|
editor.clear_command_buffer();
|
||||||
}
|
}
|
||||||
@@ -694,8 +694,7 @@ fn handle_key_press(
|
|||||||
editor.set_debug_message("Invalid command sequence".to_string());
|
editor.set_debug_message("Invalid command sequence".to_string());
|
||||||
} else {
|
} else {
|
||||||
editor.set_debug_message(format!(
|
editor.set_debug_message(format!(
|
||||||
"Unhandled: {:?} + {:?} in {:?} mode",
|
"Unhandled: {key:?} + {modifiers:?} in {mode:?} mode"
|
||||||
key, modifiers, mode
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -719,7 +718,7 @@ fn run_app<B: Backend>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
editor.set_debug_message(format!("Error: {}", e));
|
editor.set_debug_message(format!("Error: {e}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -858,7 +857,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
terminal.show_cursor()?;
|
terminal.show_cursor()?;
|
||||||
|
|
||||||
if let Err(err) = res {
|
if let Err(err) = res {
|
||||||
println!("{:?}", err);
|
println!("{err:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("🎯 Cursor automatically reset to default!");
|
println!("🎯 Cursor automatically reset to default!");
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ impl ComputedProvider for InvoiceCalculator {
|
|||||||
if qty == 0.0 || price == 0.0 {
|
if qty == 0.0 || price == 0.0 {
|
||||||
"".to_string() // Show empty if no meaningful calculation
|
"".to_string() // Show empty if no meaningful calculation
|
||||||
} else {
|
} else {
|
||||||
format!("{:.2}", subtotal)
|
format!("{subtotal:.2}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
5 => {
|
5 => {
|
||||||
@@ -147,7 +147,7 @@ impl ComputedProvider for InvoiceCalculator {
|
|||||||
if subtotal == 0.0 || tax_rate == 0.0 {
|
if subtotal == 0.0 || tax_rate == 0.0 {
|
||||||
"".to_string()
|
"".to_string()
|
||||||
} else {
|
} else {
|
||||||
format!("{:.2}", tax_amount)
|
format!("{tax_amount:.2}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
6 => {
|
6 => {
|
||||||
@@ -162,7 +162,7 @@ impl ComputedProvider for InvoiceCalculator {
|
|||||||
} else {
|
} else {
|
||||||
let tax_amount = subtotal * (tax_rate / 100.0);
|
let tax_amount = subtotal * (tax_rate / 100.0);
|
||||||
let total = subtotal + tax_amount;
|
let total = subtotal + tax_amount;
|
||||||
format!("{:.2}", total)
|
format!("{total:.2}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => "".to_string(),
|
_ => "".to_string(),
|
||||||
@@ -170,7 +170,7 @@ impl ComputedProvider for InvoiceCalculator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn handles_field(&self, field_index: usize) -> bool {
|
fn handles_field(&self, field_index: usize) -> bool {
|
||||||
matches!(field_index, 4 | 5 | 6) // Subtotal, Tax Amount, Total
|
matches!(field_index, 4..=6) // Subtotal, Tax Amount, Total
|
||||||
}
|
}
|
||||||
|
|
||||||
fn field_dependencies(&self, field_index: usize) -> Vec<usize> {
|
fn field_dependencies(&self, field_index: usize) -> Vec<usize> {
|
||||||
@@ -244,13 +244,13 @@ impl<D: DataProvider> ComputedFieldsEditor<D> {
|
|||||||
|
|
||||||
let mut parts = Vec::new();
|
let mut parts = Vec::new();
|
||||||
if !subtotal.is_empty() {
|
if !subtotal.is_empty() {
|
||||||
parts.push(format!("Subtotal=${}", subtotal));
|
parts.push(format!("Subtotal=${subtotal}"));
|
||||||
}
|
}
|
||||||
if !tax.is_empty() {
|
if !tax.is_empty() {
|
||||||
parts.push(format!("Tax=${}", tax));
|
parts.push(format!("Tax=${tax}"));
|
||||||
}
|
}
|
||||||
if !total.is_empty() {
|
if !total.is_empty() {
|
||||||
parts.push(format!("Total=${}", total));
|
parts.push(format!("Total=${total}"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if !parts.is_empty() {
|
if !parts.is_empty() {
|
||||||
@@ -268,7 +268,7 @@ impl<D: DataProvider> ComputedFieldsEditor<D> {
|
|||||||
let current_field = self.editor.current_field();
|
let current_field = self.editor.current_field();
|
||||||
let result = self.editor.insert_char(ch);
|
let result = self.editor.insert_char(ch);
|
||||||
|
|
||||||
if result.is_ok() && matches!(current_field, 1 | 2 | 3) {
|
if result.is_ok() && matches!(current_field, 1..=3) {
|
||||||
self.editor.on_field_changed(&mut self.calculator, current_field);
|
self.editor.on_field_changed(&mut self.calculator, current_field);
|
||||||
self.update_computed_fields();
|
self.update_computed_fields();
|
||||||
}
|
}
|
||||||
@@ -280,7 +280,7 @@ impl<D: DataProvider> ComputedFieldsEditor<D> {
|
|||||||
let current_field = self.editor.current_field();
|
let current_field = self.editor.current_field();
|
||||||
let result = self.editor.delete_backward();
|
let result = self.editor.delete_backward();
|
||||||
|
|
||||||
if result.is_ok() && matches!(current_field, 1 | 2 | 3) {
|
if result.is_ok() && matches!(current_field, 1..=3) {
|
||||||
self.editor.on_field_changed(&mut self.calculator, current_field);
|
self.editor.on_field_changed(&mut self.calculator, current_field);
|
||||||
self.update_computed_fields();
|
self.update_computed_fields();
|
||||||
}
|
}
|
||||||
@@ -292,7 +292,7 @@ impl<D: DataProvider> ComputedFieldsEditor<D> {
|
|||||||
let current_field = self.editor.current_field();
|
let current_field = self.editor.current_field();
|
||||||
let result = self.editor.delete_forward();
|
let result = self.editor.delete_forward();
|
||||||
|
|
||||||
if result.is_ok() && matches!(current_field, 1 | 2 | 3) {
|
if result.is_ok() && matches!(current_field, 1..=3) {
|
||||||
self.editor.on_field_changed(&mut self.calculator, current_field);
|
self.editor.on_field_changed(&mut self.calculator, current_field);
|
||||||
self.update_computed_fields();
|
self.update_computed_fields();
|
||||||
}
|
}
|
||||||
@@ -312,7 +312,7 @@ impl<D: DataProvider> ComputedFieldsEditor<D> {
|
|||||||
} else {
|
} else {
|
||||||
"editable"
|
"editable"
|
||||||
};
|
};
|
||||||
self.debug_message = format!("→ {} - {} field", field_name, field_type);
|
self.debug_message = format!("→ {field_name} - {field_type} field");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -328,7 +328,7 @@ impl<D: DataProvider> ComputedFieldsEditor<D> {
|
|||||||
} else {
|
} else {
|
||||||
"editable"
|
"editable"
|
||||||
};
|
};
|
||||||
self.debug_message = format!("← {} - {} field", field_name, field_type);
|
self.debug_message = format!("← {field_name} - {field_type} field");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -339,15 +339,14 @@ impl<D: DataProvider> ComputedFieldsEditor<D> {
|
|||||||
if self.editor.data_provider().is_computed_field(current) || self.is_computed_field(current) {
|
if self.editor.data_provider().is_computed_field(current) || self.is_computed_field(current) {
|
||||||
let field_name = self.editor.data_provider().field_name(current);
|
let field_name = self.editor.data_provider().field_name(current);
|
||||||
self.debug_message = format!(
|
self.debug_message = format!(
|
||||||
"🚫 {} is computed (read-only) - Press Tab to move to editable fields",
|
"🚫 {field_name} is computed (read-only) - Press Tab to move to editable fields"
|
||||||
field_name
|
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.editor.enter_edit_mode();
|
self.editor.enter_edit_mode();
|
||||||
let field_name = self.editor.data_provider().field_name(current);
|
let field_name = self.editor.data_provider().field_name(current);
|
||||||
self.debug_message = format!("✏️ Editing {} - Type to see calculations update", field_name);
|
self.debug_message = format!("✏️ Editing {field_name} - Type to see calculations update");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enter_append_mode(&mut self) {
|
fn enter_append_mode(&mut self) {
|
||||||
@@ -356,22 +355,21 @@ impl<D: DataProvider> ComputedFieldsEditor<D> {
|
|||||||
if self.editor.data_provider().is_computed_field(current) || self.is_computed_field(current) {
|
if self.editor.data_provider().is_computed_field(current) || self.is_computed_field(current) {
|
||||||
let field_name = self.editor.data_provider().field_name(current);
|
let field_name = self.editor.data_provider().field_name(current);
|
||||||
self.debug_message = format!(
|
self.debug_message = format!(
|
||||||
"🚫 {} is computed (read-only) - Press Tab to move to editable fields",
|
"🚫 {field_name} is computed (read-only) - Press Tab to move to editable fields"
|
||||||
field_name
|
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.editor.enter_append_mode();
|
self.editor.enter_append_mode();
|
||||||
let field_name = self.editor.data_provider().field_name(current);
|
let field_name = self.editor.data_provider().field_name(current);
|
||||||
self.debug_message = format!("✏️ Appending to {} - Type to see calculations", field_name);
|
self.debug_message = format!("✏️ Appending to {field_name} - Type to see calculations");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exit_edit_mode(&mut self) {
|
fn exit_edit_mode(&mut self) {
|
||||||
let current_field = self.editor.current_field();
|
let current_field = self.editor.current_field();
|
||||||
self.editor.exit_edit_mode();
|
self.editor.exit_edit_mode();
|
||||||
|
|
||||||
if matches!(current_field, 1 | 2 | 3) {
|
if matches!(current_field, 1..=3) {
|
||||||
self.editor.on_field_changed(&mut self.calculator, current_field);
|
self.editor.on_field_changed(&mut self.calculator, current_field);
|
||||||
self.update_computed_fields();
|
self.update_computed_fields();
|
||||||
}
|
}
|
||||||
@@ -503,7 +501,7 @@ fn run_app<B: Backend>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
editor.debug_message = format!("Error: {}", e);
|
editor.debug_message = format!("Error: {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -615,7 +613,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
terminal.show_cursor()?;
|
terminal.show_cursor()?;
|
||||||
|
|
||||||
if let Err(err) = res {
|
if let Err(err) = res {
|
||||||
println!("{:?}", err);
|
println!("{err:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("💰 Demo completed! Computed fields should have updated in real-time!");
|
println!("💰 Demo completed! Computed fields should have updated in real-time!");
|
||||||
|
|||||||
@@ -1,724 +0,0 @@
|
|||||||
// examples/full_canvas_demo.rs
|
|
||||||
//! Demonstrates the FULL potential of the canvas library using the native API
|
|
||||||
|
|
||||||
use std::io;
|
|
||||||
use crossterm::{
|
|
||||||
cursor::SetCursorStyle,
|
|
||||||
event::{
|
|
||||||
self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyModifiers,
|
|
||||||
},
|
|
||||||
execute,
|
|
||||||
terminal::{
|
|
||||||
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
use ratatui::{
|
|
||||||
backend::{Backend, CrosstermBackend},
|
|
||||||
layout::{Constraint, Direction, Layout},
|
|
||||||
style::{Color, Style},
|
|
||||||
text::{Line, Span},
|
|
||||||
widgets::{Block, Borders, Paragraph},
|
|
||||||
Frame, Terminal,
|
|
||||||
};
|
|
||||||
|
|
||||||
use canvas::{
|
|
||||||
canvas::{
|
|
||||||
gui::render_canvas_default,
|
|
||||||
modes::{AppMode, ModeManager, HighlightState},
|
|
||||||
},
|
|
||||||
DataProvider, FormEditor,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Update cursor style based on current AppMode
|
|
||||||
fn update_cursor_for_mode(mode: AppMode) -> io::Result<()> {
|
|
||||||
let style = match mode {
|
|
||||||
AppMode::Edit => SetCursorStyle::SteadyBar, // Thin line for insert mode
|
|
||||||
AppMode::ReadOnly => SetCursorStyle::SteadyBlock, // Block for normal mode
|
|
||||||
AppMode::Highlight => SetCursorStyle::BlinkingBlock, // Blinking block for visual mode
|
|
||||||
AppMode::General => SetCursorStyle::SteadyBlock, // Block for general mode
|
|
||||||
AppMode::Command => SetCursorStyle::SteadyUnderScore, // Underscore for command mode
|
|
||||||
};
|
|
||||||
|
|
||||||
execute!(io::stdout(), style)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Enhanced FormEditor that adds visual mode and status tracking
|
|
||||||
struct EnhancedFormEditor<D: DataProvider> {
|
|
||||||
editor: FormEditor<D>,
|
|
||||||
highlight_state: HighlightState,
|
|
||||||
has_unsaved_changes: bool,
|
|
||||||
debug_message: String,
|
|
||||||
command_buffer: String, // For multi-key vim commands like "gg"
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<D: DataProvider> EnhancedFormEditor<D> {
|
|
||||||
fn new(data_provider: D) -> Self {
|
|
||||||
Self {
|
|
||||||
editor: FormEditor::new(data_provider),
|
|
||||||
highlight_state: HighlightState::Off,
|
|
||||||
has_unsaved_changes: false,
|
|
||||||
debug_message: "Full Canvas Demo - All features enabled".to_string(),
|
|
||||||
command_buffer: String::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// === COMMAND BUFFER HANDLING ===
|
|
||||||
|
|
||||||
fn clear_command_buffer(&mut self) {
|
|
||||||
self.command_buffer.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_to_command_buffer(&mut self, ch: char) {
|
|
||||||
self.command_buffer.push(ch);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_command_buffer(&self) -> &str {
|
|
||||||
&self.command_buffer
|
|
||||||
}
|
|
||||||
|
|
||||||
fn has_pending_command(&self) -> bool {
|
|
||||||
!self.command_buffer.is_empty()
|
|
||||||
}
|
|
||||||
|
|
||||||
// === VISUAL/HIGHLIGHT MODE SUPPORT ===
|
|
||||||
|
|
||||||
fn enter_visual_mode(&mut self) {
|
|
||||||
if ModeManager::can_enter_highlight_mode(self.editor.mode()) {
|
|
||||||
self.editor.set_mode(AppMode::Highlight);
|
|
||||||
self.highlight_state = HighlightState::Characterwise {
|
|
||||||
anchor: (
|
|
||||||
self.editor.current_field(),
|
|
||||||
self.editor.cursor_position(),
|
|
||||||
),
|
|
||||||
};
|
|
||||||
self.debug_message = "-- VISUAL --".to_string();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn enter_visual_line_mode(&mut self) {
|
|
||||||
if ModeManager::can_enter_highlight_mode(self.editor.mode()) {
|
|
||||||
self.editor.set_mode(AppMode::Highlight);
|
|
||||||
self.highlight_state =
|
|
||||||
HighlightState::Linewise { anchor_line: self.editor.current_field() };
|
|
||||||
self.debug_message = "-- VISUAL LINE --".to_string();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn exit_visual_mode(&mut self) {
|
|
||||||
self.highlight_state = HighlightState::Off;
|
|
||||||
if self.editor.mode() == AppMode::Highlight {
|
|
||||||
self.editor.set_mode(AppMode::ReadOnly);
|
|
||||||
self.debug_message = "Visual mode exited".to_string();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update_visual_selection(&mut self) {
|
|
||||||
if self.editor.mode() == AppMode::Highlight {
|
|
||||||
match &self.highlight_state {
|
|
||||||
HighlightState::Characterwise { anchor: _ } => {
|
|
||||||
self.debug_message = format!(
|
|
||||||
"Visual selection: char {} in field {}",
|
|
||||||
self.editor.cursor_position(),
|
|
||||||
self.editor.current_field()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
HighlightState::Linewise { anchor_line: _ } => {
|
|
||||||
self.debug_message = format!(
|
|
||||||
"Visual line selection: field {}",
|
|
||||||
self.editor.current_field()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// === ENHANCED MOVEMENT WITH VISUAL UPDATES ===
|
|
||||||
|
|
||||||
fn move_left(&mut self) {
|
|
||||||
self.editor.move_left();
|
|
||||||
self.update_visual_selection();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn move_right(&mut self) {
|
|
||||||
self.editor.move_right();
|
|
||||||
self.update_visual_selection();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn move_up(&mut self) {
|
|
||||||
self.editor.move_up();
|
|
||||||
self.update_visual_selection();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn move_down(&mut self) {
|
|
||||||
self.editor.move_down();
|
|
||||||
self.update_visual_selection();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn move_word_next(&mut self) {
|
|
||||||
self.editor.move_word_next();
|
|
||||||
self.update_visual_selection();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn move_word_prev(&mut self) {
|
|
||||||
self.editor.move_word_prev();
|
|
||||||
self.update_visual_selection();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn move_word_end(&mut self) {
|
|
||||||
self.editor.move_word_end();
|
|
||||||
self.update_visual_selection();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn move_word_end_prev(&mut self) {
|
|
||||||
self.editor.move_word_end_prev();
|
|
||||||
self.update_visual_selection();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn move_line_start(&mut self) {
|
|
||||||
self.editor.move_line_start();
|
|
||||||
self.update_visual_selection();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn move_line_end(&mut self) {
|
|
||||||
self.editor.move_line_end();
|
|
||||||
self.update_visual_selection();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn move_first_line(&mut self) {
|
|
||||||
self.editor.move_first_line();
|
|
||||||
self.update_visual_selection();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn move_last_line(&mut self) {
|
|
||||||
self.editor.move_last_line();
|
|
||||||
self.update_visual_selection();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn prev_field(&mut self) {
|
|
||||||
self.editor.prev_field();
|
|
||||||
self.update_visual_selection();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn next_field(&mut self) {
|
|
||||||
self.editor.next_field();
|
|
||||||
self.update_visual_selection();
|
|
||||||
}
|
|
||||||
|
|
||||||
// === DELETE OPERATIONS ===
|
|
||||||
|
|
||||||
fn delete_backward(&mut self) -> anyhow::Result<()> {
|
|
||||||
let result = self.editor.delete_backward();
|
|
||||||
if result.is_ok() {
|
|
||||||
self.has_unsaved_changes = true;
|
|
||||||
self.debug_message = "Deleted character backward".to_string();
|
|
||||||
}
|
|
||||||
Ok(result?)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn delete_forward(&mut self) -> anyhow::Result<()> {
|
|
||||||
let result = self.editor.delete_forward();
|
|
||||||
if result.is_ok() {
|
|
||||||
self.has_unsaved_changes = true;
|
|
||||||
self.debug_message = "Deleted character forward".to_string();
|
|
||||||
}
|
|
||||||
Ok(result?)
|
|
||||||
}
|
|
||||||
|
|
||||||
// === MODE TRANSITIONS ===
|
|
||||||
|
|
||||||
fn enter_edit_mode(&mut self) {
|
|
||||||
self.editor.enter_edit_mode();
|
|
||||||
self.debug_message = "-- INSERT --".to_string();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn exit_edit_mode(&mut self) {
|
|
||||||
self.editor.exit_edit_mode();
|
|
||||||
self.exit_visual_mode();
|
|
||||||
self.debug_message = "".to_string();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn insert_char(&mut self, ch: char) -> anyhow::Result<()> {
|
|
||||||
let result = self.editor.insert_char(ch);
|
|
||||||
if result.is_ok() {
|
|
||||||
self.has_unsaved_changes = true;
|
|
||||||
}
|
|
||||||
Ok(result?)
|
|
||||||
}
|
|
||||||
|
|
||||||
// === DELEGATE TO ORIGINAL EDITOR ===
|
|
||||||
|
|
||||||
fn current_field(&self) -> usize {
|
|
||||||
self.editor.current_field()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cursor_position(&self) -> usize {
|
|
||||||
self.editor.cursor_position()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn mode(&self) -> AppMode {
|
|
||||||
self.editor.mode()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn current_text(&self) -> &str {
|
|
||||||
self.editor.current_text()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn data_provider(&self) -> &D {
|
|
||||||
self.editor.data_provider()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ui_state(&self) -> &canvas::EditorState {
|
|
||||||
self.editor.ui_state()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_mode(&mut self, mode: AppMode) {
|
|
||||||
self.editor.set_mode(mode);
|
|
||||||
if mode != AppMode::Highlight {
|
|
||||||
self.exit_visual_mode();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// === STATUS AND DEBUG ===
|
|
||||||
|
|
||||||
fn set_debug_message(&mut self, msg: String) {
|
|
||||||
self.debug_message = msg;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn debug_message(&self) -> &str {
|
|
||||||
&self.debug_message
|
|
||||||
}
|
|
||||||
|
|
||||||
fn highlight_state(&self) -> &HighlightState {
|
|
||||||
&self.highlight_state
|
|
||||||
}
|
|
||||||
|
|
||||||
fn has_unsaved_changes(&self) -> bool {
|
|
||||||
self.has_unsaved_changes
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Demo form data with interesting text for word movement
|
|
||||||
struct FullDemoData {
|
|
||||||
fields: Vec<(String, String)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FullDemoData {
|
|
||||||
fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
fields: vec![
|
|
||||||
("Name".to_string(), "John-Paul McDonald".to_string()),
|
|
||||||
(
|
|
||||||
"Email".to_string(),
|
|
||||||
"user@example-domain.com".to_string(),
|
|
||||||
),
|
|
||||||
("Phone".to_string(), "+1 (555) 123-4567".to_string()),
|
|
||||||
("Address".to_string(), "123 Main St, Apt 4B".to_string()),
|
|
||||||
(
|
|
||||||
"Tags".to_string(),
|
|
||||||
"urgent,important,follow-up".to_string(),
|
|
||||||
),
|
|
||||||
(
|
|
||||||
"Notes".to_string(),
|
|
||||||
"This is a sample note with multiple words, punctuation! And symbols @#$"
|
|
||||||
.to_string(),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DataProvider for FullDemoData {
|
|
||||||
fn field_count(&self) -> usize {
|
|
||||||
self.fields.len()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn field_name(&self, index: usize) -> &str {
|
|
||||||
&self.fields[index].0
|
|
||||||
}
|
|
||||||
|
|
||||||
fn field_value(&self, index: usize) -> &str {
|
|
||||||
&self.fields[index].1
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_field_value(&mut self, index: usize, value: String) {
|
|
||||||
self.fields[index].1 = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn supports_suggestions(&self, _field_index: usize) -> bool {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
fn display_value(&self, _index: usize) -> Option<&str> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Full vim-like key handling using the native FormEditor API
|
|
||||||
fn handle_key_press(
|
|
||||||
key: KeyCode,
|
|
||||||
modifiers: KeyModifiers,
|
|
||||||
editor: &mut EnhancedFormEditor<FullDemoData>,
|
|
||||||
) -> anyhow::Result<bool> {
|
|
||||||
let old_mode = editor.mode(); // Store mode before processing
|
|
||||||
|
|
||||||
// Quit handling
|
|
||||||
if (key == KeyCode::Char('q') && modifiers.contains(KeyModifiers::CONTROL))
|
|
||||||
|| (key == KeyCode::Char('c') && modifiers.contains(KeyModifiers::CONTROL))
|
|
||||||
|| key == KeyCode::F(10)
|
|
||||||
{
|
|
||||||
return Ok(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
match (old_mode, key, modifiers) {
|
|
||||||
// === MODE TRANSITIONS ===
|
|
||||||
(AppMode::ReadOnly, KeyCode::Char('i'), _) => {
|
|
||||||
editor.enter_edit_mode();
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
}
|
|
||||||
(AppMode::ReadOnly, KeyCode::Char('a'), _) => {
|
|
||||||
editor.move_right(); // Move after current character
|
|
||||||
editor.enter_edit_mode();
|
|
||||||
editor.set_debug_message("-- INSERT -- (append)".to_string());
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
}
|
|
||||||
(AppMode::ReadOnly, KeyCode::Char('A'), _) => {
|
|
||||||
editor.move_line_end();
|
|
||||||
editor.enter_edit_mode();
|
|
||||||
editor.set_debug_message("-- INSERT -- (end of line)".to_string());
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
}
|
|
||||||
(AppMode::ReadOnly, KeyCode::Char('o'), _) => {
|
|
||||||
editor.move_line_end();
|
|
||||||
editor.enter_edit_mode();
|
|
||||||
editor.set_debug_message("-- INSERT -- (open line)".to_string());
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
}
|
|
||||||
(AppMode::ReadOnly, KeyCode::Char('v'), _) => {
|
|
||||||
editor.enter_visual_mode();
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
}
|
|
||||||
(AppMode::ReadOnly, KeyCode::Char('V'), _) => {
|
|
||||||
editor.enter_visual_line_mode();
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
}
|
|
||||||
(_, KeyCode::Esc, _) => {
|
|
||||||
editor.exit_edit_mode();
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
// === MOVEMENT: VIM-STYLE NAVIGATION ===
|
|
||||||
|
|
||||||
// Basic movement (hjkl and arrows)
|
|
||||||
(AppMode::ReadOnly | AppMode::Highlight, KeyCode::Char('h'), _)
|
|
||||||
| (AppMode::ReadOnly | AppMode::Highlight, KeyCode::Left, _) => {
|
|
||||||
editor.move_left();
|
|
||||||
editor.set_debug_message("← left".to_string());
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
}
|
|
||||||
(AppMode::ReadOnly | AppMode::Highlight, KeyCode::Char('l'), _)
|
|
||||||
| (AppMode::ReadOnly | AppMode::Highlight, KeyCode::Right, _) => {
|
|
||||||
editor.move_right();
|
|
||||||
editor.set_debug_message("→ right".to_string());
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
}
|
|
||||||
(AppMode::ReadOnly | AppMode::Highlight, KeyCode::Char('j'), _)
|
|
||||||
| (AppMode::ReadOnly | AppMode::Highlight, KeyCode::Down, _) => {
|
|
||||||
editor.move_down();
|
|
||||||
editor.set_debug_message("↓ next field".to_string());
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
}
|
|
||||||
(AppMode::ReadOnly | AppMode::Highlight, KeyCode::Char('k'), _)
|
|
||||||
| (AppMode::ReadOnly | AppMode::Highlight, KeyCode::Up, _) => {
|
|
||||||
editor.move_up();
|
|
||||||
editor.set_debug_message("↑ previous field".to_string());
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Word movement - Full vim word navigation
|
|
||||||
(AppMode::ReadOnly | AppMode::Highlight, KeyCode::Char('w'), _) => {
|
|
||||||
editor.move_word_next();
|
|
||||||
editor.set_debug_message("w: next word start".to_string());
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
}
|
|
||||||
(AppMode::ReadOnly | AppMode::Highlight, KeyCode::Char('b'), _) => {
|
|
||||||
editor.move_word_prev();
|
|
||||||
editor.set_debug_message("b: previous word start".to_string());
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
}
|
|
||||||
(AppMode::ReadOnly | AppMode::Highlight, KeyCode::Char('e'), _) => {
|
|
||||||
editor.move_word_end();
|
|
||||||
editor.set_debug_message("e: word end".to_string());
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
}
|
|
||||||
(AppMode::ReadOnly | AppMode::Highlight, KeyCode::Char('W'), _) => {
|
|
||||||
editor.move_word_end_prev();
|
|
||||||
editor.set_debug_message("W: previous word end".to_string());
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Line movement
|
|
||||||
(AppMode::ReadOnly | AppMode::Highlight, KeyCode::Char('0'), _)
|
|
||||||
| (AppMode::ReadOnly | AppMode::Highlight, KeyCode::Home, _) => {
|
|
||||||
editor.move_line_start();
|
|
||||||
editor.set_debug_message("0: line start".to_string());
|
|
||||||
}
|
|
||||||
(AppMode::ReadOnly | AppMode::Highlight, KeyCode::Char('$'), _)
|
|
||||||
| (AppMode::ReadOnly | AppMode::Highlight, KeyCode::End, _) => {
|
|
||||||
editor.move_line_end();
|
|
||||||
editor.set_debug_message("$: line end".to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Field/document movement
|
|
||||||
(AppMode::ReadOnly | AppMode::Highlight, KeyCode::Char('g'), _) => {
|
|
||||||
if editor.get_command_buffer() == "g" {
|
|
||||||
// Second 'g' - execute "gg" command
|
|
||||||
editor.move_first_line();
|
|
||||||
editor.set_debug_message("gg: first field".to_string());
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
} else {
|
|
||||||
// First 'g' - start command buffer
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
editor.add_to_command_buffer('g');
|
|
||||||
editor.set_debug_message("g".to_string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
(AppMode::ReadOnly | AppMode::Highlight, KeyCode::Char('G'), _) => {
|
|
||||||
editor.move_last_line();
|
|
||||||
editor.set_debug_message("G: last field".to_string());
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
// === EDIT MODE MOVEMENT ===
|
|
||||||
(AppMode::Edit, KeyCode::Left, m) if m.contains(KeyModifiers::CONTROL) => {
|
|
||||||
editor.move_word_prev();
|
|
||||||
editor.set_debug_message("Ctrl+← word back".to_string());
|
|
||||||
}
|
|
||||||
(AppMode::Edit, KeyCode::Right, m) if m.contains(KeyModifiers::CONTROL) => {
|
|
||||||
editor.move_word_next();
|
|
||||||
editor.set_debug_message("Ctrl+→ word forward".to_string());
|
|
||||||
}
|
|
||||||
(AppMode::Edit, KeyCode::Left, _) => {
|
|
||||||
editor.move_left();
|
|
||||||
}
|
|
||||||
(AppMode::Edit, KeyCode::Right, _) => {
|
|
||||||
editor.move_right();
|
|
||||||
}
|
|
||||||
(AppMode::Edit, KeyCode::Up, _) => {
|
|
||||||
editor.move_up();
|
|
||||||
}
|
|
||||||
(AppMode::Edit, KeyCode::Down, _) => {
|
|
||||||
editor.move_down();
|
|
||||||
}
|
|
||||||
(AppMode::Edit, KeyCode::Home, _) => {
|
|
||||||
editor.move_line_start();
|
|
||||||
}
|
|
||||||
(AppMode::Edit, KeyCode::End, _) => {
|
|
||||||
editor.move_line_end();
|
|
||||||
}
|
|
||||||
|
|
||||||
// === DELETE OPERATIONS ===
|
|
||||||
(AppMode::Edit, KeyCode::Backspace, _) => {
|
|
||||||
editor.delete_backward()?;
|
|
||||||
}
|
|
||||||
(AppMode::Edit, KeyCode::Delete, _) => {
|
|
||||||
editor.delete_forward()?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete operations in normal mode (vim x)
|
|
||||||
(AppMode::ReadOnly, KeyCode::Char('x'), _) => {
|
|
||||||
editor.delete_forward()?;
|
|
||||||
editor.set_debug_message("x: deleted character".to_string());
|
|
||||||
}
|
|
||||||
(AppMode::ReadOnly, KeyCode::Char('X'), _) => {
|
|
||||||
editor.delete_backward()?;
|
|
||||||
editor.set_debug_message("X: deleted character backward".to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
// === TAB NAVIGATION ===
|
|
||||||
(_, KeyCode::Tab, _) => {
|
|
||||||
editor.next_field();
|
|
||||||
editor.set_debug_message("Tab: next field".to_string());
|
|
||||||
}
|
|
||||||
(_, KeyCode::BackTab, _) => {
|
|
||||||
editor.prev_field();
|
|
||||||
editor.set_debug_message("Shift+Tab: previous field".to_string());
|
|
||||||
}
|
|
||||||
|
|
||||||
// === CHARACTER INPUT ===
|
|
||||||
(AppMode::Edit, KeyCode::Char(c), m) if !m.contains(KeyModifiers::CONTROL) => {
|
|
||||||
editor.insert_char(c)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
// === DEBUG/INFO COMMANDS ===
|
|
||||||
(AppMode::ReadOnly, KeyCode::Char('?'), _) => {
|
|
||||||
editor.set_debug_message(format!(
|
|
||||||
"Field {}/{}, Pos {}, Mode: {:?}",
|
|
||||||
editor.current_field() + 1,
|
|
||||||
editor.data_provider().field_count(),
|
|
||||||
editor.cursor_position(),
|
|
||||||
editor.mode()
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => {
|
|
||||||
// If we have a pending command and this key doesn't complete it, clear the buffer
|
|
||||||
if editor.has_pending_command() {
|
|
||||||
editor.clear_command_buffer();
|
|
||||||
editor.set_debug_message("Invalid command sequence".to_string());
|
|
||||||
} else {
|
|
||||||
editor.set_debug_message(format!(
|
|
||||||
"Unhandled: {:?} + {:?} in {:?} mode",
|
|
||||||
key, modifiers, old_mode
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update cursor if mode changed
|
|
||||||
let new_mode = editor.mode();
|
|
||||||
if old_mode != new_mode {
|
|
||||||
update_cursor_for_mode(new_mode)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run_app<B: Backend>(
|
|
||||||
terminal: &mut Terminal<B>,
|
|
||||||
mut editor: EnhancedFormEditor<FullDemoData>,
|
|
||||||
) -> io::Result<()> {
|
|
||||||
loop {
|
|
||||||
terminal.draw(|f| ui(f, &editor))?;
|
|
||||||
|
|
||||||
if let Event::Key(key) = event::read()? {
|
|
||||||
match handle_key_press(key.code, key.modifiers, &mut editor) {
|
|
||||||
Ok(should_continue) => {
|
|
||||||
if !should_continue {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
editor.set_debug_message(format!("Error: {}", e));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ui(f: &mut Frame, editor: &EnhancedFormEditor<FullDemoData>) {
|
|
||||||
let chunks = Layout::default()
|
|
||||||
.direction(Direction::Vertical)
|
|
||||||
.constraints([Constraint::Min(8), Constraint::Length(8)])
|
|
||||||
.split(f.area());
|
|
||||||
|
|
||||||
render_enhanced_canvas(f, chunks[0], editor);
|
|
||||||
render_status_and_help(f, chunks[1], editor);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render_enhanced_canvas(
|
|
||||||
f: &mut Frame,
|
|
||||||
area: ratatui::layout::Rect,
|
|
||||||
editor: &EnhancedFormEditor<FullDemoData>,
|
|
||||||
) {
|
|
||||||
render_canvas_default(f, area, &editor.editor);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render_status_and_help(
|
|
||||||
f: &mut Frame,
|
|
||||||
area: ratatui::layout::Rect,
|
|
||||||
editor: &EnhancedFormEditor<FullDemoData>,
|
|
||||||
) {
|
|
||||||
let chunks = Layout::default()
|
|
||||||
.direction(Direction::Vertical)
|
|
||||||
.constraints([Constraint::Length(3), Constraint::Length(5)])
|
|
||||||
.split(area);
|
|
||||||
|
|
||||||
// Status bar
|
|
||||||
let mode_text = match editor.mode() {
|
|
||||||
AppMode::Edit => "INSERT",
|
|
||||||
AppMode::ReadOnly => "NORMAL",
|
|
||||||
AppMode::Highlight => match editor.highlight_state() {
|
|
||||||
HighlightState::Characterwise { .. } => "VISUAL",
|
|
||||||
HighlightState::Linewise { .. } => "VISUAL LINE",
|
|
||||||
_ => "VISUAL",
|
|
||||||
},
|
|
||||||
_ => "NORMAL",
|
|
||||||
};
|
|
||||||
|
|
||||||
let status_text = if editor.has_pending_command() {
|
|
||||||
format!("-- {} -- {} [{}]", mode_text, editor.debug_message(), editor.get_command_buffer())
|
|
||||||
} else if editor.has_unsaved_changes() {
|
|
||||||
format!("-- {} -- [Modified] {}", mode_text, editor.debug_message())
|
|
||||||
} else {
|
|
||||||
format!("-- {} -- {}", mode_text, editor.debug_message())
|
|
||||||
};
|
|
||||||
|
|
||||||
let status = Paragraph::new(Line::from(Span::raw(status_text)))
|
|
||||||
.block(Block::default().borders(Borders::ALL).title("Status"));
|
|
||||||
|
|
||||||
f.render_widget(status, chunks[0]);
|
|
||||||
|
|
||||||
// Help text
|
|
||||||
let help_text = match editor.mode() {
|
|
||||||
AppMode::ReadOnly => {
|
|
||||||
if editor.has_pending_command() {
|
|
||||||
match editor.get_command_buffer() {
|
|
||||||
"g" => "Press 'g' again for first field, or any other key to cancel",
|
|
||||||
_ => "Pending command... (Esc to cancel)"
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
"Normal: hjkl/arrows=move, w/b/e=words, 0/$=line, gg/G=first/last, i/a/A=insert, v/V=visual, x/X=delete, ?=info"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
AppMode::Edit => {
|
|
||||||
"Insert: arrows=move, Ctrl+arrows=words, Backspace/Del=delete, Esc=normal, Tab/Shift+Tab=fields"
|
|
||||||
}
|
|
||||||
AppMode::Highlight => {
|
|
||||||
"Visual: hjkl/arrows=extend selection, w/b/e=word selection, Esc=normal"
|
|
||||||
}
|
|
||||||
_ => "Press ? for help"
|
|
||||||
};
|
|
||||||
|
|
||||||
let help = Paragraph::new(Line::from(Span::raw(help_text)))
|
|
||||||
.block(Block::default().borders(Borders::ALL).title("Commands"))
|
|
||||||
.style(Style::default().fg(Color::Gray));
|
|
||||||
|
|
||||||
f.render_widget(help, chunks[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
enable_raw_mode()?;
|
|
||||||
let mut stdout = io::stdout();
|
|
||||||
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
|
||||||
let backend = CrosstermBackend::new(stdout);
|
|
||||||
let mut terminal = Terminal::new(backend)?;
|
|
||||||
|
|
||||||
let data = FullDemoData::new();
|
|
||||||
let mut editor = EnhancedFormEditor::new(data);
|
|
||||||
editor.set_mode(AppMode::ReadOnly); // Start in normal mode
|
|
||||||
|
|
||||||
// Set initial cursor style
|
|
||||||
update_cursor_for_mode(editor.mode())?;
|
|
||||||
|
|
||||||
let res = run_app(&mut terminal, editor);
|
|
||||||
|
|
||||||
// Reset cursor style on exit
|
|
||||||
execute!(io::stdout(), SetCursorStyle::DefaultUserShape)?;
|
|
||||||
|
|
||||||
disable_raw_mode()?;
|
|
||||||
execute!(
|
|
||||||
terminal.backend_mut(),
|
|
||||||
LeaveAlternateScreen,
|
|
||||||
DisableMouseCapture
|
|
||||||
)?;
|
|
||||||
terminal.show_cursor()?;
|
|
||||||
|
|
||||||
if let Err(err) = res {
|
|
||||||
println!("{:?}", err);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
@@ -218,7 +218,7 @@ impl<D: DataProvider> AutoCursorFormEditor<D> {
|
|||||||
self.has_unsaved_changes = true;
|
self.has_unsaved_changes = true;
|
||||||
self.debug_message = "⌫ Deleted character backward".to_string();
|
self.debug_message = "⌫ Deleted character backward".to_string();
|
||||||
}
|
}
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delete_forward(&mut self) -> anyhow::Result<()> {
|
fn delete_forward(&mut self) -> anyhow::Result<()> {
|
||||||
@@ -227,7 +227,7 @@ impl<D: DataProvider> AutoCursorFormEditor<D> {
|
|||||||
self.has_unsaved_changes = true;
|
self.has_unsaved_changes = true;
|
||||||
self.debug_message = "⌦ Deleted character forward".to_string();
|
self.debug_message = "⌦ Deleted character forward".to_string();
|
||||||
}
|
}
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
// === SUGGESTIONS CONTROL WRAPPERS ===
|
// === SUGGESTIONS CONTROL WRAPPERS ===
|
||||||
@@ -259,7 +259,7 @@ impl<D: DataProvider> AutoCursorFormEditor<D> {
|
|||||||
if result.is_ok() {
|
if result.is_ok() {
|
||||||
self.has_unsaved_changes = true;
|
self.has_unsaved_changes = true;
|
||||||
}
|
}
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
// === MANUAL CURSOR OVERRIDE DEMONSTRATION ===
|
// === MANUAL CURSOR OVERRIDE DEMONSTRATION ===
|
||||||
@@ -562,7 +562,7 @@ impl ProductionSuggestionsProvider {
|
|||||||
query.is_empty() || item.to_lowercase().starts_with(&query_lower)
|
query.is_empty() || item.to_lowercase().starts_with(&query_lower)
|
||||||
})
|
})
|
||||||
.map(|(item, description)| SuggestionItem {
|
.map(|(item, description)| SuggestionItem {
|
||||||
display_text: format!("{} - {}", item, description),
|
display_text: format!("{item} - {description}"),
|
||||||
value_to_store: item.to_string(),
|
value_to_store: item.to_string(),
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
@@ -625,7 +625,7 @@ async fn handle_key_press(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
editor.set_debug_message(format!("❌ Suggestion error: {}", e));
|
editor.set_debug_message(format!("❌ Suggestion error: {e}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -639,7 +639,7 @@ async fn handle_key_press(
|
|||||||
(_, KeyCode::Enter, _) => {
|
(_, KeyCode::Enter, _) => {
|
||||||
if editor.is_suggestions_active() {
|
if editor.is_suggestions_active() {
|
||||||
if let Some(applied) = editor.apply_suggestion() {
|
if let Some(applied) = editor.apply_suggestion() {
|
||||||
editor.set_debug_message(format!("✅ Selected: {}", applied));
|
editor.set_debug_message(format!("✅ Selected: {applied}"));
|
||||||
} else {
|
} else {
|
||||||
editor.set_debug_message("❌ No suggestion selected".to_string());
|
editor.set_debug_message("❌ No suggestion selected".to_string());
|
||||||
}
|
}
|
||||||
@@ -647,7 +647,7 @@ async fn handle_key_press(
|
|||||||
editor.next_field();
|
editor.next_field();
|
||||||
let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
|
let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
|
||||||
let field_name = field_names.get(editor.current_field()).unwrap_or(&"Field");
|
let field_name = field_names.get(editor.current_field()).unwrap_or(&"Field");
|
||||||
editor.set_debug_message(format!("Enter: moved to {} field", field_name));
|
editor.set_debug_message(format!("Enter: moved to {field_name} field"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -726,7 +726,7 @@ async fn handle_key_press(
|
|||||||
editor.move_down();
|
editor.move_down();
|
||||||
let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
|
let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
|
||||||
let field_name = field_names.get(editor.current_field()).unwrap_or(&"Field");
|
let field_name = field_names.get(editor.current_field()).unwrap_or(&"Field");
|
||||||
editor.set_debug_message(format!("↓ moved to {} field", field_name));
|
editor.set_debug_message(format!("↓ moved to {field_name} field"));
|
||||||
editor.clear_command_buffer();
|
editor.clear_command_buffer();
|
||||||
}
|
}
|
||||||
(AppMode::ReadOnly | AppMode::Highlight, KeyCode::Char('k'), _)
|
(AppMode::ReadOnly | AppMode::Highlight, KeyCode::Char('k'), _)
|
||||||
@@ -734,7 +734,7 @@ async fn handle_key_press(
|
|||||||
editor.move_up();
|
editor.move_up();
|
||||||
let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
|
let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
|
||||||
let field_name = field_names.get(editor.current_field()).unwrap_or(&"Field");
|
let field_name = field_names.get(editor.current_field()).unwrap_or(&"Field");
|
||||||
editor.set_debug_message(format!("↑ moved to {} field", field_name));
|
editor.set_debug_message(format!("↑ moved to {field_name} field"));
|
||||||
editor.clear_command_buffer();
|
editor.clear_command_buffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -829,7 +829,7 @@ async fn handle_key_press(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
editor.set_debug_message(format!("❌ Suggestion error: {}", e));
|
editor.set_debug_message(format!("❌ Suggestion error: {e}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -850,7 +850,7 @@ async fn handle_key_press(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
editor.set_debug_message(format!("❌ Suggestion error: {}", e));
|
editor.set_debug_message(format!("❌ Suggestion error: {e}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -883,7 +883,7 @@ async fn handle_key_press(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
editor.set_debug_message(format!("❌ Suggestion error: {}", e));
|
editor.set_debug_message(format!("❌ Suggestion error: {e}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -912,8 +912,7 @@ async fn handle_key_press(
|
|||||||
let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
|
let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
|
||||||
let current_field = field_names.get(editor.current_field()).unwrap_or(&"Field");
|
let current_field = field_names.get(editor.current_field()).unwrap_or(&"Field");
|
||||||
editor.set_debug_message(format!(
|
editor.set_debug_message(format!(
|
||||||
"{} field - Try: i=insert, Tab=suggestions, j/k=move. Key: {:?}",
|
"{current_field} field - Try: i=insert, Tab=suggestions, j/k=move. Key: {key:?}"
|
||||||
current_field, key
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -939,7 +938,7 @@ async fn run_app<B: Backend>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
editor.set_debug_message(format!("Error: {}", e));
|
editor.set_debug_message(format!("Error: {e}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -962,7 +961,7 @@ fn ui(f: &mut Frame, editor: &AutoCursorFormEditor<ApplicationData>) {
|
|||||||
f,
|
f,
|
||||||
chunks[0],
|
chunks[0],
|
||||||
input_rect,
|
input_rect,
|
||||||
&canvas::canvas::theme::DefaultCanvasTheme::default(),
|
&canvas::canvas::theme::DefaultCanvasTheme,
|
||||||
editor.inner(),
|
editor.inner(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -1110,7 +1109,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
terminal.show_cursor()?;
|
terminal.show_cursor()?;
|
||||||
|
|
||||||
if let Err(err) = res {
|
if let Err(err) = res {
|
||||||
println!("{:?}", err);
|
println!("{err:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("🚀 Ready to integrate this architecture into your production app!");
|
println!("🚀 Ready to integrate this architecture into your production app!");
|
||||||
|
|||||||
@@ -210,7 +210,7 @@ impl<D: DataProvider> AutoCursorFormEditor<D> {
|
|||||||
self.has_unsaved_changes = true;
|
self.has_unsaved_changes = true;
|
||||||
self.debug_message = "⌫ Deleted character backward".to_string();
|
self.debug_message = "⌫ Deleted character backward".to_string();
|
||||||
}
|
}
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delete_forward(&mut self) -> anyhow::Result<()> {
|
fn delete_forward(&mut self) -> anyhow::Result<()> {
|
||||||
@@ -219,7 +219,7 @@ impl<D: DataProvider> AutoCursorFormEditor<D> {
|
|||||||
self.has_unsaved_changes = true;
|
self.has_unsaved_changes = true;
|
||||||
self.debug_message = "⌦ Deleted character forward".to_string();
|
self.debug_message = "⌦ Deleted character forward".to_string();
|
||||||
}
|
}
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
// === SUGGESTIONS CONTROL WRAPPERS ===
|
// === SUGGESTIONS CONTROL WRAPPERS ===
|
||||||
@@ -251,7 +251,7 @@ impl<D: DataProvider> AutoCursorFormEditor<D> {
|
|||||||
if result.is_ok() {
|
if result.is_ok() {
|
||||||
self.has_unsaved_changes = true;
|
self.has_unsaved_changes = true;
|
||||||
}
|
}
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
// === PRODUCTION-READY NON-BLOCKING SUGGESTIONS ===
|
// === PRODUCTION-READY NON-BLOCKING SUGGESTIONS ===
|
||||||
@@ -275,7 +275,7 @@ impl<D: DataProvider> AutoCursorFormEditor<D> {
|
|||||||
if applied {
|
if applied {
|
||||||
self.editor.update_inline_completion();
|
self.editor.update_inline_completion();
|
||||||
if self.editor.suggestions().is_empty() {
|
if self.editor.suggestions().is_empty() {
|
||||||
self.set_debug_message(format!("🔍 No matches for '{}'", query));
|
self.set_debug_message(format!("🔍 No matches for '{query}'"));
|
||||||
} else {
|
} else {
|
||||||
self.set_debug_message(format!("✨ {} matches for '{}'", self.editor.suggestions().len(), query));
|
self.set_debug_message(format!("✨ {} matches for '{}'", self.editor.suggestions().len(), query));
|
||||||
}
|
}
|
||||||
@@ -283,7 +283,7 @@ impl<D: DataProvider> AutoCursorFormEditor<D> {
|
|||||||
// If not applied, results were stale (user kept typing)
|
// If not applied, results were stale (user kept typing)
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.set_debug_message(format!("❌ Suggestion error: {}", e));
|
self.set_debug_message(format!("❌ Suggestion error: {e}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -574,7 +574,7 @@ impl ProductionSuggestionsProvider {
|
|||||||
query.is_empty() || item.to_lowercase().starts_with(&query_lower)
|
query.is_empty() || item.to_lowercase().starts_with(&query_lower)
|
||||||
})
|
})
|
||||||
.map(|(item, description)| SuggestionItem {
|
.map(|(item, description)| SuggestionItem {
|
||||||
display_text: format!("{} - {}", item, description),
|
display_text: format!("{item} - {description}"),
|
||||||
value_to_store: item.to_string(),
|
value_to_store: item.to_string(),
|
||||||
})
|
})
|
||||||
.collect()
|
.collect()
|
||||||
@@ -634,7 +634,7 @@ async fn handle_key_press(
|
|||||||
(_, KeyCode::Enter, _) => {
|
(_, KeyCode::Enter, _) => {
|
||||||
if editor.is_suggestions_active() {
|
if editor.is_suggestions_active() {
|
||||||
if let Some(applied) = editor.apply_suggestion() {
|
if let Some(applied) = editor.apply_suggestion() {
|
||||||
editor.set_debug_message(format!("✅ Selected: {}", applied));
|
editor.set_debug_message(format!("✅ Selected: {applied}"));
|
||||||
} else {
|
} else {
|
||||||
editor.set_debug_message("❌ No suggestion selected".to_string());
|
editor.set_debug_message("❌ No suggestion selected".to_string());
|
||||||
}
|
}
|
||||||
@@ -642,7 +642,7 @@ async fn handle_key_press(
|
|||||||
editor.next_field();
|
editor.next_field();
|
||||||
let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
|
let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
|
||||||
let field_name = field_names.get(editor.current_field()).unwrap_or(&"Field");
|
let field_name = field_names.get(editor.current_field()).unwrap_or(&"Field");
|
||||||
editor.set_debug_message(format!("Enter: moved to {} field", field_name));
|
editor.set_debug_message(format!("Enter: moved to {field_name} field"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -722,7 +722,7 @@ async fn handle_key_press(
|
|||||||
editor.move_down();
|
editor.move_down();
|
||||||
let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
|
let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
|
||||||
let field_name = field_names.get(editor.current_field()).unwrap_or(&"Field");
|
let field_name = field_names.get(editor.current_field()).unwrap_or(&"Field");
|
||||||
editor.set_debug_message(format!("↓ moved to {} field", field_name));
|
editor.set_debug_message(format!("↓ moved to {field_name} field"));
|
||||||
editor.clear_command_buffer();
|
editor.clear_command_buffer();
|
||||||
}
|
}
|
||||||
(AppMode::ReadOnly | AppMode::Highlight, KeyCode::Char('k'), _)
|
(AppMode::ReadOnly | AppMode::Highlight, KeyCode::Char('k'), _)
|
||||||
@@ -730,7 +730,7 @@ async fn handle_key_press(
|
|||||||
editor.move_up();
|
editor.move_up();
|
||||||
let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
|
let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
|
||||||
let field_name = field_names.get(editor.current_field()).unwrap_or(&"Field");
|
let field_name = field_names.get(editor.current_field()).unwrap_or(&"Field");
|
||||||
editor.set_debug_message(format!("↑ moved to {} field", field_name));
|
editor.set_debug_message(format!("↑ moved to {field_name} field"));
|
||||||
editor.clear_command_buffer();
|
editor.clear_command_buffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -872,8 +872,7 @@ async fn handle_key_press(
|
|||||||
let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
|
let field_names = ["Fruit", "Job", "Language", "Country", "Color"];
|
||||||
let current_field = field_names.get(editor.current_field()).unwrap_or(&"Field");
|
let current_field = field_names.get(editor.current_field()).unwrap_or(&"Field");
|
||||||
editor.set_debug_message(format!(
|
editor.set_debug_message(format!(
|
||||||
"{} field - Try: i=insert, Tab=suggestions, j/k=move. Key: {:?}",
|
"{current_field} field - Try: i=insert, Tab=suggestions, j/k=move. Key: {key:?}"
|
||||||
current_field, key
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -899,7 +898,7 @@ async fn run_app<B: Backend>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
editor.set_debug_message(format!("Error: {}", e));
|
editor.set_debug_message(format!("Error: {e}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -922,7 +921,7 @@ fn ui(f: &mut Frame, editor: &AutoCursorFormEditor<ApplicationData>) {
|
|||||||
f,
|
f,
|
||||||
chunks[0],
|
chunks[0],
|
||||||
input_rect,
|
input_rect,
|
||||||
&canvas::canvas::theme::DefaultCanvasTheme::default(),
|
&canvas::canvas::theme::DefaultCanvasTheme,
|
||||||
&editor.editor,
|
&editor.editor,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -1071,7 +1070,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
terminal.show_cursor()?;
|
terminal.show_cursor()?;
|
||||||
|
|
||||||
if let Err(err) = res {
|
if let Err(err) = res {
|
||||||
println!("{:?}", err);
|
println!("{err:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("🚀 Ready to integrate this architecture into your production app!");
|
println!("🚀 Ready to integrate this architecture into your production app!");
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ use ratatui::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use canvas::{
|
use canvas::{
|
||||||
canvas::{modes::AppMode, CursorManager},
|
canvas::CursorManager,
|
||||||
textarea::{TextArea, TextAreaState},
|
textarea::{TextArea, TextAreaState},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -291,7 +291,7 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut editor: AutoCursorTextAre
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
editor.set_debug_message(format!("Error: {}", e));
|
editor.set_debug_message(format!("Error: {e}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -389,7 +389,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
terminal.show_cursor()?;
|
terminal.show_cursor()?;
|
||||||
|
|
||||||
if let Err(err) = res {
|
if let Err(err) = res {
|
||||||
println!("{:?}", err);
|
println!("{err:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("🎯 Cursor automatically reset to default!");
|
println!("🎯 Cursor automatically reset to default!");
|
||||||
|
|||||||
413
canvas/examples/textarea_syntax.rs
Normal file
413
canvas/examples/textarea_syntax.rs
Normal file
@@ -0,0 +1,413 @@
|
|||||||
|
// examples/textarea_syntax.rs
|
||||||
|
//! Demonstrates syntax highlighting with the textarea widget
|
||||||
|
//!
|
||||||
|
//! This example REQUIRES the `syntect` feature to compile.
|
||||||
|
//!
|
||||||
|
//! Run with:
|
||||||
|
//! cargo run --example textarea_syntax --features "gui,cursor-style,textarea,syntect,textmode-normal"
|
||||||
|
|
||||||
|
#[cfg(not(feature = "syntect"))]
|
||||||
|
compile_error!(
|
||||||
|
"This example requires the 'syntect' feature. \
|
||||||
|
Run with: cargo run --example textarea_syntax --features \"gui,cursor-style,textarea,syntect,textmode-normal\""
|
||||||
|
);
|
||||||
|
|
||||||
|
use std::io;
|
||||||
|
use crossterm::{
|
||||||
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEvent, KeyModifiers},
|
||||||
|
execute,
|
||||||
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
||||||
|
};
|
||||||
|
use ratatui::{
|
||||||
|
backend::{Backend, CrosstermBackend},
|
||||||
|
layout::{Constraint, Direction, Layout},
|
||||||
|
style::{Color, Style},
|
||||||
|
text::{Line, Span},
|
||||||
|
widgets::{Block, Borders, Paragraph},
|
||||||
|
Frame, Terminal,
|
||||||
|
};
|
||||||
|
|
||||||
|
use canvas::{
|
||||||
|
canvas::CursorManager,
|
||||||
|
textarea::highlight::{TextAreaSyntax, TextAreaSyntaxState},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Syntax highlighting TextArea demo
|
||||||
|
struct SyntaxTextAreaDemo {
|
||||||
|
textarea: TextAreaSyntaxState,
|
||||||
|
has_unsaved_changes: bool,
|
||||||
|
debug_message: String,
|
||||||
|
current_language: String,
|
||||||
|
current_theme: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SyntaxTextAreaDemo {
|
||||||
|
fn new() -> Self {
|
||||||
|
let initial_text = r#"// 🎯 Multi-language Syntax Highlighting Demo
|
||||||
|
// ==========================
|
||||||
|
// Rust
|
||||||
|
// ==========================
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, Rust 🦀");
|
||||||
|
let nums = vec![1, 2, 3, 4, 5];
|
||||||
|
for n in nums {
|
||||||
|
println!("n = {}", n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==========================
|
||||||
|
// Python
|
||||||
|
// ==========================
|
||||||
|
# 🐍 Python example
|
||||||
|
def fib(n):
|
||||||
|
if n <= 1:
|
||||||
|
return n
|
||||||
|
return fib(n-1) + fib(n-2)
|
||||||
|
|
||||||
|
print([fib(i) for i in range(6)])
|
||||||
|
|
||||||
|
# ==========================
|
||||||
|
// JavaScript
|
||||||
|
// ==========================
|
||||||
|
// 🟨 JavaScript example
|
||||||
|
function greet(name) {
|
||||||
|
console.log(`Hello, ${name}!`);
|
||||||
|
}
|
||||||
|
greet("World");
|
||||||
|
|
||||||
|
// ==========================
|
||||||
|
// Scheme
|
||||||
|
// ==========================
|
||||||
|
;; 🎭 Scheme example
|
||||||
|
(define (square x) (* x x))
|
||||||
|
(display (square 5))
|
||||||
|
(newline)
|
||||||
|
"#;
|
||||||
|
|
||||||
|
let mut textarea = TextAreaSyntaxState::from_text(initial_text);
|
||||||
|
textarea.set_placeholder("Start typing code...");
|
||||||
|
|
||||||
|
// Pick a colorful default theme
|
||||||
|
let default_theme = "base16-ocean.dark";
|
||||||
|
let _ = textarea.set_syntax_theme(default_theme);
|
||||||
|
// Default to Rust syntax
|
||||||
|
let _ = textarea.set_syntax_by_extension("rs");
|
||||||
|
|
||||||
|
Self {
|
||||||
|
textarea,
|
||||||
|
has_unsaved_changes: false,
|
||||||
|
debug_message: format!("🎯 Syntax highlighting enabled - Rust ({})", default_theme),
|
||||||
|
current_language: "Rust".to_string(),
|
||||||
|
current_theme: default_theme.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_textarea_input(&mut self, key: KeyEvent) {
|
||||||
|
self.textarea.input(key);
|
||||||
|
self.has_unsaved_changes = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn switch_to_rust(&mut self) {
|
||||||
|
let _ = self.textarea.set_syntax_by_extension("rs");
|
||||||
|
self.current_language = "Rust".to_string();
|
||||||
|
self.debug_message = format!("🦀 Switched to Rust syntax ({})", self.current_theme);
|
||||||
|
|
||||||
|
let rust_code = r#"// Rust example
|
||||||
|
fn fibonacci(n: u32) -> u32 {
|
||||||
|
match n {
|
||||||
|
0 => 0,
|
||||||
|
1 => 1,
|
||||||
|
_ => fibonacci(n - 1) + fibonacci(n - 2),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
for i in 0..10 {
|
||||||
|
println!("fib({}) = {}", i, fibonacci(i));
|
||||||
|
}
|
||||||
|
}"#;
|
||||||
|
self.textarea.set_text(rust_code);
|
||||||
|
self.has_unsaved_changes = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn switch_to_python(&mut self) {
|
||||||
|
let _ = self.textarea.set_syntax_by_extension("py");
|
||||||
|
self.current_language = "Python".to_string();
|
||||||
|
self.debug_message = format!("🐍 Switched to Python syntax ({})", self.current_theme);
|
||||||
|
|
||||||
|
let python_code = r#"# Python example
|
||||||
|
def fibonacci(n):
|
||||||
|
if n <= 1:
|
||||||
|
return n
|
||||||
|
return fibonacci(n - 1) + fibonacci(n - 2)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
for i in range(10):
|
||||||
|
print(f"fib({i}) = {fibonacci(i)}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()"#;
|
||||||
|
self.textarea.set_text(python_code);
|
||||||
|
self.has_unsaved_changes = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn switch_to_javascript(&mut self) {
|
||||||
|
let _ = self.textarea.set_syntax_by_extension("js");
|
||||||
|
self.current_language = "JavaScript".to_string();
|
||||||
|
self.debug_message = format!("🟨 Switched to JavaScript syntax ({})", self.current_theme);
|
||||||
|
|
||||||
|
let js_code = r#"// JavaScript example
|
||||||
|
function fibonacci(n) {
|
||||||
|
if (n <= 1) return n;
|
||||||
|
return fibonacci(n - 1) + fibonacci(n - 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
function main() {
|
||||||
|
for (let i = 0; i < 10; i++) {
|
||||||
|
console.log(`fib(${i}) = ${fibonacci(i)}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
main();"#;
|
||||||
|
self.textarea.set_text(js_code);
|
||||||
|
self.has_unsaved_changes = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn switch_to_scheme(&mut self) {
|
||||||
|
let _ = self.textarea.set_syntax_by_name("Scheme");
|
||||||
|
self.current_language = "Scheme".to_string();
|
||||||
|
self.debug_message = format!("🎭 Switched to Scheme syntax ({})", self.current_theme);
|
||||||
|
|
||||||
|
let scheme_code = r#";; Scheme example
|
||||||
|
(define (fibonacci n)
|
||||||
|
(cond ((= n 0) 0)
|
||||||
|
((= n 1) 1)
|
||||||
|
(else (+ (fibonacci (- n 1))
|
||||||
|
(fibonacci (- n 2))))))
|
||||||
|
|
||||||
|
(define (main)
|
||||||
|
(do ((i 0 (+ i 1)))
|
||||||
|
((= i 10))
|
||||||
|
(display (format "fib(~a) = ~a~n" i (fibonacci i)))))
|
||||||
|
|
||||||
|
(main)"#;
|
||||||
|
self.textarea.set_text(scheme_code);
|
||||||
|
self.has_unsaved_changes = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cycle_theme(&mut self) {
|
||||||
|
let themes = [
|
||||||
|
"InspiredGitHub",
|
||||||
|
"base16-ocean.dark",
|
||||||
|
"base16-eighties.dark",
|
||||||
|
"Solarized (dark)",
|
||||||
|
"Monokai Extended",
|
||||||
|
];
|
||||||
|
let current_pos = themes.iter().position(|t| *t == self.current_theme);
|
||||||
|
let next_pos = match current_pos {
|
||||||
|
Some(p) => (p + 1) % themes.len(),
|
||||||
|
None => 0,
|
||||||
|
};
|
||||||
|
let next_theme = themes[next_pos];
|
||||||
|
let _ = self.textarea.set_syntax_theme(next_theme);
|
||||||
|
self.current_theme = next_theme.to_string();
|
||||||
|
self.debug_message = format!("🎨 Theme switched to {}", next_theme);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_cursor_info(&self) -> String {
|
||||||
|
format!(
|
||||||
|
"Line {}, Col {} | Lang: {} | Theme: {}",
|
||||||
|
self.textarea.current_field() + 1,
|
||||||
|
self.textarea.cursor_position() + 1,
|
||||||
|
self.current_language,
|
||||||
|
self.current_theme
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn debug_message(&self) -> &str {
|
||||||
|
&self.debug_message
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_debug_message(&mut self, msg: String) {
|
||||||
|
self.debug_message = msg;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn has_unsaved_changes(&self) -> bool {
|
||||||
|
self.has_unsaved_changes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_key_press(
|
||||||
|
key_event: KeyEvent,
|
||||||
|
editor: &mut SyntaxTextAreaDemo,
|
||||||
|
) -> anyhow::Result<bool> {
|
||||||
|
let KeyEvent {
|
||||||
|
code: key,
|
||||||
|
modifiers,
|
||||||
|
..
|
||||||
|
} = key_event;
|
||||||
|
|
||||||
|
// Quit
|
||||||
|
if (key == KeyCode::Char('q') && modifiers.contains(KeyModifiers::CONTROL))
|
||||||
|
|| (key == KeyCode::Char('c') && modifiers.contains(KeyModifiers::CONTROL))
|
||||||
|
|| key == KeyCode::F(10)
|
||||||
|
{
|
||||||
|
return Ok(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
match (key, modifiers) {
|
||||||
|
// Language switching
|
||||||
|
(KeyCode::F(5), _) => editor.switch_to_rust(),
|
||||||
|
(KeyCode::F(6), _) => editor.switch_to_python(),
|
||||||
|
(KeyCode::F(7), _) => editor.switch_to_javascript(),
|
||||||
|
(KeyCode::F(8), _) => editor.switch_to_scheme(),
|
||||||
|
|
||||||
|
// Theme cycling
|
||||||
|
(KeyCode::F(9), _) => editor.cycle_theme(),
|
||||||
|
|
||||||
|
// Overflow modes
|
||||||
|
(KeyCode::F(1), _) => {
|
||||||
|
editor.textarea.use_overflow_indicator('$');
|
||||||
|
editor.set_debug_message(format!("Overflow: indicator '$' (wrap OFF) | Theme: {}", editor.current_theme));
|
||||||
|
}
|
||||||
|
(KeyCode::F(2), _) => {
|
||||||
|
editor.textarea.use_wrap();
|
||||||
|
editor.set_debug_message(format!("Overflow: wrap ON | Theme: {}", editor.current_theme));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wrap indent
|
||||||
|
(KeyCode::F(3), _) => {
|
||||||
|
editor.textarea.set_wrap_indent_cols(4);
|
||||||
|
editor.set_debug_message(format!("Wrap indent: 4 columns | Theme: {}", editor.current_theme));
|
||||||
|
}
|
||||||
|
(KeyCode::F(4), _) => {
|
||||||
|
editor.textarea.set_wrap_indent_cols(0);
|
||||||
|
editor.set_debug_message(format!("Wrap indent: 0 columns | Theme: {}", editor.current_theme));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info
|
||||||
|
(KeyCode::Char('?'), _) => {
|
||||||
|
editor.set_debug_message(format!(
|
||||||
|
"{} | Syntax highlighting enabled",
|
||||||
|
editor.get_cursor_info()
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Default: pass to textarea
|
||||||
|
_ => editor.handle_textarea_input(key_event),
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut editor: SyntaxTextAreaDemo) -> io::Result<()> {
|
||||||
|
loop {
|
||||||
|
terminal.draw(|f| ui(f, &mut editor))?;
|
||||||
|
|
||||||
|
if let Event::Key(key) = event::read()? {
|
||||||
|
match handle_key_press(key, &mut editor) {
|
||||||
|
Ok(should_continue) => {
|
||||||
|
if !should_continue {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
editor.set_debug_message(format!("Error: {e}"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ui(f: &mut Frame, editor: &mut SyntaxTextAreaDemo) {
|
||||||
|
let chunks = Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([Constraint::Min(8), Constraint::Length(8)])
|
||||||
|
.split(f.area());
|
||||||
|
|
||||||
|
render_textarea(f, chunks[0], editor);
|
||||||
|
render_status_and_help(f, chunks[1], editor);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_textarea(f: &mut Frame, area: ratatui::layout::Rect, editor: &mut SyntaxTextAreaDemo) {
|
||||||
|
let block = Block::default()
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.title("🎨 Syntax Highlighted Code Editor");
|
||||||
|
|
||||||
|
let textarea_widget = TextAreaSyntax::default().block(block.clone());
|
||||||
|
f.render_stateful_widget(textarea_widget, area, &mut editor.textarea);
|
||||||
|
|
||||||
|
// Reuse cursor calculation from the wrapped textarea
|
||||||
|
let (cx, cy) = editor.textarea.cursor(area, Some(&block));
|
||||||
|
f.set_cursor_position((cx, cy));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_status_and_help(f: &mut Frame, area: ratatui::layout::Rect, editor: &SyntaxTextAreaDemo) {
|
||||||
|
let chunks = Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([Constraint::Length(3), Constraint::Length(5)])
|
||||||
|
.split(area);
|
||||||
|
|
||||||
|
let status_text = if editor.has_unsaved_changes() {
|
||||||
|
format!(
|
||||||
|
"-- SYNTAX MODE (highlighting enabled) -- [Modified] {} | {}",
|
||||||
|
editor.debug_message(),
|
||||||
|
editor.get_cursor_info()
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
format!(
|
||||||
|
"-- SYNTAX MODE (highlighting enabled) -- {} | {}",
|
||||||
|
editor.debug_message(),
|
||||||
|
editor.get_cursor_info()
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
let status = Paragraph::new(Line::from(Span::raw(status_text)))
|
||||||
|
.block(Block::default().borders(Borders::ALL).title("🎨 Syntax Status"));
|
||||||
|
|
||||||
|
f.render_widget(status, chunks[0]);
|
||||||
|
|
||||||
|
let help_text = "🎨 SYNTAX HIGHLIGHTING DEMO\n\
|
||||||
|
F5=Rust, F6=Python, F7=JavaScript, F8=Scheme\n\
|
||||||
|
F1/F2=overflow modes, F3/F4=wrap indent\n\
|
||||||
|
F9=cycle themes, ?=info, Ctrl+Q=quit";
|
||||||
|
|
||||||
|
let help = Paragraph::new(help_text)
|
||||||
|
.block(Block::default().borders(Borders::ALL).title("🚀 Help"))
|
||||||
|
.style(Style::default().fg(Color::Cyan));
|
||||||
|
|
||||||
|
f.render_widget(help, chunks[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
println!("🎨 Canvas Textarea Syntax Highlighting Demo");
|
||||||
|
println!("✅ cursor-style feature: ENABLED");
|
||||||
|
println!("✅ textarea feature: ENABLED");
|
||||||
|
println!("✅ syntect feature: ENABLED");
|
||||||
|
println!("🎨 Syntax highlighting active");
|
||||||
|
println!();
|
||||||
|
|
||||||
|
enable_raw_mode()?;
|
||||||
|
let mut stdout = io::stdout();
|
||||||
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
||||||
|
let backend = CrosstermBackend::new(stdout);
|
||||||
|
let mut terminal = Terminal::new(backend)?;
|
||||||
|
|
||||||
|
let editor = SyntaxTextAreaDemo::new();
|
||||||
|
|
||||||
|
let res = run_app(&mut terminal, editor);
|
||||||
|
|
||||||
|
CursorManager::reset()?;
|
||||||
|
|
||||||
|
disable_raw_mode()?;
|
||||||
|
execute!(terminal.backend_mut(), LeaveAlternateScreen, DisableMouseCapture)?;
|
||||||
|
terminal.show_cursor()?;
|
||||||
|
|
||||||
|
if let Err(err) = res {
|
||||||
|
println!("{err:?}");
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("🎨 Syntax highlighting demo complete!");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -339,13 +339,13 @@ fn handle_key_press(
|
|||||||
// Vim o/O commands
|
// Vim o/O commands
|
||||||
(AppMode::ReadOnly, KeyCode::Char('o'), _) => {
|
(AppMode::ReadOnly, KeyCode::Char('o'), _) => {
|
||||||
if let Err(e) = editor.open_line_below() {
|
if let Err(e) = editor.open_line_below() {
|
||||||
editor.set_debug_message(format!("Error opening line below: {}", e));
|
editor.set_debug_message(format!("Error opening line below: {e}"));
|
||||||
}
|
}
|
||||||
editor.clear_command_buffer();
|
editor.clear_command_buffer();
|
||||||
}
|
}
|
||||||
(AppMode::ReadOnly, KeyCode::Char('O'), _) => {
|
(AppMode::ReadOnly, KeyCode::Char('O'), _) => {
|
||||||
if let Err(e) = editor.open_line_above() {
|
if let Err(e) = editor.open_line_above() {
|
||||||
editor.set_debug_message(format!("Error opening line above: {}", e));
|
editor.set_debug_message(format!("Error opening line above: {e}"));
|
||||||
}
|
}
|
||||||
editor.clear_command_buffer();
|
editor.clear_command_buffer();
|
||||||
}
|
}
|
||||||
@@ -482,8 +482,7 @@ fn handle_key_press(
|
|||||||
editor.set_debug_message("Invalid command sequence".to_string());
|
editor.set_debug_message("Invalid command sequence".to_string());
|
||||||
} else {
|
} else {
|
||||||
editor.set_debug_message(format!(
|
editor.set_debug_message(format!(
|
||||||
"Unhandled: {:?} + {:?} in {:?} mode",
|
"Unhandled: {key:?} + {modifiers:?} in {mode:?} mode"
|
||||||
key, modifiers, mode
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -507,7 +506,7 @@ fn run_app<B: Backend>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
editor.set_debug_message(format!("Error: {}", e));
|
editor.set_debug_message(format!("Error: {e}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -645,7 +644,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
terminal.show_cursor()?;
|
terminal.show_cursor()?;
|
||||||
|
|
||||||
if let Err(err) = res {
|
if let Err(err) = res {
|
||||||
println!("{:?}", err);
|
println!("{err:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("🎯 Cursor automatically reset to default!");
|
println!("🎯 Cursor automatically reset to default!");
|
||||||
|
|||||||
@@ -141,10 +141,10 @@ impl<D: DataProvider> ValidationFormEditor<D> {
|
|||||||
self.debug_message = "✅ Current field is valid!".to_string();
|
self.debug_message = "✅ Current field is valid!".to_string();
|
||||||
}
|
}
|
||||||
ValidationResult::Warning { message } => {
|
ValidationResult::Warning { message } => {
|
||||||
self.debug_message = format!("⚠️ Warning: {}", message);
|
self.debug_message = format!("⚠️ Warning: {message}");
|
||||||
}
|
}
|
||||||
ValidationResult::Error { message } => {
|
ValidationResult::Error { message } => {
|
||||||
self.debug_message = format!("❌ Error: {}", message);
|
self.debug_message = format!("❌ Error: {message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -189,7 +189,7 @@ impl<D: DataProvider> ValidationFormEditor<D> {
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.field_switch_blocked = true;
|
self.field_switch_blocked = true;
|
||||||
self.block_reason = Some(e.to_string());
|
self.block_reason = Some(e.to_string());
|
||||||
self.debug_message = format!("🚫 Field switch blocked: {}", e);
|
self.debug_message = format!("🚫 Field switch blocked: {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -204,7 +204,7 @@ impl<D: DataProvider> ValidationFormEditor<D> {
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.field_switch_blocked = true;
|
self.field_switch_blocked = true;
|
||||||
self.block_reason = Some(e.to_string());
|
self.block_reason = Some(e.to_string());
|
||||||
self.debug_message = format!("🚫 Field switch blocked: {}", e);
|
self.debug_message = format!("🚫 Field switch blocked: {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -289,19 +289,19 @@ impl<D: DataProvider> ValidationFormEditor<D> {
|
|||||||
if let Some(status) = limits.status_text(
|
if let Some(status) = limits.status_text(
|
||||||
self.editor.data_provider().field_value(field_index)
|
self.editor.data_provider().field_value(field_index)
|
||||||
) {
|
) {
|
||||||
self.debug_message = format!("✏️ {}", status);
|
self.debug_message = format!("✏️ {status}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ValidationResult::Warning { message } => {
|
ValidationResult::Warning { message } => {
|
||||||
self.debug_message = format!("⚠️ {}", message);
|
self.debug_message = format!("⚠️ {message}");
|
||||||
}
|
}
|
||||||
ValidationResult::Error { message } => {
|
ValidationResult::Error { message } => {
|
||||||
self.debug_message = format!("❌ {}", message);
|
self.debug_message = format!("❌ {message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_current_field_limits(&self) -> Option<&CharacterLimits> {
|
fn get_current_field_limits(&self) -> Option<&CharacterLimits> {
|
||||||
@@ -317,7 +317,7 @@ impl<D: DataProvider> ValidationFormEditor<D> {
|
|||||||
self.has_unsaved_changes = true;
|
self.has_unsaved_changes = true;
|
||||||
self.debug_message = "⌫ Deleted character".to_string();
|
self.debug_message = "⌫ Deleted character".to_string();
|
||||||
}
|
}
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delete_forward(&mut self) -> anyhow::Result<()> {
|
fn delete_forward(&mut self) -> anyhow::Result<()> {
|
||||||
@@ -326,7 +326,7 @@ impl<D: DataProvider> ValidationFormEditor<D> {
|
|||||||
self.has_unsaved_changes = true;
|
self.has_unsaved_changes = true;
|
||||||
self.debug_message = "⌦ Deleted character".to_string();
|
self.debug_message = "⌦ Deleted character".to_string();
|
||||||
}
|
}
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
// === DELEGATE TO ORIGINAL EDITOR ===
|
// === DELEGATE TO ORIGINAL EDITOR ===
|
||||||
@@ -370,7 +370,7 @@ impl<D: DataProvider> ValidationFormEditor<D> {
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.field_switch_blocked = true;
|
self.field_switch_blocked = true;
|
||||||
self.block_reason = Some(e.to_string());
|
self.block_reason = Some(e.to_string());
|
||||||
self.debug_message = format!("🚫 Cannot move to next field: {}", e);
|
self.debug_message = format!("🚫 Cannot move to next field: {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -385,7 +385,7 @@ impl<D: DataProvider> ValidationFormEditor<D> {
|
|||||||
Err(e) => {
|
Err(e) => {
|
||||||
self.field_switch_blocked = true;
|
self.field_switch_blocked = true;
|
||||||
self.block_reason = Some(e.to_string());
|
self.block_reason = Some(e.to_string());
|
||||||
self.debug_message = format!("🚫 Cannot move to previous field: {}", e);
|
self.debug_message = format!("🚫 Cannot move to previous field: {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -644,7 +644,7 @@ fn run_app<B: Backend>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
editor.set_debug_message(format!("Error: {}", e));
|
editor.set_debug_message(format!("Error: {e}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -823,7 +823,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
terminal.show_cursor()?;
|
terminal.show_cursor()?;
|
||||||
|
|
||||||
if let Err(err) = res {
|
if let Err(err) = res {
|
||||||
println!("{:?}", err);
|
println!("{err:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("🔍 Validation demo completed!");
|
println!("🔍 Validation demo completed!");
|
||||||
|
|||||||
@@ -93,14 +93,14 @@ impl<D: DataProvider> AdvancedPatternFormEditor<D> {
|
|||||||
fn move_up(&mut self) {
|
fn move_up(&mut self) {
|
||||||
match self.editor.move_up() {
|
match self.editor.move_up() {
|
||||||
Ok(()) => { self.update_field_validation_status(); self.field_switch_blocked = false; self.block_reason = None; }
|
Ok(()) => { self.update_field_validation_status(); self.field_switch_blocked = false; self.block_reason = None; }
|
||||||
Err(e) => { self.field_switch_blocked = true; self.block_reason = Some(e.to_string()); self.debug_message = format!("🚫 Field switch blocked: {}", e); }
|
Err(e) => { self.field_switch_blocked = true; self.block_reason = Some(e.to_string()); self.debug_message = format!("🚫 Field switch blocked: {e}"); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_down(&mut self) {
|
fn move_down(&mut self) {
|
||||||
match self.editor.move_down() {
|
match self.editor.move_down() {
|
||||||
Ok(()) => { self.update_field_validation_status(); self.field_switch_blocked = false; self.block_reason = None; }
|
Ok(()) => { self.update_field_validation_status(); self.field_switch_blocked = false; self.block_reason = None; }
|
||||||
Err(e) => { self.field_switch_blocked = true; self.block_reason = Some(e.to_string()); self.debug_message = format!("🚫 Field switch blocked: {}", e); }
|
Err(e) => { self.field_switch_blocked = true; self.block_reason = Some(e.to_string()); self.debug_message = format!("🚫 Field switch blocked: {e}"); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,23 +132,23 @@ impl<D: DataProvider> AdvancedPatternFormEditor<D> {
|
|||||||
let validation_result = self.editor.validate_current_field();
|
let validation_result = self.editor.validate_current_field();
|
||||||
match validation_result {
|
match validation_result {
|
||||||
ValidationResult::Valid => { self.debug_message = "✅ Character accepted".to_string(); }
|
ValidationResult::Valid => { self.debug_message = "✅ Character accepted".to_string(); }
|
||||||
ValidationResult::Warning { message } => { self.debug_message = format!("⚠️ Warning: {}", message); }
|
ValidationResult::Warning { message } => { self.debug_message = format!("⚠️ Warning: {message}"); }
|
||||||
ValidationResult::Error { message } => { self.debug_message = format!("❌ Pattern violation: {}", message); }
|
ValidationResult::Error { message } => { self.debug_message = format!("❌ Pattern violation: {message}"); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delete_backward(&mut self) -> anyhow::Result<()> {
|
fn delete_backward(&mut self) -> anyhow::Result<()> {
|
||||||
let result = self.editor.delete_backward();
|
let result = self.editor.delete_backward();
|
||||||
if result.is_ok() { self.debug_message = "⌫ Character deleted".to_string(); }
|
if result.is_ok() { self.debug_message = "⌫ Character deleted".to_string(); }
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delete_forward(&mut self) -> anyhow::Result<()> {
|
fn delete_forward(&mut self) -> anyhow::Result<()> {
|
||||||
let result = self.editor.delete_forward();
|
let result = self.editor.delete_forward();
|
||||||
if result.is_ok() { self.debug_message = "⌦ Character deleted".to_string(); }
|
if result.is_ok() { self.debug_message = "⌦ Character deleted".to_string(); }
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delegate methods
|
// Delegate methods
|
||||||
@@ -166,14 +166,14 @@ impl<D: DataProvider> AdvancedPatternFormEditor<D> {
|
|||||||
fn next_field(&mut self) {
|
fn next_field(&mut self) {
|
||||||
match self.editor.next_field() {
|
match self.editor.next_field() {
|
||||||
Ok(()) => { self.update_field_validation_status(); self.field_switch_blocked = false; self.block_reason = None; }
|
Ok(()) => { self.update_field_validation_status(); self.field_switch_blocked = false; self.block_reason = None; }
|
||||||
Err(e) => { self.field_switch_blocked = true; self.block_reason = Some(e.to_string()); self.debug_message = format!("🚫 Cannot move to next field: {}", e); }
|
Err(e) => { self.field_switch_blocked = true; self.block_reason = Some(e.to_string()); self.debug_message = format!("🚫 Cannot move to next field: {e}"); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prev_field(&mut self) {
|
fn prev_field(&mut self) {
|
||||||
match self.editor.prev_field() {
|
match self.editor.prev_field() {
|
||||||
Ok(()) => { self.update_field_validation_status(); self.field_switch_blocked = false; self.block_reason = None; }
|
Ok(()) => { self.update_field_validation_status(); self.field_switch_blocked = false; self.block_reason = None; }
|
||||||
Err(e) => { self.field_switch_blocked = true; self.block_reason = Some(e.to_string()); self.debug_message = format!("🚫 Cannot move to previous field: {}", e); }
|
Err(e) => { self.field_switch_blocked = true; self.block_reason = Some(e.to_string()); self.debug_message = format!("🚫 Cannot move to previous field: {e}"); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -492,7 +492,7 @@ fn run_app<B: Backend>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
editor.set_debug_message(format!("Error: {}", e));
|
editor.set_debug_message(format!("Error: {e}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -654,7 +654,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
terminal.show_cursor()?;
|
terminal.show_cursor()?;
|
||||||
|
|
||||||
if let Err(err) = res {
|
if let Err(err) = res {
|
||||||
println!("{:?}", err);
|
println!("{err:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("🚀 Advanced pattern validation demo completed!");
|
println!("🚀 Advanced pattern validation demo completed!");
|
||||||
|
|||||||
@@ -54,7 +54,6 @@ use canvas::{
|
|||||||
},
|
},
|
||||||
DataProvider, FormEditor,
|
DataProvider, FormEditor,
|
||||||
ValidationConfig, ValidationConfigBuilder, DisplayMask,
|
ValidationConfig, ValidationConfigBuilder, DisplayMask,
|
||||||
validation::mask::MaskDisplayMode,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Enhanced FormEditor wrapper for mask demonstration
|
// Enhanced FormEditor wrapper for mask demonstration
|
||||||
@@ -144,14 +143,14 @@ impl<D: DataProvider> MaskDemoFormEditor<D> {
|
|||||||
fn move_up(&mut self) {
|
fn move_up(&mut self) {
|
||||||
match self.editor.move_up() {
|
match self.editor.move_up() {
|
||||||
Ok(()) => { self.update_field_info(); }
|
Ok(()) => { self.update_field_info(); }
|
||||||
Err(e) => { self.debug_message = format!("🚫 Field switch blocked: {}", e); }
|
Err(e) => { self.debug_message = format!("🚫 Field switch blocked: {e}"); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_down(&mut self) {
|
fn move_down(&mut self) {
|
||||||
match self.editor.move_down() {
|
match self.editor.move_down() {
|
||||||
Ok(()) => { self.update_field_info(); }
|
Ok(()) => { self.update_field_info(); }
|
||||||
Err(e) => { self.debug_message = format!("🚫 Field switch blocked: {}", e); }
|
Err(e) => { self.debug_message = format!("🚫 Field switch blocked: {e}"); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,16 +169,16 @@ impl<D: DataProvider> MaskDemoFormEditor<D> {
|
|||||||
let raw_pos = self.editor.cursor_position();
|
let raw_pos = self.editor.cursor_position();
|
||||||
let display_pos = self.editor.display_cursor_position();
|
let display_pos = self.editor.display_cursor_position();
|
||||||
if raw_pos != display_pos {
|
if raw_pos != display_pos {
|
||||||
self.debug_message = format!("📍 Cursor: Raw pos {} → Display pos {} (mask active)", raw_pos, display_pos);
|
self.debug_message = format!("📍 Cursor: Raw pos {raw_pos} → Display pos {display_pos} (mask active)");
|
||||||
} else {
|
} else {
|
||||||
self.debug_message = format!("📍 Cursor at position {} (no mask offset)", raw_pos);
|
self.debug_message = format!("📍 Cursor at position {raw_pos} (no mask offset)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_field_info(&mut self) {
|
fn update_field_info(&mut self) {
|
||||||
let field_name = self.editor.data_provider().field_name(self.editor.current_field());
|
let field_name = self.editor.data_provider().field_name(self.editor.current_field());
|
||||||
self.debug_message = format!("📝 Switched to: {}", field_name);
|
self.debug_message = format!("📝 Switched to: {field_name}");
|
||||||
}
|
}
|
||||||
|
|
||||||
// === MODE TRANSITIONS ===
|
// === MODE TRANSITIONS ===
|
||||||
@@ -206,12 +205,12 @@ impl<D: DataProvider> MaskDemoFormEditor<D> {
|
|||||||
if result.is_ok() {
|
if result.is_ok() {
|
||||||
let (raw, display, _) = self.get_current_field_info();
|
let (raw, display, _) = self.get_current_field_info();
|
||||||
if raw != display {
|
if raw != display {
|
||||||
self.debug_message = format!("✏️ Added '{}': Raw='{}' Display='{}'", ch, raw, display);
|
self.debug_message = format!("✏️ Added '{ch}': Raw='{raw}' Display='{display}'");
|
||||||
} else {
|
} else {
|
||||||
self.debug_message = format!("✏️ Added '{}': '{}'", ch, raw);
|
self.debug_message = format!("✏️ Added '{ch}': '{raw}'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
// === DELETE OPERATIONS ===
|
// === DELETE OPERATIONS ===
|
||||||
@@ -221,7 +220,7 @@ impl<D: DataProvider> MaskDemoFormEditor<D> {
|
|||||||
self.debug_message = "⌫ Character deleted".to_string();
|
self.debug_message = "⌫ Character deleted".to_string();
|
||||||
self.update_cursor_info();
|
self.update_cursor_info();
|
||||||
}
|
}
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delete_forward(&mut self) -> anyhow::Result<()> {
|
fn delete_forward(&mut self) -> anyhow::Result<()> {
|
||||||
@@ -230,7 +229,7 @@ impl<D: DataProvider> MaskDemoFormEditor<D> {
|
|||||||
self.debug_message = "⌦ Character deleted".to_string();
|
self.debug_message = "⌦ Character deleted".to_string();
|
||||||
self.update_cursor_info();
|
self.update_cursor_info();
|
||||||
}
|
}
|
||||||
Ok(result?)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
// === DELEGATE TO ORIGINAL EDITOR ===
|
// === DELEGATE TO ORIGINAL EDITOR ===
|
||||||
@@ -251,14 +250,14 @@ impl<D: DataProvider> MaskDemoFormEditor<D> {
|
|||||||
fn next_field(&mut self) {
|
fn next_field(&mut self) {
|
||||||
match self.editor.next_field() {
|
match self.editor.next_field() {
|
||||||
Ok(()) => { self.update_field_info(); }
|
Ok(()) => { self.update_field_info(); }
|
||||||
Err(e) => { self.debug_message = format!("🚫 Cannot move to next field: {}", e); }
|
Err(e) => { self.debug_message = format!("🚫 Cannot move to next field: {e}"); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prev_field(&mut self) {
|
fn prev_field(&mut self) {
|
||||||
match self.editor.prev_field() {
|
match self.editor.prev_field() {
|
||||||
Ok(()) => { self.update_field_info(); }
|
Ok(()) => { self.update_field_info(); }
|
||||||
Err(e) => { self.debug_message = format!("🚫 Cannot move to previous field: {}", e); }
|
Err(e) => { self.debug_message = format!("🚫 Cannot move to previous field: {e}"); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -287,7 +286,7 @@ impl<D: DataProvider> MaskDemoFormEditor<D> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
format!("🎭 {} MASKS", mask_count)
|
format!("🎭 {mask_count} MASKS")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -549,7 +548,7 @@ fn run_app<B: Backend>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
editor.set_debug_message(format!("Error: {}", e));
|
editor.set_debug_message(format!("Error: {e}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -726,7 +725,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
CursorManager::reset()?;
|
CursorManager::reset()?;
|
||||||
|
|
||||||
if let Err(err) = res {
|
if let Err(err) = res {
|
||||||
println!("{:?}", err);
|
println!("{err:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("🎭 Display mask demo completed!");
|
println!("🎭 Display mask demo completed!");
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ impl CustomFormatter for PhoneFormatter {
|
|||||||
let len = raw.chars().count();
|
let len = raw.chars().count();
|
||||||
match len {
|
match len {
|
||||||
0 => FormattingResult::success(""),
|
0 => FormattingResult::success(""),
|
||||||
1..=3 => FormattingResult::success(format!("({})", raw)),
|
1..=3 => FormattingResult::success(format!("({raw})")),
|
||||||
4..=6 => FormattingResult::success(format!("({}) {}", &raw[..3], &raw[3..])),
|
4..=6 => FormattingResult::success(format!("({}) {}", &raw[..3], &raw[3..])),
|
||||||
7..=10 => FormattingResult::success(format!("({}) {}-{}", &raw[..3], &raw[3..6], &raw[6..])),
|
7..=10 => FormattingResult::success(format!("({}) {}-{}", &raw[..3], &raw[3..6], &raw[6..])),
|
||||||
10 => {
|
10 => {
|
||||||
@@ -135,7 +135,7 @@ impl CustomFormatter for CreditCardFormatter {
|
|||||||
|
|
||||||
let len = raw.chars().count();
|
let len = raw.chars().count();
|
||||||
match len {
|
match len {
|
||||||
0..=15 => FormattingResult::warning(formatted, format!("Card incomplete ({}/16 digits)", len)),
|
0..=15 => FormattingResult::warning(formatted, format!("Card incomplete ({len}/16 digits)")),
|
||||||
16 => FormattingResult::success(formatted),
|
16 => FormattingResult::success(formatted),
|
||||||
_ => FormattingResult::warning(formatted, "Card too long (extra digits shown)"),
|
_ => FormattingResult::warning(formatted, "Card too long (extra digits shown)"),
|
||||||
}
|
}
|
||||||
@@ -177,16 +177,16 @@ impl CustomFormatter for DateFormatter {
|
|||||||
|
|
||||||
if m == 0 || m > 12 {
|
if m == 0 || m > 12 {
|
||||||
FormattingResult::warning(
|
FormattingResult::warning(
|
||||||
format!("{}/{}/{}", month, day, year),
|
format!("{month}/{day}/{year}"),
|
||||||
"Invalid month (01-12)"
|
"Invalid month (01-12)"
|
||||||
)
|
)
|
||||||
} else if d == 0 || d > 31 {
|
} else if d == 0 || d > 31 {
|
||||||
FormattingResult::warning(
|
FormattingResult::warning(
|
||||||
format!("{}/{}/{}", month, day, year),
|
format!("{month}/{day}/{year}"),
|
||||||
"Invalid day (01-31)"
|
"Invalid day (01-31)"
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
FormattingResult::success(format!("{}/{}/{}", month, day, year))
|
FormattingResult::success(format!("{month}/{day}/{year}"))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => FormattingResult::error("Date too long (MMDDYYYY format)"),
|
_ => FormattingResult::error("Date too long (MMDDYYYY format)"),
|
||||||
@@ -384,7 +384,7 @@ impl<D: DataProvider> EnhancedDemoEditor<D> {
|
|||||||
|
|
||||||
let warning = if self.validation_enabled && self.has_formatter() {
|
let warning = if self.validation_enabled && self.has_formatter() {
|
||||||
// Check if there are any formatting warnings
|
// Check if there are any formatting warnings
|
||||||
if raw.len() > 0 {
|
if !raw.is_empty() {
|
||||||
match self.editor.current_field() {
|
match self.editor.current_field() {
|
||||||
0 if raw.len() < 5 => Some(format!("PSC incomplete: {}/5", raw.len())),
|
0 if raw.len() < 5 => Some(format!("PSC incomplete: {}/5", raw.len())),
|
||||||
1 if raw.len() < 10 => Some(format!("Phone incomplete: {}/10", raw.len())),
|
1 if raw.len() < 10 => Some(format!("Phone incomplete: {}/10", raw.len())),
|
||||||
@@ -408,7 +408,7 @@ impl<D: DataProvider> EnhancedDemoEditor<D> {
|
|||||||
self.editor.enter_edit_mode();
|
self.editor.enter_edit_mode();
|
||||||
let field_type = self.current_field_type();
|
let field_type = self.current_field_type();
|
||||||
let rules = self.get_input_rules();
|
let rules = self.get_input_rules();
|
||||||
self.debug_message = format!("✏️ INSERT MODE - Cursor: Steady Bar | - {} - {}", field_type, rules);
|
self.debug_message = format!("✏️ INSERT MODE - Cursor: Steady Bar | - {field_type} - {rules}");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exit_edit_mode(&mut self) {
|
fn exit_edit_mode(&mut self) {
|
||||||
@@ -429,9 +429,9 @@ impl<D: DataProvider> EnhancedDemoEditor<D> {
|
|||||||
if result.is_ok() {
|
if result.is_ok() {
|
||||||
let (raw, display, _, _) = self.get_current_field_analysis();
|
let (raw, display, _, _) = self.get_current_field_analysis();
|
||||||
if raw != display && self.validation_enabled {
|
if raw != display && self.validation_enabled {
|
||||||
self.debug_message = format!("✏️ '{}' added - Real-time formatting active", ch);
|
self.debug_message = format!("✏️ '{ch}' added - Real-time formatting active");
|
||||||
} else {
|
} else {
|
||||||
self.debug_message = format!("✏️ '{}' added", ch);
|
self.debug_message = format!("✏️ '{ch}' added");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
@@ -459,7 +459,7 @@ impl<D: DataProvider> EnhancedDemoEditor<D> {
|
|||||||
display.chars().nth(display_pos).unwrap_or('∅')
|
display.chars().nth(display_pos).unwrap_or('∅')
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
self.debug_message = format!("📍 Cursor at position {} (no mapping needed)", raw_pos);
|
self.debug_message = format!("📍 Cursor at position {raw_pos} (no mapping needed)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -530,7 +530,7 @@ fn handle_key_press(
|
|||||||
// Field analysis
|
// Field analysis
|
||||||
(AppMode::ReadOnly, KeyCode::Char('?'), _) => {
|
(AppMode::ReadOnly, KeyCode::Char('?'), _) => {
|
||||||
let (raw, display, status, warning) = editor.get_current_field_analysis();
|
let (raw, display, status, warning) = editor.get_current_field_analysis();
|
||||||
let warning_text = warning.map(|w| format!(" ⚠️ {}", w)).unwrap_or_default();
|
let warning_text = warning.map(|w| format!(" ⚠️ {w}")).unwrap_or_default();
|
||||||
editor.debug_message = format!(
|
editor.debug_message = format!(
|
||||||
"🔍 Field {}: {} | Raw: '{}' | Display: '{}'{}",
|
"🔍 Field {}: {} | Raw: '{}' | Display: '{}'{}",
|
||||||
editor.current_field() + 1, status, raw, display, warning_text
|
editor.current_field() + 1, status, raw, display, warning_text
|
||||||
@@ -558,7 +558,7 @@ fn run_app<B: Backend>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
editor.debug_message = format!("❌ Error: {}", e);
|
editor.debug_message = format!("❌ Error: {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -627,11 +627,11 @@ fn render_enhanced_status(
|
|||||||
];
|
];
|
||||||
|
|
||||||
if editor.show_raw_data || editor.mode() == AppMode::Edit {
|
if editor.show_raw_data || editor.mode() == AppMode::Edit {
|
||||||
analysis_lines.push(format!("💾 Raw Data: '{}'", raw));
|
analysis_lines.push(format!("💾 Raw Data: '{raw}'"));
|
||||||
analysis_lines.push(format!("✨ Display: '{}'", display));
|
analysis_lines.push(format!("✨ Display: '{display}'"));
|
||||||
} else {
|
} else {
|
||||||
analysis_lines.push(format!("✨ User Sees: '{}'", display));
|
analysis_lines.push(format!("✨ User Sees: '{display}'"));
|
||||||
analysis_lines.push(format!("💾 Stored As: '{}'", raw));
|
analysis_lines.push(format!("💾 Stored As: '{raw}'"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if editor.show_cursor_details {
|
if editor.show_cursor_details {
|
||||||
@@ -643,7 +643,7 @@ fn render_enhanced_status(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let Some(ref warn) = warning {
|
if let Some(ref warn) = warning {
|
||||||
analysis_lines.push(format!("⚠️ Warning: {}", warn));
|
analysis_lines.push(format!("⚠️ Warning: {warn}"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let analysis_color = if warning.is_some() {
|
let analysis_color = if warning.is_some() {
|
||||||
@@ -742,7 +742,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
CursorManager::reset()?;
|
CursorManager::reset()?;
|
||||||
|
|
||||||
if let Err(err) = res {
|
if let Err(err) = res {
|
||||||
println!("{:?}", err);
|
println!("{err:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("🧩 Enhanced custom formatter demo completed!");
|
println!("🧩 Enhanced custom formatter demo completed!");
|
||||||
|
|||||||
@@ -203,7 +203,7 @@ impl ValidationServices {
|
|||||||
|
|
||||||
/// PSC validation: simulates postal service API lookup
|
/// PSC validation: simulates postal service API lookup
|
||||||
fn validate_psc(&mut self, psc: &str) -> ExternalValidationState {
|
fn validate_psc(&mut self, psc: &str) -> ExternalValidationState {
|
||||||
let cache_key = format!("psc:{}", psc);
|
let cache_key = format!("psc:{psc}");
|
||||||
if let Some(cached) = self.cache.get(&cache_key) {
|
if let Some(cached) = self.cache.get(&cache_key) {
|
||||||
return cached.clone();
|
return cached.clone();
|
||||||
}
|
}
|
||||||
@@ -244,7 +244,7 @@ impl ValidationServices {
|
|||||||
"20" | "21" => "Brno region",
|
"20" | "21" => "Brno region",
|
||||||
_ => "Valid postal region"
|
_ => "Valid postal region"
|
||||||
};
|
};
|
||||||
ExternalValidationState::Valid(Some(format!("{} - verified", region)))
|
ExternalValidationState::Valid(Some(format!("{region} - verified")))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -254,7 +254,7 @@ impl ValidationServices {
|
|||||||
|
|
||||||
/// Email validation: simulates domain checking
|
/// Email validation: simulates domain checking
|
||||||
fn validate_email(&mut self, email: &str) -> ExternalValidationState {
|
fn validate_email(&mut self, email: &str) -> ExternalValidationState {
|
||||||
let cache_key = format!("email:{}", email);
|
let cache_key = format!("email:{email}");
|
||||||
if let Some(cached) = self.cache.get(&cache_key) {
|
if let Some(cached) = self.cache.get(&cache_key) {
|
||||||
return cached.clone();
|
return cached.clone();
|
||||||
}
|
}
|
||||||
@@ -315,7 +315,7 @@ impl ValidationServices {
|
|||||||
|
|
||||||
/// Username validation: simulates availability checking
|
/// Username validation: simulates availability checking
|
||||||
fn validate_username(&mut self, username: &str) -> ExternalValidationState {
|
fn validate_username(&mut self, username: &str) -> ExternalValidationState {
|
||||||
let cache_key = format!("username:{}", username);
|
let cache_key = format!("username:{username}");
|
||||||
if let Some(cached) = self.cache.get(&cache_key) {
|
if let Some(cached) = self.cache.get(&cache_key) {
|
||||||
return cached.clone();
|
return cached.clone();
|
||||||
}
|
}
|
||||||
@@ -371,7 +371,7 @@ impl ValidationServices {
|
|||||||
|
|
||||||
/// API Key validation: simulates authentication service
|
/// API Key validation: simulates authentication service
|
||||||
fn validate_api_key(&mut self, key: &str) -> ExternalValidationState {
|
fn validate_api_key(&mut self, key: &str) -> ExternalValidationState {
|
||||||
let cache_key = format!("apikey:{}", key);
|
let cache_key = format!("apikey:{key}");
|
||||||
if let Some(cached) = self.cache.get(&cache_key) {
|
if let Some(cached) = self.cache.get(&cache_key) {
|
||||||
return cached.clone();
|
return cached.clone();
|
||||||
}
|
}
|
||||||
@@ -429,7 +429,7 @@ impl ValidationServices {
|
|||||||
|
|
||||||
/// Credit Card validation: simulates bank verification
|
/// Credit Card validation: simulates bank verification
|
||||||
fn validate_credit_card(&mut self, card: &str) -> ExternalValidationState {
|
fn validate_credit_card(&mut self, card: &str) -> ExternalValidationState {
|
||||||
let cache_key = format!("card:{}", card);
|
let cache_key = format!("card:{card}");
|
||||||
if let Some(cached) = self.cache.get(&cache_key) {
|
if let Some(cached) = self.cache.get(&cache_key) {
|
||||||
return cached.clone();
|
return cached.clone();
|
||||||
}
|
}
|
||||||
@@ -724,8 +724,7 @@ impl<D: DataProvider> ValidationDemoEditor<D> {
|
|||||||
let duration_ms = result.duration().as_millis();
|
let duration_ms = result.duration().as_millis();
|
||||||
let cached_text = if result.cached { " (cached)" } else { "" };
|
let cached_text = if result.cached { " (cached)" } else { "" };
|
||||||
self.debug_message = format!(
|
self.debug_message = format!(
|
||||||
"🔍 {} validation completed in {}ms{} (manual)",
|
"🔍 {validation_type} validation completed in {duration_ms}ms{cached_text} (manual)"
|
||||||
validation_type, duration_ms, cached_text
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -812,7 +811,7 @@ impl<D: DataProvider> ValidationDemoEditor<D> {
|
|||||||
0
|
0
|
||||||
};
|
};
|
||||||
|
|
||||||
format!("Total: {} validations, Avg: {}ms", total_validations, avg_time_ms)
|
format!("Total: {total_validations} validations, Avg: {avg_time_ms}ms")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_field_validation_state(&self, field_index: usize) -> ExternalValidationState {
|
fn get_field_validation_state(&self, field_index: usize) -> ExternalValidationState {
|
||||||
@@ -1032,8 +1031,8 @@ fn render_validation_panel(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let field_line = Line::from(vec![
|
let field_line = Line::from(vec![
|
||||||
Span::styled(format!("{}{}: ", indicator, field_name), Style::default().fg(Color::White)),
|
Span::styled(format!("{indicator}{field_name}: "), Style::default().fg(Color::White)),
|
||||||
Span::raw(format!("'{}' → ", value_display)),
|
Span::raw(format!("'{value_display}' → ")),
|
||||||
Span::styled(state_text.to_string(), Style::default().fg(color)),
|
Span::styled(state_text.to_string(), Style::default().fg(color)),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@@ -1077,8 +1076,7 @@ fn render_validation_panel(
|
|||||||
};
|
};
|
||||||
|
|
||||||
ListItem::new(format!(
|
ListItem::new(format!(
|
||||||
"{}: '{}' → {} ({}ms{})",
|
"{field_name}: '{short_value}' → {state_summary} ({duration_ms}ms{cached_text})"
|
||||||
field_name, short_value, state_summary, duration_ms, cached_text
|
|
||||||
))
|
))
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
@@ -1162,7 +1160,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
CursorManager::reset()?;
|
CursorManager::reset()?;
|
||||||
|
|
||||||
if let Err(err) = res {
|
if let Err(err) = res {
|
||||||
println!("{:?}", err);
|
println!("{err:?}");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("🧪 Enhanced fully automatic external validation demo completed!");
|
println!("🧪 Enhanced fully automatic external validation demo completed!");
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// src/canvas/actions/mod.rs
|
// src/canvas/actions/mod.rs
|
||||||
|
//! Canvas action definitions and movement utilities
|
||||||
|
|
||||||
pub mod types;
|
pub mod types;
|
||||||
pub mod movement;
|
pub mod movement;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// src/canvas/actions/movement/char.rs
|
// src/canvas/actions/movement/char.rs
|
||||||
|
//! Character-level cursor movement functions
|
||||||
|
|
||||||
/// Calculate new position when moving left
|
/// Calculate new position when moving left
|
||||||
pub fn move_left(current_pos: usize) -> usize {
|
pub fn move_left(current_pos: usize) -> usize {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// src/canvas/actions/movement/line.rs
|
// src/canvas/actions/movement/line.rs
|
||||||
|
//! Line-level cursor movement and positioning
|
||||||
|
|
||||||
/// Calculate cursor position for line start
|
/// Calculate cursor position for line start
|
||||||
pub fn line_start_position() -> usize {
|
pub fn line_start_position() -> usize {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
// src/canvas/actions/movement/mod.rs
|
// src/canvas/actions/movement/mod.rs
|
||||||
|
//! Movement utilities for character, word, and line navigation
|
||||||
|
|
||||||
pub mod word;
|
pub mod word;
|
||||||
pub mod line;
|
pub mod line;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
// src/canvas/actions/movement/word.rs
|
// src/canvas/actions/movement/word.rs
|
||||||
// Replace the entire file with this corrected version:
|
//! Word-based cursor movement with vim-like semantics
|
||||||
|
|
||||||
#[derive(PartialEq, Copy, Clone)]
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
enum CharType {
|
enum CharType {
|
||||||
|
|||||||
@@ -1,77 +1,121 @@
|
|||||||
// src/canvas/actions/types.rs
|
// src/canvas/actions/types.rs
|
||||||
|
//! Core action types and result handling for canvas operations.
|
||||||
|
|
||||||
/// All available canvas actions
|
/// All available canvas actions.
|
||||||
|
///
|
||||||
|
/// This enum lists high-level actions that can be performed on the canvas.
|
||||||
|
/// Consumers can match on variants to implement custom handling or map input
|
||||||
|
/// events to these canonical actions.
|
||||||
|
#[non_exhaustive]
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum CanvasAction {
|
pub enum CanvasAction {
|
||||||
// Movement actions
|
// Movement actions
|
||||||
|
/// Move the cursor left by one character (or logical unit).
|
||||||
MoveLeft,
|
MoveLeft,
|
||||||
|
/// Move the cursor right by one character (or logical unit).
|
||||||
MoveRight,
|
MoveRight,
|
||||||
|
/// Move the cursor up a visual line/field.
|
||||||
MoveUp,
|
MoveUp,
|
||||||
|
/// Move the cursor down a visual line/field.
|
||||||
MoveDown,
|
MoveDown,
|
||||||
|
|
||||||
// Word movement
|
// Word movement
|
||||||
|
/// Move to the start of the next word.
|
||||||
MoveWordNext,
|
MoveWordNext,
|
||||||
|
/// Move to the start of the previous word.
|
||||||
MoveWordPrev,
|
MoveWordPrev,
|
||||||
|
/// Move to the end of the current/next word.
|
||||||
MoveWordEnd,
|
MoveWordEnd,
|
||||||
|
/// Move to the previous word end (vim `ge`).
|
||||||
MoveWordEndPrev,
|
MoveWordEndPrev,
|
||||||
|
|
||||||
// Line movement
|
// Line movement
|
||||||
|
/// Move to the start of the current line.
|
||||||
MoveLineStart,
|
MoveLineStart,
|
||||||
|
/// Move to the end of the current line.
|
||||||
MoveLineEnd,
|
MoveLineEnd,
|
||||||
|
|
||||||
// Field movement
|
// Field movement
|
||||||
|
/// Move to the next field.
|
||||||
NextField,
|
NextField,
|
||||||
|
/// Move to the previous field.
|
||||||
PrevField,
|
PrevField,
|
||||||
|
/// Move to the first field.
|
||||||
MoveFirstLine,
|
MoveFirstLine,
|
||||||
|
/// Move to the last field.
|
||||||
MoveLastLine,
|
MoveLastLine,
|
||||||
|
|
||||||
// Editing actions
|
// Editing actions
|
||||||
|
/// Insert a character at the cursor.
|
||||||
InsertChar(char),
|
InsertChar(char),
|
||||||
|
/// Delete character before the cursor.
|
||||||
DeleteBackward,
|
DeleteBackward,
|
||||||
|
/// Delete character under/after the cursor.
|
||||||
DeleteForward,
|
DeleteForward,
|
||||||
|
|
||||||
// Suggestions actions
|
// Suggestions actions
|
||||||
TriggerSuggestions,
|
/// Trigger suggestions dropdown (e.g. Tab).
|
||||||
SuggestionUp,
|
TriggerSuggestions,
|
||||||
SuggestionDown,
|
/// Move selection up in suggestions dropdown.
|
||||||
SelectSuggestion,
|
SuggestionUp,
|
||||||
ExitSuggestions,
|
/// Move selection down in suggestions dropdown.
|
||||||
|
SuggestionDown,
|
||||||
|
/// Accept the selected suggestion.
|
||||||
|
SelectSuggestion,
|
||||||
|
/// Exit suggestions UI.
|
||||||
|
ExitSuggestions,
|
||||||
|
|
||||||
// Custom actions
|
// Custom actions
|
||||||
|
/// Custom named action for application-specific behavior.
|
||||||
Custom(String),
|
Custom(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Result type for canvas actions
|
/// Result type for canvas actions.
|
||||||
|
///
|
||||||
|
/// Action handlers return an ActionResult to indicate success, user-facing
|
||||||
|
/// messages, or errors. The enum is non-exhaustive to allow extension.
|
||||||
|
#[non_exhaustive]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum ActionResult {
|
pub enum ActionResult {
|
||||||
|
/// Action completed successfully.
|
||||||
Success,
|
Success,
|
||||||
|
/// Action completed with a user-facing message.
|
||||||
Message(String),
|
Message(String),
|
||||||
|
/// Action was handled by the application with an associated message.
|
||||||
HandledByApp(String),
|
HandledByApp(String),
|
||||||
|
/// Action was handled by a feature with an associated message.
|
||||||
HandledByFeature(String), // Keep for compatibility
|
HandledByFeature(String), // Keep for compatibility
|
||||||
|
/// An error occurred while handling the action.
|
||||||
Error(String),
|
Error(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ActionResult {
|
impl ActionResult {
|
||||||
|
/// Convenience constructor for Success.
|
||||||
pub fn success() -> Self {
|
pub fn success() -> Self {
|
||||||
Self::Success
|
Self::Success
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Convenience constructor for Message.
|
||||||
pub fn success_with_message(msg: &str) -> Self {
|
pub fn success_with_message(msg: &str) -> Self {
|
||||||
Self::Message(msg.to_string())
|
Self::Message(msg.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Convenience constructor for HandledByApp.
|
||||||
pub fn handled_by_app(msg: &str) -> Self {
|
pub fn handled_by_app(msg: &str) -> Self {
|
||||||
Self::HandledByApp(msg.to_string())
|
Self::HandledByApp(msg.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Convenience constructor for Error.
|
||||||
pub fn error(msg: &str) -> Self {
|
pub fn error(msg: &str) -> Self {
|
||||||
Self::Error(msg.to_string())
|
Self::Error(msg.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns true for any variant representing a success-like outcome.
|
||||||
pub fn is_success(&self) -> bool {
|
pub fn is_success(&self) -> bool {
|
||||||
matches!(self, Self::Success | Self::Message(_) | Self::HandledByApp(_) | Self::HandledByFeature(_))
|
matches!(self, Self::Success | Self::Message(_) | Self::HandledByApp(_) | Self::HandledByFeature(_))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Extract a message from the result when present.
|
||||||
pub fn message(&self) -> Option<&str> {
|
pub fn message(&self) -> Option<&str> {
|
||||||
match self {
|
match self {
|
||||||
Self::Message(msg) | Self::HandledByApp(msg) | Self::HandledByFeature(msg) | Self::Error(msg) => Some(msg),
|
Self::Message(msg) | Self::HandledByApp(msg) | Self::HandledByFeature(msg) | Self::Error(msg) => Some(msg),
|
||||||
@@ -81,7 +125,7 @@ impl ActionResult {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl CanvasAction {
|
impl CanvasAction {
|
||||||
/// Get a human-readable description of this action
|
/// Get a human-readable description of this action.
|
||||||
pub fn description(&self) -> &'static str {
|
pub fn description(&self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
Self::MoveLeft => "move left",
|
Self::MoveLeft => "move left",
|
||||||
@@ -110,7 +154,7 @@ impl CanvasAction {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get all movement-related actions
|
/// Get all movement-related actions.
|
||||||
pub fn movement_actions() -> Vec<CanvasAction> {
|
pub fn movement_actions() -> Vec<CanvasAction> {
|
||||||
vec![
|
vec![
|
||||||
Self::MoveLeft,
|
Self::MoveLeft,
|
||||||
@@ -130,7 +174,7 @@ impl CanvasAction {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get all editing-related actions
|
/// Get all editing-related actions.
|
||||||
pub fn editing_actions() -> Vec<CanvasAction> {
|
pub fn editing_actions() -> Vec<CanvasAction> {
|
||||||
vec![
|
vec![
|
||||||
Self::InsertChar(' '), // Example char
|
Self::InsertChar(' '), // Example char
|
||||||
@@ -139,7 +183,7 @@ impl CanvasAction {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get all suggestions-related actions
|
/// Get all suggestions-related actions.
|
||||||
pub fn suggestions_actions() -> Vec<CanvasAction> {
|
pub fn suggestions_actions() -> Vec<CanvasAction> {
|
||||||
vec![
|
vec![
|
||||||
Self::TriggerSuggestions,
|
Self::TriggerSuggestions,
|
||||||
@@ -150,7 +194,7 @@ impl CanvasAction {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if this action modifies text content
|
/// Check if this action modifies text content.
|
||||||
pub fn is_editing_action(&self) -> bool {
|
pub fn is_editing_action(&self) -> bool {
|
||||||
matches!(self,
|
matches!(self,
|
||||||
Self::InsertChar(_) |
|
Self::InsertChar(_) |
|
||||||
@@ -159,7 +203,7 @@ impl CanvasAction {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check if this action moves the cursor
|
/// Check if this action moves the cursor.
|
||||||
pub fn is_movement_action(&self) -> bool {
|
pub fn is_movement_action(&self) -> bool {
|
||||||
matches!(self,
|
matches!(self,
|
||||||
Self::MoveLeft | Self::MoveRight | Self::MoveUp | Self::MoveDown |
|
Self::MoveLeft | Self::MoveRight | Self::MoveUp | Self::MoveDown |
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
// src/canvas/cursor.rs
|
// src/canvas/cursor.rs
|
||||||
//! Cursor style management for different canvas modes
|
//! Cursor style management for different canvas modes
|
||||||
|
//!
|
||||||
|
//! Provides helpers to update and reset terminal cursor style when the
|
||||||
|
//! `cursor-style` feature is enabled. When the feature is disabled the
|
||||||
|
//! functions are no-ops.
|
||||||
|
|
||||||
#[cfg(feature = "cursor-style")]
|
#[cfg(feature = "cursor-style")]
|
||||||
use crossterm::{cursor::SetCursorStyle, execute};
|
use crossterm::{cursor::SetCursorStyle, execute};
|
||||||
@@ -12,14 +16,17 @@ use crate::canvas::modes::AppMode;
|
|||||||
pub struct CursorManager;
|
pub struct CursorManager;
|
||||||
|
|
||||||
impl CursorManager {
|
impl CursorManager {
|
||||||
/// Update cursor style based on current mode
|
/// Update cursor style based on current mode.
|
||||||
|
///
|
||||||
|
/// When the `textmode-normal` feature is enabled a fixed style is applied.
|
||||||
|
/// Otherwise, the cursor style is mapped to the provided AppMode.
|
||||||
#[cfg(feature = "cursor-style")]
|
#[cfg(feature = "cursor-style")]
|
||||||
pub fn update_for_mode(mode: AppMode) -> io::Result<()> {
|
pub fn update_for_mode(mode: AppMode) -> io::Result<()> {
|
||||||
// NORMALMODE: force underscore for every mode
|
// NORMALMODE: force underscore for every mode
|
||||||
#[cfg(feature = "textmode-normal")]
|
#[cfg(feature = "textmode-normal")]
|
||||||
{
|
{
|
||||||
let style = SetCursorStyle::SteadyBar;
|
let style = SetCursorStyle::SteadyBar;
|
||||||
return execute!(io::stdout(), style);
|
execute!(io::stdout(), style)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default (not normal): original mapping
|
// Default (not normal): original mapping
|
||||||
@@ -37,18 +44,19 @@ impl CursorManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// No-op when cursor-style feature is disabled
|
/// No-op when cursor-style feature is disabled.
|
||||||
#[cfg(not(feature = "cursor-style"))]
|
#[cfg(not(feature = "cursor-style"))]
|
||||||
pub fn update_for_mode(_mode: AppMode) -> io::Result<()> {
|
pub fn update_for_mode(_mode: AppMode) -> io::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reset cursor to default on cleanup
|
/// Reset cursor to default on cleanup.
|
||||||
#[cfg(feature = "cursor-style")]
|
#[cfg(feature = "cursor-style")]
|
||||||
pub fn reset() -> io::Result<()> {
|
pub fn reset() -> io::Result<()> {
|
||||||
execute!(io::stdout(), SetCursorStyle::DefaultUserShape)
|
execute!(io::stdout(), SetCursorStyle::DefaultUserShape)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reset is a no-op when the cursor-style feature is disabled.
|
||||||
#[cfg(not(feature = "cursor-style"))]
|
#[cfg(not(feature = "cursor-style"))]
|
||||||
pub fn reset() -> io::Result<()> {
|
pub fn reset() -> io::Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
// src/canvas/gui.rs
|
// src/canvas/gui.rs
|
||||||
//! Canvas GUI updated to work with FormEditor
|
//! Canvas GUI updated to work with FormEditor
|
||||||
|
//!
|
||||||
|
//! This module provides rendering helpers for the canvas UI when the `gui`
|
||||||
|
//! feature is enabled. It exposes high-level functions to render the canvas
|
||||||
|
//! and convenience types for display options.
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
@@ -22,14 +26,20 @@ use std::cmp::{max, min};
|
|||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
/// How to handle overflow when rendering a field's content.
|
||||||
pub enum OverflowMode {
|
pub enum OverflowMode {
|
||||||
Indicator(char), // default '$'
|
/// Show an indicator character at the left/right when text is truncated.
|
||||||
|
/// Common default is '$'.
|
||||||
|
Indicator(char),
|
||||||
|
/// Wrap content into multiple visual lines.
|
||||||
Wrap,
|
Wrap,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
/// Display options controlling canvas rendering behavior.
|
||||||
pub struct CanvasDisplayOptions {
|
pub struct CanvasDisplayOptions {
|
||||||
|
/// How to handle horizontal overflow for fields.
|
||||||
pub overflow: OverflowMode,
|
pub overflow: OverflowMode,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,6 +83,9 @@ fn clip_with_indicator_line<'a>(s: &'a str, width: u16, indicator: char) -> Line
|
|||||||
Line::from(vec![Span::raw(out), Span::raw(indicator.to_string())])
|
Line::from(vec![Span::raw(out), Span::raw(indicator.to_string())])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "gui")]
|
||||||
|
const RIGHT_PAD: u16 = 3;
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
fn slice_by_display_cols(s: &str, start_cols: u16, max_cols: u16) -> String {
|
fn slice_by_display_cols(s: &str, start_cols: u16, max_cols: u16) -> String {
|
||||||
if max_cols == 0 {
|
if max_cols == 0 {
|
||||||
@@ -107,15 +120,9 @@ fn slice_by_display_cols(s: &str, start_cols: u16, max_cols: u16) -> String {
|
|||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
|
||||||
const RIGHT_PAD: u16 = 3;
|
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
fn compute_h_scroll_with_padding(cursor_cols: u16, width: u16) -> (u16, u16) {
|
fn compute_h_scroll_with_padding(cursor_cols: u16, width: u16) -> (u16, u16) {
|
||||||
// Returns (h_scroll, left_cols). left_cols = 1 if a left indicator is shown.
|
|
||||||
// We pre-emptively keep the caret out of the last RIGHT_PAD columns.
|
|
||||||
let mut h = 0u16;
|
let mut h = 0u16;
|
||||||
// Two passes are enough to converge (second pass accounts for left indicator).
|
|
||||||
for _ in 0..2 {
|
for _ in 0..2 {
|
||||||
let left_cols = if h > 0 { 1 } else { 0 };
|
let left_cols = if h > 0 { 1 } else { 0 };
|
||||||
let max_x_visible = width.saturating_sub(1 + RIGHT_PAD + left_cols);
|
let max_x_visible = width.saturating_sub(1 + RIGHT_PAD + left_cols);
|
||||||
@@ -130,21 +137,21 @@ fn compute_h_scroll_with_padding(cursor_cols: u16, width: u16) -> (u16, u16) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
fn active_indicator_viewport(
|
fn render_active_line_with_indicator<T: CanvasTheme>(
|
||||||
s: &str,
|
typed_text: &str,
|
||||||
|
completion: Option<&str>,
|
||||||
width: u16,
|
width: u16,
|
||||||
indicator: char,
|
indicator: char,
|
||||||
cursor_chars: usize,
|
cursor_chars: usize,
|
||||||
_right_padding: u16, // kept for signature symmetry; we use RIGHT_PAD constant
|
theme: &T,
|
||||||
) -> (Line<'static>, u16, u16) {
|
) -> (Line<'static>, u16, u16) {
|
||||||
if width == 0 {
|
if width == 0 {
|
||||||
return (Line::from(""), 0, 0);
|
return (Line::from(""), 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Total display width of the string and cursor display column
|
// Cursor display column
|
||||||
let total_cols = display_width(s);
|
|
||||||
let mut cursor_cols: u16 = 0;
|
let mut cursor_cols: u16 = 0;
|
||||||
for (i, ch) in s.chars().enumerate() {
|
for (i, ch) in typed_text.chars().enumerate() {
|
||||||
if i >= cursor_chars {
|
if i >= cursor_chars {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -152,22 +159,40 @@ fn active_indicator_viewport(
|
|||||||
.saturating_add(UnicodeWidthChar::width(ch).unwrap_or(0) as u16);
|
.saturating_add(UnicodeWidthChar::width(ch).unwrap_or(0) as u16);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pre-emptive scroll: never let caret enter the last RIGHT_PAD columns
|
|
||||||
let (h_scroll, left_cols) = compute_h_scroll_with_padding(cursor_cols, width);
|
let (h_scroll, left_cols) = compute_h_scroll_with_padding(cursor_cols, width);
|
||||||
|
|
||||||
// Right indicator if more content beyond the window start
|
let total_cols = display_width(typed_text);
|
||||||
let content_budget = width.saturating_sub(left_cols);
|
let content_budget = width.saturating_sub(left_cols);
|
||||||
let show_right = total_cols.saturating_sub(h_scroll) > content_budget;
|
let show_right = total_cols.saturating_sub(h_scroll) > content_budget;
|
||||||
let right_cols: u16 = if show_right { 1 } else { 0 };
|
let right_cols: u16 = if show_right { 1 } else { 0 };
|
||||||
|
|
||||||
let visible_cols = width.saturating_sub(left_cols + right_cols);
|
let visible_cols = width.saturating_sub(left_cols + right_cols);
|
||||||
let visible = slice_by_display_cols(s, h_scroll, visible_cols);
|
let visible_typed = slice_by_display_cols(typed_text, h_scroll, visible_cols);
|
||||||
|
|
||||||
|
let used_typed_cols = display_width(&visible_typed);
|
||||||
|
let mut remaining_cols = visible_cols.saturating_sub(used_typed_cols);
|
||||||
|
let mut visible_completion = String::new();
|
||||||
|
|
||||||
|
if let Some(comp) = completion {
|
||||||
|
if !comp.is_empty() && remaining_cols > 0 {
|
||||||
|
visible_completion = slice_by_display_cols(comp, 0, remaining_cols);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut spans: Vec<Span> = Vec::with_capacity(3);
|
let mut spans: Vec<Span> = Vec::with_capacity(3);
|
||||||
if left_cols == 1 {
|
if left_cols == 1 {
|
||||||
spans.push(Span::raw(indicator.to_string()));
|
spans.push(Span::raw(indicator.to_string()));
|
||||||
}
|
}
|
||||||
spans.push(Span::raw(visible));
|
spans.push(Span::styled(
|
||||||
|
visible_typed,
|
||||||
|
Style::default().fg(theme.fg()),
|
||||||
|
));
|
||||||
|
if !visible_completion.is_empty() {
|
||||||
|
spans.push(Span::styled(
|
||||||
|
visible_completion,
|
||||||
|
Style::default().fg(theme.suggestion_gray()),
|
||||||
|
));
|
||||||
|
}
|
||||||
if show_right {
|
if show_right {
|
||||||
spans.push(Span::raw(indicator.to_string()));
|
spans.push(Span::raw(indicator.to_string()));
|
||||||
}
|
}
|
||||||
@@ -175,8 +200,10 @@ fn active_indicator_viewport(
|
|||||||
(Line::from(spans), h_scroll, left_cols)
|
(Line::from(spans), h_scroll, left_cols)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Default renderer: overflow indicator '$'
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
|
/// Render the canvas into the provided frame using default display options.
|
||||||
|
///
|
||||||
|
/// Returns the rectangle of the active input field if present.
|
||||||
pub fn render_canvas<T: CanvasTheme, D: DataProvider>(
|
pub fn render_canvas<T: CanvasTheme, D: DataProvider>(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
@@ -187,8 +214,11 @@ pub fn render_canvas<T: CanvasTheme, D: DataProvider>(
|
|||||||
render_canvas_with_options(f, area, editor, theme, opts)
|
render_canvas_with_options(f, area, editor, theme, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wrapped variant: opt into soft wrap instead of overflow indicator
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
|
/// Render the canvas into the provided frame with explicit display options.
|
||||||
|
///
|
||||||
|
/// This is the more configurable entrypoint for rendering and is useful for
|
||||||
|
/// tests or when callers need to override overflow handling.
|
||||||
pub fn render_canvas_with_options<T: CanvasTheme, D: DataProvider>(
|
pub fn render_canvas_with_options<T: CanvasTheme, D: DataProvider>(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
@@ -198,10 +228,30 @@ pub fn render_canvas_with_options<T: CanvasTheme, D: DataProvider>(
|
|||||||
) -> Option<Rect> {
|
) -> Option<Rect> {
|
||||||
let highlight_state =
|
let highlight_state =
|
||||||
convert_selection_to_highlight(editor.ui_state().selection_state());
|
convert_selection_to_highlight(editor.ui_state().selection_state());
|
||||||
render_canvas_with_highlight_and_options(f, area, editor, theme, &highlight_state, opts)
|
|
||||||
|
#[cfg(feature = "suggestions")]
|
||||||
|
let active_completion = if editor.ui_state().is_suggestions_active()
|
||||||
|
&& editor.ui_state().suggestions.active_field
|
||||||
|
== Some(editor.ui_state().current_field())
|
||||||
|
{
|
||||||
|
editor.ui_state().suggestions.completion_text.clone()
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
#[cfg(not(feature = "suggestions"))]
|
||||||
|
let active_completion: Option<String> = None;
|
||||||
|
|
||||||
|
render_canvas_with_highlight_and_options(
|
||||||
|
f,
|
||||||
|
area,
|
||||||
|
editor,
|
||||||
|
theme,
|
||||||
|
&highlight_state,
|
||||||
|
active_completion,
|
||||||
|
opts,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Render canvas with explicit highlight state (with options)
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
fn render_canvas_with_highlight_and_options<T: CanvasTheme, D: DataProvider>(
|
fn render_canvas_with_highlight_and_options<T: CanvasTheme, D: DataProvider>(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
@@ -209,6 +259,7 @@ fn render_canvas_with_highlight_and_options<T: CanvasTheme, D: DataProvider>(
|
|||||||
editor: &FormEditor<D>,
|
editor: &FormEditor<D>,
|
||||||
theme: &T,
|
theme: &T,
|
||||||
highlight_state: &HighlightState,
|
highlight_state: &HighlightState,
|
||||||
|
active_completion: Option<String>,
|
||||||
opts: CanvasDisplayOptions,
|
opts: CanvasDisplayOptions,
|
||||||
) -> Option<Rect> {
|
) -> Option<Rect> {
|
||||||
let ui_state = editor.ui_state();
|
let ui_state = editor.ui_state();
|
||||||
@@ -233,17 +284,6 @@ fn render_canvas_with_highlight_and_options<T: CanvasTheme, D: DataProvider>(
|
|||||||
let current_field_idx = ui_state.current_field();
|
let current_field_idx = ui_state.current_field();
|
||||||
let is_edit_mode = matches!(ui_state.mode(), crate::canvas::modes::AppMode::Edit);
|
let is_edit_mode = matches!(ui_state.mode(), crate::canvas::modes::AppMode::Edit);
|
||||||
|
|
||||||
#[cfg(feature = "suggestions")]
|
|
||||||
let active_completion = if ui_state.is_suggestions_active()
|
|
||||||
&& ui_state.suggestions.active_field == Some(current_field_idx)
|
|
||||||
{
|
|
||||||
ui_state.suggestions.completion_text.clone()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
#[cfg(not(feature = "suggestions"))]
|
|
||||||
let active_completion: Option<String> = None;
|
|
||||||
|
|
||||||
render_canvas_fields_with_options(
|
render_canvas_fields_with_options(
|
||||||
f,
|
f,
|
||||||
area,
|
area,
|
||||||
@@ -270,13 +310,7 @@ fn render_canvas_with_highlight_and_options<T: CanvasTheme, D: DataProvider>(
|
|||||||
},
|
},
|
||||||
#[cfg(not(feature = "validation"))]
|
#[cfg(not(feature = "validation"))]
|
||||||
|_field_idx| false,
|
|_field_idx| false,
|
||||||
|field_idx| {
|
active_completion,
|
||||||
if field_idx == current_field_idx {
|
|
||||||
active_completion.clone()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
},
|
|
||||||
opts,
|
opts,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -302,7 +336,7 @@ fn convert_selection_to_highlight(
|
|||||||
|
|
||||||
/// Core canvas field rendering with options
|
/// Core canvas field rendering with options
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
fn render_canvas_fields_with_options<T: CanvasTheme, F1, F2, F3>(
|
fn render_canvas_fields_with_options<T: CanvasTheme, F1, F2>(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
fields: &[&str],
|
fields: &[&str],
|
||||||
@@ -315,13 +349,12 @@ fn render_canvas_fields_with_options<T: CanvasTheme, F1, F2, F3>(
|
|||||||
has_unsaved_changes: bool,
|
has_unsaved_changes: bool,
|
||||||
get_display_value: F1,
|
get_display_value: F1,
|
||||||
has_display_override: F2,
|
has_display_override: F2,
|
||||||
get_completion: F3,
|
active_completion: Option<String>,
|
||||||
opts: CanvasDisplayOptions,
|
opts: CanvasDisplayOptions,
|
||||||
) -> Option<Rect>
|
) -> Option<Rect>
|
||||||
where
|
where
|
||||||
F1: Fn(usize) -> String,
|
F1: Fn(usize) -> String,
|
||||||
F2: Fn(usize) -> bool,
|
F2: Fn(usize) -> bool,
|
||||||
F3: Fn(usize) -> Option<String>,
|
|
||||||
{
|
{
|
||||||
let columns = Layout::default()
|
let columns = Layout::default()
|
||||||
.direction(Direction::Horizontal)
|
.direction(Direction::Horizontal)
|
||||||
@@ -353,9 +386,6 @@ where
|
|||||||
|
|
||||||
let input_area = input_container.inner(input_block);
|
let input_area = input_container.inner(input_block);
|
||||||
|
|
||||||
// NOTE: We keep one visual row per field; Wrap mode renders wrapped content
|
|
||||||
// visually within that row (ratatui handles visual wrapping). To fully
|
|
||||||
// expand rows by wrapped height, we'd convert to per-field dynamic heights.
|
|
||||||
let input_rows = Layout::default()
|
let input_rows = Layout::default()
|
||||||
.direction(Direction::Vertical)
|
.direction(Direction::Vertical)
|
||||||
.constraints(vec![Constraint::Length(1); fields.len()])
|
.constraints(vec![Constraint::Length(1); fields.len()])
|
||||||
@@ -370,69 +400,71 @@ where
|
|||||||
let typed_text = get_display_value(i);
|
let typed_text = get_display_value(i);
|
||||||
let inner_width = input_rows[i].width;
|
let inner_width = input_rows[i].width;
|
||||||
|
|
||||||
|
// ---- BEGIN MODIFIED SECTION ----
|
||||||
let mut h_scroll_for_cursor: u16 = 0;
|
let mut h_scroll_for_cursor: u16 = 0;
|
||||||
let mut left_offset_for_cursor: u16 = 0;
|
let mut left_offset_for_cursor: u16 = 0;
|
||||||
|
|
||||||
let line = match (opts.overflow, highlight_state) {
|
let line = match highlight_state {
|
||||||
(OverflowMode::Indicator(ind), HighlightState::Off) => {
|
// Selection highlighting active: always use highlighting, even for the active field
|
||||||
if is_active {
|
HighlightState::Characterwise { .. } | HighlightState::Linewise { .. } => {
|
||||||
let (l, hs, left_cols) = active_indicator_viewport(
|
apply_highlighting(
|
||||||
&typed_text,
|
&typed_text,
|
||||||
inner_width,
|
i,
|
||||||
ind,
|
current_field_idx,
|
||||||
current_cursor_pos,
|
current_cursor_pos,
|
||||||
RIGHT_PAD,
|
highlight_state,
|
||||||
);
|
theme,
|
||||||
h_scroll_for_cursor = hs;
|
is_active,
|
||||||
left_offset_for_cursor = left_cols;
|
)
|
||||||
l
|
}
|
||||||
} else {
|
|
||||||
if display_width(&typed_text) <= inner_width {
|
// No selection highlighting
|
||||||
|
HighlightState::Off => match opts.overflow {
|
||||||
|
// Indicator mode: special-case the active field to preserve h-scroll + indicators
|
||||||
|
OverflowMode::Indicator(ind) => {
|
||||||
|
if is_active {
|
||||||
|
let (l, hs, left_cols) = render_active_line_with_indicator(
|
||||||
|
&typed_text,
|
||||||
|
active_completion.as_deref(),
|
||||||
|
inner_width,
|
||||||
|
ind,
|
||||||
|
current_cursor_pos,
|
||||||
|
theme,
|
||||||
|
);
|
||||||
|
h_scroll_for_cursor = hs;
|
||||||
|
left_offset_for_cursor = left_cols;
|
||||||
|
l
|
||||||
|
} else if display_width(&typed_text) <= inner_width {
|
||||||
Line::from(Span::raw(typed_text.clone()))
|
Line::from(Span::raw(typed_text.clone()))
|
||||||
} else {
|
} else {
|
||||||
clip_with_indicator_line(&typed_text, inner_width, ind)
|
clip_with_indicator_line(&typed_text, inner_width, ind)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Existing highlighting paths (unchanged)
|
// Wrap mode: keep active completion for active line
|
||||||
(OverflowMode::Indicator(_ind), HighlightState::Characterwise { .. }) => {
|
OverflowMode::Wrap => {
|
||||||
apply_highlighting(
|
if is_active {
|
||||||
&typed_text,
|
let mut spans: Vec<Span> = Vec::new();
|
||||||
i,
|
spans.push(Span::styled(
|
||||||
current_field_idx,
|
typed_text.clone(),
|
||||||
current_cursor_pos,
|
Style::default().fg(theme.fg()),
|
||||||
highlight_state,
|
));
|
||||||
theme,
|
if let Some(completion) = &active_completion {
|
||||||
is_active,
|
if !completion.is_empty() {
|
||||||
)
|
spans.push(Span::styled(
|
||||||
}
|
completion.clone(),
|
||||||
(OverflowMode::Indicator(_ind), HighlightState::Linewise { .. }) => {
|
Style::default().fg(theme.suggestion_gray()),
|
||||||
apply_highlighting(
|
));
|
||||||
&typed_text,
|
}
|
||||||
i,
|
}
|
||||||
current_field_idx,
|
Line::from(spans)
|
||||||
current_cursor_pos,
|
} else {
|
||||||
highlight_state,
|
Line::from(Span::raw(typed_text.clone()))
|
||||||
theme,
|
}
|
||||||
is_active,
|
}
|
||||||
)
|
},
|
||||||
}
|
};
|
||||||
|
// ---- END MODIFIED SECTION ----
|
||||||
// Wrap mode unchanged (Paragraph::wrap will handle it)
|
|
||||||
(OverflowMode::Wrap, HighlightState::Off) => Line::from(Span::raw(typed_text.clone())),
|
|
||||||
(OverflowMode::Wrap, _) => {
|
|
||||||
apply_highlighting(
|
|
||||||
&typed_text,
|
|
||||||
i,
|
|
||||||
current_field_idx,
|
|
||||||
current_cursor_pos,
|
|
||||||
highlight_state,
|
|
||||||
theme,
|
|
||||||
is_active,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut p = Paragraph::new(line).alignment(Alignment::Left);
|
let mut p = Paragraph::new(line).alignment(Alignment::Left);
|
||||||
|
|
||||||
@@ -470,7 +502,7 @@ fn render_field_labels<T: CanvasTheme>(
|
|||||||
) {
|
) {
|
||||||
for (i, field) in fields.iter().enumerate() {
|
for (i, field) in fields.iter().enumerate() {
|
||||||
let label = Paragraph::new(Line::from(Span::styled(
|
let label = Paragraph::new(Line::from(Span::styled(
|
||||||
format!("{}:", field),
|
format!("{field}:"),
|
||||||
Style::default().fg(theme.fg()),
|
Style::default().fg(theme.fg()),
|
||||||
)));
|
)));
|
||||||
f.render_widget(
|
f.render_widget(
|
||||||
@@ -579,50 +611,48 @@ fn apply_characterwise_highlighting<'a, T: CanvasTheme>(
|
|||||||
Span::styled(highlighted, highlight_style),
|
Span::styled(highlighted, highlight_style),
|
||||||
Span::styled(after, normal_style),
|
Span::styled(after, normal_style),
|
||||||
])
|
])
|
||||||
} else {
|
} else if field_index == anchor_field {
|
||||||
if field_index == anchor_field {
|
if anchor_field < *current_field_idx {
|
||||||
if anchor_field < *current_field_idx {
|
let clamped_start = anchor_char.min(text_len);
|
||||||
let clamped_start = anchor_char.min(text_len);
|
let before: String = text.chars().take(clamped_start).collect();
|
||||||
let before: String = text.chars().take(clamped_start).collect();
|
let highlighted: String = text.chars().skip(clamped_start).collect();
|
||||||
let highlighted: String = text.chars().skip(clamped_start).collect();
|
|
||||||
|
|
||||||
Line::from(vec![
|
Line::from(vec![
|
||||||
Span::styled(before, normal_style),
|
Span::styled(before, normal_style),
|
||||||
Span::styled(highlighted, highlight_style),
|
Span::styled(highlighted, highlight_style),
|
||||||
])
|
])
|
||||||
} else {
|
|
||||||
let clamped_end = anchor_char.min(text_len);
|
|
||||||
let highlighted: String = text.chars().take(clamped_end + 1).collect();
|
|
||||||
let after: String = text.chars().skip(clamped_end + 1).collect();
|
|
||||||
|
|
||||||
Line::from(vec![
|
|
||||||
Span::styled(highlighted, highlight_style),
|
|
||||||
Span::styled(after, normal_style),
|
|
||||||
])
|
|
||||||
}
|
|
||||||
} else if field_index == *current_field_idx {
|
|
||||||
if anchor_field < *current_field_idx {
|
|
||||||
let clamped_end = current_cursor_pos.min(text_len);
|
|
||||||
let highlighted: String = text.chars().take(clamped_end + 1).collect();
|
|
||||||
let after: String = text.chars().skip(clamped_end + 1).collect();
|
|
||||||
|
|
||||||
Line::from(vec![
|
|
||||||
Span::styled(highlighted, highlight_style),
|
|
||||||
Span::styled(after, normal_style),
|
|
||||||
])
|
|
||||||
} else {
|
|
||||||
let clamped_start = current_cursor_pos.min(text_len);
|
|
||||||
let before: String = text.chars().take(clamped_start).collect();
|
|
||||||
let highlighted: String = text.chars().skip(clamped_start).collect();
|
|
||||||
|
|
||||||
Line::from(vec![
|
|
||||||
Span::styled(before, normal_style),
|
|
||||||
Span::styled(highlighted, highlight_style),
|
|
||||||
])
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
Line::from(Span::styled(text, highlight_style))
|
let clamped_end = anchor_char.min(text_len);
|
||||||
|
let highlighted: String = text.chars().take(clamped_end + 1).collect();
|
||||||
|
let after: String = text.chars().skip(clamped_end + 1).collect();
|
||||||
|
|
||||||
|
Line::from(vec![
|
||||||
|
Span::styled(highlighted, highlight_style),
|
||||||
|
Span::styled(after, normal_style),
|
||||||
|
])
|
||||||
}
|
}
|
||||||
|
} else if field_index == *current_field_idx {
|
||||||
|
if anchor_field < *current_field_idx {
|
||||||
|
let clamped_end = current_cursor_pos.min(text_len);
|
||||||
|
let highlighted: String = text.chars().take(clamped_end + 1).collect();
|
||||||
|
let after: String = text.chars().skip(clamped_end + 1).collect();
|
||||||
|
|
||||||
|
Line::from(vec![
|
||||||
|
Span::styled(highlighted, highlight_style),
|
||||||
|
Span::styled(after, normal_style),
|
||||||
|
])
|
||||||
|
} else {
|
||||||
|
let clamped_start = current_cursor_pos.min(text_len);
|
||||||
|
let before: String = text.chars().take(clamped_start).collect();
|
||||||
|
let highlighted: String = text.chars().skip(clamped_start).collect();
|
||||||
|
|
||||||
|
Line::from(vec![
|
||||||
|
Span::styled(before, normal_style),
|
||||||
|
Span::styled(highlighted, highlight_style),
|
||||||
|
])
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Line::from(Span::styled(text, highlight_style))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Line::from(Span::styled(text, normal_style))
|
Line::from(Span::styled(text, normal_style))
|
||||||
@@ -675,10 +705,8 @@ fn set_cursor_position_scrolled(
|
|||||||
cols = cols.saturating_add(UnicodeWidthChar::width(ch).unwrap_or(0) as u16);
|
cols = cols.saturating_add(UnicodeWidthChar::width(ch).unwrap_or(0) as u16);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Visible x = (cursor columns - scroll) + left indicator column (if any)
|
|
||||||
let mut visible_x = cols.saturating_sub(h_scroll).saturating_add(left_offset);
|
let mut visible_x = cols.saturating_sub(h_scroll).saturating_add(left_offset);
|
||||||
|
|
||||||
// Hard clamp: keep RIGHT_PAD columns free at the right border
|
|
||||||
let limit = field_rect.width.saturating_sub(1 + RIGHT_PAD);
|
let limit = field_rect.width.saturating_sub(1 + RIGHT_PAD);
|
||||||
if visible_x > limit {
|
if visible_x > limit {
|
||||||
visible_x = limit;
|
visible_x = limit;
|
||||||
@@ -696,6 +724,6 @@ pub fn render_canvas_default<D: DataProvider>(
|
|||||||
area: Rect,
|
area: Rect,
|
||||||
editor: &FormEditor<D>,
|
editor: &FormEditor<D>,
|
||||||
) -> Option<Rect> {
|
) -> Option<Rect> {
|
||||||
let theme = DefaultCanvasTheme::default();
|
let theme = DefaultCanvasTheme;
|
||||||
render_canvas(f, area, editor, &theme)
|
render_canvas(f, area, editor, &theme)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
// src/canvas/mod.rs
|
// src/canvas/mod.rs
|
||||||
|
//! Top-level canvas module.
|
||||||
|
//!
|
||||||
|
//! Re-exports commonly used canvas types and modules so that downstream
|
||||||
|
//! consumers can import them from `crate::canvas`.
|
||||||
|
|
||||||
pub mod actions;
|
pub mod actions;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
|
|||||||
@@ -1,15 +1,18 @@
|
|||||||
// src/state/app/highlight.rs
|
// src/canvas/modes/highlight.rs
|
||||||
// canvas/src/modes/highlight.rs
|
//! Highlight state definitions for canvas visual/selection modes.
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
#[derive(Default)]
|
||||||
|
/// Represents the current highlight/visual selection state.
|
||||||
|
///
|
||||||
|
/// This enum is used by the GUI and selection logic to track whether a visual
|
||||||
|
/// selection is active and its anchor position.
|
||||||
pub enum HighlightState {
|
pub enum HighlightState {
|
||||||
|
/// No highlighting active.
|
||||||
|
#[default]
|
||||||
Off,
|
Off,
|
||||||
|
/// Characterwise selection with an anchor (field_index, char_position).
|
||||||
Characterwise { anchor: (usize, usize) }, // (field_index, char_position)
|
Characterwise { anchor: (usize, usize) }, // (field_index, char_position)
|
||||||
|
/// Linewise selection anchored at a field index.
|
||||||
Linewise { anchor_line: usize }, // field_index
|
Linewise { anchor_line: usize }, // field_index
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for HighlightState {
|
|
||||||
fn default() -> Self {
|
|
||||||
HighlightState::Off
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,45 +1,66 @@
|
|||||||
// src/modes/handlers/mode_manager.rs
|
// src/modes/handlers/mode_manager.rs
|
||||||
// canvas/src/modes/manager.rs
|
// canvas/src/modes/manager.rs
|
||||||
|
//! Mode manager utilities and the AppMode enum.
|
||||||
|
//!
|
||||||
|
//! This module defines the available canvas modes and provides helper
|
||||||
|
//! functions to validate mode transitions and perform required side-effects
|
||||||
|
//! such as updating cursor style when enabled.
|
||||||
|
|
||||||
#[cfg(feature = "cursor-style")]
|
#[cfg(feature = "cursor-style")]
|
||||||
use crate::canvas::CursorManager;
|
use crate::canvas::CursorManager;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
/// Top-level application modes used by the canvas UI.
|
||||||
|
///
|
||||||
|
/// These modes control input handling, cursor behavior, and how the UI should
|
||||||
|
/// respond to user actions.
|
||||||
pub enum AppMode {
|
pub enum AppMode {
|
||||||
General, // For intro and admin screens
|
/// For intro and admin screens
|
||||||
ReadOnly, // Canvas read-only mode
|
General,
|
||||||
Edit, // Canvas edit mode
|
/// Canvas read-only mode (navigation)
|
||||||
Highlight, // Canvas highlight/visual mode
|
ReadOnly,
|
||||||
Command, // Command mode overlay
|
/// Canvas edit mode (insertion/modification)
|
||||||
|
Edit,
|
||||||
|
/// Canvas highlight/visual mode (selection)
|
||||||
|
Highlight,
|
||||||
|
/// Command mode overlay (for commands)
|
||||||
|
Command,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct ModeManager;
|
pub struct ModeManager;
|
||||||
|
|
||||||
impl ModeManager {
|
impl ModeManager {
|
||||||
// Mode transition rules
|
// Mode transition rules
|
||||||
|
|
||||||
|
/// Return true if the system can enter Command mode from the given current mode.
|
||||||
pub fn can_enter_command_mode(current_mode: AppMode) -> bool {
|
pub fn can_enter_command_mode(current_mode: AppMode) -> bool {
|
||||||
!matches!(current_mode, AppMode::Edit)
|
!matches!(current_mode, AppMode::Edit)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return true if the system can enter Edit mode from the given current mode.
|
||||||
pub fn can_enter_edit_mode(current_mode: AppMode) -> bool {
|
pub fn can_enter_edit_mode(current_mode: AppMode) -> bool {
|
||||||
matches!(current_mode, AppMode::ReadOnly)
|
matches!(current_mode, AppMode::ReadOnly)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return true if the system can enter ReadOnly mode from the given current mode.
|
||||||
pub fn can_enter_read_only_mode(current_mode: AppMode) -> bool {
|
pub fn can_enter_read_only_mode(current_mode: AppMode) -> bool {
|
||||||
matches!(current_mode, AppMode::Edit | AppMode::Command | AppMode::Highlight)
|
matches!(current_mode, AppMode::Edit | AppMode::Command | AppMode::Highlight)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Return true if the system can enter Highlight mode from the given current mode.
|
||||||
pub fn can_enter_highlight_mode(current_mode: AppMode) -> bool {
|
pub fn can_enter_highlight_mode(current_mode: AppMode) -> bool {
|
||||||
matches!(current_mode, AppMode::ReadOnly)
|
matches!(current_mode, AppMode::ReadOnly)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Transition to new mode with automatic cursor update (when cursor-style feature enabled)
|
/// Transition to new mode with automatic cursor update (when cursor-style feature enabled).
|
||||||
|
///
|
||||||
|
/// Returns the resulting mode or an I/O error if cursor style update fails.
|
||||||
pub fn transition_to_mode(current_mode: AppMode, new_mode: AppMode) -> std::io::Result<AppMode> {
|
pub fn transition_to_mode(current_mode: AppMode, new_mode: AppMode) -> std::io::Result<AppMode> {
|
||||||
#[cfg(feature = "textmode-normal")]
|
#[cfg(feature = "textmode-normal")]
|
||||||
{
|
{
|
||||||
// Always force Edit in normalmode
|
// Always force Edit in normalmode
|
||||||
return Ok(AppMode::Edit);
|
Ok(AppMode::Edit)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "textmode-normal"))]
|
#[cfg(not(feature = "textmode-normal"))]
|
||||||
@@ -54,7 +75,10 @@ impl ModeManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Enter highlight mode with cursor styling
|
/// Enter highlight mode with cursor styling.
|
||||||
|
///
|
||||||
|
/// Returns Ok(true) if the transition succeeded (and cursor style was updated
|
||||||
|
/// when enabled), otherwise Ok(false) if the transition is not allowed.
|
||||||
pub fn enter_highlight_mode_with_cursor(current_mode: AppMode) -> std::io::Result<bool> {
|
pub fn enter_highlight_mode_with_cursor(current_mode: AppMode) -> std::io::Result<bool> {
|
||||||
if Self::can_enter_highlight_mode(current_mode) {
|
if Self::can_enter_highlight_mode(current_mode) {
|
||||||
#[cfg(feature = "cursor-style")]
|
#[cfg(feature = "cursor-style")]
|
||||||
@@ -67,7 +91,10 @@ impl ModeManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Exit highlight mode with cursor styling
|
/// Exit highlight mode with cursor styling and return the next mode.
|
||||||
|
///
|
||||||
|
/// This helper returns the mode to switch to (ReadOnly) and updates cursor
|
||||||
|
/// style if the feature is enabled.
|
||||||
pub fn exit_highlight_mode_with_cursor() -> std::io::Result<AppMode> {
|
pub fn exit_highlight_mode_with_cursor() -> std::io::Result<AppMode> {
|
||||||
let new_mode = AppMode::ReadOnly;
|
let new_mode = AppMode::ReadOnly;
|
||||||
#[cfg(feature = "cursor-style")]
|
#[cfg(feature = "cursor-style")]
|
||||||
|
|||||||
@@ -1,10 +1,20 @@
|
|||||||
// src/canvas/state.rs
|
// src/canvas/state.rs
|
||||||
//! Library-owned UI state - user never directly modifies this
|
//! Library-owned UI state - user never directly modifies this
|
||||||
|
//!
|
||||||
|
//! This module exposes the EditorState type (and related selection and
|
||||||
|
//! suggestions types) which represent the internal UI state maintained by the
|
||||||
|
//! canvas library. These types are intended for read-only access by callers
|
||||||
|
//! and are mutated only through the library's APIs.
|
||||||
|
|
||||||
use crate::canvas::modes::AppMode;
|
use crate::canvas::modes::AppMode;
|
||||||
|
|
||||||
/// Library-owned UI state - user never directly modifies this
|
/// Library-owned UI state - user never directly modifies this
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
/// Internal editor UI state managed by the canvas library.
|
||||||
|
///
|
||||||
|
/// The fields are `pub(crate)` because they should only be modified by the
|
||||||
|
/// library's internal action handlers. Consumers can use the provided getter
|
||||||
|
/// methods to observe the state.
|
||||||
pub struct EditorState {
|
pub struct EditorState {
|
||||||
// Navigation state
|
// Navigation state
|
||||||
pub(crate) current_field: usize,
|
pub(crate) current_field: usize,
|
||||||
@@ -32,6 +42,7 @@ pub struct EditorState {
|
|||||||
|
|
||||||
#[cfg(feature = "suggestions")]
|
#[cfg(feature = "suggestions")]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
/// Internal suggestions UI state used to manage the suggestions dropdown.
|
||||||
pub struct SuggestionsUIState {
|
pub struct SuggestionsUIState {
|
||||||
pub(crate) is_active: bool,
|
pub(crate) is_active: bool,
|
||||||
pub(crate) is_loading: bool,
|
pub(crate) is_loading: bool,
|
||||||
@@ -42,13 +53,19 @@ pub struct SuggestionsUIState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
/// SelectionState represents the current selection/visual mode state used by
|
||||||
|
/// the canvas (for example, Vim-like visual modes).
|
||||||
pub enum SelectionState {
|
pub enum SelectionState {
|
||||||
|
/// No selection is active.
|
||||||
None,
|
None,
|
||||||
|
/// Characterwise selection: (field_index, char_position)
|
||||||
Characterwise { anchor: (usize, usize) },
|
Characterwise { anchor: (usize, usize) },
|
||||||
|
/// Linewise selection anchored at a field (field index).
|
||||||
Linewise { anchor_field: usize },
|
Linewise { anchor_field: usize },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EditorState {
|
impl EditorState {
|
||||||
|
/// Create a new EditorState with default initial values.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
current_field: 0,
|
current_field: 0,
|
||||||
@@ -139,6 +156,10 @@ impl EditorState {
|
|||||||
// INTERNAL MUTATIONS: Only library modifies these
|
// INTERNAL MUTATIONS: Only library modifies these
|
||||||
// ===================================================================
|
// ===================================================================
|
||||||
|
|
||||||
|
/// Move internal pointer to another field index.
|
||||||
|
///
|
||||||
|
/// This method is intended for internal library use to change the current
|
||||||
|
/// field and reset the cursor to a safe value.
|
||||||
pub(crate) fn move_to_field(&mut self, field_index: usize, field_count: usize) {
|
pub(crate) fn move_to_field(&mut self, field_index: usize, field_count: usize) {
|
||||||
if field_index < field_count {
|
if field_index < field_count {
|
||||||
self.current_field = field_index;
|
self.current_field = field_index;
|
||||||
@@ -147,6 +168,11 @@ impl EditorState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set the cursor position with appropriate clamping depending on mode.
|
||||||
|
///
|
||||||
|
/// If `for_edit_mode` is true the cursor may be positioned at the end of
|
||||||
|
/// the text (allowing insertion); otherwise it will be kept within the
|
||||||
|
/// bounds of the existing text for read-only/highlight modes.
|
||||||
pub(crate) fn set_cursor(
|
pub(crate) fn set_cursor(
|
||||||
&mut self,
|
&mut self,
|
||||||
position: usize,
|
position: usize,
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
|
//! Computed fields subsystem.
|
||||||
|
//!
|
||||||
|
//! This module exposes the provider trait and the internal state management
|
||||||
|
//! for computed (display-only) fields. Computed fields are values derived
|
||||||
|
//! from other fields in the form and are not directly editable by the user.
|
||||||
|
|
||||||
pub mod provider;
|
pub mod provider;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
|
|
||||||
pub use provider::{ComputedContext, ComputedProvider};
|
pub use provider::{ComputedContext, ComputedProvider};
|
||||||
pub use state::ComputedState;
|
pub use state::ComputedState;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
// ================================================================================================
|
//! Provider interface and context for computed/display-only fields.
|
||||||
// COMPUTED FIELDS - Provider and Context
|
//!
|
||||||
// ================================================================================================
|
//! Implementors provide logic to compute a field's display value from the
|
||||||
|
//! other field values in the form.
|
||||||
|
|
||||||
/// Context information provided to computed field calculations
|
/// Context information provided to computed field calculations
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
/* file: canvas/src/computed/state.rs */
|
// src/computed/state.rs
|
||||||
/*
|
//! Computed field state: caching and dependency graph.
|
||||||
Add computed state module file implementing caching and dependencies
|
//!
|
||||||
*/
|
//! This module holds the internal state necessary to track which fields are
|
||||||
|
//! computed, their dependencies, and cached computed values. It is used by the
|
||||||
// ================================================================================================
|
//! editor to avoid unnecessary recomputation and to present computed fields as
|
||||||
// COMPUTED FIELDS - State: caching and dependencies
|
//! read-only.
|
||||||
// ================================================================================================
|
|
||||||
|
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
|
|
||||||
@@ -85,4 +84,4 @@ impl Default for ComputedState {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
Self::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
// src/editor/mod.rs
|
// src/editor/mod.rs
|
||||||
// Only module declarations and re-exports.
|
//! Editor submodule exports.
|
||||||
|
//!
|
||||||
|
//! This module exposes the internal editor pieces (core, editing, movement,
|
||||||
|
//! navigation, mode, and optional features like suggestions, validation, and
|
||||||
|
//! computed field helpers). Only module declarations and re-exports live here.
|
||||||
|
|
||||||
pub mod core;
|
pub mod core;
|
||||||
pub mod display;
|
pub mod display;
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ impl<D: DataProvider> FormEditor<D> {
|
|||||||
{
|
{
|
||||||
let _ = CursorManager::update_for_mode(AppMode::Edit);
|
let _ = CursorManager::update_for_mode(AppMode::Edit);
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default (not normal): original vim behavior
|
// Default (not normal): original vim behavior
|
||||||
@@ -119,7 +118,7 @@ impl<D: DataProvider> FormEditor<D> {
|
|||||||
{
|
{
|
||||||
self.close_suggestions();
|
self.close_suggestions();
|
||||||
}
|
}
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default (not normal): original vim behavior
|
// Default (not normal): original vim behavior
|
||||||
@@ -155,7 +154,6 @@ impl<D: DataProvider> FormEditor<D> {
|
|||||||
{
|
{
|
||||||
let _ = CursorManager::update_for_mode(AppMode::Edit);
|
let _ = CursorManager::update_for_mode(AppMode::Edit);
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default (not normal): vim behavior
|
// Default (not normal): vim behavior
|
||||||
@@ -169,7 +167,6 @@ impl<D: DataProvider> FormEditor<D> {
|
|||||||
// NORMALMODE: ignore request (stay in Edit)
|
// NORMALMODE: ignore request (stay in Edit)
|
||||||
#[cfg(feature = "textmode-normal")]
|
#[cfg(feature = "textmode-normal")]
|
||||||
{
|
{
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default (not normal): original vim
|
// Default (not normal): original vim
|
||||||
@@ -193,7 +190,6 @@ impl<D: DataProvider> FormEditor<D> {
|
|||||||
// NORMALMODE: ignore
|
// NORMALMODE: ignore
|
||||||
#[cfg(feature = "textmode-normal")]
|
#[cfg(feature = "textmode-normal")]
|
||||||
{
|
{
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default (not normal): original vim
|
// Default (not normal): original vim
|
||||||
@@ -216,7 +212,6 @@ impl<D: DataProvider> FormEditor<D> {
|
|||||||
// NORMALMODE: ignore
|
// NORMALMODE: ignore
|
||||||
#[cfg(feature = "textmode-normal")]
|
#[cfg(feature = "textmode-normal")]
|
||||||
{
|
{
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default (not normal): original vim
|
// Default (not normal): original vim
|
||||||
@@ -237,7 +232,7 @@ impl<D: DataProvider> FormEditor<D> {
|
|||||||
pub fn is_highlight_mode(&self) -> bool {
|
pub fn is_highlight_mode(&self) -> bool {
|
||||||
#[cfg(feature = "textmode-normal")]
|
#[cfg(feature = "textmode-normal")]
|
||||||
{
|
{
|
||||||
return false;
|
false
|
||||||
}
|
}
|
||||||
#[cfg(not(feature = "textmode-normal"))]
|
#[cfg(not(feature = "textmode-normal"))]
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -46,12 +46,11 @@ impl<D: DataProvider> FormEditor<D> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !moved {
|
if !moved
|
||||||
if self.ui_state.cursor_pos > 0 {
|
&& self.ui_state.cursor_pos > 0 {
|
||||||
self.ui_state.cursor_pos -= 1;
|
self.ui_state.cursor_pos -= 1;
|
||||||
self.ui_state.ideal_cursor_column = self.ui_state.cursor_pos;
|
self.ui_state.ideal_cursor_column = self.ui_state.cursor_pos;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,7 +140,7 @@ impl<D: DataProvider> FormEditor<D> {
|
|||||||
// Successfully moved to next field, try to find first word
|
// Successfully moved to next field, try to find first word
|
||||||
let new_text = self.current_text();
|
let new_text = self.current_text();
|
||||||
if !new_text.is_empty() {
|
if !new_text.is_empty() {
|
||||||
let first_word_pos = if new_text.chars().next().map_or(false, |c| !c.is_whitespace()) {
|
let first_word_pos = if new_text.chars().next().is_some_and(|c| !c.is_whitespace()) {
|
||||||
// Field starts with non-whitespace, go to position 0
|
// Field starts with non-whitespace, go to position 0
|
||||||
0
|
0
|
||||||
} else {
|
} else {
|
||||||
@@ -177,7 +176,7 @@ impl<D: DataProvider> FormEditor<D> {
|
|||||||
self.ui_state.ideal_cursor_column = 0;
|
self.ui_state.ideal_cursor_column = 0;
|
||||||
} else {
|
} else {
|
||||||
// Find first word in new field
|
// Find first word in new field
|
||||||
let first_word_pos = if new_text.chars().next().map_or(false, |c| !c.is_whitespace()) {
|
let first_word_pos = if new_text.chars().next().is_some_and(|c| !c.is_whitespace()) {
|
||||||
// Field starts with non-whitespace, go to position 0
|
// Field starts with non-whitespace, go to position 0
|
||||||
0
|
0
|
||||||
} else {
|
} else {
|
||||||
@@ -419,7 +418,7 @@ impl<D: DataProvider> FormEditor<D> {
|
|||||||
// Successfully moved to next field, try to find first big_word
|
// Successfully moved to next field, try to find first big_word
|
||||||
let new_text = self.current_text();
|
let new_text = self.current_text();
|
||||||
if !new_text.is_empty() {
|
if !new_text.is_empty() {
|
||||||
let first_big_word_pos = if new_text.chars().next().map_or(false, |c| !c.is_whitespace()) {
|
let first_big_word_pos = if new_text.chars().next().is_some_and(|c| !c.is_whitespace()) {
|
||||||
// Field starts with non-whitespace, go to position 0
|
// Field starts with non-whitespace, go to position 0
|
||||||
0
|
0
|
||||||
} else {
|
} else {
|
||||||
@@ -455,7 +454,7 @@ impl<D: DataProvider> FormEditor<D> {
|
|||||||
self.ui_state.ideal_cursor_column = 0;
|
self.ui_state.ideal_cursor_column = 0;
|
||||||
} else {
|
} else {
|
||||||
// Find first big_word in new field
|
// Find first big_word in new field
|
||||||
let first_big_word_pos = if new_text.chars().next().map_or(false, |c| !c.is_whitespace()) {
|
let first_big_word_pos = if new_text.chars().next().is_some_and(|c| !c.is_whitespace()) {
|
||||||
// Field starts with non-whitespace, go to position 0
|
// Field starts with non-whitespace, go to position 0
|
||||||
0
|
0
|
||||||
} else {
|
} else {
|
||||||
@@ -644,8 +643,8 @@ impl<D: DataProvider> FormEditor<D> {
|
|||||||
|
|
||||||
if current_text.is_empty() {
|
if current_text.is_empty() {
|
||||||
let current_field = self.ui_state.current_field;
|
let current_field = self.ui_state.current_field;
|
||||||
if self.move_up().is_ok() {
|
if self.move_up().is_ok()
|
||||||
if self.ui_state.current_field != current_field {
|
&& self.ui_state.current_field != current_field {
|
||||||
let new_text = self.current_text();
|
let new_text = self.current_text();
|
||||||
if !new_text.is_empty() {
|
if !new_text.is_empty() {
|
||||||
// Find first big_word end in new field
|
// Find first big_word end in new field
|
||||||
@@ -654,7 +653,6 @@ impl<D: DataProvider> FormEditor<D> {
|
|||||||
self.ui_state.ideal_cursor_column = last_big_word_end;
|
self.ui_state.ideal_cursor_column = last_big_word_end;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -664,8 +662,8 @@ impl<D: DataProvider> FormEditor<D> {
|
|||||||
// Only try to cross fields if we didn't move at all (stayed at same position)
|
// Only try to cross fields if we didn't move at all (stayed at same position)
|
||||||
if new_pos == current_pos {
|
if new_pos == current_pos {
|
||||||
let current_field = self.ui_state.current_field;
|
let current_field = self.ui_state.current_field;
|
||||||
if self.move_up().is_ok() {
|
if self.move_up().is_ok()
|
||||||
if self.ui_state.current_field != current_field {
|
&& self.ui_state.current_field != current_field {
|
||||||
let new_text = self.current_text();
|
let new_text = self.current_text();
|
||||||
if !new_text.is_empty() {
|
if !new_text.is_empty() {
|
||||||
let last_big_word_end = find_big_word_end(new_text, 0);
|
let last_big_word_end = find_big_word_end(new_text, 0);
|
||||||
@@ -673,7 +671,6 @@ impl<D: DataProvider> FormEditor<D> {
|
|||||||
self.ui_state.ideal_cursor_column = last_big_word_end;
|
self.ui_state.ideal_cursor_column = last_big_word_end;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Normal big_word movement within current field
|
// Normal big_word movement within current field
|
||||||
let is_edit_mode = self.ui_state.current_mode == AppMode::Edit;
|
let is_edit_mode = self.ui_state.current_mode == AppMode::Edit;
|
||||||
|
|||||||
@@ -149,9 +149,6 @@ fn calculate_dropdown_position(
|
|||||||
if dropdown_area.right() > frame_area.width {
|
if dropdown_area.right() > frame_area.width {
|
||||||
dropdown_area.x = frame_area.width.saturating_sub(dropdown_width);
|
dropdown_area.x = frame_area.width.saturating_sub(dropdown_width);
|
||||||
}
|
}
|
||||||
dropdown_area.x = dropdown_area.x.max(0);
|
|
||||||
dropdown_area.y = dropdown_area.y.max(0);
|
|
||||||
|
|
||||||
dropdown_area
|
dropdown_area
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
// src/suggestions/mod.rs
|
// src/suggestions/mod.rs
|
||||||
|
//! Suggestions subsystem - provider and optional GUI.
|
||||||
|
//!
|
||||||
|
//! Contains the suggestion provider types used by the editor and, when the GUI
|
||||||
|
//! feature is enabled, the rendering helpers for the suggestions dropdown.
|
||||||
|
|
||||||
pub mod state;
|
pub mod state;
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
|
|||||||
183
canvas/src/textarea/highlight/chunks.rs
Normal file
183
canvas/src/textarea/highlight/chunks.rs
Normal file
@@ -0,0 +1,183 @@
|
|||||||
|
// src/textarea/highlight/chunks.rs
|
||||||
|
use ratatui::text::{Line, Span};
|
||||||
|
use ratatui::style::Style;
|
||||||
|
use unicode_width::UnicodeWidthChar;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct StyledChunk {
|
||||||
|
pub text: String,
|
||||||
|
pub style: Style,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn display_width_chunks(chunks: &[StyledChunk]) -> u16 {
|
||||||
|
chunks
|
||||||
|
.iter()
|
||||||
|
.map(|c| {
|
||||||
|
c.text
|
||||||
|
.chars()
|
||||||
|
.map(|ch| UnicodeWidthChar::width(ch).unwrap_or(0) as u16)
|
||||||
|
.sum::<u16>()
|
||||||
|
})
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn slice_chunks_by_display_cols(
|
||||||
|
chunks: &[StyledChunk],
|
||||||
|
start_cols: u16,
|
||||||
|
max_cols: u16,
|
||||||
|
) -> Vec<StyledChunk> {
|
||||||
|
if max_cols == 0 {
|
||||||
|
return Vec::new();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut skipped: u16 = 0;
|
||||||
|
let mut taken: u16 = 0;
|
||||||
|
let mut out: Vec<StyledChunk> = Vec::new();
|
||||||
|
|
||||||
|
for ch in chunks {
|
||||||
|
if taken >= max_cols {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut acc = String::new();
|
||||||
|
|
||||||
|
for c in ch.text.chars() {
|
||||||
|
let w = UnicodeWidthChar::width(c).unwrap_or(0) as u16;
|
||||||
|
if skipped + w <= start_cols {
|
||||||
|
skipped += w;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if taken + w > max_cols {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
acc.push(c);
|
||||||
|
taken = taken.saturating_add(w);
|
||||||
|
if taken >= max_cols {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !acc.is_empty() {
|
||||||
|
out.push(StyledChunk {
|
||||||
|
text: acc,
|
||||||
|
style: ch.style,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clip_chunks_window_with_indicator_padded(
|
||||||
|
chunks: &[StyledChunk],
|
||||||
|
view_width: u16,
|
||||||
|
indicator: char,
|
||||||
|
start_cols: u16,
|
||||||
|
) -> Line<'static> {
|
||||||
|
if view_width == 0 {
|
||||||
|
return Line::from("");
|
||||||
|
}
|
||||||
|
|
||||||
|
let total = display_width_chunks(chunks);
|
||||||
|
let show_left = start_cols > 0;
|
||||||
|
let left_cols: u16 = if show_left { 1 } else { 0 };
|
||||||
|
|
||||||
|
let cap_with_right = view_width.saturating_sub(left_cols + 1);
|
||||||
|
let remaining = total.saturating_sub(start_cols);
|
||||||
|
let show_right = remaining > cap_with_right;
|
||||||
|
|
||||||
|
let max_visible = if show_right {
|
||||||
|
cap_with_right
|
||||||
|
} else {
|
||||||
|
view_width.saturating_sub(left_cols)
|
||||||
|
};
|
||||||
|
|
||||||
|
let visible = slice_chunks_by_display_cols(chunks, start_cols, max_visible);
|
||||||
|
let used_cols = left_cols + display_width_chunks(&visible);
|
||||||
|
|
||||||
|
let mut spans: Vec<Span> = Vec::new();
|
||||||
|
if show_left {
|
||||||
|
spans.push(Span::raw(indicator.to_string()));
|
||||||
|
}
|
||||||
|
for v in visible {
|
||||||
|
spans.push(Span::styled(v.text, v.style));
|
||||||
|
}
|
||||||
|
if show_right {
|
||||||
|
let right_pos = view_width.saturating_sub(1);
|
||||||
|
let filler = right_pos.saturating_sub(used_cols);
|
||||||
|
if filler > 0 {
|
||||||
|
spans.push(Span::raw(" ".repeat(filler as usize)));
|
||||||
|
}
|
||||||
|
spans.push(Span::raw(indicator.to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Line::from(spans)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn wrap_chunks_indented(
|
||||||
|
chunks: &[StyledChunk],
|
||||||
|
width: u16,
|
||||||
|
indent: u16,
|
||||||
|
) -> Vec<Line<'static>> {
|
||||||
|
if width == 0 {
|
||||||
|
return vec![Line::from("")];
|
||||||
|
}
|
||||||
|
let indent = indent.min(width.saturating_sub(1));
|
||||||
|
let cont_cap = width.saturating_sub(indent);
|
||||||
|
let indent_str = " ".repeat(indent as usize);
|
||||||
|
|
||||||
|
let mut lines: Vec<Line> = Vec::new();
|
||||||
|
let mut current_spans: Vec<Span> = Vec::new();
|
||||||
|
let mut used: u16 = 0;
|
||||||
|
let mut first_line = true;
|
||||||
|
|
||||||
|
// Fixed: Restructure to avoid borrow checker issues
|
||||||
|
for chunk in chunks {
|
||||||
|
let mut buf = String::new();
|
||||||
|
let mut buf_style = chunk.style;
|
||||||
|
|
||||||
|
for ch in chunk.text.chars() {
|
||||||
|
let w = UnicodeWidthChar::width(ch).unwrap_or(0) as u16;
|
||||||
|
let cap = if first_line { width } else { cont_cap };
|
||||||
|
|
||||||
|
if used > 0 && used.saturating_add(w) >= cap {
|
||||||
|
if !buf.is_empty() {
|
||||||
|
current_spans.push(Span::styled(buf.clone(), buf_style));
|
||||||
|
buf.clear();
|
||||||
|
}
|
||||||
|
lines.push(Line::from(current_spans));
|
||||||
|
current_spans = Vec::new();
|
||||||
|
first_line = false;
|
||||||
|
used = 0;
|
||||||
|
|
||||||
|
// Add indent directly instead of using closure
|
||||||
|
if !first_line && indent > 0 {
|
||||||
|
current_spans.push(Span::raw(indent_str.clone()));
|
||||||
|
used = indent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !buf.is_empty() && buf_style != chunk.style {
|
||||||
|
current_spans.push(Span::styled(buf.clone(), buf_style));
|
||||||
|
buf.clear();
|
||||||
|
}
|
||||||
|
buf_style = chunk.style;
|
||||||
|
|
||||||
|
// Add indent if needed
|
||||||
|
if used == 0 && !first_line && indent > 0 {
|
||||||
|
current_spans.push(Span::raw(indent_str.clone()));
|
||||||
|
used = indent;
|
||||||
|
}
|
||||||
|
|
||||||
|
buf.push(ch);
|
||||||
|
used = used.saturating_add(w);
|
||||||
|
}
|
||||||
|
|
||||||
|
if !buf.is_empty() {
|
||||||
|
current_spans.push(Span::styled(buf, buf_style));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lines.push(Line::from(current_spans));
|
||||||
|
lines
|
||||||
|
}
|
||||||
294
canvas/src/textarea/highlight/engine.rs
Normal file
294
canvas/src/textarea/highlight/engine.rs
Normal file
@@ -0,0 +1,294 @@
|
|||||||
|
// src/textarea/highlight/engine.rs
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
use std::collections::hash_map::DefaultHasher;
|
||||||
|
use std::hash::{Hash, Hasher};
|
||||||
|
|
||||||
|
use ratatui::style::{Modifier, Style};
|
||||||
|
use syntect::{
|
||||||
|
highlighting::{
|
||||||
|
HighlightIterator, HighlightState, Highlighter, Style as SynStyle, Theme, ThemeSet,
|
||||||
|
},
|
||||||
|
parsing::{ParseState, ScopeStack, SyntaxReference, SyntaxSet},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::data_provider::DataProvider;
|
||||||
|
use super::chunks::StyledChunk;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct SyntectEngine {
|
||||||
|
ps: SyntaxSet,
|
||||||
|
ts: ThemeSet,
|
||||||
|
theme_name: String,
|
||||||
|
syntax_name: Option<String>,
|
||||||
|
// Cached parser state (after line i)
|
||||||
|
parse_after: Vec<ParseState>,
|
||||||
|
// Cached scope stack (after line i)
|
||||||
|
stack_after: Vec<ScopeStack>,
|
||||||
|
// Hash of line contents to detect edits
|
||||||
|
line_hashes: Vec<u64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for SyntectEngine {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SyntectEngine {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
let ps = SyntaxSet::load_defaults_newlines();
|
||||||
|
let ts = ThemeSet::load_defaults();
|
||||||
|
Self {
|
||||||
|
ps,
|
||||||
|
ts,
|
||||||
|
theme_name: "InspiredGitHub".to_string(),
|
||||||
|
syntax_name: None,
|
||||||
|
parse_after: Vec::new(),
|
||||||
|
stack_after: Vec::new(),
|
||||||
|
line_hashes: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clear(&mut self) {
|
||||||
|
self.parse_after.clear();
|
||||||
|
self.stack_after.clear();
|
||||||
|
self.line_hashes.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_theme(&mut self, theme_name: &str) -> bool {
|
||||||
|
if self.ts.themes.contains_key(theme_name) {
|
||||||
|
self.theme_name = theme_name.to_string();
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_syntax_by_name(&mut self, name: &str) -> bool {
|
||||||
|
if self.ps.find_syntax_by_name(name).is_some() {
|
||||||
|
self.syntax_name = Some(name.to_string());
|
||||||
|
self.clear();
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_syntax_by_extension(&mut self, ext: &str) -> bool {
|
||||||
|
if let Some(s) = self.ps.find_syntax_by_extension(ext) {
|
||||||
|
self.syntax_name = Some(s.name.clone());
|
||||||
|
self.clear();
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn invalidate_from(&mut self, line_idx: usize) {
|
||||||
|
if line_idx < self.parse_after.len() {
|
||||||
|
self.parse_after.truncate(line_idx);
|
||||||
|
}
|
||||||
|
if line_idx < self.stack_after.len() {
|
||||||
|
self.stack_after.truncate(line_idx);
|
||||||
|
}
|
||||||
|
if line_idx < self.line_hashes.len() {
|
||||||
|
self.line_hashes.truncate(line_idx);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn on_insert_line(&mut self, at: usize) {
|
||||||
|
self.invalidate_from(at);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn on_delete_line(&mut self, at: usize) {
|
||||||
|
self.invalidate_from(at);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn theme(&self) -> &Theme {
|
||||||
|
self.ts
|
||||||
|
.themes
|
||||||
|
.get(&self.theme_name)
|
||||||
|
.expect("theme exists")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn syntax_ref(&self) -> &SyntaxReference {
|
||||||
|
if let Some(name) = &self.syntax_name {
|
||||||
|
if let Some(s) = self.ps.find_syntax_by_name(name) {
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.ps.find_syntax_plain_text()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn map_syntect_style(s: SynStyle) -> Style {
|
||||||
|
let fg =
|
||||||
|
ratatui::style::Color::Rgb(s.foreground.r, s.foreground.g, s.foreground.b);
|
||||||
|
let mut st = Style::default().fg(fg);
|
||||||
|
use syntect::highlighting::FontStyle;
|
||||||
|
if s.font_style.contains(FontStyle::BOLD) {
|
||||||
|
st = st.add_modifier(Modifier::BOLD);
|
||||||
|
}
|
||||||
|
if s.font_style.contains(FontStyle::UNDERLINE) {
|
||||||
|
st = st.add_modifier(Modifier::UNDERLINED);
|
||||||
|
}
|
||||||
|
if s.font_style.contains(FontStyle::ITALIC) {
|
||||||
|
st = st.add_modifier(Modifier::ITALIC);
|
||||||
|
}
|
||||||
|
st
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash_line(s: &str) -> u64 {
|
||||||
|
let mut h = DefaultHasher::new();
|
||||||
|
s.hash(&mut h);
|
||||||
|
h.finish()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify cached chain up to the nearest trusted predecessor of line_idx,
|
||||||
|
// using the provider to fetch the current lines.
|
||||||
|
fn verify_and_truncate_before(&mut self, line_idx: usize, provider: &dyn DataProvider) {
|
||||||
|
let mut k = std::cmp::min(line_idx, self.parse_after.len());
|
||||||
|
while k > 0 {
|
||||||
|
let j = k - 1;
|
||||||
|
let curr = Self::hash_line(provider.field_value(j));
|
||||||
|
if self.line_hashes.get(j) == Some(&curr) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
self.invalidate_from(j);
|
||||||
|
k = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure we have parser + stack for lines [0..line_idx)
|
||||||
|
fn ensure_state_before(&mut self, line_idx: usize, provider: &dyn DataProvider) {
|
||||||
|
if line_idx == 0 || self.parse_after.len() >= line_idx {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let syntax = self.syntax_ref();
|
||||||
|
let theme = self.theme().clone(); // Clone to avoid borrow conflicts
|
||||||
|
let highlighter = Highlighter::new(&theme);
|
||||||
|
|
||||||
|
let mut ps = if self.parse_after.is_empty() {
|
||||||
|
ParseState::new(syntax)
|
||||||
|
} else {
|
||||||
|
self.parse_after[self.parse_after.len() - 1].clone()
|
||||||
|
};
|
||||||
|
let mut stack = if self.stack_after.is_empty() {
|
||||||
|
ScopeStack::new()
|
||||||
|
} else {
|
||||||
|
self.stack_after[self.stack_after.len() - 1].clone()
|
||||||
|
};
|
||||||
|
|
||||||
|
let start = self.parse_after.len();
|
||||||
|
for i in start..line_idx {
|
||||||
|
let s = provider.field_value(i);
|
||||||
|
|
||||||
|
// Fix: parse_line takes 2 arguments: line and &SyntaxSet
|
||||||
|
let ops = ps.parse_line(s, &self.ps).unwrap_or_default();
|
||||||
|
|
||||||
|
// Fix: HighlightState::new requires &Highlighter and ScopeStack
|
||||||
|
let mut highlight_state = HighlightState::new(&highlighter, stack.clone());
|
||||||
|
|
||||||
|
// Fix: HighlightIterator::new expects &mut HighlightState as first parameter
|
||||||
|
let it = HighlightIterator::new(&mut highlight_state, &ops[..], s, &highlighter);
|
||||||
|
for (_style, _text) in it {
|
||||||
|
// Iterate to apply ops; we don't need the tokens here.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the stack from the highlight state
|
||||||
|
stack = highlight_state.path.clone();
|
||||||
|
|
||||||
|
let h = Self::hash_line(s);
|
||||||
|
|
||||||
|
self.parse_after.push(ps.clone());
|
||||||
|
self.stack_after.push(stack.clone());
|
||||||
|
if i >= self.line_hashes.len() {
|
||||||
|
self.line_hashes.push(h);
|
||||||
|
} else {
|
||||||
|
self.line_hashes[i] = h;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Highlight a single line using cached state; update caches for this line.
|
||||||
|
pub fn highlight_line_cached(
|
||||||
|
&mut self,
|
||||||
|
line_idx: usize,
|
||||||
|
line: &str,
|
||||||
|
provider: &dyn DataProvider,
|
||||||
|
) -> Vec<StyledChunk> {
|
||||||
|
// Auto-detect prior changes and truncate cache if needed
|
||||||
|
self.verify_and_truncate_before(line_idx, provider);
|
||||||
|
// Precompute states up to line_idx
|
||||||
|
self.ensure_state_before(line_idx, provider);
|
||||||
|
|
||||||
|
let syntax = self.syntax_ref();
|
||||||
|
let theme = self.theme().clone(); // Clone to avoid borrow conflicts
|
||||||
|
let highlighter = Highlighter::new(&theme);
|
||||||
|
|
||||||
|
let mut ps = if line_idx == 0 {
|
||||||
|
ParseState::new(syntax)
|
||||||
|
} else if self.parse_after.len() >= line_idx {
|
||||||
|
self.parse_after[line_idx - 1].clone()
|
||||||
|
} else {
|
||||||
|
ParseState::new(syntax)
|
||||||
|
};
|
||||||
|
|
||||||
|
let stack = if line_idx == 0 {
|
||||||
|
ScopeStack::new()
|
||||||
|
} else if self.stack_after.len() >= line_idx {
|
||||||
|
self.stack_after[line_idx - 1].clone()
|
||||||
|
} else {
|
||||||
|
ScopeStack::new()
|
||||||
|
};
|
||||||
|
|
||||||
|
// Fix: parse_line takes 2 arguments: line and &SyntaxSet
|
||||||
|
let ops = ps.parse_line(line, &self.ps).unwrap_or_default();
|
||||||
|
|
||||||
|
// Fix: HighlightState::new requires &Highlighter and ScopeStack
|
||||||
|
let mut highlight_state = HighlightState::new(&highlighter, stack);
|
||||||
|
|
||||||
|
// Fix: HighlightIterator::new expects &mut HighlightState as first parameter
|
||||||
|
let iter = HighlightIterator::new(&mut highlight_state, &ops[..], line, &highlighter);
|
||||||
|
|
||||||
|
let mut out: Vec<StyledChunk> = Vec::new();
|
||||||
|
for (syn_style, slice) in iter {
|
||||||
|
if slice.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let text = slice.trim_end_matches('\n').to_string();
|
||||||
|
if text.is_empty() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
out.push(StyledChunk {
|
||||||
|
text,
|
||||||
|
style: Self::map_syntect_style(syn_style),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update caches for this line (state after this line)
|
||||||
|
let h = Self::hash_line(line);
|
||||||
|
if line_idx >= self.parse_after.len() {
|
||||||
|
self.parse_after.push(ps);
|
||||||
|
} else {
|
||||||
|
self.parse_after[line_idx] = ps;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update stack from highlight state
|
||||||
|
let final_stack = highlight_state.path.clone();
|
||||||
|
if line_idx >= self.stack_after.len() {
|
||||||
|
self.stack_after.push(final_stack);
|
||||||
|
} else {
|
||||||
|
self.stack_after[line_idx] = final_stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
if line_idx >= self.line_hashes.len() {
|
||||||
|
self.line_hashes.push(h);
|
||||||
|
} else {
|
||||||
|
self.line_hashes[line_idx] = h;
|
||||||
|
}
|
||||||
|
|
||||||
|
out
|
||||||
|
}
|
||||||
|
}
|
||||||
18
canvas/src/textarea/highlight/mod.rs
Normal file
18
canvas/src/textarea/highlight/mod.rs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
// src/textarea/highlight/mod.rs
|
||||||
|
#[cfg(all(feature = "syntect", feature = "gui"))]
|
||||||
|
pub mod engine;
|
||||||
|
#[cfg(all(feature = "syntect", feature = "gui"))]
|
||||||
|
pub mod chunks;
|
||||||
|
#[cfg(all(feature = "syntect", feature = "gui"))]
|
||||||
|
pub mod state;
|
||||||
|
#[cfg(all(feature = "syntect", feature = "gui"))]
|
||||||
|
pub mod widget;
|
||||||
|
|
||||||
|
#[cfg(all(feature = "syntect", feature = "gui"))]
|
||||||
|
pub use engine::SyntectEngine;
|
||||||
|
#[cfg(all(feature = "syntect", feature = "gui"))]
|
||||||
|
pub use chunks::StyledChunk;
|
||||||
|
#[cfg(all(feature = "syntect", feature = "gui"))]
|
||||||
|
pub use state::TextAreaSyntaxState;
|
||||||
|
#[cfg(all(feature = "syntect", feature = "gui"))]
|
||||||
|
pub use widget::TextAreaSyntax;
|
||||||
45
canvas/src/textarea/highlight/state.rs
Normal file
45
canvas/src/textarea/highlight/state.rs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
// src/textarea/highlight/state.rs
|
||||||
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
|
use super::engine::SyntectEngine;
|
||||||
|
use crate::textarea::state::TextAreaState;
|
||||||
|
|
||||||
|
// Remove Debug derive since TextAreaState doesn't implement Debug
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct TextAreaSyntaxState {
|
||||||
|
pub textarea: TextAreaState,
|
||||||
|
pub engine: SyntectEngine,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TextAreaSyntaxState {
|
||||||
|
pub fn from_text<S: Into<String>>(text: S) -> Self {
|
||||||
|
let mut s = Self::default();
|
||||||
|
s.textarea.set_text(text);
|
||||||
|
s
|
||||||
|
}
|
||||||
|
|
||||||
|
// Optional: convenience setters
|
||||||
|
pub fn set_syntax_theme(&mut self, theme: &str) -> bool {
|
||||||
|
self.engine.set_theme(theme)
|
||||||
|
}
|
||||||
|
pub fn set_syntax_by_name(&mut self, name: &str) -> bool {
|
||||||
|
self.engine.set_syntax_by_name(name)
|
||||||
|
}
|
||||||
|
pub fn set_syntax_by_extension(&mut self, ext: &str) -> bool {
|
||||||
|
self.engine.set_syntax_by_extension(ext)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Deref for TextAreaSyntaxState {
|
||||||
|
type Target = TextAreaState;
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.textarea
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DerefMut for TextAreaSyntaxState {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.textarea
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
211
canvas/src/textarea/highlight/widget.rs
Normal file
211
canvas/src/textarea/highlight/widget.rs
Normal file
@@ -0,0 +1,211 @@
|
|||||||
|
// src/textarea/highlight/widget.rs
|
||||||
|
use ratatui::{
|
||||||
|
buffer::Buffer,
|
||||||
|
layout::{Alignment, Rect},
|
||||||
|
style::Style,
|
||||||
|
text::{Line, Span},
|
||||||
|
widgets::{Block, BorderType, Borders, Paragraph, StatefulWidget, Widget},
|
||||||
|
};
|
||||||
|
|
||||||
|
use unicode_width::UnicodeWidthChar;
|
||||||
|
|
||||||
|
use super::chunks::{
|
||||||
|
clip_chunks_window_with_indicator_padded,
|
||||||
|
wrap_chunks_indented,
|
||||||
|
};
|
||||||
|
use super::state::TextAreaSyntaxState;
|
||||||
|
|
||||||
|
use crate::data_provider::DataProvider;
|
||||||
|
use crate::textarea::state::{
|
||||||
|
compute_h_scroll_with_padding, count_wrapped_rows_indented, TextOverflowMode,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct TextAreaSyntax<'a> {
|
||||||
|
pub block: Option<Block<'a>>,
|
||||||
|
pub style: Style,
|
||||||
|
pub border_type: BorderType,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Default for TextAreaSyntax<'a> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
block: Some(
|
||||||
|
Block::default()
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_type(BorderType::Rounded),
|
||||||
|
),
|
||||||
|
style: Style::default(),
|
||||||
|
border_type: BorderType::Rounded,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> TextAreaSyntax<'a> {
|
||||||
|
pub fn block(mut self, block: Block<'a>) -> Self {
|
||||||
|
self.block = Some(block);
|
||||||
|
self
|
||||||
|
}
|
||||||
|
pub fn style(mut self, style: Style) -> Self {
|
||||||
|
self.style = style;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
pub fn border_type(mut self, ty: BorderType) -> Self {
|
||||||
|
self.border_type = ty;
|
||||||
|
if let Some(b) = &mut self.block {
|
||||||
|
*b = b.clone().border_type(ty);
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn display_width(s: &str) -> u16 {
|
||||||
|
s.chars()
|
||||||
|
.map(|c| UnicodeWidthChar::width(c).unwrap_or(0) as u16)
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn display_cols_up_to(s: &str, char_count: usize) -> u16 {
|
||||||
|
let mut cols: u16 = 0;
|
||||||
|
for (i, ch) in s.chars().enumerate() {
|
||||||
|
if i >= char_count {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
cols = cols.saturating_add(UnicodeWidthChar::width(ch).unwrap_or(0) as u16);
|
||||||
|
}
|
||||||
|
cols
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolve_start_line_and_intra_indented(
|
||||||
|
state: &TextAreaSyntaxState,
|
||||||
|
inner: Rect,
|
||||||
|
) -> (usize, u16) {
|
||||||
|
let provider = state.textarea.editor.data_provider();
|
||||||
|
let total = provider.line_count();
|
||||||
|
|
||||||
|
if total == 0 {
|
||||||
|
return (0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
let wrap = matches!(state.textarea.overflow_mode, TextOverflowMode::Wrap);
|
||||||
|
let width = inner.width;
|
||||||
|
let target_vis = state.textarea.scroll_y;
|
||||||
|
|
||||||
|
if !wrap {
|
||||||
|
let start = (target_vis as usize).min(total);
|
||||||
|
return (start, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
let indent = state.textarea.wrap_indent_cols;
|
||||||
|
|
||||||
|
let mut acc: u16 = 0;
|
||||||
|
for i in 0..total {
|
||||||
|
let s = provider.field_value(i);
|
||||||
|
let rows = count_wrapped_rows_indented(s, width, indent);
|
||||||
|
if acc.saturating_add(rows) > target_vis {
|
||||||
|
let intra = target_vis.saturating_sub(acc);
|
||||||
|
return (i, intra);
|
||||||
|
}
|
||||||
|
acc = acc.saturating_add(rows);
|
||||||
|
}
|
||||||
|
|
||||||
|
(total.saturating_sub(1), 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> StatefulWidget for TextAreaSyntax<'a> {
|
||||||
|
type State = TextAreaSyntaxState;
|
||||||
|
|
||||||
|
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
|
||||||
|
// Reuse existing scroll logic
|
||||||
|
state.textarea.ensure_visible(area, self.block.as_ref());
|
||||||
|
|
||||||
|
let inner = if let Some(b) = &self.block {
|
||||||
|
b.clone().render(area, buf);
|
||||||
|
b.inner(area)
|
||||||
|
} else {
|
||||||
|
area
|
||||||
|
};
|
||||||
|
|
||||||
|
let edited_now = state.textarea.take_edited_flag();
|
||||||
|
|
||||||
|
let wrap_mode = matches!(state.textarea.overflow_mode, TextOverflowMode::Wrap);
|
||||||
|
let provider = state.textarea.editor.data_provider();
|
||||||
|
let total = provider.line_count();
|
||||||
|
|
||||||
|
let (start, intra) = resolve_start_line_and_intra_indented(state, inner);
|
||||||
|
|
||||||
|
let mut display_lines: Vec<Line> = Vec::new();
|
||||||
|
|
||||||
|
if total == 0 || start >= total {
|
||||||
|
if let Some(ph) = &state.textarea.placeholder {
|
||||||
|
display_lines.push(Line::from(Span::raw(ph.clone())));
|
||||||
|
}
|
||||||
|
} else if wrap_mode {
|
||||||
|
let mut rows_left = inner.height;
|
||||||
|
let indent = state.textarea.wrap_indent_cols;
|
||||||
|
|
||||||
|
let mut i = start;
|
||||||
|
while i < total && rows_left > 0 {
|
||||||
|
let s = provider.field_value(i);
|
||||||
|
|
||||||
|
let chunks = state
|
||||||
|
.engine
|
||||||
|
.highlight_line_cached(i, s, provider);
|
||||||
|
|
||||||
|
let lines = wrap_chunks_indented(&chunks, inner.width, indent);
|
||||||
|
let skip = if i == start { intra as usize } else { 0 };
|
||||||
|
for l in lines.into_iter().skip(skip) {
|
||||||
|
display_lines.push(l);
|
||||||
|
rows_left = rows_left.saturating_sub(1);
|
||||||
|
if rows_left == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let end = (start.saturating_add(inner.height as usize)).min(total);
|
||||||
|
|
||||||
|
for i in start..end {
|
||||||
|
let s = provider.field_value(i);
|
||||||
|
|
||||||
|
let chunks = state.engine.highlight_line_cached(i, s, provider);
|
||||||
|
|
||||||
|
let fits = display_width(s) <= inner.width;
|
||||||
|
let start_cols = if i == state.textarea.current_field() {
|
||||||
|
let col_idx = state.textarea.display_cursor_position();
|
||||||
|
let cursor_cols = display_cols_up_to(s, col_idx);
|
||||||
|
let (target_h, _left_cols) =
|
||||||
|
compute_h_scroll_with_padding(cursor_cols, inner.width);
|
||||||
|
|
||||||
|
if fits {
|
||||||
|
if edited_now {
|
||||||
|
target_h
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
target_h.max(state.textarea.h_scroll)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
};
|
||||||
|
|
||||||
|
if let TextOverflowMode::Indicator { ch } = state.textarea.overflow_mode {
|
||||||
|
display_lines.push(clip_chunks_window_with_indicator_padded(
|
||||||
|
&chunks,
|
||||||
|
inner.width,
|
||||||
|
ch,
|
||||||
|
start_cols,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let p = Paragraph::new(display_lines)
|
||||||
|
.alignment(Alignment::Left)
|
||||||
|
.style(self.style);
|
||||||
|
|
||||||
|
p.render(inner, buf);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,12 +1,14 @@
|
|||||||
// src/textarea/mod.rs
|
// src/textarea/mod.rs
|
||||||
|
//! Text area convenience exports.
|
||||||
|
|
||||||
pub mod provider;
|
pub mod provider;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
pub mod widget;
|
pub mod widget;
|
||||||
|
|
||||||
#[cfg(feature = "keymaps")]
|
#[cfg(all(feature = "syntect", feature = "gui"))]
|
||||||
pub mod commands_impl;
|
pub mod highlight;
|
||||||
|
|
||||||
pub use provider::TextAreaProvider;
|
pub use provider::TextAreaProvider;
|
||||||
pub use state::{TextAreaEditor, TextAreaState, TextOverflowMode};
|
pub use state::{TextAreaEditor, TextAreaState, TextOverflowMode};
|
||||||
|
|||||||
@@ -1,121 +1,218 @@
|
|||||||
// src/textarea/provider.rs
|
// src/textarea/provider.rs
|
||||||
use crate::DataProvider;
|
use crate::DataProvider;
|
||||||
|
use once_cell::unsync::OnceCell;
|
||||||
|
use ropey::Rope;
|
||||||
|
use std::io::{self, BufReader, Read};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug)] // Clone removed: OnceCell<String> is not Clone
|
||||||
pub struct TextAreaProvider {
|
pub struct TextAreaProvider {
|
||||||
lines: Vec<String>,
|
rope: Rope,
|
||||||
name: String,
|
name: String,
|
||||||
|
// Lazy per-line cache; only lines that are actually used get materialized.
|
||||||
|
// This keeps memory low even for very large files.
|
||||||
|
line_cache: Vec<OnceCell<String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for TextAreaProvider {
|
impl Default for TextAreaProvider {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
|
let rope = Rope::from_str("");
|
||||||
Self {
|
Self {
|
||||||
lines: vec![String::new()],
|
rope,
|
||||||
name: "Text".to_string(),
|
name: "Text".to_string(),
|
||||||
|
line_cache: vec![OnceCell::new()], // at least 1 logical line
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TextAreaProvider {
|
impl TextAreaProvider {
|
||||||
pub fn from_text<S: Into<String>>(text: S) -> Self {
|
pub fn from_text<S: Into<String>>(text: S) -> Self {
|
||||||
let text = text.into();
|
let s = text.into();
|
||||||
let mut lines: Vec<String> =
|
let rope = Rope::from_str(&s);
|
||||||
text.split('\n').map(|s| s.to_string()).collect();
|
let lines = rope.len_lines().max(1);
|
||||||
if lines.is_empty() {
|
|
||||||
lines.push(String::new());
|
|
||||||
}
|
|
||||||
Self {
|
Self {
|
||||||
lines,
|
rope,
|
||||||
name: "Text".to_string(),
|
name: "Text".to_string(),
|
||||||
|
line_cache: vec![(); lines].into_iter().map(|_| OnceCell::new()).collect(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_text(&self) -> String {
|
pub fn to_text(&self) -> String {
|
||||||
self.lines.join("\n")
|
self.rope.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_file<P: AsRef<Path>>(path: P) -> io::Result<Self> {
|
||||||
|
let f = std::fs::File::open(path)?;
|
||||||
|
let mut reader = BufReader::new(f);
|
||||||
|
Self::from_reader(&mut reader)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_reader<R: Read>(reader: &mut R) -> io::Result<Self> {
|
||||||
|
let rope = Rope::from_reader(reader)?;
|
||||||
|
let lines = rope.len_lines().max(1);
|
||||||
|
Ok(Self {
|
||||||
|
rope,
|
||||||
|
name: "Text".to_string(),
|
||||||
|
line_cache: vec![(); lines].into_iter().map(|_| OnceCell::new()).collect(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_text<S: Into<String>>(&mut self, text: S) {
|
pub fn set_text<S: Into<String>>(&mut self, text: S) {
|
||||||
let text = text.into();
|
let s = text.into();
|
||||||
self.lines = text.split('\n').map(|s| s.to_string()).collect();
|
self.rope = Rope::from_str(&s);
|
||||||
if self.lines.is_empty() {
|
self.resize_cache();
|
||||||
self.lines.push(String::new());
|
self.invalidate_cache_from(0);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn line_count(&self) -> usize {
|
pub fn line_count(&self) -> usize {
|
||||||
self.lines.len()
|
self.rope.len_lines().max(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resize_cache(&mut self) {
|
||||||
|
let want = self.line_count();
|
||||||
|
if self.line_cache.len() < want {
|
||||||
|
self.line_cache
|
||||||
|
.extend((0..(want - self.line_cache.len())).map(|_| OnceCell::new()));
|
||||||
|
} else if self.line_cache.len() > want {
|
||||||
|
self.line_cache.truncate(want);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn invalidate_cache_from(&mut self, line_idx: usize) {
|
||||||
|
self.resize_cache();
|
||||||
|
if line_idx < self.line_cache.len() {
|
||||||
|
for cell in &mut self.line_cache[line_idx..] {
|
||||||
|
let _ = cell.take();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn char_to_byte_index(s: &str, char_idx: usize) -> usize {
|
fn line_bounds_chars(&self, line_idx: usize) -> (usize, usize) {
|
||||||
s.char_indices()
|
// Returns [start, end) in char indices for content only (excluding newline).
|
||||||
.nth(char_idx)
|
let total_lines = self.line_count();
|
||||||
.map(|(i, _)| i)
|
let start = self.rope.line_to_char(line_idx);
|
||||||
.unwrap_or_else(|| s.len())
|
let end_exclusive = if line_idx + 1 < total_lines {
|
||||||
|
// Next line start is at the char index right after the newline.
|
||||||
|
// Exclude the newline itself by not including it in the range.
|
||||||
|
self.rope.line_to_char(line_idx + 1) - 1
|
||||||
|
} else {
|
||||||
|
self.rope.len_chars()
|
||||||
|
};
|
||||||
|
(start, end_exclusive)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn split_line_at(&mut self, line_idx: usize, at_char: usize) -> usize {
|
fn line_content_len_chars(&self, line_idx: usize) -> usize {
|
||||||
if line_idx >= self.lines.len() {
|
let slice = self.rope.line(line_idx);
|
||||||
return self.lines.len().saturating_sub(1);
|
let mut len = slice.len_chars();
|
||||||
|
if line_idx + 1 < self.line_count() && len > 0 {
|
||||||
|
// Non-final lines include a trailing '\n' char in rope; exclude it.
|
||||||
|
len -= 1;
|
||||||
}
|
}
|
||||||
let line = &mut self.lines[line_idx];
|
len
|
||||||
let byte_idx = Self::char_to_byte_index(line, at_char);
|
|
||||||
let right = line[byte_idx..].to_string();
|
|
||||||
line.truncate(byte_idx);
|
|
||||||
let insert_at = line_idx + 1;
|
|
||||||
self.lines.insert(insert_at, right);
|
|
||||||
insert_at
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn compute_line_string(&self, index: usize) -> String {
|
||||||
|
let mut s = self.rope.line(index).to_string();
|
||||||
|
// Trim trailing newline/CR if present (for non-final lines)
|
||||||
|
if s.ends_with('\n') {
|
||||||
|
s.pop();
|
||||||
|
if s.ends_with('\r') {
|
||||||
|
s.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s
|
||||||
|
}
|
||||||
|
|
||||||
|
// --------------------------
|
||||||
|
// Editing helpers for TextAreaState (unchanged API)
|
||||||
|
// --------------------------
|
||||||
|
|
||||||
|
/// Split line at a character offset (within that line).
|
||||||
|
/// Returns the index of the newly created line (line_idx + 1).
|
||||||
|
pub fn split_line_at(&mut self, line_idx: usize, at_char: usize) -> usize {
|
||||||
|
let lines = self.line_count();
|
||||||
|
let clamped_line = line_idx.min(lines.saturating_sub(1));
|
||||||
|
let (start, end) = self.line_bounds_chars(clamped_line);
|
||||||
|
let line_len = end.saturating_sub(start);
|
||||||
|
let at = at_char.min(line_len);
|
||||||
|
|
||||||
|
let insert_at = start + at;
|
||||||
|
self.rope.insert(insert_at, "\n"); // rope insert at char index
|
||||||
|
|
||||||
|
self.resize_cache();
|
||||||
|
self.invalidate_cache_from(clamped_line);
|
||||||
|
clamped_line + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Join current line with the next by removing the newline.
|
||||||
|
/// Returns Some(new_cursor_col_on_merged_line) or None if no next line.
|
||||||
pub fn join_with_next(&mut self, line_idx: usize) -> Option<usize> {
|
pub fn join_with_next(&mut self, line_idx: usize) -> Option<usize> {
|
||||||
if line_idx + 1 >= self.lines.len() {
|
if line_idx + 1 >= self.line_count() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let left_len = self.lines[line_idx].chars().count();
|
let newline_pos = self.rope.line_to_char(line_idx + 1) - 1; // index of '\n'
|
||||||
let right = self.lines.remove(line_idx + 1);
|
let left_len = self.line_content_len_chars(line_idx);
|
||||||
self.lines[line_idx].push_str(&right);
|
self.rope.remove(newline_pos..newline_pos + 1); // remove the newline
|
||||||
|
|
||||||
|
self.resize_cache();
|
||||||
|
self.invalidate_cache_from(line_idx);
|
||||||
Some(left_len)
|
Some(left_len)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn join_with_prev(
|
/// Join current line with the previous by removing the previous newline.
|
||||||
&mut self,
|
/// Returns Some((new_prev_index, cursor_col)) or None if at line 0.
|
||||||
line_idx: usize,
|
pub fn join_with_prev(&mut self, line_idx: usize) -> Option<(usize, usize)> {
|
||||||
) -> Option<(usize, usize)> {
|
if line_idx == 0 || line_idx >= self.line_count() {
|
||||||
if line_idx == 0 || line_idx >= self.lines.len() {
|
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let prev_idx = line_idx - 1;
|
let prev_idx = line_idx - 1;
|
||||||
let prev_len = self.lines[prev_idx].chars().count();
|
let prev_len = self.line_content_len_chars(prev_idx);
|
||||||
let curr = self.lines.remove(line_idx);
|
let newline_pos = self.rope.line_to_char(line_idx) - 1; // index of '\n' before current line
|
||||||
self.lines[prev_idx].push_str(&curr);
|
self.rope.remove(newline_pos..newline_pos + 1);
|
||||||
|
|
||||||
|
self.resize_cache();
|
||||||
|
self.invalidate_cache_from(prev_idx);
|
||||||
Some((prev_idx, prev_len))
|
Some((prev_idx, prev_len))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_blank_line_after(&mut self, idx: usize) -> usize {
|
/// Insert an empty line after given index.
|
||||||
let clamped = idx.min(self.lines.len());
|
/// Returns the index of the inserted blank line (line_idx + 1).
|
||||||
let insert_at = if clamped >= self.lines.len() {
|
pub fn insert_blank_line_after(&mut self, line_idx: usize) -> usize {
|
||||||
self.lines.len()
|
let lines = self.line_count();
|
||||||
|
let clamped = line_idx.min(lines.saturating_sub(1));
|
||||||
|
let pos = if clamped + 1 < lines {
|
||||||
|
self.rope.line_to_char(clamped + 1)
|
||||||
} else {
|
} else {
|
||||||
clamped + 1
|
self.rope.len_chars()
|
||||||
};
|
};
|
||||||
if insert_at == self.lines.len() {
|
self.rope.insert(pos, "\n");
|
||||||
self.lines.push(String::new());
|
|
||||||
} else {
|
self.resize_cache();
|
||||||
self.lines.insert(insert_at, String::new());
|
self.invalidate_cache_from(clamped);
|
||||||
}
|
clamped + 1
|
||||||
insert_at
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_blank_line_before(&mut self, idx: usize) -> usize {
|
/// Insert an empty line before given index.
|
||||||
let insert_at = idx.min(self.lines.len());
|
/// Returns the index of the inserted blank line (line_idx).
|
||||||
self.lines.insert(insert_at, String::new());
|
pub fn insert_blank_line_before(&mut self, line_idx: usize) -> usize {
|
||||||
insert_at
|
let clamped = line_idx.min(self.line_count());
|
||||||
|
let pos = if clamped < self.line_count() {
|
||||||
|
self.rope.line_to_char(clamped)
|
||||||
|
} else {
|
||||||
|
self.rope.len_chars()
|
||||||
|
};
|
||||||
|
self.rope.insert(pos, "\n");
|
||||||
|
|
||||||
|
self.resize_cache();
|
||||||
|
self.invalidate_cache_from(clamped);
|
||||||
|
clamped
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DataProvider for TextAreaProvider {
|
impl DataProvider for TextAreaProvider {
|
||||||
fn field_count(&self) -> usize {
|
fn field_count(&self) -> usize {
|
||||||
self.lines.len()
|
self.line_count()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn field_name(&self, _index: usize) -> &str {
|
fn field_name(&self, _index: usize) -> &str {
|
||||||
@@ -123,12 +220,31 @@ impl DataProvider for TextAreaProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn field_value(&self, index: usize) -> &str {
|
fn field_value(&self, index: usize) -> &str {
|
||||||
self.lines.get(index).map(|s| s.as_str()).unwrap_or("")
|
if index >= self.line_cache.len() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
let cell = &self.line_cache[index];
|
||||||
|
// Fill lazily on first read, from &self (no &mut needed).
|
||||||
|
let s_ref = cell.get_or_init(|| self.compute_line_string(index));
|
||||||
|
s_ref.as_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_field_value(&mut self, index: usize, value: String) {
|
fn set_field_value(&mut self, index: usize, value: String) {
|
||||||
if index < self.lines.len() {
|
if index >= self.line_count() {
|
||||||
self.lines[index] = value;
|
return;
|
||||||
|
}
|
||||||
|
// Enforce single-line invariant: strip embedded newlines
|
||||||
|
let clean = value.replace('\n', "");
|
||||||
|
|
||||||
|
let (start, end) = self.line_bounds_chars(index);
|
||||||
|
self.rope.remove(start..end);
|
||||||
|
self.rope.insert(start, &clean);
|
||||||
|
|
||||||
|
self.resize_cache();
|
||||||
|
if index < self.line_cache.len() {
|
||||||
|
// Replace this line’s cached string only; other lines unchanged
|
||||||
|
let _ = self.line_cache[index].take();
|
||||||
|
let _ = self.line_cache[index].set(clean);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
// src/textarea/state.rs
|
// src/textarea/state.rs
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
use anyhow::Result;
|
|
||||||
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
||||||
|
|
||||||
use crate::editor::FormEditor;
|
use crate::editor::FormEditor;
|
||||||
@@ -14,45 +13,6 @@ use ratatui::{layout::Rect, widgets::Block};
|
|||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
use unicode_width::UnicodeWidthChar;
|
use unicode_width::UnicodeWidthChar;
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
|
||||||
pub(crate) fn wrapped_rows(s: &str, width: u16) -> u16 {
|
|
||||||
if width == 0 {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
let mut rows: u16 = 1;
|
|
||||||
let mut cols: u16 = 0;
|
|
||||||
for ch in s.chars() {
|
|
||||||
let w = UnicodeWidthChar::width(ch).unwrap_or(0) as u16;
|
|
||||||
if cols.saturating_add(w) > width {
|
|
||||||
rows = rows.saturating_add(1);
|
|
||||||
cols = 0;
|
|
||||||
}
|
|
||||||
cols = cols.saturating_add(w);
|
|
||||||
}
|
|
||||||
rows
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
|
||||||
pub(crate) fn wrapped_rows_to_cursor(s: &str, width: u16, cursor_chars: usize) -> (u16, u16) {
|
|
||||||
if width == 0 {
|
|
||||||
return (0, 0);
|
|
||||||
}
|
|
||||||
let mut row: u16 = 0;
|
|
||||||
let mut cols: u16 = 0;
|
|
||||||
for (i, ch) in s.chars().enumerate() {
|
|
||||||
if i >= cursor_chars {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
let w = UnicodeWidthChar::width(ch).unwrap_or(0) as u16;
|
|
||||||
if cols.saturating_add(w) > width {
|
|
||||||
row = row.saturating_add(1);
|
|
||||||
cols = 0;
|
|
||||||
}
|
|
||||||
cols = cols.saturating_add(w);
|
|
||||||
}
|
|
||||||
(row, cols)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
pub(crate) const RIGHT_PAD: u16 = 3;
|
pub(crate) const RIGHT_PAD: u16 = 3;
|
||||||
|
|
||||||
@@ -395,7 +355,7 @@ impl TextAreaState {
|
|||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
pub fn cursor(&self, area: Rect, block: Option<&Block<'_>>) -> (u16, u16) {
|
pub fn cursor(&self, area: Rect, block: Option<&Block<'_>>) -> (u16, u16) {
|
||||||
let inner = if let Some(b) = block { b.inner(area) } else { area };
|
let inner = if let Some(b) = block { b.inner(area) } else { area };
|
||||||
let line_idx = self.current_field() as usize;
|
let line_idx = self.current_field();
|
||||||
|
|
||||||
match self.overflow_mode {
|
match self.overflow_mode {
|
||||||
TextOverflowMode::Wrap => {
|
TextOverflowMode::Wrap => {
|
||||||
@@ -415,7 +375,7 @@ impl TextAreaState {
|
|||||||
let col_chars = self.display_cursor_position();
|
let col_chars = self.display_cursor_position();
|
||||||
|
|
||||||
let (subrow, x_cols) = wrapped_rows_to_cursor_indented(
|
let (subrow, x_cols) = wrapped_rows_to_cursor_indented(
|
||||||
¤t_line,
|
current_line,
|
||||||
width,
|
width,
|
||||||
indent,
|
indent,
|
||||||
col_chars,
|
col_chars,
|
||||||
@@ -518,7 +478,7 @@ impl TextAreaState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let indent = self.wrap_indent_cols;
|
let indent = self.wrap_indent_cols;
|
||||||
let line_idx = self.current_field() as usize;
|
let line_idx = self.current_field();
|
||||||
|
|
||||||
let prefix_rows =
|
let prefix_rows =
|
||||||
self.visual_rows_before_line_and_intra_indented(width, line_idx);
|
self.visual_rows_before_line_and_intra_indented(width, line_idx);
|
||||||
@@ -527,7 +487,7 @@ impl TextAreaState {
|
|||||||
let col = self.display_cursor_position();
|
let col = self.display_cursor_position();
|
||||||
|
|
||||||
let (subrow, _x_cols) =
|
let (subrow, _x_cols) =
|
||||||
wrapped_rows_to_cursor_indented(¤t_line, width, indent, col);
|
wrapped_rows_to_cursor_indented(current_line, width, indent, col);
|
||||||
|
|
||||||
let caret_vis_row = prefix_rows.saturating_add(subrow);
|
let caret_vis_row = prefix_rows.saturating_add(subrow);
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use ratatui::{
|
|||||||
style::Style,
|
style::Style,
|
||||||
text::{Line, Span},
|
text::{Line, Span},
|
||||||
widgets::{
|
widgets::{
|
||||||
Block, BorderType, Borders, Paragraph, StatefulWidget, Widget, Wrap,
|
Block, BorderType, Borders, Paragraph, StatefulWidget, Widget,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -87,31 +87,6 @@ fn display_cols_up_to(s: &str, char_count: usize) -> u16 {
|
|||||||
cols
|
cols
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
|
||||||
fn clip_with_indicator(s: &str, width: u16, indicator: char) -> Line<'static> {
|
|
||||||
if width == 0 {
|
|
||||||
return Line::from("");
|
|
||||||
}
|
|
||||||
|
|
||||||
if display_width(s) <= width {
|
|
||||||
return Line::from(Span::raw(s.to_string()));
|
|
||||||
}
|
|
||||||
|
|
||||||
let budget = width.saturating_sub(1);
|
|
||||||
let mut out = String::new();
|
|
||||||
let mut used: u16 = 0;
|
|
||||||
for ch in s.chars() {
|
|
||||||
let w = UnicodeWidthChar::width(ch).unwrap_or(0) as u16;
|
|
||||||
if used + w > budget {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
out.push(ch);
|
|
||||||
used = used.saturating_add(w);
|
|
||||||
}
|
|
||||||
|
|
||||||
Line::from(vec![Span::raw(out), Span::raw(indicator.to_string())])
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "gui")]
|
#[cfg(feature = "gui")]
|
||||||
fn slice_by_display_cols(s: &str, start_cols: u16, max_cols: u16) -> String {
|
fn slice_by_display_cols(s: &str, start_cols: u16, max_cols: u16) -> String {
|
||||||
if max_cols == 0 {
|
if max_cols == 0 {
|
||||||
@@ -339,11 +314,11 @@ impl<'a> StatefulWidget for TextArea<'a> {
|
|||||||
match state.overflow_mode {
|
match state.overflow_mode {
|
||||||
TextOverflowMode::Wrap => unreachable!(),
|
TextOverflowMode::Wrap => unreachable!(),
|
||||||
TextOverflowMode::Indicator { ch } => {
|
TextOverflowMode::Indicator { ch } => {
|
||||||
let fits = display_width(&s) <= inner.width;
|
let fits = display_width(s) <= inner.width;
|
||||||
|
|
||||||
let start_cols = if i == state.current_field() {
|
let start_cols = if i == state.current_field() {
|
||||||
let col_idx = state.display_cursor_position();
|
let col_idx = state.display_cursor_position();
|
||||||
let cursor_cols = display_cols_up_to(&s, col_idx);
|
let cursor_cols = display_cols_up_to(s, col_idx);
|
||||||
let (target_h, _left_cols) =
|
let (target_h, _left_cols) =
|
||||||
compute_h_scroll_with_padding(cursor_cols, inner.width);
|
compute_h_scroll_with_padding(cursor_cols, inner.width);
|
||||||
|
|
||||||
@@ -357,7 +332,7 @@ impl<'a> StatefulWidget for TextArea<'a> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
display_lines.push(clip_window_with_indicator_padded(
|
display_lines.push(clip_window_with_indicator_padded(
|
||||||
&s,
|
s,
|
||||||
inner.width,
|
inner.width,
|
||||||
ch,
|
ch,
|
||||||
start_cols,
|
start_cols,
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
/* canvas/src/validation/formatting.rs
|
// src/validation/formatting.rs
|
||||||
Add new formatting module with CustomFormatter, PositionMapper, DefaultPositionMapper, and FormattingResult
|
//! Custom formatting and position mapping for validation/display.
|
||||||
*/
|
//!
|
||||||
|
//! This module defines the CustomFormatter trait along with helpers to map
|
||||||
|
//! cursor positions between the raw stored text and the formatted display
|
||||||
|
//! representation. Implementors may provide a custom PositionMapper to handle
|
||||||
|
//! advanced formatting scenarios.
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
/// Bidirectional mapping between raw input positions and formatted display positions.
|
/// Bidirectional mapping between raw input positions and formatted display positions.
|
||||||
@@ -108,7 +113,7 @@ impl FormattingResult {
|
|||||||
pub fn success(formatted: impl Into<String>) -> Self {
|
pub fn success(formatted: impl Into<String>) -> Self {
|
||||||
FormattingResult::Success {
|
FormattingResult::Success {
|
||||||
formatted: formatted.into(),
|
formatted: formatted.into(),
|
||||||
mapper: Arc::new(DefaultPositionMapper::default()),
|
mapper: Arc::new(DefaultPositionMapper),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,7 +122,7 @@ impl FormattingResult {
|
|||||||
FormattingResult::Warning {
|
FormattingResult::Warning {
|
||||||
formatted: formatted.into(),
|
formatted: formatted.into(),
|
||||||
message: message.into(),
|
message: message.into(),
|
||||||
mapper: Arc::new(DefaultPositionMapper::default()),
|
mapper: Arc::new(DefaultPositionMapper),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,7 +192,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn default_mapper_roundtrip_basic() {
|
fn default_mapper_roundtrip_basic() {
|
||||||
let mapper = DefaultPositionMapper::default();
|
let mapper = DefaultPositionMapper;
|
||||||
let raw = "01001";
|
let raw = "01001";
|
||||||
let formatted = "010 01";
|
let formatted = "010 01";
|
||||||
|
|
||||||
@@ -214,4 +219,4 @@ mod tests {
|
|||||||
_ => panic!("expected success"),
|
_ => panic!("expected success"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,8 +23,10 @@ pub struct CharacterLimits {
|
|||||||
|
|
||||||
/// How to count characters for limit checking
|
/// How to count characters for limit checking
|
||||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||||
|
#[derive(Default)]
|
||||||
pub enum CountMode {
|
pub enum CountMode {
|
||||||
/// Count actual characters (default)
|
/// Count actual characters (default)
|
||||||
|
#[default]
|
||||||
Characters,
|
Characters,
|
||||||
|
|
||||||
/// Count display width (useful for CJK characters)
|
/// Count display width (useful for CJK characters)
|
||||||
@@ -34,11 +36,6 @@ pub enum CountMode {
|
|||||||
Bytes,
|
Bytes,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for CountMode {
|
|
||||||
fn default() -> Self {
|
|
||||||
CountMode::Characters
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Result of a character limit check
|
/// Result of a character limit check
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
@@ -157,9 +154,7 @@ impl CharacterLimits {
|
|||||||
if let Some(max) = self.max_length {
|
if let Some(max) = self.max_length {
|
||||||
if new_count > max {
|
if new_count > max {
|
||||||
return Some(ValidationResult::error(format!(
|
return Some(ValidationResult::error(format!(
|
||||||
"Character limit exceeded: {}/{}",
|
"Character limit exceeded: {new_count}/{max}"
|
||||||
new_count,
|
|
||||||
max
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,9 +162,7 @@ impl CharacterLimits {
|
|||||||
if let Some(warning_threshold) = self.warning_threshold {
|
if let Some(warning_threshold) = self.warning_threshold {
|
||||||
if new_count >= warning_threshold && current_count < warning_threshold {
|
if new_count >= warning_threshold && current_count < warning_threshold {
|
||||||
return Some(ValidationResult::warning(format!(
|
return Some(ValidationResult::warning(format!(
|
||||||
"Approaching character limit: {}/{}",
|
"Approaching character limit: {new_count}/{max}"
|
||||||
new_count,
|
|
||||||
max
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -186,9 +179,7 @@ impl CharacterLimits {
|
|||||||
if let Some(min) = self.min_length {
|
if let Some(min) = self.min_length {
|
||||||
if count < min {
|
if count < min {
|
||||||
return Some(ValidationResult::warning(format!(
|
return Some(ValidationResult::warning(format!(
|
||||||
"Minimum length not met: {}/{}",
|
"Minimum length not met: {count}/{min}"
|
||||||
count,
|
|
||||||
min
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -197,9 +188,7 @@ impl CharacterLimits {
|
|||||||
if let Some(max) = self.max_length {
|
if let Some(max) = self.max_length {
|
||||||
if count > max {
|
if count > max {
|
||||||
return Some(ValidationResult::error(format!(
|
return Some(ValidationResult::error(format!(
|
||||||
"Character limit exceeded: {}/{}",
|
"Character limit exceeded: {count}/{max}"
|
||||||
count,
|
|
||||||
max
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -207,9 +196,7 @@ impl CharacterLimits {
|
|||||||
if let Some(warning_threshold) = self.warning_threshold {
|
if let Some(warning_threshold) = self.warning_threshold {
|
||||||
if count >= warning_threshold {
|
if count >= warning_threshold {
|
||||||
return Some(ValidationResult::warning(format!(
|
return Some(ValidationResult::warning(format!(
|
||||||
"Approaching character limit: {}/{}",
|
"Approaching character limit: {count}/{max}"
|
||||||
count,
|
|
||||||
max
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -251,20 +238,16 @@ impl CharacterLimits {
|
|||||||
match self.check_limits(text) {
|
match self.check_limits(text) {
|
||||||
LimitCheckResult::Ok => {
|
LimitCheckResult::Ok => {
|
||||||
// Show current/max if we have a max limit
|
// Show current/max if we have a max limit
|
||||||
if let Some(max) = self.max_length {
|
self.max_length.map(|max| format!("{}/{}", self.count(text), max))
|
||||||
Some(format!("{}/{}", self.count(text), max))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
LimitCheckResult::Warning { current, max } => {
|
LimitCheckResult::Warning { current, max } => {
|
||||||
Some(format!("{}/{} (approaching limit)", current, max))
|
Some(format!("{current}/{max} (approaching limit)"))
|
||||||
},
|
},
|
||||||
LimitCheckResult::Exceeded { current, max } => {
|
LimitCheckResult::Exceeded { current, max } => {
|
||||||
Some(format!("{}/{} (exceeded)", current, max))
|
Some(format!("{current}/{max} (exceeded)"))
|
||||||
},
|
},
|
||||||
LimitCheckResult::TooShort { current, min } => {
|
LimitCheckResult::TooShort { current, min } => {
|
||||||
Some(format!("{}/{} minimum", current, min))
|
Some(format!("{current}/{min} minimum"))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -284,8 +267,7 @@ impl CharacterLimits {
|
|||||||
let count = self.count(text);
|
let count = self.count(text);
|
||||||
if count > 0 && count < min {
|
if count > 0 && count < min {
|
||||||
return Some(format!(
|
return Some(format!(
|
||||||
"Field must be empty or have at least {} characters (currently: {})",
|
"Field must be empty or have at least {min} characters (currently: {count})"
|
||||||
min, count
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,11 @@
|
|||||||
//! Pure display mask system - user-defined patterns only
|
//! Pure display mask system - user-defined patterns only
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
#[derive(Default)]
|
||||||
pub enum MaskDisplayMode {
|
pub enum MaskDisplayMode {
|
||||||
/// Only show separators as user types
|
/// Only show separators as user types
|
||||||
/// Example: "" → "", "123" → "123", "12345" → "(123) 45"
|
/// Example: "" → "", "123" → "123", "12345" → "(123) 45"
|
||||||
|
#[default]
|
||||||
Dynamic,
|
Dynamic,
|
||||||
|
|
||||||
/// Show full template with placeholders from start
|
/// Show full template with placeholders from start
|
||||||
@@ -15,11 +17,6 @@ pub enum MaskDisplayMode {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for MaskDisplayMode {
|
|
||||||
fn default() -> Self {
|
|
||||||
MaskDisplayMode::Dynamic
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct DisplayMask {
|
pub struct DisplayMask {
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
// src/validation/mod.rs
|
// src/validation/mod.rs
|
||||||
|
//! Validation subsystem re-exports and helpers.
|
||||||
|
//!
|
||||||
|
//! This module collects validation-related modules (limits, masks, patterns,
|
||||||
|
//! formatting, and state) and re-exports the most commonly used types so that
|
||||||
|
//! callers can import them from `crate::validation`.
|
||||||
|
|
||||||
// Core validation modules
|
// Core validation modules
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod limits;
|
pub mod limits;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ impl std::fmt::Debug for CharacterFilter {
|
|||||||
CharacterFilter::Alphabetic => write!(f, "Alphabetic"),
|
CharacterFilter::Alphabetic => write!(f, "Alphabetic"),
|
||||||
CharacterFilter::Numeric => write!(f, "Numeric"),
|
CharacterFilter::Numeric => write!(f, "Numeric"),
|
||||||
CharacterFilter::Alphanumeric => write!(f, "Alphanumeric"),
|
CharacterFilter::Alphanumeric => write!(f, "Alphanumeric"),
|
||||||
CharacterFilter::Exact(ch) => write!(f, "Exact('{}')", ch),
|
CharacterFilter::Exact(ch) => write!(f, "Exact('{ch}')"),
|
||||||
CharacterFilter::OneOf(chars) => write!(f, "OneOf({:?})", chars),
|
CharacterFilter::OneOf(chars) => write!(f, "OneOf({chars:?})"),
|
||||||
CharacterFilter::Custom(_) => write!(f, "Custom(<function>)"),
|
CharacterFilter::Custom(_) => write!(f, "Custom(<function>)"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -130,10 +130,10 @@ impl CharacterFilter {
|
|||||||
CharacterFilter::Alphabetic => "alphabetic characters (a-z, A-Z)".to_string(),
|
CharacterFilter::Alphabetic => "alphabetic characters (a-z, A-Z)".to_string(),
|
||||||
CharacterFilter::Numeric => "numeric characters (0-9)".to_string(),
|
CharacterFilter::Numeric => "numeric characters (0-9)".to_string(),
|
||||||
CharacterFilter::Alphanumeric => "alphanumeric characters (a-z, A-Z, 0-9)".to_string(),
|
CharacterFilter::Alphanumeric => "alphanumeric characters (a-z, A-Z, 0-9)".to_string(),
|
||||||
CharacterFilter::Exact(ch) => format!("exactly '{}'", ch),
|
CharacterFilter::Exact(ch) => format!("exactly '{ch}'"),
|
||||||
CharacterFilter::OneOf(chars) => {
|
CharacterFilter::OneOf(chars) => {
|
||||||
let char_list: String = chars.iter().collect();
|
let char_list: String = chars.iter().collect();
|
||||||
format!("one of: {}", char_list)
|
format!("one of: {char_list}")
|
||||||
},
|
},
|
||||||
CharacterFilter::Custom(_) => "custom filter".to_string(),
|
CharacterFilter::Custom(_) => "custom filter".to_string(),
|
||||||
}
|
}
|
||||||
@@ -207,9 +207,7 @@ impl PatternFilters {
|
|||||||
/// Validate entire text against all filters
|
/// Validate entire text against all filters
|
||||||
pub fn validate_text(&self, text: &str) -> Result<(), String> {
|
pub fn validate_text(&self, text: &str) -> Result<(), String> {
|
||||||
for (position, character) in text.char_indices() {
|
for (position, character) in text.char_indices() {
|
||||||
if let Err(error) = self.validate_char_at_position(position, character) {
|
self.validate_char_at_position(position, character)?
|
||||||
return Err(error);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ license.workspace = true
|
|||||||
anyhow = { workspace = true }
|
anyhow = { workspace = true }
|
||||||
async-trait = "0.1.88"
|
async-trait = "0.1.88"
|
||||||
common = { path = "../common" }
|
common = { path = "../common" }
|
||||||
canvas = { path = "../canvas", features = ["gui"] }
|
canvas = { path = "../canvas", features = ["gui", "suggestions"] }
|
||||||
|
|
||||||
ratatui = { workspace = true }
|
ratatui = { workspace = true }
|
||||||
crossterm = { workspace = true }
|
crossterm = { workspace = true }
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use crate::config::colors::themes::Theme;
|
|||||||
use crate::state::app::highlight::HighlightState;
|
use crate::state::app::highlight::HighlightState;
|
||||||
use crate::state::app::state::AppState;
|
use crate::state::app::state::AppState;
|
||||||
use crate::state::pages::add_logic::{AddLogicFocus, AddLogicState};
|
use crate::state::pages::add_logic::{AddLogicFocus, AddLogicState};
|
||||||
use canvas::canvas::{render_canvas, CanvasState, HighlightState as CanvasHighlightState}; // Use canvas library
|
use canvas::{render_canvas, FormEditor};
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||||
style::{Modifier, Style},
|
style::{Modifier, Style},
|
||||||
@@ -14,15 +14,6 @@ use ratatui::{
|
|||||||
use crate::components::common::{dialog, autocomplete}; // Added autocomplete
|
use crate::components::common::{dialog, autocomplete}; // Added autocomplete
|
||||||
use crate::config::binds::config::EditorKeybindingMode;
|
use crate::config::binds::config::EditorKeybindingMode;
|
||||||
|
|
||||||
// Helper function to convert between HighlightState types
|
|
||||||
fn convert_highlight_state(local: &HighlightState) -> CanvasHighlightState {
|
|
||||||
match local {
|
|
||||||
HighlightState::Off => CanvasHighlightState::Off,
|
|
||||||
HighlightState::Characterwise { anchor } => CanvasHighlightState::Characterwise { anchor: *anchor },
|
|
||||||
HighlightState::Linewise { anchor_line } => CanvasHighlightState::Linewise { anchor_line: *anchor_line },
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn render_add_logic(
|
pub fn render_add_logic(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
@@ -30,7 +21,6 @@ pub fn render_add_logic(
|
|||||||
app_state: &AppState,
|
app_state: &AppState,
|
||||||
add_logic_state: &mut AddLogicState,
|
add_logic_state: &mut AddLogicState,
|
||||||
is_edit_mode: bool,
|
is_edit_mode: bool,
|
||||||
highlight_state: &HighlightState,
|
|
||||||
) {
|
) {
|
||||||
let main_block = Block::default()
|
let main_block = Block::default()
|
||||||
.title(" Add New Logic Script ")
|
.title(" Add New Logic Script ")
|
||||||
@@ -168,19 +158,12 @@ pub fn render_add_logic(
|
|||||||
| AddLogicFocus::InputDescription
|
| AddLogicFocus::InputDescription
|
||||||
);
|
);
|
||||||
|
|
||||||
let canvas_highlight_state = convert_highlight_state(highlight_state);
|
let editor = FormEditor::new(add_logic_state.clone());
|
||||||
let active_field_rect = render_canvas(
|
let active_field_rect = render_canvas(f, canvas_area, &editor, theme);
|
||||||
f,
|
|
||||||
canvas_area,
|
|
||||||
add_logic_state, // AddLogicState implements CanvasState
|
|
||||||
theme, // Theme implements CanvasTheme
|
|
||||||
is_edit_mode && focus_on_canvas_inputs,
|
|
||||||
&canvas_highlight_state,
|
|
||||||
);
|
|
||||||
|
|
||||||
// --- Render Autocomplete for Target Column ---
|
// --- Render Autocomplete for Target Column ---
|
||||||
// `is_edit_mode` here refers to the general edit mode of the EventHandler
|
// `is_edit_mode` here refers to the general edit mode of the EventHandler
|
||||||
if is_edit_mode && add_logic_state.current_field() == 1 { // Target Column field
|
if is_edit_mode && editor.current_field() == 1 { // Target Column field
|
||||||
if add_logic_state.in_target_column_suggestion_mode && add_logic_state.show_target_column_suggestions {
|
if add_logic_state.in_target_column_suggestion_mode && add_logic_state.show_target_column_suggestions {
|
||||||
if !add_logic_state.target_column_suggestions.is_empty() {
|
if !add_logic_state.target_column_suggestions.is_empty() {
|
||||||
if let Some(input_rect) = active_field_rect {
|
if let Some(input_rect) = active_field_rect {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use crate::config::colors::themes::Theme;
|
|||||||
use crate::state::app::highlight::HighlightState;
|
use crate::state::app::highlight::HighlightState;
|
||||||
use crate::state::app::state::AppState;
|
use crate::state::app::state::AppState;
|
||||||
use crate::state::pages::add_table::{AddTableFocus, AddTableState};
|
use crate::state::pages::add_table::{AddTableFocus, AddTableState};
|
||||||
use canvas::canvas::{render_canvas, CanvasState, HighlightState as CanvasHighlightState};
|
use canvas::{render_canvas_default, render_canvas, FormEditor};
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||||
style::{Modifier, Style},
|
style::{Modifier, Style},
|
||||||
@@ -13,15 +13,6 @@ use ratatui::{
|
|||||||
};
|
};
|
||||||
use crate::components::common::dialog;
|
use crate::components::common::dialog;
|
||||||
|
|
||||||
// Helper function to convert between HighlightState types
|
|
||||||
fn convert_highlight_state(local: &HighlightState) -> CanvasHighlightState {
|
|
||||||
match local {
|
|
||||||
HighlightState::Off => CanvasHighlightState::Off,
|
|
||||||
HighlightState::Characterwise { anchor } => CanvasHighlightState::Characterwise { anchor: *anchor },
|
|
||||||
HighlightState::Linewise { anchor_line } => CanvasHighlightState::Linewise { anchor_line: *anchor_line },
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Renders the Add New Table page layout, structuring the display of table information,
|
/// Renders the Add New Table page layout, structuring the display of table information,
|
||||||
/// input fields, and action buttons. Adapts layout based on terminal width.
|
/// input fields, and action buttons. Adapts layout based on terminal width.
|
||||||
pub fn render_add_table(
|
pub fn render_add_table(
|
||||||
@@ -31,7 +22,6 @@ pub fn render_add_table(
|
|||||||
app_state: &AppState,
|
app_state: &AppState,
|
||||||
add_table_state: &mut AddTableState,
|
add_table_state: &mut AddTableState,
|
||||||
is_edit_mode: bool, // Determines if canvas inputs are in edit mode
|
is_edit_mode: bool, // Determines if canvas inputs are in edit mode
|
||||||
highlight_state: &HighlightState, // For text highlighting in canvas
|
|
||||||
) {
|
) {
|
||||||
// --- Configuration ---
|
// --- Configuration ---
|
||||||
// Threshold width to switch between wide and narrow layouts
|
// Threshold width to switch between wide and narrow layouts
|
||||||
@@ -357,15 +347,8 @@ pub fn render_add_table(
|
|||||||
);
|
);
|
||||||
|
|
||||||
// --- Canvas Rendering (Column Definition Input) - USING CANVAS LIBRARY ---
|
// --- Canvas Rendering (Column Definition Input) - USING CANVAS LIBRARY ---
|
||||||
let canvas_highlight_state = convert_highlight_state(highlight_state);
|
let editor = FormEditor::new(add_table_state.clone());
|
||||||
let _active_field_rect = render_canvas(
|
let _active_field_rect = render_canvas(f, canvas_area, &editor, theme);
|
||||||
f,
|
|
||||||
canvas_area,
|
|
||||||
add_table_state, // AddTableState implements CanvasState
|
|
||||||
theme, // Theme implements CanvasTheme
|
|
||||||
is_edit_mode && focus_on_canvas_inputs,
|
|
||||||
&canvas_highlight_state,
|
|
||||||
);
|
|
||||||
|
|
||||||
// --- Button Style Helpers ---
|
// --- Button Style Helpers ---
|
||||||
let get_button_style = |button_focus: AddTableFocus, current_focus| {
|
let get_button_style = |button_focus: AddTableFocus, current_focus| {
|
||||||
|
|||||||
@@ -13,25 +13,21 @@ use ratatui::{
|
|||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
use crate::state::app::highlight::HighlightState;
|
use crate::state::app::highlight::HighlightState;
|
||||||
use canvas::canvas::{render_canvas, HighlightState as CanvasHighlightState}; // Use canvas library's render function
|
use canvas::{
|
||||||
|
FormEditor,
|
||||||
// Helper function to convert between HighlightState types
|
render_canvas,
|
||||||
fn convert_highlight_state(local: &HighlightState) -> CanvasHighlightState {
|
render_suggestions_dropdown,
|
||||||
match local {
|
DefaultCanvasTheme,
|
||||||
HighlightState::Off => CanvasHighlightState::Off,
|
};
|
||||||
HighlightState::Characterwise { anchor } => CanvasHighlightState::Characterwise { anchor: *anchor },
|
|
||||||
HighlightState::Linewise { anchor_line } => CanvasHighlightState::Linewise { anchor_line: *anchor_line },
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn render_login(
|
pub fn render_login(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
theme: &Theme,
|
theme: &Theme,
|
||||||
|
// FIX: take &LoginState (reference), not owned
|
||||||
login_state: &LoginState,
|
login_state: &LoginState,
|
||||||
app_state: &AppState,
|
app_state: &AppState,
|
||||||
is_edit_mode: bool,
|
is_edit_mode: bool,
|
||||||
highlight_state: &HighlightState,
|
|
||||||
) {
|
) {
|
||||||
// Main container
|
// Main container
|
||||||
let block = Block::default()
|
let block = Block::default()
|
||||||
@@ -58,15 +54,15 @@ pub fn render_login(
|
|||||||
])
|
])
|
||||||
.split(inner_area);
|
.split(inner_area);
|
||||||
|
|
||||||
// --- FORM RENDERING (Using canvas library directly) ---
|
// Wrap LoginState in FormEditor (no clone needed)
|
||||||
let canvas_highlight_state = convert_highlight_state(highlight_state);
|
let editor = FormEditor::new(login_state.clone());
|
||||||
render_canvas(
|
|
||||||
|
// Use DefaultCanvasTheme instead of app Theme
|
||||||
|
let input_rect = render_canvas(
|
||||||
f,
|
f,
|
||||||
chunks[0],
|
chunks[0],
|
||||||
login_state, // LoginState implements CanvasState
|
&editor,
|
||||||
theme, // Theme implements CanvasTheme
|
&DefaultCanvasTheme,
|
||||||
is_edit_mode,
|
|
||||||
&canvas_highlight_state,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// --- ERROR MESSAGE ---
|
// --- ERROR MESSAGE ---
|
||||||
@@ -88,7 +84,7 @@ pub fn render_login(
|
|||||||
// Login Button
|
// Login Button
|
||||||
let login_button_index = 0;
|
let login_button_index = 0;
|
||||||
let login_active = if app_state.ui.focus_outside_canvas {
|
let login_active = if app_state.ui.focus_outside_canvas {
|
||||||
app_state.focused_button_index== login_button_index
|
app_state.focused_button_index == login_button_index
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
};
|
};
|
||||||
@@ -115,7 +111,7 @@ pub fn render_login(
|
|||||||
// Return Button
|
// Return Button
|
||||||
let return_button_index = 1;
|
let return_button_index = 1;
|
||||||
let return_active = if app_state.ui.focus_outside_canvas {
|
let return_active = if app_state.ui.focus_outside_canvas {
|
||||||
app_state.focused_button_index== return_button_index
|
app_state.focused_button_index == return_button_index
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
};
|
};
|
||||||
@@ -139,6 +135,19 @@ pub fn render_login(
|
|||||||
button_chunks[1],
|
button_chunks[1],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// --- SUGGESTIONS DROPDOWN (if active) ---
|
||||||
|
if app_state.current_mode == crate::modes::handlers::mode_manager::AppMode::Edit {
|
||||||
|
if let Some(input_rect) = input_rect {
|
||||||
|
render_suggestions_dropdown(
|
||||||
|
f,
|
||||||
|
f.area(),
|
||||||
|
input_rect,
|
||||||
|
&DefaultCanvasTheme,
|
||||||
|
&editor, // FIX: pass &editor
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// --- DIALOG ---
|
// --- DIALOG ---
|
||||||
if app_state.ui.dialog.dialog_show {
|
if app_state.ui.dialog.dialog_show {
|
||||||
dialog::render_dialog(
|
dialog::render_dialog(
|
||||||
|
|||||||
@@ -14,9 +14,8 @@ use ratatui::{
|
|||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
use crate::state::app::highlight::HighlightState;
|
use crate::state::app::highlight::HighlightState;
|
||||||
use canvas::canvas::{render_canvas, HighlightState as CanvasHighlightState}; // Use canvas library's render function
|
use canvas::{FormEditor, render_canvas_default, render_canvas, render_suggestions_dropdown, DefaultCanvasTheme};
|
||||||
use canvas::autocomplete::gui::render_autocomplete_dropdown;
|
use canvas::canvas::HighlightState as CanvasHighlightState;
|
||||||
use canvas::autocomplete::AutocompleteCanvasState;
|
|
||||||
|
|
||||||
// Helper function to convert between HighlightState types
|
// Helper function to convert between HighlightState types
|
||||||
fn convert_highlight_state(local: &HighlightState) -> CanvasHighlightState {
|
fn convert_highlight_state(local: &HighlightState) -> CanvasHighlightState {
|
||||||
@@ -34,7 +33,6 @@ pub fn render_register(
|
|||||||
state: &RegisterState,
|
state: &RegisterState,
|
||||||
app_state: &AppState,
|
app_state: &AppState,
|
||||||
is_edit_mode: bool,
|
is_edit_mode: bool,
|
||||||
highlight_state: &HighlightState,
|
|
||||||
) {
|
) {
|
||||||
let block = Block::default()
|
let block = Block::default()
|
||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
@@ -60,15 +58,14 @@ pub fn render_register(
|
|||||||
])
|
])
|
||||||
.split(inner_area);
|
.split(inner_area);
|
||||||
|
|
||||||
// --- FORM RENDERING (Using canvas library directly) ---
|
// Wrap RegisterState in FormEditor
|
||||||
let canvas_highlight_state = convert_highlight_state(highlight_state);
|
let editor = FormEditor::new(state.clone());
|
||||||
|
|
||||||
let input_rect = render_canvas(
|
let input_rect = render_canvas(
|
||||||
f,
|
f,
|
||||||
chunks[0],
|
chunks[0],
|
||||||
state, // RegisterState implements CanvasState
|
&editor,
|
||||||
theme, // Theme implements CanvasTheme
|
theme,
|
||||||
is_edit_mode,
|
|
||||||
&canvas_highlight_state,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// --- HELP TEXT ---
|
// --- HELP TEXT ---
|
||||||
@@ -96,7 +93,7 @@ pub fn render_register(
|
|||||||
// Register Button
|
// Register Button
|
||||||
let register_button_index = 0;
|
let register_button_index = 0;
|
||||||
let register_active = if app_state.ui.focus_outside_canvas {
|
let register_active = if app_state.ui.focus_outside_canvas {
|
||||||
app_state.focused_button_index== register_button_index
|
app_state.focused_button_index == register_button_index
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
};
|
};
|
||||||
@@ -123,7 +120,7 @@ pub fn render_register(
|
|||||||
// Return Button
|
// Return Button
|
||||||
let return_button_index = 1;
|
let return_button_index = 1;
|
||||||
let return_active = if app_state.ui.focus_outside_canvas {
|
let return_active = if app_state.ui.focus_outside_canvas {
|
||||||
app_state.focused_button_index== return_button_index
|
app_state.focused_button_index == return_button_index
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
};
|
};
|
||||||
@@ -147,18 +144,16 @@ pub fn render_register(
|
|||||||
button_chunks[1],
|
button_chunks[1],
|
||||||
);
|
);
|
||||||
|
|
||||||
// --- AUTOCOMPLETE DROPDOWN (Using canvas library directly) ---
|
// --- AUTOCOMPLETE DROPDOWN (Using new canvas suggestions) ---
|
||||||
if app_state.current_mode == AppMode::Edit {
|
if app_state.current_mode == AppMode::Edit {
|
||||||
if let Some(autocomplete_state) = state.autocomplete_state() {
|
if let Some(input_rect) = input_rect {
|
||||||
if let Some(input_rect) = input_rect {
|
render_suggestions_dropdown(
|
||||||
render_autocomplete_dropdown(
|
f,
|
||||||
f,
|
f.area(), // Frame area
|
||||||
f.area(), // Frame area
|
input_rect, // Current input field rect
|
||||||
input_rect, // Current input field rect
|
&DefaultCanvasTheme,
|
||||||
theme, // Theme implements CanvasTheme
|
&editor,
|
||||||
autocomplete_state,
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
// src/components/form/form.rs
|
// src/components/form/form.rs
|
||||||
use crate::components::common::autocomplete;
|
|
||||||
use crate::config::colors::themes::Theme;
|
use crate::config::colors::themes::Theme;
|
||||||
use canvas::canvas::{CanvasState, render_canvas, HighlightState};
|
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::form::FormState;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
layout::{Alignment, Constraint, Direction, Layout, Margin, Rect},
|
layout::{Alignment, Constraint, Direction, Layout, Margin, Rect},
|
||||||
@@ -9,17 +7,19 @@ use ratatui::{
|
|||||||
widgets::{Block, Borders, Paragraph},
|
widgets::{Block, Borders, Paragraph},
|
||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
|
use canvas::canvas::HighlightState;
|
||||||
|
use canvas::{FormEditor, render_canvas_default, render_canvas, render_suggestions_dropdown, DefaultCanvasTheme};
|
||||||
|
|
||||||
pub fn render_form(
|
pub fn render_form(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
form_state: &FormState,
|
form_state: &FormState,
|
||||||
fields: &[&str],
|
fields: &[&str], // no longer needed, FormEditor handles this
|
||||||
current_field_idx: &usize,
|
current_field_idx: &usize, // no longer needed
|
||||||
inputs: &[&String],
|
inputs: &[&String], // no longer needed
|
||||||
table_name: &str,
|
table_name: &str,
|
||||||
theme: &Theme,
|
theme: &Theme,
|
||||||
is_edit_mode: bool,
|
is_edit_mode: bool, // FormEditor tracks mode internally
|
||||||
highlight_state: &HighlightState,
|
highlight_state: &HighlightState,
|
||||||
total_count: u64,
|
total_count: u64,
|
||||||
current_position: u64,
|
current_position: u64,
|
||||||
@@ -56,43 +56,30 @@ pub fn render_form(
|
|||||||
total_count, current_position, total_count
|
total_count, current_position, total_count
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let count_para = Paragraph::new(count_position_text)
|
let count_para = Paragraph::new(count_position_text)
|
||||||
.style(Style::default().fg(theme.fg))
|
.style(Style::default().fg(theme.fg))
|
||||||
.alignment(Alignment::Left);
|
.alignment(Alignment::Left);
|
||||||
f.render_widget(count_para, main_layout[0]);
|
f.render_widget(count_para, main_layout[0]);
|
||||||
|
|
||||||
// Use the canvas library's render_canvas function
|
// --- FORM RENDERING (Using new canvas API) ---
|
||||||
|
let editor = FormEditor::new(form_state.clone());
|
||||||
|
|
||||||
let active_field_rect = render_canvas(
|
let active_field_rect = render_canvas(
|
||||||
f,
|
f,
|
||||||
main_layout[1],
|
main_layout[1],
|
||||||
form_state,
|
&editor,
|
||||||
theme,
|
theme,
|
||||||
is_edit_mode,
|
|
||||||
highlight_state,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// --- RENDER RICH AUTOCOMPLETE ONLY ---
|
// --- SUGGESTIONS DROPDOWN ---
|
||||||
if form_state.autocomplete_active {
|
if let Some(active_rect) = active_field_rect {
|
||||||
if let Some(active_rect) = active_field_rect {
|
render_suggestions_dropdown(
|
||||||
// Get selected index directly from form_state
|
f,
|
||||||
let selected_index = form_state.selected_suggestion_index;
|
main_layout[1],
|
||||||
|
active_rect,
|
||||||
// Only render rich suggestions (your Hit objects)
|
&DefaultCanvasTheme,
|
||||||
if let Some(rich_suggestions) = form_state.get_rich_suggestions() {
|
&editor,
|
||||||
if !rich_suggestions.is_empty() {
|
);
|
||||||
autocomplete::render_hit_autocomplete_dropdown(
|
|
||||||
f,
|
|
||||||
active_rect,
|
|
||||||
f.area(),
|
|
||||||
theme,
|
|
||||||
rich_suggestions,
|
|
||||||
selected_index,
|
|
||||||
form_state,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Removed simple suggestions - we only use rich ones now!
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// src/config/colors/themes.rs
|
// src/config/colors/themes.rs
|
||||||
use ratatui::style::Color;
|
use ratatui::style::Color;
|
||||||
use canvas::canvas::CanvasTheme;
|
use canvas::CanvasTheme;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Theme {
|
pub struct Theme {
|
||||||
@@ -12,7 +12,7 @@ pub struct Theme {
|
|||||||
pub warning: Color,
|
pub warning: Color,
|
||||||
pub border: Color,
|
pub border: Color,
|
||||||
pub highlight_bg: Color,
|
pub highlight_bg: Color,
|
||||||
pub inactive_highlight_bg: Color,// admin panel no idea what it really is
|
pub inactive_highlight_bg: Color, // admin panel no idea what it really is
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Theme {
|
impl Theme {
|
||||||
@@ -108,4 +108,9 @@ impl CanvasTheme for Theme {
|
|||||||
fn warning(&self) -> Color {
|
fn warning(&self) -> Color {
|
||||||
self.warning
|
self.warning
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn suggestion_gray(&self) -> Color {
|
||||||
|
// Neutral gray for suggestions
|
||||||
|
Color::Rgb(128, 128, 128)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +0,0 @@
|
|||||||
// src/client/modes/canvas.rs
|
|
||||||
pub mod edit;
|
|
||||||
pub mod common_mode;
|
|
||||||
pub mod read_only;
|
|
||||||
|
|||||||
@@ -1,500 +0,0 @@
|
|||||||
// src/modes/canvas/edit.rs
|
|
||||||
use crate::config::binds::config::Config;
|
|
||||||
use crate::modes::handlers::event::EventHandler;
|
|
||||||
use crate::services::grpc_client::GrpcClient;
|
|
||||||
use crate::state::app::state::AppState;
|
|
||||||
use crate::state::pages::admin::AdminState;
|
|
||||||
use crate::state::pages::{
|
|
||||||
auth::{LoginState, RegisterState},
|
|
||||||
form::FormState,
|
|
||||||
};
|
|
||||||
use canvas::canvas::CanvasState;
|
|
||||||
use canvas::{canvas::CanvasAction, dispatcher::ActionDispatcher, canvas::ActionResult};
|
|
||||||
use anyhow::Result;
|
|
||||||
use common::proto::komp_ac::search::search_response::Hit;
|
|
||||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
|
||||||
use tokio::sync::mpsc;
|
|
||||||
use tracing::info;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
||||||
pub enum EditEventOutcome {
|
|
||||||
Message(String),
|
|
||||||
ExitEditMode,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Helper function to spawn a non-blocking search task for autocomplete.
|
|
||||||
async fn trigger_form_autocomplete_search(
|
|
||||||
form_state: &mut FormState,
|
|
||||||
grpc_client: &mut GrpcClient,
|
|
||||||
sender: mpsc::UnboundedSender<Vec<Hit>>,
|
|
||||||
) {
|
|
||||||
if let Some(field_def) = form_state.fields.get(form_state.current_field) {
|
|
||||||
if field_def.is_link {
|
|
||||||
if let Some(target_table) = &field_def.link_target_table {
|
|
||||||
// 1. Update state for immediate UI feedback
|
|
||||||
form_state.autocomplete_loading = true;
|
|
||||||
form_state.autocomplete_active = true;
|
|
||||||
form_state.autocomplete_suggestions.clear();
|
|
||||||
form_state.selected_suggestion_index = None;
|
|
||||||
|
|
||||||
// 2. Clone everything needed for the background task
|
|
||||||
let query = form_state.get_current_input().to_string();
|
|
||||||
let table_to_search = target_table.clone();
|
|
||||||
let mut grpc_client_clone = grpc_client.clone();
|
|
||||||
|
|
||||||
info!(
|
|
||||||
"[Autocomplete] Spawning search in '{}' for query: '{}'",
|
|
||||||
table_to_search, query
|
|
||||||
);
|
|
||||||
|
|
||||||
// 3. Spawn the non-blocking task
|
|
||||||
tokio::spawn(async move {
|
|
||||||
match grpc_client_clone
|
|
||||||
.search_table(table_to_search, query)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(response) => {
|
|
||||||
// Send results back through the channel
|
|
||||||
let _ = sender.send(response.hits);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
tracing::error!(
|
|
||||||
"[Autocomplete] Search failed: {:?}",
|
|
||||||
e
|
|
||||||
);
|
|
||||||
// Send an empty vec on error so the UI can stop loading
|
|
||||||
let _ = sender.send(vec![]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn handle_form_edit_with_canvas(
|
|
||||||
key_event: KeyEvent,
|
|
||||||
config: &Config,
|
|
||||||
form_state: &mut FormState,
|
|
||||||
ideal_cursor_column: &mut usize,
|
|
||||||
) -> Result<String> {
|
|
||||||
// Try canvas action from key first
|
|
||||||
let canvas_config = canvas::config::CanvasConfig::load();
|
|
||||||
if let Some(action_name) = canvas_config.get_edit_action(key_event.code, key_event.modifiers) {
|
|
||||||
let canvas_action = CanvasAction::from_string(action_name);
|
|
||||||
match ActionDispatcher::dispatch(canvas_action, form_state, ideal_cursor_column).await {
|
|
||||||
Ok(ActionResult::Success(msg)) => {
|
|
||||||
return Ok(msg.unwrap_or_default());
|
|
||||||
}
|
|
||||||
Ok(ActionResult::HandledByFeature(msg)) => {
|
|
||||||
return Ok(msg);
|
|
||||||
}
|
|
||||||
Ok(ActionResult::Error(msg)) => {
|
|
||||||
return Ok(format!("Error: {}", msg));
|
|
||||||
}
|
|
||||||
Ok(ActionResult::RequiresContext(msg)) => {
|
|
||||||
return Ok(format!("Context needed: {}", msg));
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
// Fall through to try config mapping
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try config-mapped action
|
|
||||||
if let Some(action_str) = config.get_edit_action_for_key(key_event.code, key_event.modifiers) {
|
|
||||||
let canvas_action = CanvasAction::from_string(&action_str);
|
|
||||||
match ActionDispatcher::dispatch(canvas_action, form_state, ideal_cursor_column).await {
|
|
||||||
Ok(ActionResult::Success(msg)) => {
|
|
||||||
return Ok(msg.unwrap_or_default());
|
|
||||||
}
|
|
||||||
Ok(ActionResult::HandledByFeature(msg)) => {
|
|
||||||
return Ok(msg);
|
|
||||||
}
|
|
||||||
Ok(ActionResult::Error(msg)) => {
|
|
||||||
return Ok(format!("Error: {}", msg));
|
|
||||||
}
|
|
||||||
Ok(ActionResult::RequiresContext(msg)) => {
|
|
||||||
return Ok(format!("Context needed: {}", msg));
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
return Ok(format!("Action failed: {}", e));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(String::new())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Helper function to execute a specific action using canvas library
|
|
||||||
async fn execute_canvas_action(
|
|
||||||
action: &str,
|
|
||||||
key: KeyEvent,
|
|
||||||
form_state: &mut FormState,
|
|
||||||
ideal_cursor_column: &mut usize,
|
|
||||||
) -> Result<String> {
|
|
||||||
let canvas_action = CanvasAction::from_string(action);
|
|
||||||
match ActionDispatcher::dispatch(canvas_action, form_state, ideal_cursor_column).await {
|
|
||||||
Ok(ActionResult::Success(msg)) => Ok(msg.unwrap_or_default()),
|
|
||||||
Ok(ActionResult::HandledByFeature(msg)) => Ok(msg),
|
|
||||||
Ok(ActionResult::Error(msg)) => Ok(format!("Error: {}", msg)),
|
|
||||||
Ok(ActionResult::RequiresContext(msg)) => Ok(format!("Context needed: {}", msg)),
|
|
||||||
Err(e) => Ok(format!("Action failed: {}", e)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// FIXED: Unified canvas action handler with proper priority order for edit mode
|
|
||||||
async fn handle_canvas_state_edit<S: CanvasState>(
|
|
||||||
key: KeyEvent,
|
|
||||||
config: &Config,
|
|
||||||
state: &mut S,
|
|
||||||
ideal_cursor_column: &mut usize,
|
|
||||||
) -> Result<String> {
|
|
||||||
// println!("DEBUG: Key pressed: {:?}", key); // DEBUG
|
|
||||||
|
|
||||||
// PRIORITY 1: Character insertion in edit mode comes FIRST
|
|
||||||
if let KeyCode::Char(c) = key.code {
|
|
||||||
// Only insert if no modifiers or just shift (for uppercase)
|
|
||||||
if key.modifiers.is_empty() || key.modifiers == KeyModifiers::SHIFT {
|
|
||||||
// println!("DEBUG: Using character insertion priority for: {}", c); // DEBUG
|
|
||||||
let canvas_action = CanvasAction::InsertChar(c);
|
|
||||||
match ActionDispatcher::dispatch(canvas_action, state, ideal_cursor_column).await {
|
|
||||||
Ok(ActionResult::Success(msg)) => {
|
|
||||||
return Ok(msg.unwrap_or_default());
|
|
||||||
}
|
|
||||||
Ok(ActionResult::HandledByFeature(msg)) => {
|
|
||||||
return Ok(msg);
|
|
||||||
}
|
|
||||||
Ok(ActionResult::Error(msg)) => {
|
|
||||||
return Ok(format!("Error: {}", msg));
|
|
||||||
}
|
|
||||||
Ok(ActionResult::RequiresContext(msg)) => {
|
|
||||||
return Ok(format!("Context needed: {}", msg));
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
// println!("DEBUG: Character insertion failed: {:?}, trying config", e);
|
|
||||||
// Fall through to try config mappings
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// PRIORITY 2: Check canvas config for special keys/combinations
|
|
||||||
let canvas_config = canvas::config::CanvasConfig::load();
|
|
||||||
if let Some(action_name) = canvas_config.get_edit_action(key.code, key.modifiers) {
|
|
||||||
// println!("DEBUG: Canvas config mapped to: {}", action_name); // DEBUG
|
|
||||||
let canvas_action = CanvasAction::from_string(action_name);
|
|
||||||
|
|
||||||
match ActionDispatcher::dispatch(canvas_action, state, ideal_cursor_column).await {
|
|
||||||
Ok(ActionResult::Success(msg)) => {
|
|
||||||
return Ok(msg.unwrap_or_default());
|
|
||||||
}
|
|
||||||
Ok(ActionResult::HandledByFeature(msg)) => {
|
|
||||||
return Ok(msg);
|
|
||||||
}
|
|
||||||
Ok(ActionResult::Error(msg)) => {
|
|
||||||
return Ok(format!("Error: {}", msg));
|
|
||||||
}
|
|
||||||
Ok(ActionResult::RequiresContext(msg)) => {
|
|
||||||
return Ok(format!("Context needed: {}", msg));
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
// println!("DEBUG: Canvas action failed, trying client config"); // DEBUG
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// println!("DEBUG: No canvas config mapping found"); // DEBUG
|
|
||||||
}
|
|
||||||
|
|
||||||
// PRIORITY 3: Check client config ONLY for non-character keys or modified keys
|
|
||||||
if !matches!(key.code, KeyCode::Char(_)) || !key.modifiers.is_empty() {
|
|
||||||
if let Some(action_str) = config.get_edit_action_for_key(key.code, key.modifiers) {
|
|
||||||
// println!("DEBUG: Client config mapped to: {} (for non-char key)", action_str); // DEBUG
|
|
||||||
let canvas_action = CanvasAction::from_string(&action_str);
|
|
||||||
match ActionDispatcher::dispatch(canvas_action, state, ideal_cursor_column).await {
|
|
||||||
Ok(ActionResult::Success(msg)) => {
|
|
||||||
return Ok(msg.unwrap_or_default());
|
|
||||||
}
|
|
||||||
Ok(ActionResult::HandledByFeature(msg)) => {
|
|
||||||
return Ok(msg);
|
|
||||||
}
|
|
||||||
Ok(ActionResult::Error(msg)) => {
|
|
||||||
return Ok(format!("Error: {}", msg));
|
|
||||||
}
|
|
||||||
Ok(ActionResult::RequiresContext(msg)) => {
|
|
||||||
return Ok(format!("Context needed: {}", msg));
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
return Ok(format!("Action failed: {}", e));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// println!("DEBUG: No client config mapping found for non-char key"); // DEBUG
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// println!("DEBUG: Skipping client config for character key in edit mode"); // DEBUG
|
|
||||||
}
|
|
||||||
|
|
||||||
// println!("DEBUG: No action taken for key: {:?}", key); // DEBUG
|
|
||||||
Ok(String::new())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
|
||||||
pub async fn handle_edit_event(
|
|
||||||
key: KeyEvent,
|
|
||||||
config: &Config,
|
|
||||||
form_state: &mut FormState,
|
|
||||||
login_state: &mut LoginState,
|
|
||||||
register_state: &mut RegisterState,
|
|
||||||
admin_state: &mut AdminState,
|
|
||||||
current_position: &mut u64,
|
|
||||||
total_count: u64,
|
|
||||||
event_handler: &mut EventHandler,
|
|
||||||
app_state: &AppState,
|
|
||||||
) -> Result<EditEventOutcome> {
|
|
||||||
// --- AUTOCOMPLETE-SPECIFIC KEY HANDLING ---
|
|
||||||
if app_state.ui.show_form && form_state.autocomplete_active {
|
|
||||||
if let Some(action) =
|
|
||||||
config.get_edit_action_for_key(key.code, key.modifiers)
|
|
||||||
{
|
|
||||||
match action {
|
|
||||||
"suggestion_down" => {
|
|
||||||
if !form_state.autocomplete_suggestions.is_empty() {
|
|
||||||
let current =
|
|
||||||
form_state.selected_suggestion_index.unwrap_or(0);
|
|
||||||
let next = (current + 1)
|
|
||||||
% form_state.autocomplete_suggestions.len();
|
|
||||||
form_state.selected_suggestion_index = Some(next);
|
|
||||||
}
|
|
||||||
return Ok(EditEventOutcome::Message(String::new()));
|
|
||||||
}
|
|
||||||
"suggestion_up" => {
|
|
||||||
if !form_state.autocomplete_suggestions.is_empty() {
|
|
||||||
let current =
|
|
||||||
form_state.selected_suggestion_index.unwrap_or(0);
|
|
||||||
let prev = if current == 0 {
|
|
||||||
form_state.autocomplete_suggestions.len() - 1
|
|
||||||
} else {
|
|
||||||
current - 1
|
|
||||||
};
|
|
||||||
form_state.selected_suggestion_index = Some(prev);
|
|
||||||
}
|
|
||||||
return Ok(EditEventOutcome::Message(String::new()));
|
|
||||||
}
|
|
||||||
"exit" => {
|
|
||||||
form_state.deactivate_autocomplete();
|
|
||||||
return Ok(EditEventOutcome::Message(
|
|
||||||
"Autocomplete cancelled".to_string(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
"enter_decider" => {
|
|
||||||
if let Some(selected_idx) =
|
|
||||||
form_state.selected_suggestion_index
|
|
||||||
{
|
|
||||||
if let Some(selection) = form_state
|
|
||||||
.autocomplete_suggestions
|
|
||||||
.get(selected_idx)
|
|
||||||
.cloned()
|
|
||||||
{
|
|
||||||
// --- THIS IS THE CORE LOGIC CHANGE ---
|
|
||||||
|
|
||||||
// 1. Get the friendly display name for the UI
|
|
||||||
let display_name =
|
|
||||||
form_state.get_display_name_for_hit(&selection);
|
|
||||||
|
|
||||||
// 2. Store the REAL ID in the form's values
|
|
||||||
let current_input =
|
|
||||||
form_state.get_current_input_mut();
|
|
||||||
*current_input = selection.id.to_string();
|
|
||||||
|
|
||||||
// 3. Set the persistent display override in the map
|
|
||||||
form_state.link_display_map.insert(
|
|
||||||
form_state.current_field,
|
|
||||||
display_name,
|
|
||||||
);
|
|
||||||
|
|
||||||
// 4. Finalize state
|
|
||||||
form_state.deactivate_autocomplete();
|
|
||||||
form_state.set_has_unsaved_changes(true);
|
|
||||||
return Ok(EditEventOutcome::Message(
|
|
||||||
"Selection made".to_string(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
form_state.deactivate_autocomplete();
|
|
||||||
// Fall through to default 'enter' behavior
|
|
||||||
}
|
|
||||||
_ => {} // Let other keys fall through to the live search logic
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- LIVE AUTOCOMPLETE TRIGGER LOGIC ---
|
|
||||||
let mut trigger_search = false;
|
|
||||||
|
|
||||||
if app_state.ui.show_form {
|
|
||||||
// Manual trigger
|
|
||||||
if let Some("trigger_autocomplete") =
|
|
||||||
config.get_edit_action_for_key(key.code, key.modifiers)
|
|
||||||
{
|
|
||||||
if !form_state.autocomplete_active {
|
|
||||||
trigger_search = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Live search trigger while typing
|
|
||||||
else if form_state.autocomplete_active {
|
|
||||||
if let KeyCode::Char(_) | KeyCode::Backspace = key.code {
|
|
||||||
let action = if let KeyCode::Backspace = key.code {
|
|
||||||
"delete_char_backward"
|
|
||||||
} else {
|
|
||||||
"insert_char"
|
|
||||||
};
|
|
||||||
// FIXED: Use canvas library instead of form_e::execute_edit_action
|
|
||||||
execute_canvas_action(
|
|
||||||
action,
|
|
||||||
key,
|
|
||||||
form_state,
|
|
||||||
&mut event_handler.ideal_cursor_column,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
trigger_search = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if trigger_search {
|
|
||||||
trigger_form_autocomplete_search(
|
|
||||||
form_state,
|
|
||||||
&mut event_handler.grpc_client,
|
|
||||||
event_handler.autocomplete_result_sender.clone(),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
return Ok(EditEventOutcome::Message("Searching...".to_string()));
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- GENERAL EDIT MODE EVENT HANDLING (IF NOT AUTOCOMPLETE) ---
|
|
||||||
|
|
||||||
if let Some(action_str) =
|
|
||||||
config.get_edit_action_for_key(key.code, key.modifiers)
|
|
||||||
{
|
|
||||||
// Handle Enter key (next field)
|
|
||||||
if action_str == "enter_decider" {
|
|
||||||
// FIXED: Use canvas library instead of form_e::execute_edit_action
|
|
||||||
let msg = execute_canvas_action(
|
|
||||||
"next_field",
|
|
||||||
key,
|
|
||||||
form_state,
|
|
||||||
&mut event_handler.ideal_cursor_column,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
return Ok(EditEventOutcome::Message(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle exiting edit mode
|
|
||||||
if action_str == "exit" {
|
|
||||||
return Ok(EditEventOutcome::ExitEditMode);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle all other edit actions - NOW USING CANVAS LIBRARY
|
|
||||||
let msg = if app_state.ui.show_login {
|
|
||||||
// NEW: Use unified canvas handler instead of auth_e::execute_edit_action
|
|
||||||
handle_canvas_state_edit(
|
|
||||||
key,
|
|
||||||
config,
|
|
||||||
login_state,
|
|
||||||
&mut event_handler.ideal_cursor_column,
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
} else if app_state.ui.show_add_table {
|
|
||||||
// NEW: Use unified canvas handler instead of add_table_e::execute_edit_action
|
|
||||||
handle_canvas_state_edit(
|
|
||||||
key,
|
|
||||||
config,
|
|
||||||
&mut admin_state.add_table_state,
|
|
||||||
&mut event_handler.ideal_cursor_column,
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
} else if app_state.ui.show_add_logic {
|
|
||||||
// NEW: Use unified canvas handler instead of add_logic_e::execute_edit_action
|
|
||||||
handle_canvas_state_edit(
|
|
||||||
key,
|
|
||||||
config,
|
|
||||||
&mut admin_state.add_logic_state,
|
|
||||||
&mut event_handler.ideal_cursor_column,
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
} else if app_state.ui.show_register {
|
|
||||||
// NEW: Use unified canvas handler instead of auth_e::execute_edit_action
|
|
||||||
handle_canvas_state_edit(
|
|
||||||
key,
|
|
||||||
config,
|
|
||||||
register_state,
|
|
||||||
&mut event_handler.ideal_cursor_column,
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
} else {
|
|
||||||
// FIXED: Use canvas library instead of form_e::execute_edit_action
|
|
||||||
execute_canvas_action(
|
|
||||||
action_str,
|
|
||||||
key,
|
|
||||||
form_state,
|
|
||||||
&mut event_handler.ideal_cursor_column,
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
};
|
|
||||||
return Ok(EditEventOutcome::Message(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
// --- FALLBACK FOR CHARACTER INSERTION (IF NO OTHER BINDING MATCHED) ---
|
|
||||||
if let KeyCode::Char(_) = key.code {
|
|
||||||
let msg = if app_state.ui.show_login {
|
|
||||||
// NEW: Use unified canvas handler instead of auth_e::execute_edit_action
|
|
||||||
handle_canvas_state_edit(
|
|
||||||
key,
|
|
||||||
config,
|
|
||||||
login_state,
|
|
||||||
&mut event_handler.ideal_cursor_column,
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
} else if app_state.ui.show_add_table {
|
|
||||||
// NEW: Use unified canvas handler instead of add_table_e::execute_edit_action
|
|
||||||
handle_canvas_state_edit(
|
|
||||||
key,
|
|
||||||
config,
|
|
||||||
&mut admin_state.add_table_state,
|
|
||||||
&mut event_handler.ideal_cursor_column,
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
} else if app_state.ui.show_add_logic {
|
|
||||||
// NEW: Use unified canvas handler instead of add_logic_e::execute_edit_action
|
|
||||||
handle_canvas_state_edit(
|
|
||||||
key,
|
|
||||||
config,
|
|
||||||
&mut admin_state.add_logic_state,
|
|
||||||
&mut event_handler.ideal_cursor_column,
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
} else if app_state.ui.show_register {
|
|
||||||
// NEW: Use unified canvas handler instead of auth_e::execute_edit_action
|
|
||||||
handle_canvas_state_edit(
|
|
||||||
key,
|
|
||||||
config,
|
|
||||||
register_state,
|
|
||||||
&mut event_handler.ideal_cursor_column,
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
} else {
|
|
||||||
// FIXED: Use canvas library instead of form_e::execute_edit_action
|
|
||||||
execute_canvas_action(
|
|
||||||
"insert_char",
|
|
||||||
key,
|
|
||||||
form_state,
|
|
||||||
&mut event_handler.ideal_cursor_column,
|
|
||||||
)
|
|
||||||
.await?
|
|
||||||
};
|
|
||||||
return Ok(EditEventOutcome::Message(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(EditEventOutcome::Message(String::new())) // No action taken
|
|
||||||
}
|
|
||||||
@@ -1,313 +0,0 @@
|
|||||||
// src/modes/canvas/read_only.rs
|
|
||||||
|
|
||||||
use crate::config::binds::config::Config;
|
|
||||||
use crate::config::binds::key_sequences::KeySequenceTracker;
|
|
||||||
use crate::services::grpc_client::GrpcClient;
|
|
||||||
use crate::state::pages::auth::LoginState;
|
|
||||||
use crate::state::pages::auth::RegisterState;
|
|
||||||
use crate::state::pages::form::FormState;
|
|
||||||
use crate::state::pages::add_logic::AddLogicState;
|
|
||||||
use crate::state::pages::add_table::AddTableState;
|
|
||||||
use crate::state::app::state::AppState;
|
|
||||||
use canvas::{canvas::{CanvasAction, CanvasState, ActionResult}, dispatcher::ActionDispatcher};
|
|
||||||
use crossterm::event::KeyEvent;
|
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
/// Helper function to dispatch canvas action for any CanvasState
|
|
||||||
async fn dispatch_canvas_action<S: CanvasState>(
|
|
||||||
action: &str,
|
|
||||||
state: &mut S,
|
|
||||||
ideal_cursor_column: &mut usize,
|
|
||||||
) -> String {
|
|
||||||
let canvas_action = CanvasAction::from_string(action);
|
|
||||||
match ActionDispatcher::dispatch(canvas_action, state, ideal_cursor_column).await {
|
|
||||||
Ok(ActionResult::Success(msg)) => msg.unwrap_or_default(),
|
|
||||||
Ok(ActionResult::HandledByFeature(msg)) => msg,
|
|
||||||
Ok(ActionResult::Error(msg)) => format!("Error: {}", msg),
|
|
||||||
Ok(ActionResult::RequiresContext(msg)) => format!("Context needed: {}", msg),
|
|
||||||
Err(e) => format!("Action failed: {}", e),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Helper function to dispatch canvas action to the appropriate state based on UI
|
|
||||||
async fn dispatch_to_active_state(
|
|
||||||
action: &str,
|
|
||||||
app_state: &AppState,
|
|
||||||
form_state: &mut FormState,
|
|
||||||
login_state: &mut LoginState,
|
|
||||||
register_state: &mut RegisterState,
|
|
||||||
add_table_state: &mut AddTableState,
|
|
||||||
add_logic_state: &mut AddLogicState,
|
|
||||||
ideal_cursor_column: &mut usize,
|
|
||||||
) -> String {
|
|
||||||
if app_state.ui.show_add_table {
|
|
||||||
dispatch_canvas_action(action, add_table_state, ideal_cursor_column).await
|
|
||||||
} else if app_state.ui.show_add_logic {
|
|
||||||
dispatch_canvas_action(action, add_logic_state, ideal_cursor_column).await
|
|
||||||
} else if app_state.ui.show_register {
|
|
||||||
dispatch_canvas_action(action, register_state, ideal_cursor_column).await
|
|
||||||
} else if app_state.ui.show_login {
|
|
||||||
dispatch_canvas_action(action, login_state, ideal_cursor_column).await
|
|
||||||
} else {
|
|
||||||
dispatch_canvas_action(action, form_state, ideal_cursor_column).await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Helper function to handle context-specific actions that need special treatment
|
|
||||||
async fn handle_context_action(
|
|
||||||
action: &str,
|
|
||||||
app_state: &AppState,
|
|
||||||
form_state: &mut FormState,
|
|
||||||
grpc_client: &mut GrpcClient,
|
|
||||||
ideal_cursor_column: &mut usize,
|
|
||||||
) -> Result<Option<String>> {
|
|
||||||
const CONTEXT_ACTIONS_FORM: &[&str] = &[
|
|
||||||
"previous_entry",
|
|
||||||
"next_entry",
|
|
||||||
];
|
|
||||||
const CONTEXT_ACTIONS_LOGIN: &[&str] = &[
|
|
||||||
"previous_entry",
|
|
||||||
"next_entry",
|
|
||||||
];
|
|
||||||
|
|
||||||
if app_state.ui.show_form && CONTEXT_ACTIONS_FORM.contains(&action) {
|
|
||||||
Ok(Some(crate::tui::functions::form::handle_action(
|
|
||||||
action,
|
|
||||||
form_state,
|
|
||||||
grpc_client,
|
|
||||||
ideal_cursor_column,
|
|
||||||
).await?))
|
|
||||||
} else if app_state.ui.show_login && CONTEXT_ACTIONS_LOGIN.contains(&action) {
|
|
||||||
Ok(Some(crate::tui::functions::login::handle_action(action).await?))
|
|
||||||
} else {
|
|
||||||
Ok(None) // Not a context action, use regular canvas dispatch
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn handle_form_readonly_with_canvas(
|
|
||||||
key_event: KeyEvent,
|
|
||||||
config: &Config,
|
|
||||||
form_state: &mut FormState,
|
|
||||||
ideal_cursor_column: &mut usize,
|
|
||||||
) -> Result<String> {
|
|
||||||
// Try canvas action from key first
|
|
||||||
let canvas_config = canvas::config::CanvasConfig::load();
|
|
||||||
if let Some(action_name) = canvas_config.get_read_only_action(key_event.code, key_event.modifiers) {
|
|
||||||
let canvas_action = CanvasAction::from_string(action_name);
|
|
||||||
match ActionDispatcher::dispatch(canvas_action, form_state, ideal_cursor_column).await {
|
|
||||||
Ok(ActionResult::Success(msg)) => {
|
|
||||||
return Ok(msg.unwrap_or_default());
|
|
||||||
}
|
|
||||||
Ok(ActionResult::HandledByFeature(msg)) => {
|
|
||||||
return Ok(msg);
|
|
||||||
}
|
|
||||||
Ok(ActionResult::Error(msg)) => {
|
|
||||||
return Ok(format!("Error: {}", msg));
|
|
||||||
}
|
|
||||||
Ok(ActionResult::RequiresContext(msg)) => {
|
|
||||||
return Ok(format!("Context needed: {}", msg));
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
// Fall through to try config mapping
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try config-mapped action
|
|
||||||
if let Some(action_str) = config.get_read_only_action_for_key(key_event.code, key_event.modifiers) {
|
|
||||||
let canvas_action = CanvasAction::from_string(&action_str);
|
|
||||||
match ActionDispatcher::dispatch(canvas_action, form_state, ideal_cursor_column).await {
|
|
||||||
Ok(ActionResult::Success(msg)) => {
|
|
||||||
return Ok(msg.unwrap_or_default());
|
|
||||||
}
|
|
||||||
Ok(ActionResult::HandledByFeature(msg)) => {
|
|
||||||
return Ok(msg);
|
|
||||||
}
|
|
||||||
Ok(ActionResult::Error(msg)) => {
|
|
||||||
return Ok(format!("Error: {}", msg));
|
|
||||||
}
|
|
||||||
Ok(ActionResult::RequiresContext(msg)) => {
|
|
||||||
return Ok(format!("Context needed: {}", msg));
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
return Ok(format!("Action failed: {}", e));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(String::new())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn handle_read_only_event(
|
|
||||||
app_state: &mut AppState,
|
|
||||||
key: KeyEvent,
|
|
||||||
config: &Config,
|
|
||||||
form_state: &mut FormState,
|
|
||||||
login_state: &mut LoginState,
|
|
||||||
register_state: &mut RegisterState,
|
|
||||||
add_table_state: &mut AddTableState,
|
|
||||||
add_logic_state: &mut AddLogicState,
|
|
||||||
key_sequence_tracker: &mut KeySequenceTracker,
|
|
||||||
grpc_client: &mut GrpcClient,
|
|
||||||
command_message: &mut String,
|
|
||||||
edit_mode_cooldown: &mut bool,
|
|
||||||
ideal_cursor_column: &mut usize,
|
|
||||||
) -> Result<(bool, String)> {
|
|
||||||
if config.is_enter_edit_mode_before(key.code, key.modifiers) {
|
|
||||||
*edit_mode_cooldown = true;
|
|
||||||
*command_message = "Entering Edit mode".to_string();
|
|
||||||
return Ok((false, command_message.clone()));
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.is_enter_edit_mode_after(key.code, key.modifiers) {
|
|
||||||
// Determine target state to adjust cursor - all states now use CanvasState trait
|
|
||||||
if app_state.ui.show_login {
|
|
||||||
let current_input = login_state.get_current_input();
|
|
||||||
let current_pos = login_state.current_cursor_pos();
|
|
||||||
if !current_input.is_empty() && current_pos < current_input.len() {
|
|
||||||
login_state.set_current_cursor_pos(current_pos + 1);
|
|
||||||
*ideal_cursor_column = login_state.current_cursor_pos();
|
|
||||||
}
|
|
||||||
} else if app_state.ui.show_add_logic {
|
|
||||||
let current_input = add_logic_state.get_current_input();
|
|
||||||
let current_pos = add_logic_state.current_cursor_pos();
|
|
||||||
if !current_input.is_empty() && current_pos < current_input.len() {
|
|
||||||
add_logic_state.set_current_cursor_pos(current_pos + 1);
|
|
||||||
*ideal_cursor_column = add_logic_state.current_cursor_pos();
|
|
||||||
}
|
|
||||||
} else if app_state.ui.show_register {
|
|
||||||
let current_input = register_state.get_current_input();
|
|
||||||
let current_pos = register_state.current_cursor_pos();
|
|
||||||
if !current_input.is_empty() && current_pos < current_input.len() {
|
|
||||||
register_state.set_current_cursor_pos(current_pos + 1);
|
|
||||||
*ideal_cursor_column = register_state.current_cursor_pos();
|
|
||||||
}
|
|
||||||
} else if app_state.ui.show_add_table {
|
|
||||||
let current_input = add_table_state.get_current_input();
|
|
||||||
let current_pos = add_table_state.current_cursor_pos();
|
|
||||||
if !current_input.is_empty() && current_pos < current_input.len() {
|
|
||||||
add_table_state.set_current_cursor_pos(current_pos + 1);
|
|
||||||
*ideal_cursor_column = add_table_state.current_cursor_pos();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Handle FormState
|
|
||||||
let current_input = form_state.get_current_input();
|
|
||||||
let current_pos = form_state.current_cursor_pos();
|
|
||||||
if !current_input.is_empty() && current_pos < current_input.len() {
|
|
||||||
form_state.set_current_cursor_pos(current_pos + 1);
|
|
||||||
*ideal_cursor_column = form_state.current_cursor_pos();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
*edit_mode_cooldown = true;
|
|
||||||
*command_message = "Entering Edit mode (after cursor)".to_string();
|
|
||||||
return Ok((false, command_message.clone()));
|
|
||||||
}
|
|
||||||
|
|
||||||
if key.modifiers.is_empty() {
|
|
||||||
key_sequence_tracker.add_key(key.code);
|
|
||||||
let sequence = key_sequence_tracker.get_sequence();
|
|
||||||
|
|
||||||
if let Some(action) = config.matches_key_sequence_generalized(&sequence).as_deref() {
|
|
||||||
// Try context-specific actions first, otherwise use canvas dispatch
|
|
||||||
let result = if let Some(context_result) = handle_context_action(
|
|
||||||
action,
|
|
||||||
app_state,
|
|
||||||
form_state,
|
|
||||||
grpc_client,
|
|
||||||
ideal_cursor_column,
|
|
||||||
).await? {
|
|
||||||
context_result
|
|
||||||
} else {
|
|
||||||
dispatch_to_active_state(
|
|
||||||
action,
|
|
||||||
app_state,
|
|
||||||
form_state,
|
|
||||||
login_state,
|
|
||||||
register_state,
|
|
||||||
add_table_state,
|
|
||||||
add_logic_state,
|
|
||||||
ideal_cursor_column,
|
|
||||||
).await
|
|
||||||
};
|
|
||||||
key_sequence_tracker.reset();
|
|
||||||
return Ok((false, result));
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.is_key_sequence_prefix(&sequence) {
|
|
||||||
return Ok((false, command_message.clone()));
|
|
||||||
}
|
|
||||||
|
|
||||||
if sequence.len() == 1 && !config.is_key_sequence_prefix(&sequence) {
|
|
||||||
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers).as_deref() {
|
|
||||||
// Try context-specific actions first, otherwise use canvas dispatch
|
|
||||||
let result = if let Some(context_result) = handle_context_action(
|
|
||||||
action,
|
|
||||||
app_state,
|
|
||||||
form_state,
|
|
||||||
grpc_client,
|
|
||||||
ideal_cursor_column,
|
|
||||||
).await? {
|
|
||||||
context_result
|
|
||||||
} else {
|
|
||||||
dispatch_to_active_state(
|
|
||||||
action,
|
|
||||||
app_state,
|
|
||||||
form_state,
|
|
||||||
login_state,
|
|
||||||
register_state,
|
|
||||||
add_table_state,
|
|
||||||
add_logic_state,
|
|
||||||
ideal_cursor_column,
|
|
||||||
).await
|
|
||||||
};
|
|
||||||
key_sequence_tracker.reset();
|
|
||||||
return Ok((false, result));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
key_sequence_tracker.reset();
|
|
||||||
} else {
|
|
||||||
key_sequence_tracker.reset();
|
|
||||||
|
|
||||||
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers).as_deref() {
|
|
||||||
// Try context-specific actions first, otherwise use canvas dispatch
|
|
||||||
let result = if let Some(context_result) = handle_context_action(
|
|
||||||
action,
|
|
||||||
app_state,
|
|
||||||
form_state,
|
|
||||||
grpc_client,
|
|
||||||
ideal_cursor_column,
|
|
||||||
).await? {
|
|
||||||
context_result
|
|
||||||
} else {
|
|
||||||
dispatch_to_active_state(
|
|
||||||
action,
|
|
||||||
app_state,
|
|
||||||
form_state,
|
|
||||||
login_state,
|
|
||||||
register_state,
|
|
||||||
add_table_state,
|
|
||||||
add_logic_state,
|
|
||||||
ideal_cursor_column,
|
|
||||||
).await
|
|
||||||
};
|
|
||||||
return Ok((false, result));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !*edit_mode_cooldown {
|
|
||||||
let default_key = "i".to_string();
|
|
||||||
let edit_key = config
|
|
||||||
.keybindings
|
|
||||||
.read_only
|
|
||||||
.get("enter_edit_mode_before")
|
|
||||||
.and_then(|keys| keys.first())
|
|
||||||
.map(|k| k.to_string())
|
|
||||||
.unwrap_or(default_key);
|
|
||||||
*command_message = format!("Read-only mode - press {} to edit", edit_key);
|
|
||||||
}
|
|
||||||
|
|
||||||
*edit_mode_cooldown = false;
|
|
||||||
|
|
||||||
Ok((false, command_message.clone()))
|
|
||||||
}
|
|
||||||
@@ -2,7 +2,6 @@
|
|||||||
use crate::tui::terminal::core::TerminalCore;
|
use crate::tui::terminal::core::TerminalCore;
|
||||||
use crate::state::app::state::AppState;
|
use crate::state::app::state::AppState;
|
||||||
use crate::state::pages::{form::FormState, auth::LoginState, auth::RegisterState};
|
use crate::state::pages::{form::FormState, auth::LoginState, auth::RegisterState};
|
||||||
use canvas::canvas::CanvasState;
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
pub struct CommandHandler;
|
pub struct CommandHandler;
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ use crate::state::pages::admin::AdminState;
|
|||||||
use crate::ui::handlers::context::UiContext;
|
use crate::ui::handlers::context::UiContext;
|
||||||
use crate::modes::handlers::event::EventOutcome;
|
use crate::modes::handlers::event::EventOutcome;
|
||||||
use crate::modes::general::command_navigation::{handle_command_navigation_event, NavigationState};
|
use crate::modes::general::command_navigation::{handle_command_navigation_event, NavigationState};
|
||||||
use canvas::canvas::CanvasState;
|
use canvas::DataProvider;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
pub async fn handle_navigation_event(
|
pub async fn handle_navigation_event(
|
||||||
@@ -90,10 +90,10 @@ pub fn move_up(app_state: &mut AppState, login_state: &mut LoginState, register_
|
|||||||
if app_state.focused_button_index == 0 {
|
if app_state.focused_button_index == 0 {
|
||||||
app_state.ui.focus_outside_canvas = false;
|
app_state.ui.focus_outside_canvas = false;
|
||||||
if app_state.ui.show_login {
|
if app_state.ui.show_login {
|
||||||
let last_field_index = login_state.fields().len().saturating_sub(1);
|
let last_field_index = login_state.field_count().saturating_sub(1);
|
||||||
login_state.set_current_field(last_field_index);
|
login_state.set_current_field(last_field_index);
|
||||||
} else {
|
} else {
|
||||||
let last_field_index = register_state.fields().len().saturating_sub(1);
|
let last_field_index = register_state.field_count().saturating_sub(1);
|
||||||
register_state.set_current_field(last_field_index);
|
register_state.set_current_field(last_field_index);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -10,15 +10,13 @@ use crate::modes::general::command_navigation::{
|
|||||||
handle_command_navigation_event, NavigationState,
|
handle_command_navigation_event, NavigationState,
|
||||||
};
|
};
|
||||||
use crate::modes::{
|
use crate::modes::{
|
||||||
canvas::{common_mode, edit, read_only},
|
|
||||||
common::{command_mode, commands::CommandHandler},
|
common::{command_mode, commands::CommandHandler},
|
||||||
general::{dialog, navigation},
|
general::{dialog, navigation},
|
||||||
handlers::mode_manager::{AppMode, ModeManager},
|
handlers::mode_manager::{AppMode, ModeManager},
|
||||||
};
|
};
|
||||||
use crate::services::auth::AuthClient;
|
use crate::services::auth::AuthClient;
|
||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
use canvas::{canvas::CanvasAction, dispatcher::ActionDispatcher};
|
use canvas::FormEditor;
|
||||||
use canvas::canvas::CanvasState; // Only need this import now
|
|
||||||
use crate::state::{
|
use crate::state::{
|
||||||
app::{
|
app::{
|
||||||
buffer::{AppView, BufferState},
|
buffer::{AppView, BufferState},
|
||||||
@@ -756,18 +754,17 @@ impl EventHandler {
|
|||||||
if let Some(action) = config.get_common_action(key_code, modifiers) {
|
if let Some(action) = config.get_common_action(key_code, modifiers) {
|
||||||
match action {
|
match action {
|
||||||
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
||||||
return common_mode::handle_core_action(
|
return self
|
||||||
action,
|
.handle_core_action(
|
||||||
form_state,
|
action,
|
||||||
auth_state,
|
form_state,
|
||||||
login_state,
|
auth_state,
|
||||||
register_state,
|
login_state,
|
||||||
&mut self.grpc_client,
|
register_state,
|
||||||
&mut self.auth_client,
|
terminal,
|
||||||
terminal,
|
app_state,
|
||||||
app_state,
|
)
|
||||||
)
|
.await;
|
||||||
.await;
|
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
@@ -775,34 +772,18 @@ impl EventHandler {
|
|||||||
|
|
||||||
// Try canvas action for form first
|
// Try canvas action for form first
|
||||||
if app_state.ui.show_form {
|
if app_state.ui.show_form {
|
||||||
|
let mut editor = FormEditor::new(form_state.clone());
|
||||||
if let Ok(Some(canvas_message)) = self.handle_form_canvas_action(
|
if let Ok(Some(canvas_message)) = self.handle_form_canvas_action(
|
||||||
key_event,
|
key_event,
|
||||||
form_state,
|
&mut editor,
|
||||||
|
config,
|
||||||
false,
|
false,
|
||||||
).await {
|
).await {
|
||||||
return Ok(EventOutcome::Ok(canvas_message));
|
return Ok(EventOutcome::Ok(canvas_message));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to legacy read-only event handling
|
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||||
let (_should_exit, message) = read_only::handle_read_only_event(
|
|
||||||
app_state,
|
|
||||||
key_event,
|
|
||||||
config,
|
|
||||||
form_state,
|
|
||||||
login_state,
|
|
||||||
register_state,
|
|
||||||
&mut admin_state.add_table_state,
|
|
||||||
&mut admin_state.add_logic_state,
|
|
||||||
&mut self.key_sequence_tracker,
|
|
||||||
&mut self.grpc_client,
|
|
||||||
&mut self.command_message,
|
|
||||||
&mut self.edit_mode_cooldown,
|
|
||||||
&mut self.ideal_cursor_column,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
return Ok(EventOutcome::Ok(message));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AppMode::Highlight => {
|
AppMode::Highlight => {
|
||||||
@@ -820,24 +801,7 @@ impl EventHandler {
|
|||||||
return Ok(EventOutcome::Ok("".to_string()));
|
return Ok(EventOutcome::Ok("".to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let (_should_exit, message) =
|
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||||
read_only::handle_read_only_event(
|
|
||||||
app_state,
|
|
||||||
key_event,
|
|
||||||
config,
|
|
||||||
form_state,
|
|
||||||
login_state,
|
|
||||||
register_state,
|
|
||||||
&mut admin_state.add_table_state,
|
|
||||||
&mut admin_state.add_logic_state,
|
|
||||||
&mut self.key_sequence_tracker,
|
|
||||||
&mut self.grpc_client,
|
|
||||||
&mut self.command_message,
|
|
||||||
&mut self.edit_mode_cooldown,
|
|
||||||
&mut self.ideal_cursor_column,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
return Ok(EventOutcome::Ok(message));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AppMode::Edit => {
|
AppMode::Edit => {
|
||||||
@@ -845,18 +809,17 @@ impl EventHandler {
|
|||||||
if let Some(action) = config.get_common_action(key_code, modifiers) {
|
if let Some(action) = config.get_common_action(key_code, modifiers) {
|
||||||
match action {
|
match action {
|
||||||
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
||||||
return common_mode::handle_core_action(
|
return self
|
||||||
action,
|
.handle_core_action(
|
||||||
form_state,
|
action,
|
||||||
auth_state,
|
form_state,
|
||||||
login_state,
|
auth_state,
|
||||||
register_state,
|
login_state,
|
||||||
&mut self.grpc_client,
|
register_state,
|
||||||
&mut self.auth_client,
|
terminal,
|
||||||
terminal,
|
app_state,
|
||||||
app_state,
|
)
|
||||||
)
|
.await;
|
||||||
.await;
|
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
@@ -864,9 +827,11 @@ impl EventHandler {
|
|||||||
|
|
||||||
// Try canvas action for form first
|
// Try canvas action for form first
|
||||||
if app_state.ui.show_form {
|
if app_state.ui.show_form {
|
||||||
|
let mut editor = FormEditor::new(form_state.clone());
|
||||||
if let Ok(Some(canvas_message)) = self.handle_form_canvas_action(
|
if let Ok(Some(canvas_message)) = self.handle_form_canvas_action(
|
||||||
key_event,
|
key_event,
|
||||||
form_state,
|
&mut editor,
|
||||||
|
config,
|
||||||
true,
|
true,
|
||||||
).await {
|
).await {
|
||||||
if !canvas_message.is_empty() {
|
if !canvas_message.is_empty() {
|
||||||
@@ -876,88 +841,7 @@ impl EventHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle legacy edit events
|
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||||
let mut current_position = form_state.current_position;
|
|
||||||
let total_count = form_state.total_count;
|
|
||||||
|
|
||||||
let edit_result = edit::handle_edit_event(
|
|
||||||
key_event,
|
|
||||||
config,
|
|
||||||
form_state,
|
|
||||||
login_state,
|
|
||||||
register_state,
|
|
||||||
admin_state,
|
|
||||||
&mut current_position,
|
|
||||||
total_count,
|
|
||||||
self,
|
|
||||||
app_state,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
match edit_result {
|
|
||||||
Ok(edit::EditEventOutcome::ExitEditMode) => {
|
|
||||||
self.is_edit_mode = false;
|
|
||||||
self.edit_mode_cooldown = true;
|
|
||||||
|
|
||||||
// Check for unsaved changes across all states
|
|
||||||
let has_changes = Self::get_has_unsaved_changes_for_state(
|
|
||||||
app_state,
|
|
||||||
login_state,
|
|
||||||
register_state,
|
|
||||||
form_state
|
|
||||||
);
|
|
||||||
|
|
||||||
// Set appropriate message based on changes
|
|
||||||
self.command_message = if has_changes {
|
|
||||||
"Exited edit mode (unsaved changes remain)".to_string()
|
|
||||||
} else {
|
|
||||||
"Read-only mode".to_string()
|
|
||||||
};
|
|
||||||
|
|
||||||
terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
|
|
||||||
|
|
||||||
// Get current input and cursor position
|
|
||||||
let current_input = Self::get_current_input_for_state(
|
|
||||||
app_state,
|
|
||||||
login_state,
|
|
||||||
register_state,
|
|
||||||
form_state
|
|
||||||
);
|
|
||||||
let current_cursor_pos = Self::get_current_cursor_pos_for_state(
|
|
||||||
app_state,
|
|
||||||
login_state,
|
|
||||||
register_state,
|
|
||||||
form_state
|
|
||||||
);
|
|
||||||
|
|
||||||
// Adjust cursor if it's beyond the input length
|
|
||||||
if !current_input.is_empty() && current_cursor_pos >= current_input.len() {
|
|
||||||
let new_pos = current_input.len() - 1;
|
|
||||||
Self::set_current_cursor_pos_for_state(
|
|
||||||
app_state,
|
|
||||||
login_state,
|
|
||||||
register_state,
|
|
||||||
form_state,
|
|
||||||
new_pos
|
|
||||||
);
|
|
||||||
self.ideal_cursor_column = new_pos;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(edit::EditEventOutcome::Message(msg)) => {
|
|
||||||
if !msg.is_empty() {
|
|
||||||
self.command_message = msg;
|
|
||||||
}
|
|
||||||
self.key_sequence_tracker.reset();
|
|
||||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
|
||||||
}
|
|
||||||
|
|
||||||
Err(e) => {
|
|
||||||
return Err(e.into());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AppMode::Command => {
|
AppMode::Command => {
|
||||||
@@ -1101,67 +985,159 @@ impl EventHandler {
|
|||||||
async fn handle_form_canvas_action(
|
async fn handle_form_canvas_action(
|
||||||
&mut self,
|
&mut self,
|
||||||
key_event: KeyEvent,
|
key_event: KeyEvent,
|
||||||
form_state: &mut FormState,
|
editor: &mut FormEditor<FormState>,
|
||||||
|
config: &Config,
|
||||||
is_edit_mode: bool,
|
is_edit_mode: bool,
|
||||||
) -> Result<Option<String>> {
|
) -> Result<Option<String>> {
|
||||||
let canvas_config = canvas::config::CanvasConfig::load();
|
use crossterm::event::KeyCode;
|
||||||
|
|
||||||
// PRIORITY 1: Handle character insertion in edit mode FIRST
|
|
||||||
if is_edit_mode {
|
if is_edit_mode {
|
||||||
if let KeyCode::Char(c) = key_event.code {
|
if let KeyCode::Char(c) = key_event.code {
|
||||||
// Only insert if it's not a special modifier combination
|
|
||||||
if key_event.modifiers.is_empty() || key_event.modifiers == KeyModifiers::SHIFT {
|
if key_event.modifiers.is_empty() || key_event.modifiers == KeyModifiers::SHIFT {
|
||||||
let canvas_action = CanvasAction::InsertChar(c);
|
editor.insert_char(c)?;
|
||||||
match ActionDispatcher::dispatch(
|
return Ok(Some(format!("Inserted '{}'", c)));
|
||||||
canvas_action,
|
}
|
||||||
form_state,
|
}
|
||||||
&mut self.ideal_cursor_column,
|
}
|
||||||
).await {
|
|
||||||
Ok(result) => {
|
// Use your config to resolve actions
|
||||||
return Ok(Some(result.message().unwrap_or("").to_string()));
|
if let Some(action) = config.get_edit_action_for_key(key_event.code, key_event.modifiers) {
|
||||||
}
|
match action {
|
||||||
Err(_) => {
|
"delete_char_backward" => { editor.delete_backward()?; return Ok(Some("Deleted backward".to_string())); }
|
||||||
return Ok(Some("Character insertion failed".to_string()));
|
"delete_char_forward" => { editor.delete_forward()?; return Ok(Some("Deleted forward".to_string())); }
|
||||||
}
|
"move_left" => { editor.move_left()?; return Ok(Some("Moved left".to_string())); }
|
||||||
|
"move_right" => { editor.move_right()?; return Ok(Some("Moved right".to_string())); }
|
||||||
|
"move_up" => { editor.move_up()?; return Ok(Some("Moved up".to_string())); }
|
||||||
|
"move_down" => { editor.move_down()?; return Ok(Some("Moved down".to_string())); }
|
||||||
|
|
||||||
|
"move_line_start" => { editor.move_line_start(); return Ok(Some("Line start".to_string())); }
|
||||||
|
"move_line_end" => { editor.move_line_end(); return Ok(Some("Line end".to_string())); }
|
||||||
|
"move_word_next" => { editor.move_word_next(); return Ok(Some("Next word".to_string())); }
|
||||||
|
"move_word_prev" => { editor.move_word_prev(); return Ok(Some("Prev word".to_string())); }
|
||||||
|
"move_word_end" => { editor.move_word_end(); return Ok(Some("Word end".to_string())); }
|
||||||
|
"move_word_end_prev" => { editor.move_word_end_prev(); return Ok(Some("Prev word end".to_string())); }
|
||||||
|
|
||||||
|
"next_field" => { editor.next_field()?; return Ok(Some("Next field".to_string())); }
|
||||||
|
"prev_field" => { editor.prev_field()?; return Ok(Some("Prev field".to_string())); }
|
||||||
|
"open_suggestions" => {
|
||||||
|
let field_index = editor.current_field();
|
||||||
|
editor.open_suggestions(field_index);
|
||||||
|
return Ok(Some("Opened suggestions".to_string()));
|
||||||
|
}
|
||||||
|
"apply_suggestion" | "enter_decider" => {
|
||||||
|
if let Some(s) = editor.apply_suggestion() {
|
||||||
|
return Ok(Some(format!("Applied suggestion: {}", s)));
|
||||||
|
} else {
|
||||||
|
return Ok(Some("No suggestion applied".to_string()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
"exit" | "exit_edit_mode" => {
|
||||||
|
editor.exit_edit_mode()?;
|
||||||
|
return Ok(Some("Exited edit mode".to_string()));
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PRIORITY 2: Handle config-mapped actions for non-character keys
|
|
||||||
let action_str = canvas_config.get_action_for_key(
|
|
||||||
key_event.code,
|
|
||||||
key_event.modifiers,
|
|
||||||
is_edit_mode,
|
|
||||||
form_state.autocomplete_active,
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Some(action_str) = action_str {
|
|
||||||
// Skip mode transition actions - let the main event handler deal with them
|
|
||||||
if Self::is_mode_transition_action(action_str) {
|
|
||||||
return Ok(None);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Execute the config-mapped action
|
|
||||||
let canvas_action = CanvasAction::from_string(action_str);
|
|
||||||
match ActionDispatcher::dispatch(
|
|
||||||
canvas_action,
|
|
||||||
form_state,
|
|
||||||
&mut self.ideal_cursor_column,
|
|
||||||
).await {
|
|
||||||
Ok(result) => {
|
|
||||||
return Ok(Some(result.message().unwrap_or("").to_string()));
|
|
||||||
}
|
|
||||||
Err(_) => {
|
|
||||||
return Ok(Some("Canvas action failed".to_string()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// No action found
|
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn handle_core_action(
|
||||||
|
&mut self,
|
||||||
|
action: &str,
|
||||||
|
form_state: &mut FormState,
|
||||||
|
auth_state: &mut AuthState,
|
||||||
|
login_state: &mut LoginState,
|
||||||
|
register_state: &mut RegisterState,
|
||||||
|
terminal: &mut TerminalCore,
|
||||||
|
app_state: &mut AppState,
|
||||||
|
) -> Result<EventOutcome> {
|
||||||
|
match action {
|
||||||
|
"save" => {
|
||||||
|
if app_state.ui.show_login {
|
||||||
|
let message = crate::tui::functions::common::login::save(
|
||||||
|
auth_state,
|
||||||
|
login_state,
|
||||||
|
&mut self.auth_client,
|
||||||
|
app_state,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
Ok(EventOutcome::Ok(message))
|
||||||
|
} else {
|
||||||
|
let save_outcome = crate::tui::functions::common::form::save(
|
||||||
|
app_state,
|
||||||
|
form_state,
|
||||||
|
&mut self.grpc_client,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
let message = match save_outcome {
|
||||||
|
SaveOutcome::NoChange => "No changes to save.".to_string(),
|
||||||
|
SaveOutcome::UpdatedExisting => "Entry updated.".to_string(),
|
||||||
|
SaveOutcome::CreatedNew(_) => "New entry created.".to_string(),
|
||||||
|
};
|
||||||
|
Ok(EventOutcome::DataSaved(save_outcome, message))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"force_quit" => {
|
||||||
|
terminal.cleanup()?;
|
||||||
|
Ok(EventOutcome::Exit(
|
||||||
|
"Force exiting without saving.".to_string(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
"save_and_quit" => {
|
||||||
|
let message = if app_state.ui.show_login {
|
||||||
|
crate::tui::functions::common::login::save(
|
||||||
|
auth_state,
|
||||||
|
login_state,
|
||||||
|
&mut self.auth_client,
|
||||||
|
app_state,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
} else {
|
||||||
|
let save_outcome = crate::tui::functions::common::form::save(
|
||||||
|
app_state,
|
||||||
|
form_state,
|
||||||
|
&mut self.grpc_client,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
match save_outcome {
|
||||||
|
SaveOutcome::NoChange => "No changes to save.".to_string(),
|
||||||
|
SaveOutcome::UpdatedExisting => "Entry updated.".to_string(),
|
||||||
|
SaveOutcome::CreatedNew(_) => "New entry created.".to_string(),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
terminal.cleanup()?;
|
||||||
|
Ok(EventOutcome::Exit(format!(
|
||||||
|
"{}. Exiting application.",
|
||||||
|
message
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
"revert" => {
|
||||||
|
let message = if app_state.ui.show_login {
|
||||||
|
crate::tui::functions::common::login::revert(login_state, app_state)
|
||||||
|
.await
|
||||||
|
} else if app_state.ui.show_register {
|
||||||
|
crate::tui::functions::common::register::revert(
|
||||||
|
register_state,
|
||||||
|
app_state,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
} else {
|
||||||
|
crate::tui::functions::common::form::revert(
|
||||||
|
form_state,
|
||||||
|
&mut self.grpc_client,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
};
|
||||||
|
Ok(EventOutcome::Ok(message))
|
||||||
|
}
|
||||||
|
_ => Ok(EventOutcome::Ok(format!(
|
||||||
|
"Core action not handled: {}",
|
||||||
|
action
|
||||||
|
))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn is_mode_transition_action(action: &str) -> bool {
|
fn is_mode_transition_action(action: &str) -> bool {
|
||||||
matches!(action,
|
matches!(action,
|
||||||
"exit" |
|
"exit" |
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
// src/client/modes/highlight.rs
|
|
||||||
pub mod highlight;
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
// src/modes/highlight/highlight.rs
|
|
||||||
// (This file is intentionally simple for now, reusing ReadOnly logic)
|
|
||||||
|
|
||||||
use crate::config::binds::config::Config;
|
|
||||||
use crate::config::binds::key_sequences::KeySequenceTracker;
|
|
||||||
use crate::services::grpc_client::GrpcClient;
|
|
||||||
use crate::state::app::state::AppState;
|
|
||||||
use crate::state::pages::auth::{LoginState, RegisterState};
|
|
||||||
use crate::state::pages::admin::AdminState;
|
|
||||||
use crate::state::pages::form::FormState;
|
|
||||||
use crate::modes::handlers::event::EventOutcome;
|
|
||||||
use crate::modes::read_only;
|
|
||||||
use crossterm::event::KeyEvent;
|
|
||||||
use anyhow::Result;
|
|
||||||
|
|
||||||
/// Handles events when in Highlight mode.
|
|
||||||
/// Currently, it mostly delegates to the read_only handler for movement.
|
|
||||||
/// Exiting highlight mode is handled directly in the main event handler.
|
|
||||||
pub async fn handle_highlight_event(
|
|
||||||
app_state: &mut AppState,
|
|
||||||
key: KeyEvent,
|
|
||||||
config: &Config,
|
|
||||||
form_state: &mut FormState,
|
|
||||||
login_state: &mut LoginState,
|
|
||||||
register_state: &mut RegisterState,
|
|
||||||
admin_state: &mut AdminState,
|
|
||||||
key_sequence_tracker: &mut KeySequenceTracker,
|
|
||||||
current_position: &mut u64,
|
|
||||||
total_count: u64,
|
|
||||||
grpc_client: &mut GrpcClient,
|
|
||||||
command_message: &mut String,
|
|
||||||
edit_mode_cooldown: &mut bool,
|
|
||||||
ideal_cursor_column: &mut usize,
|
|
||||||
) -> Result<EventOutcome> {
|
|
||||||
// Delegate movement and other actions to the read_only handler
|
|
||||||
// The rendering logic will use the highlight_anchor to draw the selection
|
|
||||||
let (should_exit, message) = read_only::handle_read_only_event(
|
|
||||||
app_state,
|
|
||||||
key,
|
|
||||||
config,
|
|
||||||
form_state,
|
|
||||||
login_state,
|
|
||||||
register_state,
|
|
||||||
&mut admin_state.add_table_state,
|
|
||||||
&mut admin_state.add_logic_state,
|
|
||||||
key_sequence_tracker,
|
|
||||||
grpc_client,
|
|
||||||
command_message, // Pass the message buffer
|
|
||||||
edit_mode_cooldown,
|
|
||||||
ideal_cursor_column,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// ReadOnly handler doesn't return EventOutcome directly, adapt if needed
|
|
||||||
// For now, assume Ok outcome unless ReadOnly signals an exit (which we ignore here)
|
|
||||||
if should_exit {
|
|
||||||
// This exit is likely for the whole app, let the main loop handle it
|
|
||||||
// We just return the message from read_only
|
|
||||||
Ok(EventOutcome::Ok(message))
|
|
||||||
} else {
|
|
||||||
Ok(EventOutcome::Ok(message))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,11 +1,10 @@
|
|||||||
// src/client/modes/mod.rs
|
// src/client/modes/mod.rs
|
||||||
pub mod handlers;
|
pub mod handlers;
|
||||||
pub mod canvas;
|
|
||||||
pub mod general;
|
pub mod general;
|
||||||
pub mod common;
|
pub mod common;
|
||||||
pub mod highlight;
|
pub mod canvas;
|
||||||
|
|
||||||
pub use handlers::*;
|
pub use handlers::*;
|
||||||
pub use canvas::*;
|
|
||||||
pub use general::*;
|
pub use general::*;
|
||||||
pub use common::*;
|
pub use common::*;
|
||||||
|
pub use canvas::*;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// src/state/pages/add_logic.rs
|
// src/state/pages/add_logic.rs
|
||||||
use crate::config::binds::config::{EditorConfig, EditorKeybindingMode};
|
use crate::config::binds::config::{EditorConfig, EditorKeybindingMode};
|
||||||
use canvas::canvas::{CanvasState, ActionContext, CanvasAction, AppMode};
|
|
||||||
use crate::components::common::text_editor::{TextEditor, VimState};
|
use crate::components::common::text_editor::{TextEditor, VimState};
|
||||||
|
use canvas::{DataProvider, AppMode};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use tui_textarea::TextArea;
|
use tui_textarea::TextArea;
|
||||||
@@ -277,174 +277,41 @@ impl Default for AddLogicState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implement external library's CanvasState for AddLogicState
|
impl DataProvider for AddLogicState {
|
||||||
impl CanvasState for AddLogicState {
|
fn field_count(&self) -> usize {
|
||||||
fn current_field(&self) -> usize {
|
3 // Logic Name, Target Column, Description
|
||||||
match self.current_focus {
|
}
|
||||||
AddLogicFocus::InputLogicName => 0,
|
|
||||||
AddLogicFocus::InputTargetColumn => 1,
|
fn field_name(&self, index: usize) -> &str {
|
||||||
AddLogicFocus::InputDescription => 2,
|
match index {
|
||||||
// If focus is elsewhere, return the last canvas field used
|
0 => "Logic Name",
|
||||||
_ => self.last_canvas_field,
|
1 => "Target Column",
|
||||||
|
2 => "Description",
|
||||||
|
_ => "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_current_field(&mut self, index: usize) {
|
fn field_value(&self, index: usize) -> &str {
|
||||||
let new_focus = match index {
|
match index {
|
||||||
0 => AddLogicFocus::InputLogicName,
|
0 => &self.logic_name_input,
|
||||||
1 => AddLogicFocus::InputTargetColumn,
|
1 => &self.target_column_input,
|
||||||
2 => AddLogicFocus::InputDescription,
|
2 => &self.description_input,
|
||||||
_ => return,
|
_ => "",
|
||||||
};
|
|
||||||
if self.current_focus != new_focus {
|
|
||||||
if self.current_focus == AddLogicFocus::InputTargetColumn {
|
|
||||||
self.in_target_column_suggestion_mode = false;
|
|
||||||
self.show_target_column_suggestions = false;
|
|
||||||
}
|
|
||||||
self.current_focus = new_focus;
|
|
||||||
self.last_canvas_field = index;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn current_cursor_pos(&self) -> usize {
|
fn set_field_value(&mut self, index: usize, value: String) {
|
||||||
match self.current_focus {
|
match index {
|
||||||
AddLogicFocus::InputLogicName => self.logic_name_cursor_pos,
|
0 => self.logic_name_input = value,
|
||||||
AddLogicFocus::InputTargetColumn => self.target_column_cursor_pos,
|
1 => self.target_column_input = value,
|
||||||
AddLogicFocus::InputDescription => self.description_cursor_pos,
|
2 => self.description_input = value,
|
||||||
_ => 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_current_cursor_pos(&mut self, pos: usize) {
|
|
||||||
match self.current_focus {
|
|
||||||
AddLogicFocus::InputLogicName => {
|
|
||||||
self.logic_name_cursor_pos = pos.min(self.logic_name_input.len());
|
|
||||||
}
|
|
||||||
AddLogicFocus::InputTargetColumn => {
|
|
||||||
self.target_column_cursor_pos = pos.min(self.target_column_input.len());
|
|
||||||
}
|
|
||||||
AddLogicFocus::InputDescription => {
|
|
||||||
self.description_cursor_pos = pos.min(self.description_input.len());
|
|
||||||
}
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
self.has_unsaved_changes = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_current_input(&self) -> &str {
|
fn supports_suggestions(&self, field_index: usize) -> bool {
|
||||||
match self.current_focus {
|
// Only Target Column supports suggestions
|
||||||
AddLogicFocus::InputLogicName => &self.logic_name_input,
|
field_index == 1
|
||||||
AddLogicFocus::InputTargetColumn => &self.target_column_input,
|
|
||||||
AddLogicFocus::InputDescription => &self.description_input,
|
|
||||||
_ => "", // Should not happen if called correctly
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_current_input_mut(&mut self) -> &mut String {
|
|
||||||
match self.current_focus {
|
|
||||||
AddLogicFocus::InputLogicName => &mut self.logic_name_input,
|
|
||||||
AddLogicFocus::InputTargetColumn => &mut self.target_column_input,
|
|
||||||
AddLogicFocus::InputDescription => &mut self.description_input,
|
|
||||||
_ => &mut self.logic_name_input, // Fallback
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn inputs(&self) -> Vec<&String> {
|
|
||||||
vec![
|
|
||||||
&self.logic_name_input,
|
|
||||||
&self.target_column_input,
|
|
||||||
&self.description_input,
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fields(&self) -> Vec<&str> {
|
|
||||||
vec!["Logic Name", "Target Column", "Description"]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn has_unsaved_changes(&self) -> bool {
|
|
||||||
self.has_unsaved_changes
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_has_unsaved_changes(&mut self, changed: bool) {
|
|
||||||
self.has_unsaved_changes = changed;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_feature_action(&mut self, action: &CanvasAction, _context: &ActionContext) -> Option<String> {
|
|
||||||
match action {
|
|
||||||
// Handle saving logic script
|
|
||||||
CanvasAction::Custom(action_str) if action_str == "save_logic" => {
|
|
||||||
self.save_logic()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle clearing the form
|
|
||||||
CanvasAction::Custom(action_str) if action_str == "clear_form" => {
|
|
||||||
self.clear_form()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle target column autocomplete activation
|
|
||||||
CanvasAction::Custom(action_str) if action_str == "activate_autocomplete" => {
|
|
||||||
if self.current_field() == 1 { // Target Column field
|
|
||||||
self.in_target_column_suggestion_mode = true;
|
|
||||||
self.update_target_column_suggestions();
|
|
||||||
Some("Autocomplete activated".to_string())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle target column suggestion selection
|
|
||||||
CanvasAction::Custom(action_str) if action_str == "select_suggestion" => {
|
|
||||||
if self.current_field() == 1 && self.in_target_column_suggestion_mode {
|
|
||||||
if let Some(selected_idx) = self.selected_target_column_suggestion_index {
|
|
||||||
if let Some(suggestion) = self.target_column_suggestions.get(selected_idx) {
|
|
||||||
self.target_column_input = suggestion.clone();
|
|
||||||
self.target_column_cursor_pos = suggestion.len();
|
|
||||||
self.in_target_column_suggestion_mode = false;
|
|
||||||
self.show_target_column_suggestions = false;
|
|
||||||
self.has_unsaved_changes = true;
|
|
||||||
return Some(format!("Selected: {}", suggestion));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
|
|
||||||
// Custom validation when moving between fields
|
|
||||||
CanvasAction::NextField => {
|
|
||||||
match self.current_field() {
|
|
||||||
0 => { // Logic Name field
|
|
||||||
if self.logic_name_input.trim().is_empty() {
|
|
||||||
Some("Logic name cannot be empty".to_string())
|
|
||||||
} else {
|
|
||||||
None // Let canvas library handle the normal field movement
|
|
||||||
}
|
|
||||||
}
|
|
||||||
1 => { // Target Column field
|
|
||||||
// Update suggestions when entering target column field
|
|
||||||
self.update_target_column_suggestions();
|
|
||||||
None
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle character insertion with validation
|
|
||||||
CanvasAction::InsertChar(c) => {
|
|
||||||
if self.current_field() == 1 { // Target Column field
|
|
||||||
// Update suggestions after character insertion
|
|
||||||
// Note: Canvas library will handle the actual insertion
|
|
||||||
// This is just for triggering suggestion updates
|
|
||||||
None // Let canvas handle insertion, then we'll update suggestions
|
|
||||||
} else {
|
|
||||||
None // Let canvas handle normally
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Let canvas library handle everything else
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn current_mode(&self) -> AppMode {
|
|
||||||
self.app_mode
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
// src/state/pages/add_table.rs
|
// src/state/pages/add_table.rs
|
||||||
use canvas::canvas::{CanvasState, ActionContext, CanvasAction, AppMode};
|
|
||||||
|
use canvas::{DataProvider, CanvasAction, AppMode};
|
||||||
use ratatui::widgets::TableState;
|
use ratatui::widgets::TableState;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
@@ -170,137 +171,40 @@ impl AddTableState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implement external library's CanvasState for AddTableState
|
impl DataProvider for AddTableState {
|
||||||
impl CanvasState for AddTableState {
|
fn field_count(&self) -> usize {
|
||||||
fn current_field(&self) -> usize {
|
3 // Table name, Column name, Column type
|
||||||
match self.current_focus {
|
}
|
||||||
AddTableFocus::InputTableName => 0,
|
|
||||||
AddTableFocus::InputColumnName => 1,
|
fn field_name(&self, index: usize) -> &str {
|
||||||
AddTableFocus::InputColumnType => 2,
|
match index {
|
||||||
// If focus is elsewhere, return the last canvas field used
|
0 => "Table name",
|
||||||
_ => self.last_canvas_field,
|
1 => "Name",
|
||||||
|
2 => "Type",
|
||||||
|
_ => "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn current_cursor_pos(&self) -> usize {
|
fn field_value(&self, index: usize) -> &str {
|
||||||
match self.current_focus {
|
match index {
|
||||||
AddTableFocus::InputTableName => self.table_name_cursor_pos,
|
0 => &self.table_name_input,
|
||||||
AddTableFocus::InputColumnName => self.column_name_cursor_pos,
|
1 => &self.column_name_input,
|
||||||
AddTableFocus::InputColumnType => self.column_type_cursor_pos,
|
2 => &self.column_type_input,
|
||||||
_ => 0, // Default if focus is not on an input field
|
_ => "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_current_field(&mut self, index: usize) {
|
fn set_field_value(&mut self, index: usize, value: String) {
|
||||||
// Update both current focus and last canvas field
|
match index {
|
||||||
self.current_focus = match index {
|
0 => self.table_name_input = value,
|
||||||
0 => {
|
1 => self.column_name_input = value,
|
||||||
self.last_canvas_field = 0;
|
2 => self.column_type_input = value,
|
||||||
AddTableFocus::InputTableName
|
_ => {}
|
||||||
},
|
|
||||||
1 => {
|
|
||||||
self.last_canvas_field = 1;
|
|
||||||
AddTableFocus::InputColumnName
|
|
||||||
},
|
|
||||||
2 => {
|
|
||||||
self.last_canvas_field = 2;
|
|
||||||
AddTableFocus::InputColumnType
|
|
||||||
},
|
|
||||||
_ => self.current_focus, // Stay on current focus if index is out of bounds
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_current_cursor_pos(&mut self, pos: usize) {
|
|
||||||
match self.current_focus {
|
|
||||||
AddTableFocus::InputTableName => self.table_name_cursor_pos = pos,
|
|
||||||
AddTableFocus::InputColumnName => self.column_name_cursor_pos = pos,
|
|
||||||
AddTableFocus::InputColumnType => self.column_type_cursor_pos = pos,
|
|
||||||
_ => {} // Do nothing if focus is not on an input field
|
|
||||||
}
|
}
|
||||||
|
self.has_unsaved_changes = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_current_input(&self) -> &str {
|
fn supports_suggestions(&self, _field_index: usize) -> bool {
|
||||||
match self.current_focus {
|
false // AddTableState doesn’t use suggestions
|
||||||
AddTableFocus::InputTableName => &self.table_name_input,
|
|
||||||
AddTableFocus::InputColumnName => &self.column_name_input,
|
|
||||||
AddTableFocus::InputColumnType => &self.column_type_input,
|
|
||||||
_ => "", // Should not happen if called correctly
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_current_input_mut(&mut self) -> &mut String {
|
|
||||||
match self.current_focus {
|
|
||||||
AddTableFocus::InputTableName => &mut self.table_name_input,
|
|
||||||
AddTableFocus::InputColumnName => &mut self.column_name_input,
|
|
||||||
AddTableFocus::InputColumnType => &mut self.column_type_input,
|
|
||||||
_ => &mut self.table_name_input, // Fallback
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn inputs(&self) -> Vec<&String> {
|
|
||||||
vec![&self.table_name_input, &self.column_name_input, &self.column_type_input]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fields(&self) -> Vec<&str> {
|
|
||||||
// These must match the order used in render_add_table
|
|
||||||
vec!["Table name", "Name", "Type"]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn has_unsaved_changes(&self) -> bool {
|
|
||||||
self.has_unsaved_changes
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_has_unsaved_changes(&mut self, changed: bool) {
|
|
||||||
self.has_unsaved_changes = changed;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_feature_action(&mut self, action: &CanvasAction, _context: &ActionContext) -> Option<String> {
|
|
||||||
match action {
|
|
||||||
// Handle adding column when user presses Enter on the Add button or uses specific action
|
|
||||||
CanvasAction::Custom(action_str) if action_str == "add_column" => {
|
|
||||||
self.add_column_from_inputs()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle table saving
|
|
||||||
CanvasAction::Custom(action_str) if action_str == "save_table" => {
|
|
||||||
if self.table_name_input.trim().is_empty() {
|
|
||||||
Some("Table name is required".to_string())
|
|
||||||
} else if self.columns.is_empty() {
|
|
||||||
Some("At least one column is required".to_string())
|
|
||||||
} else {
|
|
||||||
Some(format!("Saving table: {}", self.table_name_input))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle deleting selected items
|
|
||||||
CanvasAction::Custom(action_str) if action_str == "delete_selected" => {
|
|
||||||
self.delete_selected_items()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle canceling (clear form)
|
|
||||||
CanvasAction::Custom(action_str) if action_str == "cancel" => {
|
|
||||||
// Reset to defaults but keep profile_name
|
|
||||||
let profile = self.profile_name.clone();
|
|
||||||
*self = Self::default();
|
|
||||||
self.profile_name = profile;
|
|
||||||
Some("Form cleared".to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Custom validation when moving between fields
|
|
||||||
CanvasAction::NextField => {
|
|
||||||
// When leaving table name field, update the table_name for display
|
|
||||||
if self.current_field() == 0 && !self.table_name_input.trim().is_empty() {
|
|
||||||
self.table_name = self.table_name_input.trim().to_string();
|
|
||||||
}
|
|
||||||
None // Let canvas library handle the normal field movement
|
|
||||||
}
|
|
||||||
|
|
||||||
// Let canvas library handle everything else
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn current_mode(&self) -> AppMode {
|
|
||||||
self.app_mode
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
// src/state/pages/auth.rs
|
// src/state/pages/auth.rs
|
||||||
use canvas::canvas::{CanvasState, ActionContext, CanvasAction, AppMode};
|
use canvas::{DataProvider, AppMode, SuggestionItem};
|
||||||
use canvas::autocomplete::{AutocompleteCanvasState, AutocompleteState, SuggestionItem};
|
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
@@ -22,6 +21,7 @@ pub struct AuthState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Represents the state of the Login form UI
|
/// Represents the state of the Login form UI
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct LoginState {
|
pub struct LoginState {
|
||||||
pub username: String,
|
pub username: String,
|
||||||
pub password: String,
|
pub password: String,
|
||||||
@@ -60,8 +60,10 @@ pub struct RegisterState {
|
|||||||
pub current_field: usize,
|
pub current_field: usize,
|
||||||
pub current_cursor_pos: usize,
|
pub current_cursor_pos: usize,
|
||||||
pub has_unsaved_changes: bool,
|
pub has_unsaved_changes: bool,
|
||||||
pub autocomplete: AutocompleteState<String>,
|
|
||||||
pub app_mode: AppMode,
|
pub app_mode: AppMode,
|
||||||
|
// Keep role suggestions for later integration
|
||||||
|
pub role_suggestions: Vec<String>,
|
||||||
|
pub role_suggestions_active: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for RegisterState {
|
impl Default for RegisterState {
|
||||||
@@ -76,8 +78,9 @@ impl Default for RegisterState {
|
|||||||
current_field: 0,
|
current_field: 0,
|
||||||
current_cursor_pos: 0,
|
current_cursor_pos: 0,
|
||||||
has_unsaved_changes: false,
|
has_unsaved_changes: false,
|
||||||
autocomplete: AutocompleteState::new(),
|
|
||||||
app_mode: AppMode::Edit,
|
app_mode: AppMode::Edit,
|
||||||
|
role_suggestions: AVAILABLE_ROLES.clone(),
|
||||||
|
role_suggestions_active: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,51 +98,27 @@ impl LoginState {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl RegisterState {
|
// Legacy method compatibility
|
||||||
pub fn new() -> Self {
|
pub fn current_field(&self) -> usize {
|
||||||
let mut state = Self {
|
|
||||||
autocomplete: AutocompleteState::new(),
|
|
||||||
app_mode: AppMode::Edit,
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
|
|
||||||
// Initialize autocomplete with role suggestions
|
|
||||||
let suggestions: Vec<SuggestionItem<String>> = AVAILABLE_ROLES
|
|
||||||
.iter()
|
|
||||||
.map(|role| SuggestionItem::simple(role.clone(), role.clone()))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
// Set suggestions but keep inactive initially
|
|
||||||
state.autocomplete.set_suggestions(suggestions);
|
|
||||||
state.autocomplete.is_active = false; // Not active by default
|
|
||||||
|
|
||||||
state
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Implement external library's CanvasState for LoginState
|
|
||||||
impl CanvasState for LoginState {
|
|
||||||
fn current_field(&self) -> usize {
|
|
||||||
self.current_field
|
self.current_field
|
||||||
}
|
}
|
||||||
|
|
||||||
fn current_cursor_pos(&self) -> usize {
|
pub fn current_cursor_pos(&self) -> usize {
|
||||||
self.current_cursor_pos
|
self.current_cursor_pos
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_current_field(&mut self, index: usize) {
|
pub fn set_current_field(&mut self, index: usize) {
|
||||||
if index < 2 {
|
if index < 2 {
|
||||||
self.current_field = index;
|
self.current_field = index;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_current_cursor_pos(&mut self, pos: usize) {
|
pub fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||||
self.current_cursor_pos = pos;
|
self.current_cursor_pos = pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_current_input(&self) -> &str {
|
pub fn get_current_input(&self) -> &str {
|
||||||
match self.current_field {
|
match self.current_field {
|
||||||
0 => &self.username,
|
0 => &self.username,
|
||||||
1 => &self.password,
|
1 => &self.password,
|
||||||
@@ -147,7 +126,7 @@ impl CanvasState for LoginState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_current_input_mut(&mut self) -> &mut String {
|
pub fn get_current_input_mut(&mut self) -> &mut String {
|
||||||
match self.current_field {
|
match self.current_field {
|
||||||
0 => &mut self.username,
|
0 => &mut self.username,
|
||||||
1 => &mut self.password,
|
1 => &mut self.password,
|
||||||
@@ -155,68 +134,57 @@ impl CanvasState for LoginState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn inputs(&self) -> Vec<&String> {
|
pub fn current_mode(&self) -> AppMode {
|
||||||
vec![&self.username, &self.password]
|
self.app_mode
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fields(&self) -> Vec<&str> {
|
// Add missing methods that used to come from CanvasState trait
|
||||||
vec!["Username/Email", "Password"]
|
pub fn has_unsaved_changes(&self) -> bool {
|
||||||
}
|
|
||||||
|
|
||||||
fn has_unsaved_changes(&self) -> bool {
|
|
||||||
self.has_unsaved_changes
|
self.has_unsaved_changes
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_has_unsaved_changes(&mut self, changed: bool) {
|
pub fn set_has_unsaved_changes(&mut self, changed: bool) {
|
||||||
self.has_unsaved_changes = changed;
|
self.has_unsaved_changes = changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_feature_action(&mut self, action: &CanvasAction, _context: &ActionContext) -> Option<String> {
|
|
||||||
match action {
|
|
||||||
CanvasAction::Custom(action_str) if action_str == "submit" => {
|
|
||||||
if !self.username.is_empty() && !self.password.is_empty() {
|
|
||||||
Some(format!("Submitting login for: {}", self.username))
|
|
||||||
} else {
|
|
||||||
Some("Please fill in all required fields".to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn current_mode(&self) -> AppMode {
|
|
||||||
self.app_mode
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Implement external library's CanvasState for RegisterState
|
impl RegisterState {
|
||||||
impl CanvasState for RegisterState {
|
pub fn new() -> Self {
|
||||||
fn current_field(&self) -> usize {
|
Self {
|
||||||
|
app_mode: AppMode::Edit,
|
||||||
|
role_suggestions: AVAILABLE_ROLES.clone(),
|
||||||
|
role_suggestions_active: false,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Legacy method compatibility
|
||||||
|
pub fn current_field(&self) -> usize {
|
||||||
self.current_field
|
self.current_field
|
||||||
}
|
}
|
||||||
|
|
||||||
fn current_cursor_pos(&self) -> usize {
|
pub fn current_cursor_pos(&self) -> usize {
|
||||||
self.current_cursor_pos
|
self.current_cursor_pos
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_current_field(&mut self, index: usize) {
|
pub fn set_current_field(&mut self, index: usize) {
|
||||||
if index < 5 {
|
if index < 5 {
|
||||||
self.current_field = index;
|
self.current_field = index;
|
||||||
|
|
||||||
// Auto-activate autocomplete when moving to role field (index 4)
|
// Auto-activate role suggestions when moving to role field (index 4)
|
||||||
if index == 4 && !self.autocomplete.is_active {
|
if index == 4 {
|
||||||
self.activate_autocomplete();
|
self.activate_role_suggestions();
|
||||||
} else if index != 4 && self.autocomplete.is_active {
|
} else {
|
||||||
self.deactivate_autocomplete();
|
self.deactivate_role_suggestions();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_current_cursor_pos(&mut self, pos: usize) {
|
pub fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||||
self.current_cursor_pos = pos;
|
self.current_cursor_pos = pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_current_input(&self) -> &str {
|
pub fn get_current_input(&self) -> &str {
|
||||||
match self.current_field {
|
match self.current_field {
|
||||||
0 => &self.username,
|
0 => &self.username,
|
||||||
1 => &self.email,
|
1 => &self.email,
|
||||||
@@ -227,7 +195,7 @@ impl CanvasState for RegisterState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_current_input_mut(&mut self) -> &mut String {
|
pub fn get_current_input_mut(&mut self) -> &mut String {
|
||||||
match self.current_field {
|
match self.current_field {
|
||||||
0 => &mut self.username,
|
0 => &mut self.username,
|
||||||
1 => &mut self.email,
|
1 => &mut self.email,
|
||||||
@@ -238,123 +206,121 @@ impl CanvasState for RegisterState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn inputs(&self) -> Vec<&String> {
|
pub fn current_mode(&self) -> AppMode {
|
||||||
vec![
|
self.app_mode
|
||||||
&self.username,
|
|
||||||
&self.email,
|
|
||||||
&self.password,
|
|
||||||
&self.password_confirmation,
|
|
||||||
&self.role,
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fields(&self) -> Vec<&str> {
|
// Role suggestions management
|
||||||
vec![
|
pub fn activate_role_suggestions(&mut self) {
|
||||||
"Username",
|
self.role_suggestions_active = true;
|
||||||
"Email (Optional)",
|
// Filter suggestions based on current input
|
||||||
"Password (Optional)",
|
let current_input = self.role.to_lowercase();
|
||||||
"Confirm Password",
|
self.role_suggestions = AVAILABLE_ROLES
|
||||||
"Role (Optional)"
|
.iter()
|
||||||
]
|
.filter(|role| role.to_lowercase().contains(¤t_input))
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn has_unsaved_changes(&self) -> bool {
|
pub fn deactivate_role_suggestions(&mut self) {
|
||||||
|
self.role_suggestions_active = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_role_suggestions_active(&self) -> bool {
|
||||||
|
self.role_suggestions_active
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_role_suggestions(&self) -> &[String] {
|
||||||
|
&self.role_suggestions
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add missing methods that used to come from CanvasState trait
|
||||||
|
pub fn has_unsaved_changes(&self) -> bool {
|
||||||
self.has_unsaved_changes
|
self.has_unsaved_changes
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_has_unsaved_changes(&mut self, changed: bool) {
|
pub fn set_has_unsaved_changes(&mut self, changed: bool) {
|
||||||
self.has_unsaved_changes = changed;
|
self.has_unsaved_changes = changed;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn handle_feature_action(&mut self, action: &CanvasAction, _context: &ActionContext) -> Option<String> {
|
// Step 2: Implement DataProvider for LoginState
|
||||||
match action {
|
impl DataProvider for LoginState {
|
||||||
CanvasAction::Custom(action_str) if action_str == "submit" => {
|
fn field_count(&self) -> usize {
|
||||||
if !self.username.is_empty() {
|
2
|
||||||
Some(format!("Submitting registration for: {}", self.username))
|
}
|
||||||
} else {
|
|
||||||
Some("Username is required".to_string())
|
fn field_name(&self, index: usize) -> &str {
|
||||||
}
|
match index {
|
||||||
}
|
0 => "Username/Email",
|
||||||
_ => None,
|
1 => "Password",
|
||||||
|
_ => "",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn current_mode(&self) -> AppMode {
|
fn field_value(&self, index: usize) -> &str {
|
||||||
self.app_mode
|
match index {
|
||||||
|
0 => &self.username,
|
||||||
|
1 => &self.password,
|
||||||
|
_ => "",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_field_value(&mut self, index: usize, value: String) {
|
||||||
|
match index {
|
||||||
|
0 => self.username = value,
|
||||||
|
1 => self.password = value,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
self.has_unsaved_changes = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn supports_suggestions(&self, _field_index: usize) -> bool {
|
||||||
|
false // Login form doesn't support suggestions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add autocomplete support for RegisterState
|
// Step 3: Implement DataProvider for RegisterState
|
||||||
impl AutocompleteCanvasState for RegisterState {
|
impl DataProvider for RegisterState {
|
||||||
type SuggestionData = String;
|
fn field_count(&self) -> usize {
|
||||||
|
5
|
||||||
fn supports_autocomplete(&self, field_index: usize) -> bool {
|
|
||||||
field_index == 4 // Only role field supports autocomplete
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn autocomplete_state(&self) -> Option<&AutocompleteState<Self::SuggestionData>> {
|
fn field_name(&self, index: usize) -> &str {
|
||||||
Some(&self.autocomplete)
|
match index {
|
||||||
}
|
0 => "Username",
|
||||||
|
1 => "Email (Optional)",
|
||||||
fn autocomplete_state_mut(&mut self) -> Option<&mut AutocompleteState<Self::SuggestionData>> {
|
2 => "Password (Optional)",
|
||||||
Some(&mut self.autocomplete)
|
3 => "Confirm Password",
|
||||||
}
|
4 => "Role (Optional)",
|
||||||
|
_ => "",
|
||||||
fn activate_autocomplete(&mut self) {
|
|
||||||
let current_field = self.current_field();
|
|
||||||
if self.supports_autocomplete(current_field) {
|
|
||||||
self.autocomplete.activate(current_field);
|
|
||||||
|
|
||||||
// Re-filter suggestions based on current input
|
|
||||||
let current_input = self.role.to_lowercase();
|
|
||||||
let filtered_suggestions: Vec<SuggestionItem<String>> = AVAILABLE_ROLES
|
|
||||||
.iter()
|
|
||||||
.filter(|role| role.to_lowercase().contains(¤t_input))
|
|
||||||
.map(|role| SuggestionItem::simple(role.clone(), role.clone()))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
self.autocomplete.set_suggestions(filtered_suggestions);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deactivate_autocomplete(&mut self) {
|
fn field_value(&self, index: usize) -> &str {
|
||||||
self.autocomplete.deactivate();
|
match index {
|
||||||
}
|
0 => &self.username,
|
||||||
|
1 => &self.email,
|
||||||
fn is_autocomplete_active(&self) -> bool {
|
2 => &self.password,
|
||||||
self.autocomplete.is_active
|
3 => &self.password_confirmation,
|
||||||
}
|
4 => &self.role,
|
||||||
|
_ => "",
|
||||||
fn is_autocomplete_ready(&self) -> bool {
|
|
||||||
self.autocomplete.is_ready()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn apply_autocomplete_selection(&mut self) -> Option<String> {
|
|
||||||
// First, get the data we need and clone it to avoid borrowing conflicts
|
|
||||||
let selection_info = self.autocomplete.get_selected().map(|selected| {
|
|
||||||
(selected.value_to_store.clone(), selected.display_text.clone())
|
|
||||||
});
|
|
||||||
|
|
||||||
// Now do the mutable operations
|
|
||||||
if let Some((value, display_text)) = selection_info {
|
|
||||||
self.role = value;
|
|
||||||
self.set_has_unsaved_changes(true);
|
|
||||||
self.deactivate_autocomplete();
|
|
||||||
Some(format!("Selected role: {}", display_text))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_autocomplete_suggestions(&mut self, suggestions: Vec<SuggestionItem<Self::SuggestionData>>) {
|
fn set_field_value(&mut self, index: usize, value: String) {
|
||||||
if let Some(state) = self.autocomplete_state_mut() {
|
match index {
|
||||||
state.set_suggestions(suggestions);
|
0 => self.username = value,
|
||||||
|
1 => self.email = value,
|
||||||
|
2 => self.password = value,
|
||||||
|
3 => self.password_confirmation = value,
|
||||||
|
4 => self.role = value,
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
|
self.has_unsaved_changes = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_autocomplete_loading(&mut self, loading: bool) {
|
fn supports_suggestions(&self, field_index: usize) -> bool {
|
||||||
if let Some(state) = self.autocomplete_state_mut() {
|
field_index == 4 // only Role field supports suggestions
|
||||||
state.is_loading = loading;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
// src/state/pages/form.rs
|
// src/state/pages/form.rs
|
||||||
|
|
||||||
use crate::config::colors::themes::Theme;
|
use crate::config::colors::themes::Theme;
|
||||||
use canvas::canvas::{CanvasState, CanvasAction, ActionContext, HighlightState, AppMode};
|
use canvas::{DataProvider, AppMode, EditorState, FormEditor};
|
||||||
|
use canvas::canvas::HighlightState;
|
||||||
use common::proto::komp_ac::search::search_response::Hit;
|
use common::proto::komp_ac::search::search_response::Hit;
|
||||||
use ratatui::layout::Rect;
|
use ratatui::layout::Rect;
|
||||||
use ratatui::Frame;
|
use ratatui::Frame;
|
||||||
@@ -122,26 +123,19 @@ impl FormState {
|
|||||||
area: Rect,
|
area: Rect,
|
||||||
theme: &Theme,
|
theme: &Theme,
|
||||||
is_edit_mode: bool,
|
is_edit_mode: bool,
|
||||||
highlight_state: &HighlightState, // Now using canvas::HighlightState
|
highlight_state: &HighlightState,
|
||||||
) {
|
) {
|
||||||
let fields_str_slice: Vec<&str> =
|
// Wrap in FormEditor for new API
|
||||||
self.fields().iter().map(|s| *s).collect();
|
let mut editor = FormEditor::new(self.clone());
|
||||||
let values_str_slice: Vec<&String> = self.values.iter().collect();
|
|
||||||
|
// Use new canvas rendering
|
||||||
crate::components::form::form::render_form(
|
canvas::render_canvas_default(f, area, &editor);
|
||||||
f,
|
|
||||||
area,
|
// If autocomplete is active, render suggestions
|
||||||
self,
|
if self.autocomplete_active && !self.autocomplete_suggestions.is_empty() {
|
||||||
&fields_str_slice,
|
// Note: This will need to be updated when suggestions are integrated
|
||||||
&self.current_field,
|
// canvas::render_suggestions_dropdown(f, area, input_rect, &canvas::DefaultCanvasTheme, &editor);
|
||||||
&values_str_slice,
|
}
|
||||||
&self.table_name,
|
|
||||||
theme,
|
|
||||||
is_edit_mode,
|
|
||||||
highlight_state,
|
|
||||||
self.total_count,
|
|
||||||
self.current_position,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reset_to_empty(&mut self) {
|
pub fn reset_to_empty(&mut self) {
|
||||||
@@ -242,97 +236,84 @@ impl FormState {
|
|||||||
pub fn set_readonly_mode(&mut self) {
|
pub fn set_readonly_mode(&mut self) {
|
||||||
self.app_mode = AppMode::ReadOnly;
|
self.app_mode = AppMode::ReadOnly;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl CanvasState for FormState {
|
// Legacy method compatibility
|
||||||
fn current_field(&self) -> usize {
|
pub fn fields(&self) -> Vec<&str> {
|
||||||
self.current_field
|
|
||||||
}
|
|
||||||
|
|
||||||
fn current_cursor_pos(&self) -> usize {
|
|
||||||
self.current_cursor_pos
|
|
||||||
}
|
|
||||||
|
|
||||||
fn has_unsaved_changes(&self) -> bool {
|
|
||||||
self.has_unsaved_changes
|
|
||||||
}
|
|
||||||
|
|
||||||
fn inputs(&self) -> Vec<&String> {
|
|
||||||
self.values.iter().collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_current_input(&self) -> &str {
|
|
||||||
FormState::get_current_input(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_current_input_mut(&mut self) -> &mut String {
|
|
||||||
FormState::get_current_input_mut(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fields(&self) -> Vec<&str> {
|
|
||||||
self.fields
|
self.fields
|
||||||
.iter()
|
.iter()
|
||||||
.map(|f| f.display_name.as_str())
|
.map(|f| f.display_name.as_str())
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_current_field(&mut self, index: usize) {
|
pub fn get_display_value_for_field(&self, index: usize) -> &str {
|
||||||
|
if let Some(display_text) = self.link_display_map.get(&index) {
|
||||||
|
return display_text.as_str();
|
||||||
|
}
|
||||||
|
self.values
|
||||||
|
.get(index)
|
||||||
|
.map(|s| s.as_str())
|
||||||
|
.unwrap_or("")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn has_display_override(&self, index: usize) -> bool {
|
||||||
|
self.link_display_map.contains_key(&index)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn current_mode(&self) -> AppMode {
|
||||||
|
self.app_mode
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add missing methods that used to come from CanvasState trait
|
||||||
|
pub fn has_unsaved_changes(&self) -> bool {
|
||||||
|
self.has_unsaved_changes
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_has_unsaved_changes(&mut self, changed: bool) {
|
||||||
|
self.has_unsaved_changes = changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn current_field(&self) -> usize {
|
||||||
|
self.current_field
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_current_field(&mut self, index: usize) {
|
||||||
if index < self.fields.len() {
|
if index < self.fields.len() {
|
||||||
self.current_field = index;
|
self.current_field = index;
|
||||||
}
|
}
|
||||||
self.deactivate_autocomplete();
|
self.deactivate_autocomplete();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_current_cursor_pos(&mut self, pos: usize) {
|
pub fn current_cursor_pos(&self) -> usize {
|
||||||
|
self.current_cursor_pos
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||||
self.current_cursor_pos = pos;
|
self.current_cursor_pos = pos;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn set_has_unsaved_changes(&mut self, changed: bool) {
|
// Step 2: Implement DataProvider for FormState
|
||||||
self.has_unsaved_changes = changed;
|
impl DataProvider for FormState {
|
||||||
|
fn field_count(&self) -> usize {
|
||||||
|
self.fields.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- FEATURE-SPECIFIC ACTION HANDLING ---
|
fn field_name(&self, index: usize) -> &str {
|
||||||
fn handle_feature_action(&mut self, action: &CanvasAction, _context: &ActionContext) -> Option<String> {
|
&self.fields[index].display_name
|
||||||
match action {
|
}
|
||||||
CanvasAction::SelectSuggestion => {
|
|
||||||
if let Some(selected_idx) = self.selected_suggestion_index {
|
fn field_value(&self, index: usize) -> &str {
|
||||||
if let Some(hit) = self.autocomplete_suggestions.get(selected_idx).cloned() {
|
&self.values[index]
|
||||||
// Extract the value from the selected suggestion
|
}
|
||||||
if let Ok(content_map) = serde_json::from_str::<HashMap<String, serde_json::Value>>(&hit.content_json) {
|
|
||||||
let current_field_def = &self.fields[self.current_field];
|
fn set_field_value(&mut self, index: usize, value: String) {
|
||||||
if let Some(value) = content_map.get(¤t_field_def.data_key) {
|
if let Some(v) = self.values.get_mut(index) {
|
||||||
let new_value = json_value_to_string(value);
|
*v = value;
|
||||||
let display_name = self.get_display_name_for_hit(&hit);
|
self.has_unsaved_changes = true;
|
||||||
*self.get_current_input_mut() = new_value.clone();
|
|
||||||
self.set_current_cursor_pos(new_value.len());
|
|
||||||
self.set_has_unsaved_changes(true);
|
|
||||||
self.deactivate_autocomplete();
|
|
||||||
return Some(format!("Selected: {}", display_name));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None
|
|
||||||
}
|
|
||||||
_ => None, // Let canvas handle other actions
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_display_value_for_field(&self, index: usize) -> &str {
|
fn supports_suggestions(&self, field_index: usize) -> bool {
|
||||||
if let Some(display_text) = self.link_display_map.get(&index) {
|
self.fields.get(field_index).map(|f| f.is_link).unwrap_or(false)
|
||||||
return display_text.as_str();
|
|
||||||
}
|
|
||||||
self.inputs()
|
|
||||||
.get(index)
|
|
||||||
.map(|s| s.as_str())
|
|
||||||
.unwrap_or("")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn has_display_override(&self, index: usize) -> bool {
|
|
||||||
self.link_display_map.contains_key(&index)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn current_mode(&self) -> AppMode {
|
|
||||||
self.app_mode
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ use crate::state::app::buffer::{AppView, BufferState};
|
|||||||
use crate::config::storage::storage::{StoredAuthData, save_auth_data};
|
use crate::config::storage::storage::{StoredAuthData, save_auth_data};
|
||||||
use crate::ui::handlers::context::DialogPurpose;
|
use crate::ui::handlers::context::DialogPurpose;
|
||||||
use common::proto::komp_ac::auth::LoginResponse;
|
use common::proto::komp_ac::auth::LoginResponse;
|
||||||
use canvas::canvas::CanvasState;
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use tokio::spawn;
|
use tokio::spawn;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ use crate::state::{
|
|||||||
use crate::ui::handlers::context::DialogPurpose;
|
use crate::ui::handlers::context::DialogPurpose;
|
||||||
use crate::state::app::buffer::{AppView, BufferState};
|
use crate::state::app::buffer::{AppView, BufferState};
|
||||||
use common::proto::komp_ac::auth::AuthResponse;
|
use common::proto::komp_ac::auth::AuthResponse;
|
||||||
use canvas::canvas::CanvasState;
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use tokio::spawn;
|
use tokio::spawn;
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
// src/tui/functions/form.rs
|
// src/tui/functions/form.rs
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::form::FormState;
|
||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
use canvas::canvas::CanvasState;
|
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
|
|
||||||
pub async fn handle_action(
|
pub async fn handle_action(
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ use crate::components::{
|
|||||||
};
|
};
|
||||||
use crate::config::colors::themes::Theme;
|
use crate::config::colors::themes::Theme;
|
||||||
use crate::modes::general::command_navigation::NavigationState;
|
use crate::modes::general::command_navigation::NavigationState;
|
||||||
use canvas::canvas::CanvasState;
|
|
||||||
use crate::state::app::buffer::BufferState;
|
use crate::state::app::buffer::BufferState;
|
||||||
use crate::state::app::highlight::HighlightState as LocalHighlightState;
|
use crate::state::app::highlight::HighlightState as LocalHighlightState;
|
||||||
use canvas::canvas::HighlightState as CanvasHighlightState;
|
use canvas::canvas::HighlightState as CanvasHighlightState;
|
||||||
@@ -137,7 +136,6 @@ pub fn render_ui(
|
|||||||
register_state,
|
register_state,
|
||||||
app_state,
|
app_state,
|
||||||
register_state.current_field() < 4, // Now using CanvasState trait method
|
register_state.current_field() < 4, // Now using CanvasState trait method
|
||||||
highlight_state, // Uses local version
|
|
||||||
);
|
);
|
||||||
} else if app_state.ui.show_add_table {
|
} else if app_state.ui.show_add_table {
|
||||||
render_add_table(
|
render_add_table(
|
||||||
@@ -147,7 +145,6 @@ pub fn render_ui(
|
|||||||
app_state,
|
app_state,
|
||||||
&mut admin_state.add_table_state,
|
&mut admin_state.add_table_state,
|
||||||
is_event_handler_edit_mode,
|
is_event_handler_edit_mode,
|
||||||
highlight_state, // Uses local version
|
|
||||||
);
|
);
|
||||||
} else if app_state.ui.show_add_logic {
|
} else if app_state.ui.show_add_logic {
|
||||||
render_add_logic(
|
render_add_logic(
|
||||||
@@ -157,7 +154,6 @@ pub fn render_ui(
|
|||||||
app_state,
|
app_state,
|
||||||
&mut admin_state.add_logic_state,
|
&mut admin_state.add_logic_state,
|
||||||
is_event_handler_edit_mode,
|
is_event_handler_edit_mode,
|
||||||
highlight_state, // Uses local version
|
|
||||||
);
|
);
|
||||||
} else if app_state.ui.show_login {
|
} else if app_state.ui.show_login {
|
||||||
render_login(
|
render_login(
|
||||||
@@ -167,7 +163,6 @@ pub fn render_ui(
|
|||||||
login_state,
|
login_state,
|
||||||
app_state,
|
app_state,
|
||||||
login_state.current_field() < 2, // Now using CanvasState trait method
|
login_state.current_field() < 2, // Now using CanvasState trait method
|
||||||
highlight_state, // Uses local version
|
|
||||||
);
|
);
|
||||||
} else if app_state.ui.show_admin {
|
} else if app_state.ui.show_admin {
|
||||||
crate::components::admin::admin_panel::render_admin_panel(
|
crate::components::admin::admin_panel::render_admin_panel(
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ use crate::config::storage::storage::load_auth_data;
|
|||||||
use crate::modes::common::commands::CommandHandler;
|
use crate::modes::common::commands::CommandHandler;
|
||||||
use crate::modes::handlers::event::{EventHandler, EventOutcome};
|
use crate::modes::handlers::event::{EventHandler, EventOutcome};
|
||||||
use crate::modes::handlers::mode_manager::{AppMode, ModeManager};
|
use crate::modes::handlers::mode_manager::{AppMode, ModeManager};
|
||||||
use canvas::canvas::CanvasState; // Only external library import
|
|
||||||
use crate::state::pages::form::{FormState, FieldDefinition};
|
use crate::state::pages::form::{FormState, FieldDefinition};
|
||||||
use crate::state::pages::auth::AuthState;
|
use crate::state::pages::auth::AuthState;
|
||||||
use crate::state::pages::auth::LoginState;
|
use crate::state::pages::auth::LoginState;
|
||||||
|
|||||||
Reference in New Issue
Block a user