Allow listing or searching with only the command as output (#89)

Should be useful for using other tools, such as FZF
This commit is contained in:
Ellie Huxtable 2021-05-09 19:01:21 +01:00 committed by GitHub
parent d39e3cb479
commit bd4db1fa03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 5 deletions

View File

@ -42,6 +42,9 @@ pub enum Cmd {
#[structopt(long, short)] #[structopt(long, short)]
human: bool, human: bool,
#[structopt(long, about = "Show only the text of the command")]
cmd_only: bool,
}, },
#[structopt( #[structopt(
@ -51,11 +54,14 @@ pub enum Cmd {
Last { Last {
#[structopt(long, short)] #[structopt(long, short)]
human: bool, human: bool,
#[structopt(long, about = "Show only the text of the command")]
cmd_only: bool,
}, },
} }
#[allow(clippy::clippy::cast_sign_loss)] #[allow(clippy::clippy::cast_sign_loss)]
pub fn print_list(h: &[History], human: bool) { pub fn print_list(h: &[History], human: bool, cmd_only: bool) {
let mut writer = TabWriter::new(std::io::stdout()).padding(2); let mut writer = TabWriter::new(std::io::stdout()).padding(2);
let lines = h.iter().map(|h| { let lines = h.iter().map(|h| {
@ -73,6 +79,8 @@ pub fn print_list(h: &[History], human: bool) {
h.command.trim(), h.command.trim(),
duration, duration,
) )
} else if cmd_only {
format!("{}\n", h.command.trim())
} else { } else {
format!( format!(
"{}\t{}\t{}\n", "{}\t{}\t{}\n",
@ -145,6 +153,7 @@ impl Cmd {
session, session,
cwd, cwd,
human, human,
cmd_only,
} => { } => {
let params = (session, cwd); let params = (session, cwd);
let cwd = env::current_dir()?.display().to_string(); let cwd = env::current_dir()?.display().to_string();
@ -165,14 +174,14 @@ impl Cmd {
(true, true) => db.query_history(query_session_dir.as_str()).await?, (true, true) => db.query_history(query_session_dir.as_str()).await?,
}; };
print_list(&history, *human); print_list(&history, *human, *cmd_only);
Ok(()) Ok(())
} }
Self::Last { human } => { Self::Last { human, cmd_only } => {
let last = db.last().await?; let last = db.last().await?;
print_list(&[last], *human); print_list(&[last], *human, *cmd_only);
Ok(()) Ok(())
} }

View File

@ -69,6 +69,9 @@ pub enum AtuinCmd {
human: bool, human: bool,
query: Vec<String>, query: Vec<String>,
#[structopt(long, about = "Show only the text of the command")]
cmd_only: bool,
}, },
#[structopt(about = "sync with the configured server")] #[structopt(about = "sync with the configured server")]
@ -112,6 +115,7 @@ impl AtuinCmd {
before, before,
after, after,
query, query,
cmd_only,
} => { } => {
search::run( search::run(
&client_settings, &client_settings,
@ -123,6 +127,7 @@ impl AtuinCmd {
exclude_cwd, exclude_cwd,
before, before,
after, after,
cmd_only,
&query, &query,
&mut db, &mut db,
) )

View File

@ -329,6 +329,7 @@ pub async fn run(
exclude_cwd: Option<String>, exclude_cwd: Option<String>,
before: Option<String>, before: Option<String>,
after: Option<String>, after: Option<String>,
cmd_only: bool,
query: &[String], query: &[String],
db: &mut (impl Database + Send + Sync), db: &mut (impl Database + Send + Sync),
) -> Result<()> { ) -> Result<()> {
@ -412,7 +413,7 @@ pub async fn run(
.map(std::borrow::ToOwned::to_owned) .map(std::borrow::ToOwned::to_owned)
.collect(); .collect();
super::history::print_list(&results, human); super::history::print_list(&results, human, cmd_only);
} }
Ok(()) Ok(())