Remove history.rs file

This commit is contained in:
Dheepak Krishnamurthy 2021-03-31 15:26:21 -06:00
parent a1cd39a863
commit 483e7e8ad5
4 changed files with 1 additions and 220 deletions

82
Cargo.lock generated
View file

@ -41,18 +41,6 @@ version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28b2cd92db5cbd74e8e5028f7e27dd7aa3090e89e4f2a197cc7c8dfb69c7063b"
[[package]]
name = "arrayref"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544"
[[package]]
name = "arrayvec"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b"
[[package]]
name = "async-attributes"
version = "1.1.2"
@ -230,12 +218,6 @@ dependencies = [
"rustc-demangle",
]
[[package]]
name = "base64"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
[[package]]
name = "better-panic"
version = "0.2.0"
@ -252,17 +234,6 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "blake2b_simd"
version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afa748e348ad3be8263be728124b24a24f268266f6f5d58af9d75f6a40b5c587"
dependencies = [
"arrayref",
"arrayvec",
"constant_time_eq",
]
[[package]]
name = "blocking"
version = "1.0.2"
@ -386,12 +357,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "constant_time_eq"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
[[package]]
name = "crossbeam-utils"
version = "0.8.3"
@ -499,16 +464,6 @@ dependencies = [
"syn",
]
[[package]]
name = "dirs"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3"
dependencies = [
"cfg-if 0.1.10",
"dirs-sys",
]
[[package]]
name = "dirs-next"
version = "2.0.0"
@ -519,17 +474,6 @@ dependencies = [
"dirs-sys-next",
]
[[package]]
name = "dirs-sys"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a"
dependencies = [
"libc",
"redox_users 0.3.5",
"winapi",
]
[[package]]
name = "dirs-sys-next"
version = "0.1.2"
@ -537,7 +481,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d"
dependencies = [
"libc",
"redox_users 0.4.0",
"redox_users",
"winapi",
]
@ -1130,17 +1074,6 @@ dependencies = [
"bitflags",
]
[[package]]
name = "redox_users"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d"
dependencies = [
"getrandom 0.1.16",
"redox_syscall 0.1.57",
"rust-argon2",
]
[[package]]
name = "redox_users"
version = "0.4.0"
@ -1168,18 +1101,6 @@ version = "0.6.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5f089152e60f62d28b835fbff2cd2e8dc0baf1ac13343bef92ab7eed84548"
[[package]]
name = "rust-argon2"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b18820d944b33caa75a71378964ac46f58517c92b6ae5f762636247c09e78fb"
dependencies = [
"base64",
"blake2b_simd",
"constant_time_eq",
"crossbeam-utils",
]
[[package]]
name = "rustc-demangle"
version = "0.1.18"
@ -1380,7 +1301,6 @@ dependencies = [
"chrono",
"clap",
"crossterm",
"dirs",
"futures",
"futures-timer",
"itertools",

View file

@ -40,7 +40,6 @@ anyhow = "1"
async-std = { version = "1", features = ["attributes", "unstable"] }
futures = "0.3"
futures-timer = "3.0"
dirs = "2.0.2"
[package.metadata.rpm]
package = "taskwarrior-tui"

View file

@ -1,137 +0,0 @@
use itertools::Itertools;
use std::{
fs::{create_dir_all, write, File},
io::{BufRead, BufReader, Error, Read},
path::{Path, PathBuf},
};
#[derive(Clone)]
pub struct Commands {
pub all: Vec<String>,
}
impl Commands {
pub fn from_history(shell: &str, history: &[String]) -> Self {
Self {
all: history
.to_vec()
.into_iter()
.filter(|s| s.starts_with("task"))
.unique()
.collect(),
}
}
}
pub struct History {
pub history: Vec<String>,
pub search: String,
pub shell: String,
}
impl History {
pub fn new(shell: String) -> Self {
let (history, commands) = match shell.as_str() {
"bash" => get_bash_history(),
"zsh" => get_zsh_history(),
_ => unreachable!(),
};
Self {
history: commands.all,
search: String::new(),
shell,
}
}
}
pub fn get_bash_history() -> (Vec<String>, Commands) {
let history = read_from_home(".bash_history").unwrap();
let commands = Commands::from_history("bash", &history);
(history, commands)
}
pub fn get_zsh_history() -> (Vec<String>, Commands) {
let history = zsh::process_history()
.split('\n')
.map(|x| x.to_string())
.collect::<Vec<String>>();
let commands = Commands::from_history("zsh", &history);
(history, commands)
}
pub fn read_from_home(path: impl AsRef<Path>) -> Result<Vec<String>, Error> {
/* `path` is relative to home directory */
let home = dirs::home_dir().unwrap();
let target = home.join(path);
if target.exists() {
read_file(target)
} else {
Ok(Vec::new())
}
}
fn read_file(target: PathBuf) -> Result<Vec<String>, Error> {
let file = File::open(target)?;
let reader = BufReader::new(file);
reader.lines().collect::<Result<Vec<_>, _>>()
}
pub mod zsh {
use super::*;
use regex::Regex;
pub fn read_as_bytes(path: impl AsRef<Path>) -> Result<Vec<u8>, Error> {
let home = dirs::home_dir().unwrap();
let target = home.join(path);
let mut file = File::open(target)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
Ok(buffer)
}
pub fn process_history() -> String {
let history = read_as_bytes(".zsh_history").unwrap();
let unmetafied = unmetafy(history);
remove_timestamps(String::from_utf8(unmetafied).unwrap())
}
fn unmetafy(mut bytestring: Vec<u8>) -> Vec<u8> {
/* Unmetafying zsh history requires looping over the bytestring, removing
* each encountered Meta character, and XOR-ing the following byte with 32.
*
* For instance:
*
* Input: ('a', 'b', 'c', Meta, 'd', 'e', 'f')
* Wanted: ('a', 'b', 'c', 'd' ^ 32, 'e', 'f')
*/
const ZSH_META: u8 = 0x83;
for index in (0..bytestring.len()).rev() {
if bytestring[index] == ZSH_META {
bytestring.remove(index);
bytestring[index] ^= 32;
}
}
bytestring
}
fn remove_timestamps(history: String) -> String {
/* The preceding metadata needs to be stripped
* because zsh history entries look like below:
*
* `: 1330648651:0;sudo reboot`
*/
let r = Regex::new(r"^: \d{10}:\d;").unwrap();
history.lines().map(|x| r.replace(x, "") + "\n").collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_zsh_history() {
let h = History::new("zsh".to_string());
dbg!(h.history);
}
}

View file

@ -7,7 +7,6 @@ mod calendar;
mod config;
mod context;
mod help;
mod history;
mod keyconfig;
mod table;
mod task_report;