From 937bbbacdd097f71af7569d4dc79cc583af90f4e Mon Sep 17 00:00:00 2001 From: pagedmov Date: Sun, 23 Mar 2025 17:41:12 -0400 Subject: [PATCH] Cleaned up warnings --- src/builtin/echo.rs | 4 +- src/builtin/export.rs | 2 +- src/builtin/flowctl.rs | 2 +- src/builtin/jobctl.rs | 2 +- src/builtin/pwd.rs | 4 +- src/builtin/shift.rs | 2 +- src/builtin/source.rs | 2 +- src/expand.rs | 4 +- src/fern.rs | 2 + src/getopt.rs | 4 +- src/parse/execute.rs | 9 +- src/parse/lex.rs | 2 - src/parse/mod.rs | 2 +- src/procio.rs | 2 +- src/prompt/history.rs | 260 ----------------------------------------- src/prompt/mod.rs | 1 - src/prompt/readline.rs | 3 +- src/state.rs | 4 +- 18 files changed, 23 insertions(+), 288 deletions(-) delete mode 100644 src/prompt/history.rs diff --git a/src/builtin/echo.rs b/src/builtin/echo.rs index 5b972b4..f3e7dc3 100644 --- a/src/builtin/echo.rs +++ b/src/builtin/echo.rs @@ -1,6 +1,4 @@ -use std::sync::Arc; - -use crate::{builtin::setup_builtin, getopt::{get_opts_from_tokens, Opt, ECHO_OPTS}, jobs::{ChildProc, JobBldr}, libsh::error::{ShErr, ShErrKind, ShResult, ShResultExt}, parse::{execute::prepare_argv, NdRule, Node}, prelude::*, procio::{borrow_fd, IoStack}, state}; +use crate::{builtin::setup_builtin, getopt::{get_opts_from_tokens, Opt, ECHO_OPTS}, jobs::JobBldr, libsh::error::{ShErr, ShErrKind, ShResult, ShResultExt}, parse::{NdRule, Node}, prelude::*, procio::{borrow_fd, IoStack}, state}; bitflags! { pub struct EchoFlags: u32 { diff --git a/src/builtin/export.rs b/src/builtin/export.rs index cbe4973..ab6c9cd 100644 --- a/src/builtin/export.rs +++ b/src/builtin/export.rs @@ -1,4 +1,4 @@ -use crate::{jobs::{ChildProc, JobBldr}, libsh::error::{ShErr, ShErrKind, ShResult}, parse::{execute::prepare_argv, NdRule, Node}, prelude::*, procio::{borrow_fd, IoStack}, state}; +use crate::{jobs::JobBldr, libsh::error::{ShErr, ShErrKind, ShResult}, parse::{NdRule, Node}, prelude::*, procio::{borrow_fd, IoStack}, state}; use super::setup_builtin; diff --git a/src/builtin/flowctl.rs b/src/builtin/flowctl.rs index 3ae295c..f28a39b 100644 --- a/src/builtin/flowctl.rs +++ b/src/builtin/flowctl.rs @@ -1,4 +1,4 @@ -use crate::{builtin::setup_builtin, jobs::JobBldr, libsh::error::{ShErr, ShErrKind, ShResult}, parse::{execute::prepare_argv, NdRule, Node}, prelude::*, procio::IoStack, state}; +use crate::{libsh::error::{ShErr, ShErrKind, ShResult}, parse::{execute::prepare_argv, NdRule, Node}, prelude::*}; pub fn flowctl(node: Node, kind: ShErrKind) -> ShResult<()> { use ShErrKind::*; diff --git a/src/builtin/jobctl.rs b/src/builtin/jobctl.rs index 9f17c50..fa47d6f 100644 --- a/src/builtin/jobctl.rs +++ b/src/builtin/jobctl.rs @@ -1,4 +1,4 @@ -use crate::{jobs::{ChildProc, JobBldr, JobCmdFlags, JobID}, libsh::error::{ShErr, ShErrKind, ShResult}, parse::{execute::prepare_argv, lex::Span, NdRule, Node}, prelude::*, procio::{borrow_fd, IoStack}, state::{self, read_jobs, write_jobs}}; +use crate::{jobs::{JobBldr, JobCmdFlags, JobID}, libsh::error::{ShErr, ShErrKind, ShResult}, parse::{lex::Span, NdRule, Node}, prelude::*, procio::{borrow_fd, IoStack}, state::{self, read_jobs, write_jobs}}; use super::setup_builtin; diff --git a/src/builtin/pwd.rs b/src/builtin/pwd.rs index f76fa4a..7925d69 100644 --- a/src/builtin/pwd.rs +++ b/src/builtin/pwd.rs @@ -1,9 +1,9 @@ -use crate::{jobs::{ChildProc, JobBldr}, libsh::error::ShResult, parse::{NdRule, Node}, prelude::*, procio::{borrow_fd, IoStack}, state}; +use crate::{jobs::JobBldr, libsh::error::ShResult, parse::{NdRule, Node}, prelude::*, procio::{borrow_fd, IoStack}, state}; use super::setup_builtin; pub fn pwd(node: Node, io_stack: &mut IoStack, job: &mut JobBldr) -> ShResult<()> { - let NdRule::Command { assignments, argv } = node.class else { + let NdRule::Command { assignments: _, argv } = node.class else { unreachable!() }; diff --git a/src/builtin/shift.rs b/src/builtin/shift.rs index 3b2b401..aaf61b1 100644 --- a/src/builtin/shift.rs +++ b/src/builtin/shift.rs @@ -1,4 +1,4 @@ -use crate::{jobs::{ChildProc, JobBldr}, libsh::error::{ShErr, ShErrKind, ShResult}, parse::{execute::prepare_argv, NdRule, Node}, prelude::*, state::{self, write_vars}}; +use crate::{jobs::JobBldr, libsh::error::{ShErr, ShErrKind, ShResult}, parse::{NdRule, Node}, state::{self, write_vars}}; use super::setup_builtin; diff --git a/src/builtin/source.rs b/src/builtin/source.rs index ae8cca8..d47efd5 100644 --- a/src/builtin/source.rs +++ b/src/builtin/source.rs @@ -1,4 +1,4 @@ -use crate::{jobs::{ChildProc, JobBldr}, libsh::error::{ShErr, ShErrKind, ShResult}, parse::{execute::prepare_argv, NdRule, Node}, prelude::*, state::{self, source_file}}; +use crate::{jobs::JobBldr, libsh::error::{ShErr, ShErrKind, ShResult}, parse::{NdRule, Node}, prelude::*, state::{self, source_file}}; use super::setup_builtin; diff --git a/src/expand.rs b/src/expand.rs index 7bbe291..fb4c9ee 100644 --- a/src/expand.rs +++ b/src/expand.rs @@ -1,6 +1,6 @@ -use std::collections::{HashSet, VecDeque}; +use std::collections::HashSet; -use crate::{exec_input, libsh::error::{ShErr, ShErrKind, ShResult}, parse::{lex::{is_field_sep, is_hard_sep, is_keyword, LexFlags, LexStream, Span, Tk, TkFlags, TkRule}, Redir, RedirType}, prelude::*, procio::{IoBuf, IoFrame, IoMode}, state::{read_logic, read_meta, read_vars, write_meta}}; +use crate::{exec_input, libsh::error::{ShErr, ShErrKind, ShResult}, parse::{lex::{is_field_sep, is_hard_sep, LexFlags, LexStream, Span, Tk, TkFlags, TkRule}, Redir, RedirType}, prelude::*, procio::{IoBuf, IoFrame, IoMode}, state::{read_logic, read_vars, write_meta}}; /// Variable substitution marker pub const VAR_SUB: char = '\u{fdd0}'; diff --git a/src/fern.rs b/src/fern.rs index 418047a..f20da81 100644 --- a/src/fern.rs +++ b/src/fern.rs @@ -37,6 +37,8 @@ pub fn save_termios() { }); } } + +#[allow(static_mut_refs)] pub fn get_saved_termios() -> Option { unsafe { // This is only used when the shell exits so it's fine diff --git a/src/getopt.rs b/src/getopt.rs index f170bbe..3060e8e 100644 --- a/src/getopt.rs +++ b/src/getopt.rs @@ -1,8 +1,8 @@ -use std::{ops::Deref, str::FromStr, sync::{Arc, LazyLock}}; +use std::sync::{Arc, LazyLock}; use fmt::Display; -use crate::{libsh::error::ShResult, parse::lex::Tk, prelude::*}; +use crate::{parse::lex::Tk, prelude::*}; type OptSet = Arc<[Opt]>; diff --git a/src/parse/execute.rs b/src/parse/execute.rs index 05a936e..d657aa7 100644 --- a/src/parse/execute.rs +++ b/src/parse/execute.rs @@ -1,9 +1,9 @@ use std::collections::VecDeque; -use crate::{builtin::{alias::alias, cd::cd, echo::echo, export::export, flowctl::flowctl, jobctl::{continue_job, jobs, JobBehavior}, pwd::pwd, shift::shift, source::source}, exec_input, jobs::{dispatch_job, ChildProc, Job, JobBldr, JobStack}, libsh::{error::{ShErr, ShErrKind, ShResult, ShResultExt}, utils::RedirVecUtils}, prelude::*, procio::{IoFrame, IoMode, IoStack}, state::{self, read_logic, read_vars, write_logic, write_vars, ShFunc, VarTab}}; +use crate::{builtin::{alias::alias, cd::cd, echo::echo, export::export, flowctl::flowctl, jobctl::{continue_job, jobs, JobBehavior}, pwd::pwd, shift::shift, source::source}, jobs::{dispatch_job, ChildProc, JobBldr, JobStack}, libsh::{error::{ShErr, ShErrKind, ShResult, ShResultExt}, utils::RedirVecUtils}, prelude::*, procio::{IoFrame, IoMode, IoStack}, state::{self, read_logic, read_vars, write_logic, write_vars, ShFunc, VarTab}}; -use super::{lex::{LexFlags, LexStream, Span, Tk, TkFlags, KEYWORDS}, AssignKind, CaseNode, CondNode, ConjunctNode, ConjunctOp, LoopKind, NdFlags, NdRule, Node, ParseStream, ParsedSrc, Redir, RedirType}; +use super::{lex::{Span, Tk, TkFlags, KEYWORDS}, AssignKind, CaseNode, CondNode, ConjunctNode, ConjunctOp, LoopKind, NdFlags, NdRule, Node, ParsedSrc, Redir, RedirType}; pub enum AssignBehavior { Export, @@ -476,7 +476,7 @@ pub fn run_fork<'t,C,P>( ) -> ShResult<()> where C: Fn(IoFrame,Option), - P: Fn(IoFrame,&mut JobBldr,Option<&str>,Pid) -> ShResult<()> + P: Fn(&mut JobBldr,Option<&str>,Pid) -> ShResult<()> { match unsafe { fork()? } { ForkResult::Child => { @@ -489,7 +489,7 @@ where } else { None }; - parent_action(io_frame,job,cmd.as_deref(),child) + parent_action(job,cmd.as_deref(),child) } } } @@ -529,7 +529,6 @@ pub fn def_child_action(mut io_frame: IoFrame, exec_args: Option) { /// The default behavior for the parent process after forking pub fn def_parent_action( - io_frame: IoFrame, job: &mut JobBldr, cmd: Option<&str>, child_pid: Pid diff --git a/src/parse/lex.rs b/src/parse/lex.rs index f1a8841..b166eb6 100644 --- a/src/parse/lex.rs +++ b/src/parse/lex.rs @@ -311,7 +311,6 @@ impl LexStream { let slice = self.slice_from_cursor().unwrap().to_string(); let mut pos = self.cursor; let mut chars = slice.chars().peekable(); - let mut quote_pos = None; if let Some(count) = case_pat_lookahead(chars.clone()) { pos += count; @@ -434,7 +433,6 @@ impl LexStream { } '"' | '\'' => { self.in_quote = true; - quote_pos = Some(pos); pos += 1; while let Some(q_ch) = chars.next() { match q_ch { diff --git a/src/parse/mod.rs b/src/parse/mod.rs index e902698..7670228 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -570,7 +570,7 @@ impl ParseStream { let mut node_tks: Vec = vec![]; let pattern: Tk; let mut case_blocks: Vec = vec![]; - let mut redirs: Vec = vec![]; + let redirs: Vec = vec![]; if !self.check_keyword("case") || !self.next_tk_is_some() { return Ok(None) diff --git a/src/procio.rs b/src/procio.rs index 1e10df1..f405f17 100644 --- a/src/procio.rs +++ b/src/procio.rs @@ -1,6 +1,6 @@ use std::{fmt::Debug, ops::{Deref, DerefMut}}; -use crate::{libsh::{error::{ShErr, ShErrKind, ShResult}, utils::RedirVecUtils}, parse::{Redir, RedirType}, prelude::*}; +use crate::{libsh::{error::{ShErr, ShErrKind, ShResult}, utils::RedirVecUtils}, parse::Redir, prelude::*}; // Credit to fish-shell for many of the implementation ideas present in this module // https://fishshell.com/ diff --git a/src/prompt/history.rs b/src/prompt/history.rs deleted file mode 100644 index 016bbfc..0000000 --- a/src/prompt/history.rs +++ /dev/null @@ -1,260 +0,0 @@ -use std::{fs::File, ops::{Deref, DerefMut}, path::PathBuf}; - -use bitflags::bitflags; -use rustyline::history::{History, SearchResult}; -use serde::{Deserialize, Serialize}; - -use crate::{libsh::error::{ShErr, ShErrKind, ShResult}, prelude::*}; - -#[derive(Deserialize,Serialize,Debug)] -pub struct HistEntry { - body: String, - id: usize -} - -impl HistEntry { - pub fn new(body: String, id: usize) -> Self { - Self { body, id } - } - pub fn cmd(&self) -> &str { - &self.body - } - pub fn id(&self) -> usize { - self.id - } -} - -#[derive(Deserialize,Serialize,Default)] -pub struct HistEntries { - entries: Vec -} - -impl HistEntries { - pub fn new() -> Self { - Self { entries: vec![] } - } -} - -impl Deref for HistEntries { - type Target = Vec; - fn deref(&self) -> &Self::Target { - &self.entries - } -} - -impl DerefMut for HistEntries { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.entries - } -} - -pub struct FernHist { - file_path: Option, - entries: HistEntries, - max_len: usize, - pub flags: HistFlags -} - -bitflags! { - pub struct HistFlags: u32 { - const NO_DUPES = 0b0000001; - const IGNORE_SPACE = 0b0000010; - } -} - -impl<'e> FernHist { - pub fn new() -> Self { - Self { file_path: None, entries: HistEntries::new(), max_len: 1000, flags: HistFlags::empty() } - } - pub fn from_path(file_path: PathBuf) -> ShResult { - let mut new_hist = FernHist::new(); - new_hist.file_path = Some(file_path); - new_hist.load_hist()?; - Ok(new_hist) - } - pub fn create_entry(&self, body: &str) -> HistEntry { - let id = self.len() + 1; - HistEntry::new(body.to_string(), id) - } - pub fn init_hist_file(&mut self) -> ShResult<()> { - let Some(path) = self.file_path.clone() else { - return Ok(()); - }; - self.save(&path)?; - Ok(()) - } - pub fn load_hist(&mut self) -> ShResult<()> { - let Some(file_path) = self.file_path.clone() else { - return Err( - ShErr::simple( - ShErrKind::InternalErr, - "History file not set" - ) - ) - }; - if !file_path.is_file() { - self.init_hist_file()?; - } - let hist_file = File::open(&file_path)?; - self.entries = serde_yaml::from_reader(hist_file).unwrap_or_default(); - Ok(()) - } - -} - -impl Default for FernHist { - fn default() -> Self { - let home = std::env::var("HOME").unwrap(); - let file_path = PathBuf::from(&format!("{home}/.fernhist")); - Self::from_path(file_path).unwrap() - } -} - -impl History for FernHist { - fn add(&mut self, line: &str) -> rustyline::Result { - let new_entry = self.create_entry(line); - if self.flags.contains(HistFlags::NO_DUPES) { - let most_recent = self.get(self.len(), rustyline::history::SearchDirection::Reverse)?.unwrap(); - dbg!(&most_recent); - if new_entry.body == most_recent.entry.to_string() { - return Ok(false) - } - } - self.entries.push(new_entry); - Ok(true) - } - - fn get(&self, index: usize, dir: rustyline::history::SearchDirection) -> rustyline::Result> { - Ok(self.entries.iter().find(|ent| ent.id() == index).map(|ent| { - SearchResult { entry: ent.cmd().to_string().into(), idx: index, pos: 0 } - })) - } - - fn add_owned(&mut self, line: String) -> rustyline::Result { - todo!() - } - - fn len(&self) -> usize { - self.entries.len() - } - - fn is_empty(&self) -> bool { - self.entries.is_empty() - } - - fn set_max_len(&mut self, len: usize) -> rustyline::Result<()> { - self.max_len = len; - Ok(()) - } - - fn ignore_dups(&mut self, yes: bool) -> rustyline::Result<()> { - if yes { - self.flags |= HistFlags::NO_DUPES; - } else { - self.flags &= !HistFlags::NO_DUPES; - } - Ok(()) - } - - fn ignore_space(&mut self, yes: bool) { - if yes { - self.flags |= HistFlags::IGNORE_SPACE; - } else { - self.flags &= !HistFlags::IGNORE_SPACE; - } - } - - fn save(&mut self, path: &std::path::Path) -> rustyline::Result<()> { - let hist_file = File::create(path)?; - serde_yaml::to_writer(hist_file, &self.entries).unwrap(); - Ok(()) - } - - fn append(&mut self, path: &std::path::Path) -> rustyline::Result<()> { - todo!() - } - - fn load(&mut self, path: &std::path::Path) -> rustyline::Result<()> { - let path = path.to_path_buf(); - self.file_path = Some(path); - self.load_hist().map_err(|_| rustyline::error::ReadlineError::Io(std::io::Error::last_os_error())) - } - - fn clear(&mut self) -> rustyline::Result<()> { - self.entries.clear(); - if self.file_path.is_some() { - self.save(&self.file_path.clone().unwrap())?; - } - Ok(()) - } - - fn search( - &self, - term: &str, - start: usize, - dir: rustyline::history::SearchDirection, - ) -> rustyline::Result> { - if term.is_empty() { - return Ok(None) - } - let mut matches: Vec<&HistEntry> = self.entries.iter() - .filter(|ent| is_subsequence(&ent.body, term)) - .collect(); - - matches.sort_by(|ent_a, ent_b| { - let ent_a_rank = fuzzy_rank(term, &ent_a.body); - let ent_b_rank = fuzzy_rank(term, &ent_b.body); - ent_a_rank.cmp(&ent_b_rank) - .then(ent_a.id().cmp(&ent_b.id())) - }); - - Ok(matches.last().map(|ent| { - SearchResult { - entry: ent.body.clone().into(), - idx: ent.id(), - pos: start - } - })) - } - - fn starts_with( - &self, - term: &str, - start: usize, - dir: rustyline::history::SearchDirection, - ) -> rustyline::Result> { - let mut matches: Vec<&HistEntry> = self.entries.iter() - .filter(|ent| ent.body.starts_with(term)) - .collect(); - - matches.sort_by(|ent_a, ent_b| ent_a.id().cmp(&ent_b.id())); - dbg!(&matches); - Ok(matches.first().map(|ent| { - SearchResult { - entry: ent.body.clone().into(), - idx: ent.id(), - pos: start - } - })) - } -} - -fn fuzzy_rank(search_term: &str, search_result: &str) -> u8 { - if search_result == search_term { - 4 - } else if search_result.starts_with(search_term) { - 3 - } else if search_result.contains(search_term) { - 2 - } else if is_subsequence(search_result, search_term) { - 1 - } else { - 0 - } -} - -// Check if a search term is a subsequence of the body (characters in order but not necessarily adjacent) -fn is_subsequence(search_result: &str, search_term: &str) -> bool { - let mut result_chars = search_result.chars(); - search_term.chars().all(|ch| result_chars.any(|c| c == ch)) -} diff --git a/src/prompt/mod.rs b/src/prompt/mod.rs index 0fe5c2c..c32bc38 100644 --- a/src/prompt/mod.rs +++ b/src/prompt/mod.rs @@ -1,4 +1,3 @@ -pub mod history; pub mod readline; use std::path::Path; diff --git a/src/prompt/readline.rs b/src/prompt/readline.rs index 8c9798b..dc72e63 100644 --- a/src/prompt/readline.rs +++ b/src/prompt/readline.rs @@ -59,14 +59,13 @@ impl Hinter for FernReadline { } impl Highlighter for FernReadline { - fn highlight<'l>(&self, line: &'l str, pos: usize) -> std::borrow::Cow<'l, str> { + fn highlight<'l>(&self, line: &'l str, _pos: usize) -> std::borrow::Cow<'l, str> { Cow::Owned(line.to_string()) } } impl Validator for FernReadline { fn validate(&self, ctx: &mut rustyline::validate::ValidationContext) -> rustyline::Result { - return Ok(ValidationResult::Valid(None)); let mut tokens = vec![]; let tk_stream = LexStream::new(Arc::new(ctx.input().to_string()), LexFlags::empty()); for tk in tk_stream { diff --git a/src/state.rs b/src/state.rs index 6026cae..ed34898 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,8 +1,8 @@ -use std::{collections::{HashMap, VecDeque}, ops::{Deref, Range}, sync::{LazyLock, RwLock, RwLockReadGuard, RwLockWriteGuard}, time::Duration}; +use std::{collections::{HashMap, VecDeque}, ops::Deref, sync::{LazyLock, RwLock, RwLockReadGuard, RwLockWriteGuard}, time::Duration}; use nix::unistd::{gethostname, getppid, User}; -use crate::{exec_input, jobs::JobTab, libsh::{error::{ShErr, ShErrKind, ShResult}, utils::VecDequeExt}, parse::{lex::{get_char, Tk}, ConjunctNode, NdRule, Node, ParsedSrc}, prelude::*}; +use crate::{exec_input, jobs::JobTab, libsh::{error::{ShErr, ShErrKind, ShResult}, utils::VecDequeExt}, parse::{lex::get_char, ConjunctNode, NdRule, Node, ParsedSrc}, prelude::*}; pub static JOB_TABLE: LazyLock> = LazyLock::new(|| RwLock::new(JobTab::new()));