Compare commits

..

No commits in common. "295982f6e6e01e316f58666e187fd7e03b236c1c" and "d2e28b4608dc28b06b2b7247d9abae084222b295" have entirely different histories.

2 changed files with 19 additions and 35 deletions

View file

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

View file

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