diff options
author | jakobst1n <jakob.stendahl@outlook.com> | 2024-06-16 17:07:21 +0200 |
---|---|---|
committer | jakobst1n <jakob.stendahl@outlook.com> | 2024-06-16 17:07:21 +0200 |
commit | d82a5f6e1bc878b0e6a695f88eb69404ca6d571d (patch) | |
tree | 321d55c3ff700187cba1938da119825260780193 | |
parent | 93883014f75ee9005a69d57ccb2d653015b6c216 (diff) | |
download | textgraph-d82a5f6e1bc878b0e6a695f88eb69404ca6d571d.tar.gz textgraph-d82a5f6e1bc878b0e6a695f88eb69404ca6d571d.zip |
Make platform dependent code possible to disable by disabling libc and ansi features
-rw-r--r-- | Cargo.toml | 7 | ||||
-rw-r--r-- | src/graph.rs | 21 | ||||
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/main.rs | 16 | ||||
-rw-r--r-- | src/parseopts.rs | 14 |
5 files changed, 53 insertions, 6 deletions
@@ -3,6 +3,11 @@ name = "textgraph" version = "0.1.0" edition = "2021" +[features] +default = ["libc", "ansi"] +ansi = [] +libc = ["dep:libc"] + [dependencies] -libc = "0.2.155" +libc = { version = "0.2.155", optional = true } diff --git a/src/graph.rs b/src/graph.rs index 0d645f7..a3af887 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -82,12 +82,33 @@ impl<T: std::fmt::Display> std::fmt::Display for GraphPixel<T> { "{}", match self { GraphPixel::Normal(c) => format!("{}", c), + + #[cfg(feature = "ansi")] GraphPixel::Green(c) => format!("\x1b[32m{}\x1b[0m", c), + #[cfg(feature = "ansi")] GraphPixel::Blue(c) => format!("\x1b[34m{}\x1b[0m", c), + #[cfg(feature = "ansi")] GraphPixel::Red(c) => format!("\x1b[31m{}\x1b[0m", c), + #[cfg(feature = "ansi")] GraphPixel::Yellow(c) => format!("\x1b[33m{}\x1b[0m", c), + #[cfg(feature = "ansi")] GraphPixel::Magenta(c) => format!("\x1b[33m{}\x1b[0m", c), + #[cfg(feature = "ansi")] GraphPixel::Cyan(c) => format!("\x1b[36m{}\x1b[0m", c), + + #[cfg(not(feature = "ansi"))] + GraphPixel::Green(c) => format!("{}, c), + #[cfg(not(feature = "ansi"))] + GraphPixel::Blue(c) => format!("{}, c), + #[cfg(not(feature = "ansi"))] + GraphPixel::Red(c) => format!("{}, c), + #[cfg(not(feature = "ansi"))] + GraphPixel::Yellow(c) => format!("{}, c), + #[cfg(not(feature = "ansi"))] + GraphPixel::Magenta(c) => format!("{}, c), + #[cfg(not(feature = "ansi"))] + GraphPixel::Cyan(c) => format!("{}, c), + GraphPixel::Blank => String::from(" "), } ) @@ -1,3 +1,4 @@ pub mod graph; pub mod parseopts; +#[cfg(feature = "libc")] pub mod term; diff --git a/src/main.rs b/src/main.rs index 16de4cd..e97b085 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,11 @@ -use std::io::{self, BufRead, Write}; +#[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) { print!("\x1b[?25h"); print!("\x1B[?1049l"); @@ -11,6 +14,7 @@ extern "C" fn handle_sigint(_sig: i32) { } /// Set a signalhandler for swapping back to the the main screen on sigint +#[cfg(feature = "libc")] fn set_filter_signalhandler() { let mut sig_action: libc::sigaction = unsafe { std::mem::zeroed() }; sig_action.sa_flags = 0; @@ -56,9 +60,12 @@ fn build_graph(x_values: &Vec<f64>, y_values: &Vec<f64>, opts: &OptsBuilder) -> /// /// * `opts` - textgraph::parseopts::OptBuilder fn filter(opts: OptsBuilder) { - set_filter_signalhandler(); - print!("\x1b[?1049h"); - print!("\x1b[?25l"); + #[cfg(feature = "libc")] + { + set_filter_signalhandler(); + print!("\x1b[?1049h"); + print!("\x1b[?25l"); + } let mut x_values: Vec<f64> = Vec::new(); let mut y_values: Vec<f64> = Vec::new(); @@ -73,6 +80,7 @@ fn filter(opts: OptsBuilder) { y_values.push(y); x_values.push(i); + #[cfg(feature = "ansi")] print!("\x1B[2J\x1B[H"); println!("{}", build_graph(&x_values, &y_values, &opts)); } diff --git a/src/parseopts.rs b/src/parseopts.rs index 60f811e..fb04c0d 100644 --- a/src/parseopts.rs +++ b/src/parseopts.rs @@ -40,20 +40,32 @@ impl OptsBuilder { pub fn build(self) -> Opts { Opts { width: self.width.unwrap_or_else(|| { + #[cfg(feature = "libc")] if let Ok((width, _)) = crate::term::get_terminal_size() { width as usize } else { - println!("Could not determine TTY columns, specify with -r"); + println!("Could not determine TTY columns, specify with -w"); + std::process::exit(1); + } + #[cfg(not(feature = "libc"))] + { + println!("Not compiled with libc, cannot detect columns, specify with -w"); std::process::exit(1); } }), height: self.height.unwrap_or_else(|| { + #[cfg(feature = "libc")] if let Ok((_, height)) = crate::term::get_terminal_size() { height as usize - 1 } else { println!("Could not determine TTY rows, specify with -h"); std::process::exit(1); } + #[cfg(not(feature = "libc"))] + { + println!("Not compiled with libc, cannot detect rows, specify with -h"); + std::process::exit(1); + } }), graph_type: self.graph_type, silent: self.silent, |