From 7b5c3d543d198a18884c990d540f5debc8a4d8d5 Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Mon, 26 Apr 2021 11:50:31 +0100 Subject: Support bash, resolves #3 --- atuin-client/src/import/bash.rs | 79 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 atuin-client/src/import/bash.rs (limited to 'atuin-client/src/import/bash.rs') diff --git a/atuin-client/src/import/bash.rs b/atuin-client/src/import/bash.rs new file mode 100644 index 00000000..d5fbef46 --- /dev/null +++ b/atuin-client/src/import/bash.rs @@ -0,0 +1,79 @@ +use std::io::{BufRead, BufReader}; +use std::{fs::File, path::Path}; + +use eyre::{eyre, Result}; + +use super::count_lines; +use crate::history::History; + +#[derive(Debug)] +pub struct Bash { + file: BufReader, + + pub loc: u64, + pub counter: i64, +} + +impl Bash { + pub fn new(path: impl AsRef) -> Result { + let file = File::open(path)?; + let mut buf = BufReader::new(file); + let loc = count_lines(&mut buf)?; + + Ok(Self { + file: buf, + loc: loc as u64, + counter: 0, + }) + } + + fn read_line(&mut self) -> Option> { + let mut line = String::new(); + + match self.file.read_line(&mut line) { + Ok(0) => None, + Ok(_) => Some(Ok(line)), + Err(e) => Some(Err(eyre!("failed to read line: {}", e))), // we can skip past things like invalid utf8 + } + } +} + +impl Iterator for Bash { + type Item = Result; + + fn next(&mut self) -> Option { + let line = self.read_line()?; + + if let Err(e) = line { + return Some(Err(e)); // :( + } + + let mut line = line.unwrap(); + + while line.ends_with("\\\n") { + let next_line = self.read_line()?; + + if next_line.is_err() { + break; + } + + line.push_str(next_line.unwrap().as_str()); + } + + let time = chrono::Utc::now(); + let offset = chrono::Duration::seconds(self.counter); + let time = time - offset; + + self.counter += 1; + + Some(Ok(History::new( + time, + line.trim_end().to_string(), + String::from("unknown"), + -1, + -1, + None, + None, + ))) + } +} -- cgit v1.3.1