Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TUI, resolve #19, #17, #16 #21

Merged
merged 4 commits into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 66 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "atuin"
version = "0.3.3"
version = "0.4.0"
authors = ["Ellie Huxtable <e@elm.sh>"]
edition = "2018"
license = "MIT"
Expand All @@ -23,6 +23,10 @@ cli-table = "0.4"
config = "0.10"
serde_derive = "1.0.124"
serde = "1.0.124"
tui = "0.14"
termion = "1.5"
unicode-width = "0.1"
itertools = "0.10.0"

[dependencies.rusqlite]
version = "0.24"
Expand Down
32 changes: 3 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ As well as the expected command, A'tuin stores

- zsh

## Requirements

- [fzf](https://github.com/junegunn/fzf)

## Install

### AUR
Expand Down Expand Up @@ -77,38 +73,16 @@ to your `.zshrc`/`.bashrc`/whatever your shell uses.

### History search

By default A'tuin will rebind ctrl-r to use fzf to fuzzy search your history.
It will also rebind the up arrow to use fzf, just without sorting. You can
prevent this by putting
By default A'tuin will rebind ctrl-r and the up arrow to search your history.

You can prevent this by putting

```
export ATUIN_BINDKEYS="false"
```

into your shell config.

You may also change the default history selection. The default behaviour will search your entire history, however

```
export ATUIN_HISTORY="atuin history list --cwd"
```

will switch to only searching history for the current directory.

Similarly,

```
export ATUIN_HISTORY="atuin history list --session"
```

will search for the current session only, and

```
export ATUIN_HISTORY="atuin history list --session --cwd"
```

will do both!

### Import history

```
Expand Down
68 changes: 68 additions & 0 deletions src/command/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use std::sync::mpsc;
use std::thread;
use std::time::Duration;

use termion::event::Key;
use termion::input::TermRead;

pub enum Event<I> {
Input(I),
Tick,
}

/// A small event handler that wrap termion input and tick events. Each event
/// type is handled in its own thread and returned to a common `Receiver`
pub struct Events {
rx: mpsc::Receiver<Event<Key>>,
}

#[derive(Debug, Clone, Copy)]
pub struct Config {
pub exit_key: Key,
pub tick_rate: Duration,
}

impl Default for Config {
fn default() -> Config {
Config {
exit_key: Key::Char('q'),
tick_rate: Duration::from_millis(250),
}
}
}

impl Events {
pub fn new() -> Events {
Events::with_config(Config::default())
}

pub fn with_config(config: Config) -> Events {
let (tx, rx) = mpsc::channel();

{
let tx = tx.clone();
thread::spawn(move || {
let tty = termion::get_tty().expect("Could not find tty");
for key in tty.keys().flatten() {
if let Err(err) = tx.send(Event::Input(key)) {
eprintln!("{}", err);
return;
}
}
})
};

thread::spawn(move || loop {
if tx.send(Event::Tick).is_err() {
break;
}
thread::sleep(config.tick_rate);
});

Events { rx }
}

pub fn next(&self) -> Result<Event<Key>, mpsc::RecvError> {
self.rx.recv()
}
}
13 changes: 13 additions & 0 deletions src/command/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ pub enum Cmd {
#[structopt(long, short)]
session: bool,
},

#[structopt(
about="search for a command",
aliases=&["se", "sea", "sear", "searc"],
)]
Search { query: Vec<String> },
}

fn print_list(h: &[History]) {
Expand Down Expand Up @@ -102,6 +108,13 @@ impl Cmd {

Ok(())
}

Self::Search { query } => {
let history = db.prefix_search(&query.join(""))?;
print_list(&history);

Ok(())
}
}
}
}
6 changes: 6 additions & 0 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use uuid::Uuid;
use crate::local::database::Database;
use crate::settings::Settings;

mod event;
mod history;
mod import;
mod init;
mod search;
mod server;
mod stats;

Expand All @@ -33,6 +35,9 @@ pub enum AtuinCmd {

#[structopt(about = "generates a UUID")]
Uuid,

#[structopt(about = "interactive history search")]
Search { query: Vec<String> },
}

pub fn uuid_v4() -> String {
Expand All @@ -47,6 +52,7 @@ impl AtuinCmd {
Self::Server(server) => server.run(),
Self::Stats(stats) => stats.run(db, settings),
Self::Init => init::init(),
Self::Search { query } => search::run(&query, db),

Self::Uuid => {
println!("{}", uuid_v4());
Expand Down