From 8ea7035581c4d5aa24e5f3995659171b3707556c Mon Sep 17 00:00:00 2001 From: jakobst1n Date: Wed, 12 Jun 2024 00:29:01 +0200 Subject: Add libc crate ( :-( ), and make a signal handler for swapping back to main screen --- Cargo.lock | 9 +++++++++ Cargo.toml | 1 + README.md | 2 +- src/main.rs | 32 +++++++++++++++++++++++++++----- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 53dcf10..ef753cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + [[package]] name = "textgraph" version = "0.1.0" +dependencies = [ + "libc", +] diff --git a/Cargo.toml b/Cargo.toml index f3e67b1..a5f66ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] +libc = "0.2.155" diff --git a/README.md b/README.md index 2d4ce69..cc8bac4 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # TextGraph A small terminal utility which displays graphs as plain text, -with the goal of having no dependencies, +with the goal of having few dependencies, being lightweight, and reasonably performant. It is meant to be used in a traditional unix way, diff --git a/src/main.rs b/src/main.rs index 07434db..1d3ba02 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,30 @@ use std::str::FromStr; use textgraph::graph::GraphBuilder; use textgraph::parseopts::{parseopts, OptsBuilder}; +extern "C" fn handle_sigint(_sig: i32) { + print!("\x1B[?1049l"); + io::stdout().flush().unwrap(); + std::process::exit(0); +} + +/// Set a signalhandler for swapping back to the the main screen on sigint +fn set_filter_signalhandler() { + let mut sig_action: libc::sigaction = unsafe { std::mem::zeroed() }; + sig_action.sa_flags = 0; + sig_action.sa_sigaction = handle_sigint as usize; + + unsafe { + let mut signal_set = std::mem::zeroed(); + libc::sigemptyset(&mut signal_set); + sig_action.sa_mask = signal_set; + + libc::sigaction(libc::SIGINT, &sig_action, std::ptr::null_mut()); + libc::sigaction(libc::SIGKILL, &sig_action, std::ptr::null_mut()); + libc::sigaction(libc::SIGTSTP, &sig_action, std::ptr::null_mut()); + libc::sigaction(libc::SIGSTOP, &sig_action, std::ptr::null_mut()); + } +} + /// Build a graph text string, based on values and a OptsBuilder /// /// # Arguments @@ -30,7 +54,8 @@ fn build_graph(x_values: &Vec, y_values: &Vec, opts: &OptsBuilder) -> /// /// * `opts` - textgraph::parseopts::OptBuilder fn filter(opts: OptsBuilder) { - //print!("\x1b[?1049h"); + set_filter_signalhandler(); + print!("\x1b[?1049h"); let mut x_values: Vec = Vec::new(); let mut y_values: Vec = Vec::new(); @@ -45,12 +70,9 @@ fn filter(opts: OptsBuilder) { y_values.push(y); x_values.push(i); - //print!("\x1B[2J\x1B[H"); + print!("\x1B[2J\x1B[H"); println!("{}", build_graph(&x_values, &y_values, &opts)); } - - //print!("\x1B[?1049l"); - io::stdout().flush().unwrap(); } /// Will graph the contents of a file -- cgit v1.2.3