add saving

This commit is contained in:
Crispy 2023-02-26 19:24:27 +01:00
parent 3f2d3491ed
commit 856951a7a0

View file

@ -1,6 +1,6 @@
use std::{ use std::{
fs, fs::{self, File},
io::{stdin, stdout, Write}, io::{stdin, stdout, Stdout, Write},
ops::Range, ops::Range,
process::exit, process::exit,
}; };
@ -8,17 +8,18 @@ use termion::{
clear, cursor, clear, cursor,
event::{Event, Key}, event::{Event, Key},
input::TermRead, input::TermRead,
raw::IntoRawMode, raw::{IntoRawMode, RawTerminal},
terminal_size, terminal_size,
}; };
const TAB_SIZE: usize = 4; const TAB_SIZE: usize = 4;
#[derive(Debug)]
pub struct Editor { pub struct Editor {
text: String, text: String,
lines: Vec<Line>, lines: Vec<Line>,
cursor: Cursor, cursor: Cursor,
path: Option<String>,
term: RawTerminal<Stdout>,
quit: bool, quit: bool,
} }
@ -34,6 +35,7 @@ type Line = Range<usize>;
impl Editor { impl Editor {
pub fn new(path: Option<String>) -> Self { pub fn new(path: Option<String>) -> Self {
let text = path let text = path
.as_ref()
.map(|path| { .map(|path| {
fs::read_to_string(path).unwrap_or_else(|err| { fs::read_to_string(path).unwrap_or_else(|err| {
println!("Error: {err}"); println!("Error: {err}");
@ -42,27 +44,29 @@ impl Editor {
}) })
.unwrap_or_default(); .unwrap_or_default();
let term = stdout().into_raw_mode().unwrap();
Editor { Editor {
text, text,
lines: Vec::new(), lines: Vec::new(),
cursor: Cursor { line: 0, column: 0 }, cursor: Cursor { line: 0, column: 0 },
term,
path,
quit: false, quit: false,
} }
} }
pub fn run(mut self) { pub fn run(mut self) {
println!("{}", clear::All); print!("{}", clear::All);
stdout().flush().unwrap(); stdout().flush().unwrap();
let _t = stdout().into_raw_mode().unwrap();
self.find_lines();
while !self.quit { while !self.quit {
self.find_lines();
self.draw(); self.draw();
self.input(); self.input();
} }
println!("{}", clear::All); print!("{}", clear::All);
stdout().flush().unwrap();
// self.term.suspend_raw_mode().unwrap();
} }
fn input(&mut self) { fn input(&mut self) {
@ -78,9 +82,7 @@ impl Editor {
Key::Right => self.move_right(), Key::Right => self.move_right(),
Key::Up => self.move_up(), Key::Up => self.move_up(),
Key::Down => self.move_down(), Key::Down => self.move_down(),
Key::F(1) => { Key::Ctrl('s') => self.save(),
dbg!(&self);
}
_ => (), _ => (),
} }
} }
@ -238,4 +240,23 @@ impl Editor {
let preceding_tabs = self.text[start..end].chars().filter(|&c| c == '\t').count(); let preceding_tabs = self.text[start..end].chars().filter(|&c| c == '\t').count();
preceding_chars + preceding_tabs * (TAB_SIZE - 1) preceding_chars + preceding_tabs * (TAB_SIZE - 1)
} }
fn save(&mut self) {
if self.path.is_none() {
self.path = Some(self.read_line("Save as: "));
}
let mut file = File::create(self.path.as_ref().unwrap()).unwrap();
file.write_all(self.text.as_bytes()).unwrap();
}
fn read_line(&self, prompt: &str) -> String {
// TODO: use events instead and allow cancelling with esc
self.term.suspend_raw_mode().unwrap();
print!("{}{prompt}", cursor::Goto(1, terminal_size().unwrap().1));
stdout().flush().unwrap();
let mut response = String::new();
stdin().read_line(&mut response).unwrap();
self.term.activate_raw_mode().unwrap();
response.trim_end().into()
}
} }