diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Cargo.lock | 225 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | client.db | bin | 12288 -> 0 bytes | |||
| -rw-r--r-- | src/bin/client/connection.rs | 2 | ||||
| -rw-r--r-- | src/bin/client/repl.rs | 215 |
6 files changed, 388 insertions, 56 deletions
| @@ -1 +1,2 @@ | |||
| 1 | /target | 1 | /target |
| 2 | client.db \ No newline at end of file | ||
| @@ -33,6 +33,41 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
| 33 | checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" | 33 | checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" |
| 34 | 34 | ||
| 35 | [[package]] | 35 | [[package]] |
| 36 | name = "crossterm" | ||
| 37 | version = "0.27.0" | ||
| 38 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 39 | checksum = "f476fe445d41c9e991fd07515a6f463074b782242ccf4a5b7b1d1012e70824df" | ||
| 40 | dependencies = [ | ||
| 41 | "bitflags", | ||
| 42 | "crossterm_winapi", | ||
| 43 | "libc", | ||
| 44 | "mio", | ||
| 45 | "parking_lot", | ||
| 46 | "signal-hook", | ||
| 47 | "signal-hook-mio", | ||
| 48 | "winapi", | ||
| 49 | ] | ||
| 50 | |||
| 51 | [[package]] | ||
| 52 | name = "crossterm_winapi" | ||
| 53 | version = "0.9.1" | ||
| 54 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 55 | checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b" | ||
| 56 | dependencies = [ | ||
| 57 | "winapi", | ||
| 58 | ] | ||
| 59 | |||
| 60 | [[package]] | ||
| 61 | name = "errno" | ||
| 62 | version = "0.3.14" | ||
| 63 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 64 | checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" | ||
| 65 | dependencies = [ | ||
| 66 | "libc", | ||
| 67 | "windows-sys 0.61.2", | ||
| 68 | ] | ||
| 69 | |||
| 70 | [[package]] | ||
| 36 | name = "fallible-iterator" | 71 | name = "fallible-iterator" |
| 37 | version = "0.2.0" | 72 | version = "0.2.0" |
| 38 | source = "registry+https://github.com/rust-lang/crates.io-index" | 73 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -64,6 +99,12 @@ dependencies = [ | |||
| 64 | ] | 99 | ] |
| 65 | 100 | ||
| 66 | [[package]] | 101 | [[package]] |
| 102 | name = "libc" | ||
| 103 | version = "0.2.182" | ||
| 104 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 105 | checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112" | ||
| 106 | |||
| 107 | [[package]] | ||
| 67 | name = "libsqlite3-sys" | 108 | name = "libsqlite3-sys" |
| 68 | version = "0.26.0" | 109 | version = "0.26.0" |
| 69 | source = "registry+https://github.com/rust-lang/crates.io-index" | 110 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -74,12 +115,62 @@ dependencies = [ | |||
| 74 | ] | 115 | ] |
| 75 | 116 | ||
| 76 | [[package]] | 117 | [[package]] |
| 118 | name = "lock_api" | ||
| 119 | version = "0.4.14" | ||
| 120 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 121 | checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" | ||
| 122 | dependencies = [ | ||
| 123 | "scopeguard", | ||
| 124 | ] | ||
| 125 | |||
| 126 | [[package]] | ||
| 127 | name = "log" | ||
| 128 | version = "0.4.29" | ||
| 129 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 130 | checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" | ||
| 131 | |||
| 132 | [[package]] | ||
| 133 | name = "mio" | ||
| 134 | version = "0.8.11" | ||
| 135 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 136 | checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c" | ||
| 137 | dependencies = [ | ||
| 138 | "libc", | ||
| 139 | "log", | ||
| 140 | "wasi", | ||
| 141 | "windows-sys 0.48.0", | ||
| 142 | ] | ||
| 143 | |||
| 144 | [[package]] | ||
| 77 | name = "once_cell" | 145 | name = "once_cell" |
| 78 | version = "1.21.3" | 146 | version = "1.21.3" |
| 79 | source = "registry+https://github.com/rust-lang/crates.io-index" | 147 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 80 | checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" | 148 | checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" |
| 81 | 149 | ||
| 82 | [[package]] | 150 | [[package]] |
| 151 | name = "parking_lot" | ||
| 152 | version = "0.12.5" | ||
| 153 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 154 | checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" | ||
| 155 | dependencies = [ | ||
| 156 | "lock_api", | ||
| 157 | "parking_lot_core", | ||
| 158 | ] | ||
| 159 | |||
| 160 | [[package]] | ||
| 161 | name = "parking_lot_core" | ||
| 162 | version = "0.9.12" | ||
| 163 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 164 | checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" | ||
| 165 | dependencies = [ | ||
| 166 | "cfg-if", | ||
| 167 | "libc", | ||
| 168 | "redox_syscall", | ||
| 169 | "smallvec", | ||
| 170 | "windows-link", | ||
| 171 | ] | ||
| 172 | |||
| 173 | [[package]] | ||
| 83 | name = "pkg-config" | 174 | name = "pkg-config" |
| 84 | version = "0.3.32" | 175 | version = "0.3.32" |
| 85 | source = "registry+https://github.com/rust-lang/crates.io-index" | 176 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -104,6 +195,15 @@ dependencies = [ | |||
| 104 | ] | 195 | ] |
| 105 | 196 | ||
| 106 | [[package]] | 197 | [[package]] |
| 198 | name = "redox_syscall" | ||
| 199 | version = "0.5.18" | ||
| 200 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 201 | checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" | ||
| 202 | dependencies = [ | ||
| 203 | "bitflags", | ||
| 204 | ] | ||
| 205 | |||
| 206 | [[package]] | ||
| 107 | name = "rusqlite" | 207 | name = "rusqlite" |
| 108 | version = "0.29.0" | 208 | version = "0.29.0" |
| 109 | source = "registry+https://github.com/rust-lang/crates.io-index" | 209 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -118,6 +218,43 @@ dependencies = [ | |||
| 118 | ] | 218 | ] |
| 119 | 219 | ||
| 120 | [[package]] | 220 | [[package]] |
| 221 | name = "scopeguard" | ||
| 222 | version = "1.2.0" | ||
| 223 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 224 | checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" | ||
| 225 | |||
| 226 | [[package]] | ||
| 227 | name = "signal-hook" | ||
| 228 | version = "0.3.18" | ||
| 229 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 230 | checksum = "d881a16cf4426aa584979d30bd82cb33429027e42122b169753d6ef1085ed6e2" | ||
| 231 | dependencies = [ | ||
| 232 | "libc", | ||
| 233 | "signal-hook-registry", | ||
| 234 | ] | ||
| 235 | |||
| 236 | [[package]] | ||
| 237 | name = "signal-hook-mio" | ||
| 238 | version = "0.2.5" | ||
| 239 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 240 | checksum = "b75a19a7a740b25bc7944bdee6172368f988763b744e3d4dfe753f6b4ece40cc" | ||
| 241 | dependencies = [ | ||
| 242 | "libc", | ||
| 243 | "mio", | ||
| 244 | "signal-hook", | ||
| 245 | ] | ||
| 246 | |||
| 247 | [[package]] | ||
| 248 | name = "signal-hook-registry" | ||
| 249 | version = "1.4.8" | ||
| 250 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 251 | checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" | ||
| 252 | dependencies = [ | ||
| 253 | "errno", | ||
| 254 | "libc", | ||
| 255 | ] | ||
| 256 | |||
| 257 | [[package]] | ||
| 121 | name = "smallvec" | 258 | name = "smallvec" |
| 122 | version = "1.15.1" | 259 | version = "1.15.1" |
| 123 | source = "registry+https://github.com/rust-lang/crates.io-index" | 260 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -153,6 +290,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
| 153 | checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" | 290 | checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" |
| 154 | 291 | ||
| 155 | [[package]] | 292 | [[package]] |
| 293 | name = "wasi" | ||
| 294 | version = "0.11.1+wasi-snapshot-preview1" | ||
| 295 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 296 | checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" | ||
| 297 | |||
| 298 | [[package]] | ||
| 156 | name = "winapi" | 299 | name = "winapi" |
| 157 | version = "0.3.9" | 300 | version = "0.3.9" |
| 158 | source = "registry+https://github.com/rust-lang/crates.io-index" | 301 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| @@ -175,9 +318,91 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
| 175 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" | 318 | checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" |
| 176 | 319 | ||
| 177 | [[package]] | 320 | [[package]] |
| 321 | name = "windows-link" | ||
| 322 | version = "0.2.1" | ||
| 323 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 324 | checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" | ||
| 325 | |||
| 326 | [[package]] | ||
| 327 | name = "windows-sys" | ||
| 328 | version = "0.48.0" | ||
| 329 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 330 | checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" | ||
| 331 | dependencies = [ | ||
| 332 | "windows-targets", | ||
| 333 | ] | ||
| 334 | |||
| 335 | [[package]] | ||
| 336 | name = "windows-sys" | ||
| 337 | version = "0.61.2" | ||
| 338 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 339 | checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" | ||
| 340 | dependencies = [ | ||
| 341 | "windows-link", | ||
| 342 | ] | ||
| 343 | |||
| 344 | [[package]] | ||
| 345 | name = "windows-targets" | ||
| 346 | version = "0.48.5" | ||
| 347 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 348 | checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" | ||
| 349 | dependencies = [ | ||
| 350 | "windows_aarch64_gnullvm", | ||
| 351 | "windows_aarch64_msvc", | ||
| 352 | "windows_i686_gnu", | ||
| 353 | "windows_i686_msvc", | ||
| 354 | "windows_x86_64_gnu", | ||
| 355 | "windows_x86_64_gnullvm", | ||
| 356 | "windows_x86_64_msvc", | ||
| 357 | ] | ||
| 358 | |||
| 359 | [[package]] | ||
| 360 | name = "windows_aarch64_gnullvm" | ||
| 361 | version = "0.48.5" | ||
| 362 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 363 | checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" | ||
| 364 | |||
| 365 | [[package]] | ||
| 366 | name = "windows_aarch64_msvc" | ||
| 367 | version = "0.48.5" | ||
| 368 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 369 | checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" | ||
| 370 | |||
| 371 | [[package]] | ||
| 372 | name = "windows_i686_gnu" | ||
| 373 | version = "0.48.5" | ||
| 374 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 375 | checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" | ||
| 376 | |||
| 377 | [[package]] | ||
| 378 | name = "windows_i686_msvc" | ||
| 379 | version = "0.48.5" | ||
| 380 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 381 | checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" | ||
| 382 | |||
| 383 | [[package]] | ||
| 384 | name = "windows_x86_64_gnu" | ||
| 385 | version = "0.48.5" | ||
| 386 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 387 | checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" | ||
| 388 | |||
| 389 | [[package]] | ||
| 390 | name = "windows_x86_64_gnullvm" | ||
| 391 | version = "0.48.5" | ||
| 392 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 393 | checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" | ||
| 394 | |||
| 395 | [[package]] | ||
| 396 | name = "windows_x86_64_msvc" | ||
| 397 | version = "0.48.5" | ||
| 398 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 399 | checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" | ||
| 400 | |||
| 401 | [[package]] | ||
| 178 | name = "zagan-bet" | 402 | name = "zagan-bet" |
| 179 | version = "0.1.0" | 403 | version = "0.1.0" |
| 180 | dependencies = [ | 404 | dependencies = [ |
| 405 | "crossterm", | ||
| 181 | "rusqlite", | 406 | "rusqlite", |
| 182 | "winapi", | 407 | "winapi", |
| 183 | ] | 408 | ] |
| @@ -6,3 +6,4 @@ edition = "2024" | |||
| 6 | [dependencies] | 6 | [dependencies] |
| 7 | rusqlite = "0.29" | 7 | rusqlite = "0.29" |
| 8 | winapi = { version = "0.3", features = ["consoleapi", "processenv", "wincon", "minwindef", "winbase"] } | 8 | winapi = { version = "0.3", features = ["consoleapi", "processenv", "wincon", "minwindef", "winbase"] } |
| 9 | crossterm = "0.27" | ||
diff --git a/client.db b/client.db deleted file mode 100644 index 8ead0e2..0000000 --- a/client.db +++ /dev/null | |||
| Binary files differ | |||
diff --git a/src/bin/client/connection.rs b/src/bin/client/connection.rs index 9e402cb..d03dc1a 100644 --- a/src/bin/client/connection.rs +++ b/src/bin/client/connection.rs | |||
| @@ -22,7 +22,7 @@ pub fn connect(addr: &str) -> Result<TcpStream, Box<dyn std::error::Error>> { | |||
| 22 | let mut response = String::new(); | 22 | let mut response = String::new(); |
| 23 | ping_reader.read_line(&mut response).unwrap(); | 23 | ping_reader.read_line(&mut response).unwrap(); |
| 24 | let elapsed = start.elapsed(); | 24 | let elapsed = start.elapsed(); |
| 25 | println!("Connected to {} with ping delay: {:?}", addr, elapsed); | 25 | println!("\n\rConnected to {} with ping delay: {:?}", addr, elapsed); |
| 26 | Ok(s) | 26 | Ok(s) |
| 27 | } | 27 | } |
| 28 | Ok(Err(e)) => Err(Box::new(e)), | 28 | Ok(Err(e)) => Err(Box::new(e)), |
diff --git a/src/bin/client/repl.rs b/src/bin/client/repl.rs index d38c54d..1512091 100644 --- a/src/bin/client/repl.rs +++ b/src/bin/client/repl.rs | |||
| @@ -1,15 +1,29 @@ | |||
| 1 | use std::io::{BufRead, BufReader, Write, stdin, stdout}; | 1 | use std::io::{BufRead, BufReader, Write, stdout}; |
| 2 | use std::net::TcpStream; | 2 | use std::net::TcpStream; |
| 3 | use rusqlite::Connection; | 3 | use rusqlite::Connection; |
| 4 | use std::time::Instant; | ||
| 4 | 5 | ||
| 5 | use crate::connection; | 6 | use crate::connection; |
| 6 | use crate::database; | 7 | use crate::database; |
| 7 | 8 | ||
| 9 | use crossterm::{event::{read, Event, KeyCode, KeyModifiers}, terminal::{disable_raw_mode, enable_raw_mode, Clear, ClearType}, cursor::{MoveToColumn, Show}, execute}; | ||
| 10 | |||
| 11 | macro_rules! println { | ||
| 12 | () => { | ||
| 13 | print!("\r\n") | ||
| 14 | }; | ||
| 15 | ($($arg:tt)*) => { | ||
| 16 | print!("\r{}\n", format_args!($($arg)*)) | ||
| 17 | }; | ||
| 18 | } | ||
| 19 | |||
| 8 | pub struct ClientState { | 20 | pub struct ClientState { |
| 9 | pub stream: Option<TcpStream>, | 21 | pub stream: Option<TcpStream>, |
| 10 | pub conn: Connection, | 22 | pub conn: Connection, |
| 11 | pub addr: Option<String>, | 23 | pub addr: Option<String>, |
| 12 | pub default_port: u16, | 24 | pub default_port: u16, |
| 25 | pub history: Vec<String>, | ||
| 26 | pub history_index: usize, | ||
| 13 | } | 27 | } |
| 14 | 28 | ||
| 15 | impl ClientState { | 29 | impl ClientState { |
| @@ -26,6 +40,8 @@ impl ClientState { | |||
| 26 | conn, | 40 | conn, |
| 27 | addr, | 41 | addr, |
| 28 | default_port, | 42 | default_port, |
| 43 | history: vec![], | ||
| 44 | history_index: 0, | ||
| 29 | } | 45 | } |
| 30 | } | 46 | } |
| 31 | 47 | ||
| @@ -41,68 +57,157 @@ impl ClientState { | |||
| 41 | } | 57 | } |
| 42 | 58 | ||
| 43 | pub fn run(&mut self) { | 59 | pub fn run(&mut self) { |
| 44 | loop { | 60 | enable_raw_mode().unwrap(); |
| 45 | print!("> "); | 61 | 'outer: loop { |
| 46 | stdout().flush().unwrap(); | 62 | let mut current_input = String::new(); |
| 47 | let mut line = String::new(); | 63 | self.history_index = self.history.len(); |
| 48 | stdin().read_line(&mut line).unwrap(); | 64 | redraw(""); |
| 49 | let line = line.trim(); | 65 | loop { |
| 50 | if line.is_empty() { | 66 | match read().unwrap() { |
| 51 | continue; | 67 | Event::Key(key) => match key.code { |
| 52 | } | 68 | KeyCode::Up => { |
| 53 | let parts: Vec<&str> = line.splitn(2, ' ').collect(); | 69 | if self.history_index > 0 { |
| 54 | match parts[0] { | 70 | self.history_index -= 1; |
| 55 | "connect" => { | 71 | current_input = self.history[self.history_index].clone(); |
| 56 | let addr_str = if parts.len() < 2 { | 72 | redraw(¤t_input); |
| 57 | if let Some(h) = &self.addr { | 73 | } |
| 58 | h.clone() | ||
| 59 | } else { | ||
| 60 | println!("No host specified and no previous connection. Usage: connect <ip>:<port>"); | ||
| 61 | continue; | ||
| 62 | } | 74 | } |
| 63 | } else { | 75 | KeyCode::Down => { |
| 64 | let mut a = parts[1].to_string(); | 76 | if self.history_index < self.history.len() { |
| 65 | if !a.contains(':') { | 77 | self.history_index += 1; |
| 66 | a = format!("{}:{}", a, self.default_port); | 78 | current_input = if self.history_index == self.history.len() { "".to_string() } else { self.history[self.history_index].clone() }; |
| 79 | redraw(¤t_input); | ||
| 80 | } | ||
| 67 | } | 81 | } |
| 68 | a | 82 | KeyCode::Enter => { |
| 69 | }; | 83 | let line = current_input.trim().to_string(); |
| 70 | self.connect_to(&addr_str); | 84 | if !line.is_empty() { |
| 71 | } | 85 | self.history.push(line.clone()); |
| 72 | "send" => { | 86 | } |
| 73 | if self.stream.is_none() { | 87 | // Process command |
| 74 | println!("Not connected. Use 'connect <ip>:<port>' first."); | 88 | let parts: Vec<&str> = line.splitn(2, ' ').collect(); |
| 75 | continue; | 89 | match parts[0] { |
| 76 | } | 90 | "connect" => { |
| 77 | if parts.len() < 2 { | 91 | let addr_str = if parts.len() < 2 { |
| 78 | println!("Usage: send <message>"); | 92 | if let Some(h) = &self.addr { |
| 79 | continue; | 93 | h.clone() |
| 94 | } else { | ||
| 95 | println!("\nNo host specified and no previous connection. Usage: connect <ip>:<port>"); | ||
| 96 | continue 'outer; | ||
| 97 | } | ||
| 98 | } else { | ||
| 99 | let mut a = parts[1].to_string(); | ||
| 100 | if !a.contains(':') { | ||
| 101 | a = format!("{}:{}", a, self.default_port); | ||
| 102 | } | ||
| 103 | a | ||
| 104 | }; | ||
| 105 | self.connect_to(&addr_str); | ||
| 106 | } | ||
| 107 | "send" => { | ||
| 108 | if self.stream.is_none() { | ||
| 109 | println!("\nNot connected. Use 'connect <ip>:<port>' first."); | ||
| 110 | continue 'outer; | ||
| 111 | } | ||
| 112 | if parts.len() < 2 { | ||
| 113 | println!("\nUsage: send <message>"); | ||
| 114 | continue 'outer; | ||
| 115 | } | ||
| 116 | let msg = parts[1]; | ||
| 117 | let s = self.stream.as_mut().unwrap(); | ||
| 118 | let writer_stream = s.try_clone().unwrap(); | ||
| 119 | let mut writer = writer_stream; | ||
| 120 | let response_reader_stream = s.try_clone().unwrap(); | ||
| 121 | let mut response_reader = BufReader::new(response_reader_stream); | ||
| 122 | if let Err(_) = writeln!(writer, "{}", msg) { | ||
| 123 | self.stream = None; | ||
| 124 | println!("\nConnection lost"); | ||
| 125 | continue 'outer; | ||
| 126 | } | ||
| 127 | let mut response = String::new(); | ||
| 128 | if let Err(_) = response_reader.read_line(&mut response) { | ||
| 129 | self.stream = None; | ||
| 130 | println!("\nConnection lost"); | ||
| 131 | } else { | ||
| 132 | println!("\nEcho: {}", response.trim()); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | "disconnect" => { | ||
| 136 | self.stream = None; | ||
| 137 | println!("\nDisconnected"); | ||
| 138 | } | ||
| 139 | "ping" => { | ||
| 140 | if self.stream.is_none() { | ||
| 141 | println!("\nNot connected. Use 'connect <ip>:<port>' first."); | ||
| 142 | continue 'outer; | ||
| 143 | } | ||
| 144 | let s = self.stream.as_mut().unwrap(); | ||
| 145 | let writer_stream = s.try_clone().unwrap(); | ||
| 146 | let mut writer = writer_stream; | ||
| 147 | let start = Instant::now(); | ||
| 148 | let response_reader_stream = s.try_clone().unwrap(); | ||
| 149 | let mut response_reader = BufReader::new(response_reader_stream); | ||
| 150 | if let Err(_) = writeln!(writer, "PING") { | ||
| 151 | self.stream = None; | ||
| 152 | println!("\nConnection lost"); | ||
| 153 | continue 'outer; | ||
| 154 | } | ||
| 155 | let mut response = String::new(); | ||
| 156 | if let Err(_) = response_reader.read_line(&mut response) { | ||
| 157 | self.stream = None; | ||
| 158 | println!("\nConnection lost"); | ||
| 159 | } else { | ||
| 160 | if response.trim() == "PING" { | ||
| 161 | let elapsed = start.elapsed(); | ||
| 162 | println!("\nPong ({:?})", elapsed); | ||
| 163 | } else { | ||
| 164 | self.stream = None; | ||
| 165 | println!("\nConnection lost"); | ||
| 166 | } | ||
| 167 | } | ||
| 168 | } | ||
| 169 | "help" => { | ||
| 170 | help(); | ||
| 171 | } | ||
| 172 | "quit" => break 'outer, | ||
| 173 | _ => help(), | ||
| 174 | } | ||
| 175 | break; | ||
| 176 | } | ||
| 177 | KeyCode::Backspace => { | ||
| 178 | if !current_input.is_empty() { | ||
| 179 | current_input.pop(); | ||
| 180 | redraw(¤t_input); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | KeyCode::Char(c) => { | ||
| 184 | if key.modifiers.contains(KeyModifiers::CONTROL) && c == 'c' { | ||
| 185 | disable_raw_mode().unwrap(); | ||
| 186 | execute!(stdout(), Show).unwrap(); | ||
| 187 | return; | ||
| 188 | } else { | ||
| 189 | current_input.push(c); | ||
| 190 | redraw(¤t_input); | ||
| 191 | } | ||
| 192 | } | ||
| 193 | _ => {} | ||
| 80 | } | 194 | } |
| 81 | let msg = parts[1]; | 195 | _ => {} |
| 82 | let s = self.stream.as_mut().unwrap(); | ||
| 83 | let writer_stream = s.try_clone().unwrap(); | ||
| 84 | let mut writer = writer_stream; | ||
| 85 | let response_reader_stream = s.try_clone().unwrap(); | ||
| 86 | let mut response_reader = BufReader::new(response_reader_stream); | ||
| 87 | writeln!(writer, "{}", msg).unwrap(); | ||
| 88 | let mut response = String::new(); | ||
| 89 | response_reader.read_line(&mut response).unwrap(); | ||
| 90 | println!("Echo: {}", response.trim()); | ||
| 91 | } | ||
| 92 | "disconnect" => { | ||
| 93 | self.stream = None; | ||
| 94 | println!("Disconnected"); | ||
| 95 | } | 196 | } |
| 96 | "help" => { | ||
| 97 | help(); | ||
| 98 | } | ||
| 99 | "quit" => break, | ||
| 100 | _ => help(), | ||
| 101 | } | 197 | } |
| 102 | } | 198 | } |
| 199 | disable_raw_mode().unwrap(); | ||
| 200 | execute!(stdout(), Show).unwrap(); | ||
| 103 | } | 201 | } |
| 104 | } | 202 | } |
| 105 | 203 | ||
| 204 | fn redraw(current_input: &str) { | ||
| 205 | execute!(stdout(), Clear(ClearType::CurrentLine), MoveToColumn(0)).unwrap(); | ||
| 206 | print!("> {}", current_input); | ||
| 207 | stdout().flush().unwrap(); | ||
| 208 | execute!(stdout(), MoveToColumn((2 + current_input.len()) as u16)).unwrap(); | ||
| 209 | } | ||
| 210 | |||
| 106 | fn help() { | 211 | fn help() { |
| 107 | println!("Available commands: connect <ip>:<port>, send <message>, disconnect, help, quit"); | 212 | println!("Available commands: connect <ip>:<port>, send <message>, disconnect, ping, help, quit"); |
| 108 | } | 213 | } |
