From d82a5f6e1bc878b0e6a695f88eb69404ca6d571d Mon Sep 17 00:00:00 2001 From: jakobst1n Date: Sun, 16 Jun 2024 17:07:21 +0200 Subject: Make platform dependent code possible to disable by disabling libc and ansi features --- Cargo.toml | 7 ++++++- src/graph.rs | 21 +++++++++++++++++++++ src/lib.rs | 1 + src/main.rs | 16 ++++++++++++---- src/parseopts.rs | 14 +++++++++++++- 5 files changed, 53 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a5f66ca..5b85730 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 std::fmt::Display for GraphPixel { "{}", 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(" "), } ) diff --git a/src/lib.rs b/src/lib.rs index b85ba6f..9f0cae5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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, y_values: &Vec, 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 = Vec::new(); let mut y_values: Vec = 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, -- cgit v1.2.3