aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorjakobst1n <jakob.stendahl@outlook.com>2024-06-11 23:45:58 +0200
committerjakobst1n <jakob.stendahl@outlook.com>2024-06-11 23:45:58 +0200
commit26ad27d899a27b0abefea70c592e883c96487f38 (patch)
treee1a2da81c970ff014478894c6357e35499ed27bd /src/main.rs
parent2e56a0d2e80416904712514da6dab788567a719b (diff)
downloadtextgraph-26ad27d899a27b0abefea70c592e883c96487f38.tar.gz
textgraph-26ad27d899a27b0abefea70c592e883c96487f38.zip
Abstract slightly, add cut functionality
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs49
1 files changed, 28 insertions, 21 deletions
diff --git a/src/main.rs b/src/main.rs
index 728f42a..07434db 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,15 +1,35 @@
use std::io::{self, BufRead, Write};
use std::str::FromStr;
-use textgraph::graph;
-use textgraph::parseopts::{parseopts, Opts};
+use textgraph::graph::GraphBuilder;
+use textgraph::parseopts::{parseopts, OptsBuilder};
+
+/// Build a graph text string, based on values and a OptsBuilder
+///
+/// # Arguments
+///
+/// * `opts` - textgraph::parseopts::OptBuilder
+fn build_graph(x_values: &Vec<f64>, y_values: &Vec<f64>, opts: &OptsBuilder) -> String {
+ let opts = opts.clone().build();
+
+ let mut gb = GraphBuilder::new(&x_values, &y_values, opts.width, opts.height);
+ gb.axis(!opts.silent);
+ gb.graph_type(opts.graph_type.clone());
+ if opts.cut {
+ gb.cut_overflow(true);
+ } else if let Some(n) = opts.last_n {
+ gb.keep_tail(n as usize);
+ }
+
+ gb.build()
+}
/// Will graph what comes in through stdin,
/// For each new line, the graph will be re-drawn.
///
/// # Arguments
///
-/// * `opts` - textgraph::parseopts::Opts
-fn filter(opts: Opts) {
+/// * `opts` - textgraph::parseopts::OptBuilder
+fn filter(opts: OptsBuilder) {
//print!("\x1b[?1049h");
let mut x_values: Vec<f64> = Vec::new();
@@ -25,15 +45,8 @@ fn filter(opts: Opts) {
y_values.push(y);
x_values.push(i);
- let mut gb = graph::GraphBuilder::new(&x_values, &y_values, opts.width, opts.height);
- gb.axis(!opts.silent);
- gb.graph_type(opts.graph_type.clone());
- if let Some(n) = opts.last_n {
- gb.keep_tail(n as usize);
- }
-
//print!("\x1B[2J\x1B[H");
- println!("{}", gb.build());
+ println!("{}", build_graph(&x_values, &y_values, &opts));
}
//print!("\x1B[?1049l");
@@ -46,8 +59,8 @@ fn filter(opts: Opts) {
///
/// # Arguments
///
-/// * `opts` - textgraph::parseopts::Opts
-fn graph_file(opts: Opts) {
+/// * `opts` - textgraph::parseopts::OptBuilder
+fn graph_file(opts: OptsBuilder) {
let raw_y_values = std::fs::read_to_string(opts.in_file.clone().unwrap()).expect("TG6");
let mut y_values: Vec<f64> = Vec::new();
@@ -57,13 +70,7 @@ fn graph_file(opts: Opts) {
x_values.push(i as f64);
}
- let mut gb = graph::GraphBuilder::new(&x_values, &y_values, opts.width, opts.height);
- gb.axis(!opts.silent);
- gb.graph_type(opts.graph_type);
- if let Some(n) = opts.last_n {
- gb.keep_tail(n as usize);
- }
- println!("{}", gb.build());
+ println!("{}", build_graph(&x_values, &y_values, &opts));
}
/// Main entry point for the binary of textgraph