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

feat: add an inline view mode #648

Merged
merged 2 commits into from
Mar 25, 2023
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
3 changes: 1 addition & 2 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,6 @@ version = "0.3"
default-features = false
features = ["ansi", "fmt", "registry", "env-filter"]
optional = true

[patch.crates-io]
ratatui = { git = "https://github.com/conradludgate/tui-rs-revival", branch = "inline" }
2 changes: 2 additions & 0 deletions atuin-client/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ pub struct Settings {
pub filter_mode: FilterMode,
pub filter_mode_shell_up_key_binding: Option<FilterMode>,
pub shell_up_key_binding: bool,
pub inline_height: u16,
pub show_preview: bool,
pub exit_mode: ExitMode,
pub word_jump_mode: WordJumpMode,
Expand Down Expand Up @@ -336,6 +337,7 @@ impl Settings {
.set_default("search_mode", "fuzzy")?
.set_default("filter_mode", "global")?
.set_default("shell_up_key_binding", false)?
.set_default("inline_height", 0)?
.set_default("show_preview", false)?
.set_default("exit_mode", "return-original")?
.set_default("session_token", "")?
Expand Down
40 changes: 31 additions & 9 deletions src/command/client/search/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use ratatui::{
style::{Color, Modifier, Style},
text::{Span, Spans, Text},
widgets::{Block, BorderType, Borders, Paragraph},
Frame, Terminal,
Frame, Terminal, TerminalOptions, Viewport,
};

const RETURN_ORIGINAL: usize = usize::MAX;
Expand Down Expand Up @@ -466,29 +466,37 @@ impl State {

struct Stdout {
stdout: std::io::Stdout,
inline_mode: bool,
}

impl Stdout {
pub fn new() -> std::io::Result<Self> {
pub fn new(inline_mode: bool) -> std::io::Result<Self> {
terminal::enable_raw_mode()?;
let mut stdout = stdout();
if !inline_mode {
execute!(stdout, terminal::EnterAlternateScreen)?;
}
execute!(
stdout,
terminal::EnterAlternateScreen,
event::EnableMouseCapture,
event::EnableBracketedPaste
event::EnableBracketedPaste,
)?;
Ok(Self { stdout })
Ok(Self {
stdout,
inline_mode,
})
}
}

impl Drop for Stdout {
fn drop(&mut self) {
if !self.inline_mode {
execute!(self.stdout, terminal::LeaveAlternateScreen).unwrap();
}
execute!(
self.stdout,
terminal::LeaveAlternateScreen,
event::DisableMouseCapture,
event::DisableBracketedPaste
event::DisableBracketedPaste,
)
.unwrap();
terminal::disable_raw_mode().unwrap();
Expand Down Expand Up @@ -531,9 +539,18 @@ pub async fn history(
settings: &Settings,
db: &mut impl Database,
) -> Result<String> {
let stdout = Stdout::new()?;
let stdout = Stdout::new(settings.inline_height > 0)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
let mut terminal = Terminal::with_options(
backend,
TerminalOptions {
viewport: if settings.inline_height > 0 {
Viewport::Inline(settings.inline_height)
} else {
Viewport::Fullscreen
},
},
)?;

let mut input = Cursor::from(query.join(" "));
// Put the cursor at the end of the query by default
Expand Down Expand Up @@ -608,6 +625,11 @@ pub async fn history(
results = app.query_results(db).await?;
}
};

if settings.inline_height > 0 {
terminal.clear()?;
}

if index < results.len() {
// index is in bounds so we return that entry
Ok(results.swap_remove(index).command.clone())
Expand Down