Basic editing

This commit is contained in:
Jarkko Toivanen 2024-08-13 02:21:26 +03:00
parent cb2511adf2
commit 20b5c0735d
Signed by: jt
GPG key ID: 9151B109B73ECAD5

View file

@ -13,6 +13,7 @@ struct Nuudel {
cursorx: u16, cursorx: u16,
cursory: u16, cursory: u16,
buffer: Buffer, buffer: Buffer,
filename: String,
} }
#[derive(Default)] #[derive(Default)]
@ -31,7 +32,13 @@ fn main() -> Result<()> {
// Basic stuff // Basic stuff
let mut nuudel = Nuudel::default(); let mut nuudel = Nuudel::default();
Nuudel::init_terminal()?; Nuudel::init_terminal()?;
nuudel.open_file("Cargo.toml")?;
// Handle args
let args: Vec<String> = std::env::args().collect();
if let Some(arg) = args.get(1) {
nuudel.open_file(arg)?;
}
nuudel.run(&mut stdout())?; nuudel.run(&mut stdout())?;
Nuudel::restore_terminal()?; Nuudel::restore_terminal()?;
@ -68,12 +75,9 @@ impl Nuudel {
fn handle_events(&mut self) -> Result<()> { fn handle_events(&mut self) -> Result<()> {
match read()? { match read()? {
Event::Key(event) => match event.code { Event::Key(event) => match event.code {
KeyCode::Char('q') => { KeyCode::Esc => {
self.quit(); self.quit();
}, },
KeyCode::Char('a') => {
print!("");
},
KeyCode::Left => { KeyCode::Left => {
if self.cursorx > 0 { if self.cursorx > 0 {
self.cursorx -= 1; self.cursorx -= 1;
@ -94,6 +98,11 @@ impl Nuudel {
self.cursory += 1; self.cursory += 1;
self.cursory = self.cursory.clamp(0, terminal::size()?.1-1); self.cursory = self.cursory.clamp(0, terminal::size()?.1-1);
}, },
KeyCode::Char(_) => {
self.buffer.lines[usize::try_from(self.cursory).unwrap()].insert(self.cursorx.into(), 'A');
self.cursorx += 1;
},
_ => {} _ => {}
}, },
_ => {} _ => {}
@ -120,7 +129,7 @@ impl Nuudel {
stdout.execute(cursor::MoveToNextLine(1))?; stdout.execute(cursor::MoveToNextLine(1))?;
y += 1; y += 1;
} }
print!("Press Q to quit"); print!("Press ESC to quit");
//stdout.execute(terminal::EndSynchronizedUpdate)?; //stdout.execute(terminal::EndSynchronizedUpdate)?;
stdout.execute(cursor::MoveTo(self.cursorx, self.cursory))?; stdout.execute(cursor::MoveTo(self.cursorx, self.cursory))?;
@ -136,6 +145,7 @@ impl Nuudel {
.lines() .lines()
.map(String::from) .map(String::from)
.collect(); .collect();
Ok(()) self.filename = filename.to_string();
Ok(())
} }
} }