diff options
author | jakobst1n <jakob.stendahl@outlook.com> | 2024-06-25 22:21:48 +0200 |
---|---|---|
committer | jakobst1n <jakob.stendahl@outlook.com> | 2024-06-25 22:21:48 +0200 |
commit | 93d64ae68b2ee271b3a8215c50925f492778e5ef (patch) | |
tree | 5ad700b27c0d06345def609f5ef09145e6870505 | |
parent | c82432863462bc893b5e1cc2e56fe1bfa9f4ba23 (diff) | |
download | textgraph-93d64ae68b2ee271b3a8215c50925f492778e5ef.tar.gz textgraph-93d64ae68b2ee271b3a8215c50925f492778e5ef.zip |
Remove dependency on libc crate
-rw-r--r-- | Cargo.lock | 11 | ||||
-rw-r--r-- | Cargo.toml | 5 | ||||
-rw-r--r-- | README.md | 16 | ||||
-rw-r--r-- | src/main.rs | 39 | ||||
-rw-r--r-- | src/term.rs | 18 |
5 files changed, 56 insertions, 33 deletions
@@ -3,14 +3,5 @@ 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", -] +version = "1.0.0" @@ -1,13 +1,12 @@ [package] name = "textgraph" -version = "0.1.0" +version = "1.0.0" edition = "2021" [features] default = ["libc", "ansi"] ansi = [] -libc = ["dep:libc"] +libc = [] [dependencies] -libc = { version = "0.2.155", optional = true } @@ -29,6 +29,22 @@ sudo install target/release/textgraph /usr/local/bin/textgraph sudo install textgraph.1 /usr/share/man/man1/textgraph.1 ``` +### Features + +By default `libc` and `ansi` is enabled. + +The `libc` feature makes the program able to get the terminal size, +and catch signals like SIGINT. +The last part is needed if you want the program to use the alternate screen +in filter-mode. +It does not currently use the `libc`-crate, but it does use c-functions. + +The `ansi` feature enables the use of ansi escape codes, +this is used for manipulating the terminal. +Like adding colors to the graph, +and switching back and fourth from the alternate screen +(this will only happen when `libc` also is enabled). + ## Example 1 Some examples of the different modes, from the same input file with random numbers ### Star mode diff --git a/src/main.rs b/src/main.rs index 3a0a72c..ab4d5e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,13 @@ -#[cfg(feature = "libc")] -use std::io::Write; use std::io::{self, BufRead}; use std::str::FromStr; use textgraph::graph::GraphBuilder; use textgraph::parseopts::{parseopts, OptsBuilder}; -#[cfg(feature = "libc")] -extern "C" fn handle_sigint(_sig: i32) { +#[cfg(all(feature = "libc", feature = "ansi"))] +use std::io::Write; + +#[cfg(all(feature = "libc", feature = "ansi"))] +extern "C" fn handle_sigint(_sig: std::os::raw::c_int) { print!("\x1b[?25h"); print!("\x1B[?1049l"); io::stdout().flush().unwrap(); @@ -14,21 +15,19 @@ extern "C" fn handle_sigint(_sig: i32) { } /// Set a signalhandler for swapping back to the the main screen on sigint -#[cfg(feature = "libc")] +#[cfg(all(feature = "libc", feature = "ansi"))] 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()); + let mut action: textgraph::term::SigAction = std::mem::zeroed(); + action.sa_flags = 0; + action.sa_sigaction = handle_sigint as usize; + + textgraph::term::sigemptyset(&mut action.sa_mask); + textgraph::term::sigaction(15, &action, std::ptr::null_mut()); // 15 is SIGTERM + textgraph::term::sigaction(2, &action, std::ptr::null_mut()); // 2 is SIGINT + textgraph::term::sigaction(9, &action, std::ptr::null_mut()); // 9 is SIGKILL + textgraph::term::sigaction(20, &action, std::ptr::null_mut()); // 20 is SIGSTP + textgraph::term::sigaction(19, &action, std::ptr::null_mut()); // 19 is SIGSTOP } } @@ -60,7 +59,7 @@ fn build_graph(x_values: &Vec<f64>, y_values: &Vec<f64>, opts: &OptsBuilder) -> /// /// * `opts` - textgraph::parseopts::OptBuilder fn filter(opts: OptsBuilder) { - #[cfg(feature = "libc")] + #[cfg(all(feature = "libc", feature = "ansi"))] { set_filter_signalhandler(); print!("\x1b[?1049h"); @@ -77,11 +76,11 @@ fn filter(opts: OptsBuilder) { let line = line.expect("Could not read..."); let y = f64::from_str(line.as_str()); - if let Err(_) = y { + if let Err(_) = y { print!("Could not parse line as f64."); continue; } - if let Ok(y) = y { + if let Ok(y) = y { y_values.push(y); x_values.push(i); } diff --git a/src/term.rs b/src/term.rs index 275ab60..9f2d5e2 100644 --- a/src/term.rs +++ b/src/term.rs @@ -28,3 +28,21 @@ pub fn get_terminal_size() -> Result<(u16, u16), Error> { } Ok((ws.ws_col, ws.ws_row)) } + +#[repr(C)] +pub struct SigAction { + pub sa_sigaction: usize, + pub sa_mask: SigSet, + pub sa_flags: c_int, + pub sa_restorer: Option<unsafe extern "C" fn()>, +} + +#[repr(C)] +pub struct SigSet { + pub __val: [u64; 16], +} + +extern "C" { + pub fn sigemptyset(set: *mut SigSet) -> c_int; + pub fn sigaction(signum: c_int, act: *const SigAction, oldact: *mut SigAction) -> c_int; +} |