cwd in menu to allow saving files outside original directory
This commit is contained in:
parent
79e4364f5a
commit
c4f0d85e24
2 changed files with 7 additions and 2 deletions
|
@ -6,6 +6,7 @@ use crossterm::{
|
||||||
terminal::{self, Clear, ClearType},
|
terminal::{self, Clear, ClearType},
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
|
env,
|
||||||
fs::{self, File},
|
fs::{self, File},
|
||||||
io::{stdout, Write},
|
io::{stdout, Write},
|
||||||
ops::Range,
|
ops::Range,
|
||||||
|
@ -443,7 +444,7 @@ impl Editor {
|
||||||
|
|
||||||
fn save(&mut self) {
|
fn save(&mut self) {
|
||||||
if self.path.is_none() {
|
if self.path.is_none() {
|
||||||
self.path = read_line("Enter path: ").map(PathBuf::from);
|
self.path = read_line("Enter path: ").map(|s| env::current_dir().unwrap().join(s));
|
||||||
if self.path.is_none() {
|
if self.path.is_none() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@ struct Navigator {
|
||||||
files: Vec<PathBuf>,
|
files: Vec<PathBuf>,
|
||||||
selected: usize,
|
selected: usize,
|
||||||
path: PathBuf,
|
path: PathBuf,
|
||||||
|
init_path: PathBuf,
|
||||||
immediate_open: bool,
|
immediate_open: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,6 +65,7 @@ impl Navigator {
|
||||||
editors,
|
editors,
|
||||||
selected: 0,
|
selected: 0,
|
||||||
files: Vec::new(),
|
files: Vec::new(),
|
||||||
|
init_path: path.clone(),
|
||||||
path,
|
path,
|
||||||
immediate_open,
|
immediate_open,
|
||||||
}
|
}
|
||||||
|
@ -131,7 +133,7 @@ impl Navigator {
|
||||||
KeyCode::Up => self.nav_up(),
|
KeyCode::Up => self.nav_up(),
|
||||||
KeyCode::Down => self.nav_down(),
|
KeyCode::Down => self.nav_down(),
|
||||||
KeyCode::Enter => self.enter(),
|
KeyCode::Enter => self.enter(),
|
||||||
KeyCode::Home => self.path = env::current_dir().unwrap(),
|
KeyCode::Home => self.path = self.init_path.clone(),
|
||||||
KeyCode::Char('n') => {
|
KeyCode::Char('n') => {
|
||||||
if event.modifiers == KeyModifiers::CONTROL {
|
if event.modifiers == KeyModifiers::CONTROL {
|
||||||
self.new_editor();
|
self.new_editor();
|
||||||
|
@ -158,11 +160,13 @@ impl Navigator {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
if let Some(parent) = self.path.parent() {
|
if let Some(parent) = self.path.parent() {
|
||||||
self.path = parent.to_owned();
|
self.path = parent.to_owned();
|
||||||
|
env::set_current_dir(&self.path).unwrap();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let path = &self.files[i];
|
let path = &self.files[i];
|
||||||
if path.is_dir() {
|
if path.is_dir() {
|
||||||
self.path = self.path.join(path);
|
self.path = self.path.join(path);
|
||||||
|
env::set_current_dir(&self.path).unwrap();
|
||||||
self.selected = self.editors.len();
|
self.selected = self.editors.len();
|
||||||
} else if path.is_file() {
|
} else if path.is_file() {
|
||||||
if let Some(editor) =
|
if let Some(editor) =
|
||||||
|
|
Loading…
Reference in a new issue