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,
cursory: u16,
buffer: Buffer,
filename: String,
}
#[derive(Default)]
@ -31,7 +32,13 @@ fn main() -> Result<()> {
// Basic stuff
let mut nuudel = Nuudel::default();
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::restore_terminal()?;
@ -68,12 +75,9 @@ impl Nuudel {
fn handle_events(&mut self) -> Result<()> {
match read()? {
Event::Key(event) => match event.code {
KeyCode::Char('q') => {
KeyCode::Esc => {
self.quit();
},
KeyCode::Char('a') => {
print!("");
},
KeyCode::Left => {
if self.cursorx > 0 {
self.cursorx -= 1;
@ -94,6 +98,11 @@ impl Nuudel {
self.cursory += 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))?;
y += 1;
}
print!("Press Q to quit");
print!("Press ESC to quit");
//stdout.execute(terminal::EndSynchronizedUpdate)?;
stdout.execute(cursor::MoveTo(self.cursorx, self.cursory))?;
@ -136,6 +145,7 @@ impl Nuudel {
.lines()
.map(String::from)
.collect();
self.filename = filename.to_string();
Ok(())
}
}