diff options
| author | Rose Hogenson <rosehogenson@posteo.net> | 2023-12-29 11:20:05 -0800 |
|---|---|---|
| committer | Rose Hogenson <rosehogenson@posteo.net> | 2023-12-29 11:20:05 -0800 |
| commit | f898a97005844d5c451842eca63871ba382f5d96 (patch) | |
| tree | 6bad85df432f89888df52e8db414d963a164de37 /src/main.rs | |
| parent | 2a2181daaf96d2646a8001b9f138099510d0b34e (diff) | |
| download | editor-f898a97005844d5c451842eca63871ba382f5d96.tar.zst | |
Allow moving the cursor with the arrow keys.
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 47 |
1 files changed, 27 insertions, 20 deletions
diff --git a/src/main.rs b/src/main.rs index 75a6ebf..592f89c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,24 +3,9 @@ mod term; use rope::Rope; use std::ffi::{OsStr, OsString}; -use std::io::{Read, Stdin, Write}; +use std::io::Write; use std::path::Path; - -const CTRL_Q: u8 = 17; - -fn read_key(stdin: &mut Stdin) -> Result<u8, String> { - let mut buf = vec![0; 1]; - match stdin.read(&mut buf) { - Err(err) => { - return Err(format!("read input: {}", err)); - } - Ok(0) => { - return Err(String::from("no input")); - } - _ => (), - } - return Ok(buf[0]); -} +use term::Key; fn edit(file: &OsStr) -> Result<(), String> { let _raw_handle = match term::raw() { @@ -53,14 +38,36 @@ fn edit(file: &OsStr) -> Result<(), String> { let mut stdin = std::io::stdin(); loop { - let c = match read_key(&mut stdin) { + let c = match term::read_key(&mut stdin) { Ok(c) => c, Err(err) => { return Err(err); } }; - if c == CTRL_Q { - break; + match c { + Key::CtrlQ => { + break; + } + Key::Up => { + let _ = stdout.write(b"\x1b[A"); + let _ = stdout.flush(); + } + Key::Down => { + let _ = stdout.write(b"\x1b[B"); + let _ = stdout.flush(); + } + Key::Left => { + let _ = stdout.write(b"\x1b[D"); + let _ = stdout.flush(); + } + Key::Right => { + let _ = stdout.write(b"\x1b[C"); + let _ = stdout.flush(); + } + Key::Char(c) => { + print!("{} ", c); + let _ = stdout.flush(); + } } } return Ok(()); |
