persistent history and cargo fmt

This commit is contained in:
Arthur Beck 2025-05-06 16:41:28 -05:00
parent 0453209025
commit f96a1292e7
3 changed files with 46 additions and 14 deletions

View file

@ -9,7 +9,7 @@ pub const BUILTINS: [(
fn(args: Vec<String>, unsplit_args: String, state: &mut super::State) -> i32,
&str,
&str,
); 20] = [
); 21] = [
(
"cd",
cd,
@ -110,6 +110,12 @@ pub const BUILTINS: [(
"",
"Change the colors of the terminal to cycle through the pride flag colors!",
),
(
"history",
history,
"",
"Output the full history being used by this shell, prefixed by numbers.",
),
];
/// Change the directory
@ -518,3 +524,25 @@ pub fn gay(_: Vec<String>, _: String, state: &mut super::State) -> i32 {
state.in_mode = true;
0
}
/// Output the history
pub fn history(_: Vec<String>, _: String, state: &mut super::State) -> i32 {
for (i, item) in state.history.iter().enumerate() {
let item = item.trim_matches(|c: char| c.is_control());
if state.in_mode {
let table = [
"\x1b[31;1m",
"\x1b[38;2;255;165;0m",
"\x1b[33;1m",
"\x1b[32;1m",
"\x1b[34;1m",
"\x1b[36;1m",
"\x1b[35;1m",
];
let idx = i % table.len();
print!("{}", table[idx]);
}
println!("{}: {}", i + 1, item);
}
0
}

View file

@ -100,6 +100,8 @@ struct State {
in_mode: bool,
/// sh
entries: usize,
/// The history
history: Vec<String>,
}
unsafe impl Sync for State {}
@ -435,6 +437,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
raw_term: None,
in_mode: false,
entries: 0,
history: std::fs::read_to_string(std::env::home_dir().unwrap().join(".sesh_history"))
.unwrap_or_default()
.split("\n")
.map(|v| v.trim_matches(|ch: char| ch.is_control()))
.map(|v| v.to_string())
.filter(|v| !v.is_empty())
.collect(),
};
state.shell_env.push(ShellVar {
name: "PROMPT1".to_string(),
@ -483,13 +492,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
eval(&options.run_before, &mut state)
}
let mut history: Vec<String> =
std::fs::read_to_string(std::env::home_dir().unwrap().join(".sesh_history"))
.unwrap_or_default()
.split("\n")
.map(|v| v.to_string())
.collect();
let mut hist_ptr: usize = history.len();
let mut hist_ptr: usize = state.history.len();
state.raw_term = Some(Arc::new(RwLock::new(std::io::stdout().into_raw_mode()?)));
@ -552,14 +555,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
curr_inp_hist = input;
input = history[hist_ptr].clone();
input = state.history[hist_ptr].clone();
writer.write_all(input.as_bytes())?;
writer.flush()?;
}
}
[91, 66] => {
// down arrow
if hist_ptr + 1 < history.len() {
if hist_ptr + 1 < state.history.len() {
hist_ptr += 1;
let writer = state.raw_term.clone().unwrap();
let mut writer = writer.write().unwrap();
@ -568,11 +571,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
write_prompt(state.clone())?;
writer.write_all(b"\x1b[0K")?;
input = history[hist_ptr].clone();
input = state.history[hist_ptr].clone();
writer.write_all(input.as_bytes())?;
writer.flush()?;
} else {
hist_ptr = history.len();
hist_ptr = state.history.len();
let writer = state.raw_term.clone().unwrap();
let mut writer = writer.write().unwrap();
@ -637,7 +640,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("\x0D");
input = input.clone().trim().to_string();
history.push(input.clone());
state.history.push(input.clone());
std::fs::OpenOptions::new()
.create(true)
@ -647,7 +650,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.write_all((input.clone() + "\n").into_bytes().as_slice())
.unwrap();
hist_ptr = history.len();
hist_ptr = state.history.len();
state.entries += 1;
eval(&input, &mut state);

View file

@ -15,6 +15,7 @@ pub fn bench_eval(bencher: &mut test::Bencher) {
raw_term: None,
in_mode: false,
entries: 0,
history: vec![],
};
state.shell_env.push(ShellVar {
name: "PROMPT1".to_string(),