Compare commits

..

2 commits

Author SHA1 Message Date
295982f6e6 show unsaved changes in editor view 2023-03-14 13:04:51 +01:00
baba71fff7 add scrolling to file view 2023-03-14 13:00:44 +01:00
2 changed files with 35 additions and 19 deletions

View file

@ -73,23 +73,20 @@ impl Editor {
}
}
pub fn name(&self) -> String {
pub fn title(&self) -> String {
if let Some(path) = &self.path {
if let Some(name) = path.file_name() {
return name.to_string_lossy().to_string();
let decorator = if self.unsaved_changes { "*" } else { " " };
return format!("{}{}", decorator, name.to_string_lossy());
}
}
"untitled".into()
"*untitled".into()
}
pub fn path(&self) -> Option<&PathBuf> {
self.path.as_ref()
}
pub fn has_unsaved_changes(&self) -> bool {
self.unsaved_changes
}
pub fn enter(&mut self) {
self.active = true;
self.find_lines();
@ -227,7 +224,7 @@ impl Editor {
"({},{}) {}",
self.cursor.line,
self.physical_column(),
self.name(),
self.title(),
);
}
}

View file

@ -34,6 +34,7 @@ struct Navigator {
init_path: PathBuf,
immediate_open: bool,
message: Option<String>,
scroll: usize,
}
impl Navigator {
@ -70,6 +71,7 @@ impl Navigator {
path,
immediate_open,
message: None,
scroll: 0,
}
}
@ -98,20 +100,20 @@ impl Navigator {
queue!(stdout(), SetColors(Colors::new(Color::Black, Color::White))).unwrap();
}
queue!(stdout(), MoveTo(1, index as u16 + 1)).unwrap();
print!(
"{}{}",
editor.has_unsaved_changes().then_some("*").unwrap_or(" "),
editor.name()
);
print!("{}", editor.title());
queue!(stdout(), ResetColor).unwrap();
}
let offset = self.editors.len() as u16 + 2;
queue!(stdout(), MoveTo(0, offset)).unwrap();
print!("Current dir: {}", self.path.to_string_lossy());
for (index, path) in self.files.iter().enumerate() {
if index == self.selected.wrapping_sub(self.editors.len()) {
let max_rows = terminal::size().unwrap().1 as usize - self.editors.len() - 4;
let end = (self.scroll + max_rows).min(self.files.len());
let visible_rows = self.scroll..end;
for (index, path) in self.files[visible_rows].iter().enumerate() {
if index + self.scroll == self.selected.wrapping_sub(self.editors.len()) {
queue!(stdout(), SetColors(Colors::new(Color::Black, Color::White))).unwrap();
}
queue!(stdout(), MoveTo(1, index as u16 + 1 + offset)).unwrap();
@ -157,16 +159,32 @@ impl Navigator {
}
fn nav_up(&mut self) {
self.selected = self.selected.saturating_sub(1);
if self.selected > 0 {
self.selected -= 1;
} else {
let selected_max = self.editors.len() + self.files.len();
self.selected = selected_max - 1;
}
self.update_scroll();
}
fn nav_down(&mut self) {
self.selected = (self.selected + 1).min(self.editors.len() + self.files.len() - 1);
let selected_max = self.editors.len() + self.files.len();
self.selected = (self.selected + 1) % selected_max;
self.update_scroll();
}
fn update_scroll(&mut self) {
let height = terminal::size().unwrap().1 as usize - self.editors.len() - 5;
let selected_file = self.selected.saturating_sub(self.editors.len());
self.scroll = self
.scroll
.clamp(selected_file.saturating_sub(height), selected_file);
}
fn enter(&mut self) {
if self.selected < self.editors.len() {
self.editors[self.selected].enter();
self.open_selected();
return;
}
@ -220,6 +238,7 @@ impl Navigator {
fn open_selected(&mut self) {
if self.selected < self.editors.len() {
self.scroll = 0;
self.editors[self.selected].enter();
}
}