diff options
author | jakobst1n <jakob.stendahl@outlook.com> | 2024-06-12 00:29:01 +0200 |
---|---|---|
committer | jakobst1n <jakob.stendahl@outlook.com> | 2024-06-12 00:29:01 +0200 |
commit | 8ea7035581c4d5aa24e5f3995659171b3707556c (patch) | |
tree | 66ed75ce19de90a06183ed046bb845556c49e5a1 | |
parent | 26ad27d899a27b0abefea70c592e883c96487f38 (diff) | |
download | textgraph-8ea7035581c4d5aa24e5f3995659171b3707556c.tar.gz textgraph-8ea7035581c4d5aa24e5f3995659171b3707556c.zip |
Add libc crate ( :-( ), and make a signal handler for swapping back to main screen
-rw-r--r-- | Cargo.lock | 9 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | src/main.rs | 32 |
4 files changed, 38 insertions, 6 deletions
@@ -3,5 +3,14 @@ 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", +] @@ -4,4 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] +libc = "0.2.155" @@ -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<f64>, y_values: &Vec<f64>, opts: &OptsBuilder) -> /// /// * `opts` - textgraph::parseopts::OptBuilder fn filter(opts: OptsBuilder) { - //print!("\x1b[?1049h"); + set_filter_signalhandler(); + print!("\x1b[?1049h"); let mut x_values: Vec<f64> = Vec::new(); let mut y_values: Vec<f64> = 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 |