aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjakobst1n <jakob.stendahl@outlook.com>2024-06-12 00:29:01 +0200
committerjakobst1n <jakob.stendahl@outlook.com>2024-06-12 00:29:01 +0200
commit8ea7035581c4d5aa24e5f3995659171b3707556c (patch)
tree66ed75ce19de90a06183ed046bb845556c49e5a1
parent26ad27d899a27b0abefea70c592e883c96487f38 (diff)
downloadtextgraph-8ea7035581c4d5aa24e5f3995659171b3707556c.tar.gz
textgraph-8ea7035581c4d5aa24e5f3995659171b3707556c.zip
Add libc crate ( :-( ), and make a signal handler for swapping back to main screen
-rw-r--r--Cargo.lock9
-rw-r--r--Cargo.toml1
-rw-r--r--README.md2
-rw-r--r--src/main.rs32
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
@@ -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",
+]
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<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