Skip to content

Commit

Permalink
Use termion buffered backend to avoid dependency from cursive
Browse files Browse the repository at this point in the history
There are some issues with depeendency from the ncurses library, it does
not allows to compile chdig in a static binary (this could be done, but
requires some work), and this leads to a requirement for minimal glibc
version, recently I tried to run chdig on debian buster, and it fails:

    $ chdig
    chdig: /lib/x86_64-linux-gnu/libm.so.6: version `GLIBC_2.29' not found (required by chdig)
    chdig: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.29' not found (required by chdig)

    # apt-cache policy libc6
    libc6:
      Installed: 2.28-10+deb10u2
      Candidate: 2.28-10+deb10u2
      Version table:
     *** 2.28-10+deb10u2 500
            500 http://security.debian.org/debian-security buster/updates/main amd64 Packages
            100 /var/lib/dpkg/status
         2.28-10+deb10u1 500
            500 http://deb.debian.org/debian buster/main amd64 Packages

Last time I've tried other backends, they all experience flickering
issues [1], but, cursive_buffered_backend [2] - solves the problem!

  [1]: gyscos/cursive#142
  [2]: https://github.com/agavrilov/cursive_buffered_backend

Also I have to remove --mouse/--no-mouse, since for this the cursive
should be patched.
  • Loading branch information
azat committed Oct 28, 2023
1 parent 0b53c1a commit 131112f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 30 deletions.
44 changes: 43 additions & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -37,8 +37,8 @@ warp = "*"
clap = { version = "*", features = ["derive", "env"] }
clap_complete = { version = "*" }
# UI
ncurses = "*"
cursive = "*"
cursive = { version = "*", features = ["termion-backend"] }
cursive_buffered_backend = "0.6.1"
cursive-syntect = "*"
cursive_table_view = "0.14"
# Patches:
Expand Down
6 changes: 3 additions & 3 deletions src/interpreter/flamegraph.rs
@@ -1,7 +1,7 @@
use crate::interpreter::clickhouse::Columns;
use anyhow::{Error, Result};
use futures::channel::mpsc;
use ncurses;
// use ncurses;
use std::io::Write;
use std::process::{Command, Stdio};
use tempfile::NamedTempFile;
Expand Down Expand Up @@ -61,7 +61,7 @@ pub fn show(block: Columns) -> Result<()> {
}

// After tfg arrows stops working, fix it:
ncurses::keypad(ncurses::stdscr(), true);
// ncurses::keypad(ncurses::stdscr(), true);
// If something else will not work take a look at [1].
//
// [1]: https://github.com/gyscos/cursive/blob/1a0cc868b41232c0d5290a11b4b987ffed757798/cursive/src/backends/curses/n.rs#L115
Expand Down Expand Up @@ -128,7 +128,7 @@ pub async fn open_in_speedscope(block: Columns) -> Result<()> {
)));
}

ncurses::keypad(ncurses::stdscr(), true);
// ncurses::keypad(ncurses::stdscr(), true);

// TODO: correctly wait the server stopped serving
server.await;
Expand Down
12 changes: 1 addition & 11 deletions src/interpreter/options.rs
Expand Up @@ -119,12 +119,7 @@ pub struct ViewOptions {
#[arg(long, default_value_t = false)]
/// Do not accumulate metrics for subqueries in the initial query
pub no_subqueries: bool,

#[arg(short('m'), long, action = ArgAction::SetTrue, default_value_t = true)]
/// Mouse support (turned on by default)
pub mouse: bool,
#[arg(short('M'), long, action = ArgAction::SetTrue, overrides_with = "mouse")]
no_mouse: bool,
// TODO: --mouse/--no-mouse (see EXIT_MOUSE_SEQUENCE in termion)
}

#[derive(Args, Clone)]
Expand Down Expand Up @@ -347,11 +342,6 @@ fn adjust_defaults(options: &mut ChDigOptions) {
if options.view.no_group_by {
options.view.group_by = false;
}

// FIXME: apparently overrides_with works before default_value_t
if options.view.no_mouse {
options.view.mouse = false;
}
}

// TODO:
Expand Down
19 changes: 6 additions & 13 deletions src/main.rs
@@ -1,7 +1,6 @@
use anyhow::Result;
use backtrace::Backtrace;
use flexi_logger::{LogSpecification, Logger};
use ncurses;
use std::panic::{self, PanicInfo};

mod interpreter;
Expand Down Expand Up @@ -29,9 +28,8 @@ fn panic_hook(info: &PanicInfo<'_>) {
// - trim panic frames
let stacktrace: String = format!("{:?}", Backtrace::new()).replace('\n', "\n\r");

ncurses::noraw();
ncurses::clear();
ncurses::refresh();
// FIXME: restore the terminal back from raw mode (see Backend::drop for termion)

print!(
"thread '<unnamed>' panicked at '{}', {}\n\r{}",
msg, location, stacktrace
Expand All @@ -41,10 +39,13 @@ fn panic_hook(info: &PanicInfo<'_>) {

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
let mut siv = cursive::default();
let backend = cursive::backends::termion::Backend::init()?;
let buffered_backend = Box::new(cursive_buffered_backend::BufferedBackend::new(backend));
let mut siv = cursive::CursiveRunner::new(cursive::Cursive::new(), buffered_backend);

// Override with RUST_LOG
// NOTE: hyper also has trace_span() which will not be overwritten
// FIXME: "flexi_logger has to work with UTC rather than with local time, caused by IndeterminateOffset"
let mut logger = Logger::try_with_env_or_str(
"trace,cursive=info,clickhouse_rs=info,skim=info,tuikit=info,hyper=info",
)?
Expand All @@ -62,14 +63,6 @@ async fn main() -> Result<()> {
panic_hook(info);
}));

if !context.lock().unwrap().options.view.mouse {
siv.cb_sink()
.send(Box::new(move |_: &mut cursive::Cursive| {
ncurses::mousemask(0, None);
}))
.unwrap();
}

log::info!("chdig started");
siv.run();

Expand Down

0 comments on commit 131112f

Please sign in to comment.