Skip to content

Commit

Permalink
feat: 🚧 experimental but funcional nushell support
Browse files Browse the repository at this point in the history
  • Loading branch information
romancitodev committed Dec 8, 2024
1 parent 75d10ed commit 1830f89
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub enum HistoryProvider {
Atuin,
#[strum(serialize = "fish")]
Fish,
#[strum(serialize = "nu")]
Nu,
}

impl HistoryProvider {
Expand Down Expand Up @@ -52,6 +54,14 @@ impl HistoryProvider {
.output()?;
Ok(Box::new(Cursor::new(output.stdout)))
}
HistoryProvider::Nu => {
let output = Command::new("nu")
.arg("-l")
.arg("-c")
.arg("history | default [] | each {|i| $\"($i.start_timestamp);($i.command)\"} | table --flatten -i false --theme none")
.output()?;
Ok(Box::new(Cursor::new(output.stdout)))
}
}
}
}
Expand All @@ -75,7 +85,10 @@ impl Iterator for History {

fn next(&mut self) -> Option<Self::Item> {
match self.provider {
HistoryProvider::Zsh | HistoryProvider::Atuin | HistoryProvider::Fish => {
HistoryProvider::Zsh
| HistoryProvider::Atuin
| HistoryProvider::Nu
| HistoryProvider::Fish => {
let mut block = String::new();
let mut buf = vec![];
loop {
Expand Down
18 changes: 16 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl CommandParser {
HistoryProvider::Bash => self.parse_bash_raw(),
HistoryProvider::Atuin => self.parse_atuin_raw(),
HistoryProvider::Fish => self.parse_fish_raw(),
HistoryProvider::Nu => self.parse_nu_raw(),
}?;
let commands_splitted = RE_COMMAND.split(&commands_combined);
for commandline in commands_splitted {
Expand Down Expand Up @@ -115,17 +116,30 @@ impl CommandParser {
))
}

pub fn parse_nu_raw(&self) -> Result<ParsingData, Box<dyn Error>> {
let (time_raw, commands_raw) = self
.raw
.split_once(';')
.ok_or("failed to split nu command")?;

let time = NaiveDateTime::parse_and_remainder(time_raw.trim(), "%Y-%m-%d %H:%M:%S")
.ok()
.and_then(|naive_time| Local.from_local_datetime(&naive_time.0).single());

Ok((commands_raw.trim().into(), time))
}

pub fn parse_atuin_raw(&self) -> Result<ParsingData, Box<dyn Error>> {
let (time_raw, commands_raw) = self
.raw
.split_once(';')
.ok_or("failed to split atuin command")?;

let time = NaiveDateTime::parse_from_str(time_raw, "%Y-%m-%d %H:%M:%S")
let time = NaiveDateTime::parse_from_str(time_raw.trim(), "%Y-%m-%d %H:%M:%S")
.ok()
.and_then(|naive_time| Local.from_local_datetime(&naive_time).single());

Ok((commands_raw.into(), time))
Ok((commands_raw.trim().into(), time))
}

pub fn parse_fish_raw(&self) -> Result<ParsingData, Box<dyn Error>> {
Expand Down

0 comments on commit 1830f89

Please sign in to comment.