Skip to content

Commit

Permalink
a command line argument for setting y-labels. closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
aantn committed Dec 15, 2020
1 parent 3a27dfe commit 24083e9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "smag"
version = "0.5.5"
version = "0.6.0"
authors = ["Natan Yellin", "Tom Forbes <tom@tomforb.es>"]
edition = "2018"
repository = "https://github.com/orf/gping"
Expand Down
18 changes: 10 additions & 8 deletions src/datastore.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
use super::ringbuffer;
use super::Args;
use histogram::Histogram;
use tui::style::{Color, Style};
use tui::text::Span;

pub struct DataStore {
pub styles: Vec<Style>,
pub data: Vec<ringbuffer::FixedRingBuffer<(f64, f64)>>,
buffer_size: usize,
args : Args,
window_min: Vec<f64>,
window_max: Vec<f64>,
}

impl DataStore {
pub fn new(host_count: usize, buffer_size: usize) -> Self {
pub fn new(args : Args) -> Self {
let host_count = args.cmds.len();
DataStore {
styles: (0..host_count)
.map(|i| Style::default().fg(Color::Indexed(i as u8 + 1)))
.collect(),
data: (0..host_count)
.map(|_| ringbuffer::FixedRingBuffer::new(buffer_size))
.map(|_| ringbuffer::FixedRingBuffer::new(args.buffer_size))
.collect(),
buffer_size,
window_min: vec![0.0; host_count],
window_max: vec![buffer_size as f64; host_count],
window_max: vec![args.buffer_size as f64; host_count],
args: args,
}
}
pub fn update(&mut self, cmd_index: usize, x_index: u64, item: Option<f64>) {
let data = &mut self.data[cmd_index];
if data.len() >= self.buffer_size {
if data.len() >= self.args.buffer_size {
self.window_min[cmd_index] += 1_f64;
self.window_max[cmd_index] += 1_f64;
}
Expand Down Expand Up @@ -83,7 +85,7 @@ impl DataStore {
[min - min_10_percent, max + max_10_percent]
}

fn get_label(&self, increment: f64, value: f64) -> String {
fn format_tick(&self, increment: f64, value: f64) -> String {
if increment > 1.0 {
format!("{:.0}", value)
} else if increment < 1.0 && increment >= 0.1 {
Expand All @@ -110,7 +112,7 @@ impl DataStore {
let increment = difference / (ticks as f64 - 1.0);

(0..ticks)
.map(|i| Span::raw(self.get_label(increment, min + increment * i as f64)))
.map(|i| Span::raw(self.format_tick(increment, min + increment * i as f64) + " " + &self.args.y_label))
.collect()
}
}
15 changes: 13 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,38 @@ use structopt::StructOpt;
use tui::backend::CrosstermBackend;
use tui::Terminal;

#[derive(Debug, StructOpt)]
#[derive(Clone, Debug, StructOpt)]
#[structopt(
name = "smag",
about = "Show Me A Graph - Like the `watch` command but with a graph of previous values."
)]
pub struct Args {
#[structopt(help = "Command(s) to run", required = true)]
cmds: Vec<String>,

#[structopt(
short = "n",
long = "interval",
default_value = "1.0",
help = "Specify update interval in seconds."
)]
polling_interval: f64,

#[structopt(
short = "y",
long = "y-label",
default_value = "",
help = "Label/units for y-axis (e.g. 'MB', 'Seconds')"
)]
y_label: String,

#[structopt(
short = "d",
long = "diff",
help = "Graph the diff of subsequent command outputs"
)]
diff: bool,

#[structopt(
short = "h",
long = "history",
Expand Down Expand Up @@ -75,7 +86,7 @@ fn run_command(cmd: &str) -> Result<f64> {

fn main() -> Result<()> {
let args = Args::from_args();
let mut app = DataStore::new(args.cmds.len(), args.buffer_size);
let mut app = DataStore::new(args.clone());
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
Expand Down

0 comments on commit 24083e9

Please sign in to comment.