Compare commits
No commits in common. "295982f6e6e01e316f58666e187fd7e03b236c1c" and "d2e28b4608dc28b06b2b7247d9abae084222b295" have entirely different histories.
295982f6e6
...
d2e28b4608
2 changed files with 19 additions and 35 deletions
|
@ -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(name) = path.file_name() {
|
||||
let decorator = if self.unsaved_changes { "*" } else { " " };
|
||||
return format!("{}{}", decorator, name.to_string_lossy());
|
||||
return name.to_string_lossy().to_string();
|
||||
}
|
||||
}
|
||||
"*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();
|
||||
|
@ -224,7 +227,7 @@ impl Editor {
|
|||
"({},{}) {}",
|
||||
self.cursor.line,
|
||||
self.physical_column(),
|
||||
self.title(),
|
||||
self.name(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
41
src/main.rs
41
src/main.rs
|
@ -34,7 +34,6 @@ struct Navigator {
|
|||
init_path: PathBuf,
|
||||
immediate_open: bool,
|
||||
message: Option<String>,
|
||||
scroll: usize,
|
||||
}
|
||||
|
||||
impl Navigator {
|
||||
|
@ -71,7 +70,6 @@ impl Navigator {
|
|||
path,
|
||||
immediate_open,
|
||||
message: None,
|
||||
scroll: 0,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -100,20 +98,20 @@ impl Navigator {
|
|||
queue!(stdout(), SetColors(Colors::new(Color::Black, Color::White))).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();
|
||||
}
|
||||
|
||||
let offset = self.editors.len() as u16 + 2;
|
||||
queue!(stdout(), MoveTo(0, offset)).unwrap();
|
||||
|
||||
print!("Current dir: {}", self.path.to_string_lossy());
|
||||
|
||||
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()) {
|
||||
for (index, path) in self.files.iter().enumerate() {
|
||||
if index == 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();
|
||||
|
@ -159,32 +157,16 @@ impl Navigator {
|
|||
}
|
||||
|
||||
fn nav_up(&mut self) {
|
||||
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();
|
||||
self.selected = self.selected.saturating_sub(1);
|
||||
}
|
||||
|
||||
fn nav_down(&mut self) {
|
||||
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);
|
||||
self.selected = (self.selected + 1).min(self.editors.len() + self.files.len() - 1);
|
||||
}
|
||||
|
||||
fn enter(&mut self) {
|
||||
if self.selected < self.editors.len() {
|
||||
self.open_selected();
|
||||
self.editors[self.selected].enter();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -238,7 +220,6 @@ impl Navigator {
|
|||
|
||||
fn open_selected(&mut self) {
|
||||
if self.selected < self.editors.len() {
|
||||
self.scroll = 0;
|
||||
self.editors[self.selected].enter();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue