aboutsummaryrefslogtreecommitdiffstats
path: root/atuin-client/src/import.rs
diff options
context:
space:
mode:
authorEllie Huxtable <e@elm.sh>2021-04-26 11:50:31 +0100
committerEllie Huxtable <e@elm.sh>2021-04-26 11:57:30 +0100
commit7b5c3d543d198a18884c990d540f5debc8a4d8d5 (patch)
treeffc6b9121f1b1299f1b30a26322e1988683c06c7 /atuin-client/src/import.rs
parentRevert to storing history as nanos (diff)
downloadatuin-7b5c3d543d198a18884c990d540f5debc8a4d8d5.zip
Support bash, resolves #3
Diffstat (limited to '')
-rw-r--r--atuin-client/src/import/zsh.rs (renamed from atuin-client/src/import.rs)69
1 files changed, 30 insertions, 39 deletions
diff --git a/atuin-client/src/import.rs b/atuin-client/src/import/zsh.rs
index 3b0b2a69..46e9af63 100644
--- a/atuin-client/src/import.rs
+++ b/atuin-client/src/import/zsh.rs
@@ -1,7 +1,7 @@
// import old shell history!
// automatically hoover up all that we can find
-use std::io::{BufRead, BufReader, Seek, SeekFrom};
+use std::io::{BufRead, BufReader};
use std::{fs::File, path::Path};
use chrono::prelude::*;
@@ -9,7 +9,8 @@ use chrono::Utc;
use eyre::{eyre, Result};
use itertools::Itertools;
-use super::history::History;
+use super::count_lines;
+use crate::history::History;
#[derive(Debug)]
pub struct Zsh {
@@ -19,14 +20,6 @@ pub struct Zsh {
pub counter: i64,
}
-// this could probably be sped up
-fn count_lines(buf: &mut BufReader<File>) -> Result<usize> {
- let lines = buf.lines().count();
- buf.seek(SeekFrom::Start(0))?;
-
- Ok(lines)
-}
-
impl Zsh {
pub fn new(path: impl AsRef<Path>) -> Result<Self> {
let file = File::open(path)?;
@@ -39,36 +32,7 @@ impl Zsh {
counter: 0,
})
}
-}
-
-fn parse_extended(line: &str, counter: i64) -> History {
- let line = line.replacen(": ", "", 2);
- let (time, duration) = line.splitn(2, ':').collect_tuple().unwrap();
- let (duration, command) = duration.splitn(2, ';').collect_tuple().unwrap();
-
- let time = time
- .parse::<i64>()
- .unwrap_or_else(|_| chrono::Utc::now().timestamp());
-
- let offset = chrono::Duration::milliseconds(counter);
- let time = Utc.timestamp(time, 0);
- let time = time + offset;
-
- let duration = duration.parse::<i64>().map_or(-1, |t| t * 1_000_000_000);
- // use nanos, because why the hell not? we won't display them.
- History::new(
- time,
- command.trim_end().to_string(),
- String::from("unknown"),
- 0, // assume 0, we have no way of knowing :(
- duration,
- None,
- None,
- )
-}
-
-impl Zsh {
fn read_line(&mut self) -> Option<Result<String>> {
let mut line = String::new();
@@ -140,6 +104,33 @@ impl Iterator for Zsh {
}
}
+fn parse_extended(line: &str, counter: i64) -> History {
+ let line = line.replacen(": ", "", 2);
+ let (time, duration) = line.splitn(2, ':').collect_tuple().unwrap();
+ let (duration, command) = duration.splitn(2, ';').collect_tuple().unwrap();
+
+ let time = time
+ .parse::<i64>()
+ .unwrap_or_else(|_| chrono::Utc::now().timestamp());
+
+ let offset = chrono::Duration::milliseconds(counter);
+ let time = Utc.timestamp(time, 0);
+ let time = time + offset;
+
+ let duration = duration.parse::<i64>().map_or(-1, |t| t * 1_000_000_000);
+
+ // use nanos, because why the hell not? we won't display them.
+ History::new(
+ time,
+ command.trim_end().to_string(),
+ String::from("unknown"),
+ 0, // assume 0, we have no way of knowing :(
+ duration,
+ None,
+ None,
+ )
+}
+
#[cfg(test)]
mod test {
use chrono::prelude::*;