the history file now stores the first line properly
This commit is contained in:
parent
ab349f7894
commit
ca639db826
@ -126,27 +126,23 @@ impl Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn history_write(&self) {
|
pub fn spawn_handle(&mut self, command_process: io::Result<process::Child>) {
|
||||||
if let Some(history_file) = &self.history {
|
|
||||||
history_file.write(&self.input);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn spawn(&self, command_process: io::Result<process::Child>) {
|
|
||||||
if let Ok(mut child) = command_process {
|
if let Ok(mut child) = command_process {
|
||||||
self.history_write();
|
if let Some(history_file) = self.history.as_mut() {
|
||||||
|
history_file.write(&self.input);
|
||||||
|
};
|
||||||
child.wait().ok();
|
child.wait().ok();
|
||||||
} else {
|
} else {
|
||||||
println!("Unknown command: {}", self.input)
|
println!("Unknown command: {}", self.input)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn exec(&self) {
|
pub fn exec(&mut self) {
|
||||||
let mut args = self.input.split_whitespace();
|
let mut args = self.input.split_whitespace();
|
||||||
if let Some(command) = args.next() {
|
if let Some(command) = args.next() {
|
||||||
match command {
|
match command {
|
||||||
"cd" => { self.change_directory(args); },
|
"cd" => { self.change_directory(args); },
|
||||||
command => { self.spawn(process::Command::new(command).args(args).spawn()); }
|
command => { self.spawn_handle(process::Command::new(command).args(args).spawn()); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
33
src/rc.rs
33
src/rc.rs
@ -95,29 +95,46 @@ pub fn config_file() -> Option<PathBuf> {
|
|||||||
config_file.is_valid_file_or_create(DEFAULT_CONFIG_CONTENT.as_bytes())
|
config_file.is_valid_file_or_create(DEFAULT_CONFIG_CONTENT.as_bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct History(PathBuf);
|
pub struct History {
|
||||||
|
history_file: PathBuf,
|
||||||
|
checked_empty: bool
|
||||||
|
}
|
||||||
impl History {
|
impl History {
|
||||||
pub fn init() -> Option<Self> {
|
pub fn init() -> Option<Self> {
|
||||||
config_dir().map(|mut config| {
|
config_dir().map(|mut config| {
|
||||||
config.push(".history");
|
config.push(".history");
|
||||||
config.is_valid_file_or_create(b"");
|
config.is_valid_file_or_create(b"");
|
||||||
Self(config)
|
Self {
|
||||||
|
history_file: config,
|
||||||
|
checked_empty: false
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write<S: AsRef<str>>(&self, content: S) {
|
pub fn is_empty(&mut self) -> bool {
|
||||||
//feasible hack instead of using about 10 functions
|
match self.checked_empty {
|
||||||
OpenOptions::new().append(true).open(self.0.as_path()).map_or_display(|mut file| {
|
true => true,
|
||||||
const NEXTLINE: &str = "\n";
|
false => self.read().map_or(false, |history_l| {
|
||||||
|
self.checked_empty = true;
|
||||||
|
history_l.is_empty()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if let Err(write_err) = file.write_all(format!("{}{}", NEXTLINE, content.as_ref()).as_bytes()) {
|
pub fn write<S: AsRef<str>>(&mut self, content: S) {
|
||||||
|
OpenOptions::new().append(true).open(self.history_file.as_path()).map_or_display(|mut file| {
|
||||||
|
let write_data = match self.is_empty() {
|
||||||
|
true => content.as_ref().to_owned(),
|
||||||
|
false => format!("\n{}", content.as_ref()),
|
||||||
|
};
|
||||||
|
if let Err(write_err) = file.write_all(write_data.as_bytes()) {
|
||||||
shell_error(write_err);
|
shell_error(write_err);
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(&self) -> Option<Vec<String>> {
|
pub fn read(&self) -> Option<Vec<String>> {
|
||||||
File::open(&self.0).map_or_display_none(|file| {
|
File::open(&self.history_file).map_or_display_none(|file| {
|
||||||
Some(BufReader::new(file).lines().map_while(Result::ok).collect::<Vec<String>>())
|
Some(BufReader::new(file).lines().map_while(Result::ok).collect::<Vec<String>>())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user