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 /src | |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 32 |
1 files changed, 27 insertions, 5 deletions
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 |