Skip to content

Commit

Permalink
feat: use color constants to improve ux
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishbagdaneofficial committed Nov 11, 2024
1 parent e2692b5 commit a720010
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod printer;
mod result_data;
mod timescale;
mod url_generator;
mod colors;

#[cfg(unix)]
#[global_allocator]
Expand Down
52 changes: 24 additions & 28 deletions src/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@ use ratatui::{
Terminal,
};
use std::{collections::BTreeMap, io};

use crate::{
client::{ClientError, RequestResult},
printer::PrintMode,
result_data::{MinMaxMean, ResultData},
timescale::{TimeLabel, TimeScale},
};
use ratatui::prelude::Stylize;
use crate::{client::{ClientError, RequestResult}, colors, printer::PrintMode, result_data::{MinMaxMean, ResultData}, timescale::{TimeLabel, TimeScale}};

/// When the monitor ends
pub enum EndLine {
Expand All @@ -46,9 +41,9 @@ impl ColorScheme {
}

fn set_colors(&mut self) {
self.light_blue = Some(Color::Cyan);
self.green = Some(Color::Green);
self.yellow = Some(Color::Yellow);
self.light_blue = Some(Color::from(colors::FASTEST));
self.green = Some(Color::from(colors::SUCCESS));
self.yellow = Some(Color::from(colors::WARNING));
}
}

Expand All @@ -68,7 +63,7 @@ pub struct Monitor {
struct IntoRawMode;

impl IntoRawMode {
pub fn new() -> Result<Self, std::io::Error> {
pub fn new() -> Result<Self, io::Error> {
crossterm::terminal::enable_raw_mode()?;
io::stdout().execute(crossterm::terminal::EnterAlternateScreen)?;
io::stdout().execute(crossterm::cursor::Hide)?;
Expand All @@ -85,7 +80,7 @@ impl Drop for IntoRawMode {
}

impl Monitor {
pub async fn monitor(self) -> Result<ResultData, std::io::Error> {
pub async fn monitor(self) -> Result<ResultData, io::Error> {
let raw_mode = IntoRawMode::new()?;

let mut terminal = {
Expand Down Expand Up @@ -225,25 +220,22 @@ impl Monitor {
EndLine::NumQuery(n) => format!("{} / {}", all.len(), n),
};
let gauge = Gauge::default()
.block(Block::default().title("Progress").borders(Borders::ALL))
.gauge_style(Style::default().fg(colors.light_blue.unwrap_or(Color::White)))
.block(Block::default().title("Progress").title_style(Style::default().bold().fg(Color::White)).borders(Borders::ALL).border_style(Style::default().fg(Color::DarkGray)).bg(Color::from(colors::BACKGROUND)))
.gauge_style(Style::default().bg(Color::from(colors::BACKGROUND)).fg(Color::from(colors::FASTEST)))
.label(Span::raw(gauge_label))
.ratio(progress);
f.render_widget(gauge, row4[0]);

let last_1_timescale = {
let success = all.success();
let index = match success.binary_search_by(|probe| {
let index = success.binary_search_by(|probe| {
(now - probe.end)
.as_secs_f64()
.partial_cmp(&timescale.as_secs_f64())
// Should be fine
.unwrap()
.reverse()
}) {
Ok(i) => i,
Err(i) => i,
};
}).unwrap_or_else(|i| i);

&success[index..]
};
Expand Down Expand Up @@ -290,11 +282,12 @@ impl Monitor {
.unwrap_or_else(|_| "Unknown".to_string())
)),
];
let stats_title = format!("stats for last {timescale}");
let stats_title = format!("Stats for last {timescale}");
let stats = Paragraph::new(stats_text).block(
Block::default()
.title(Span::raw(stats_title))
.borders(Borders::ALL),
.title_style(Style::default().bold())
.style(Style::default().bg(Color::from(colors::BACKGROUND))),
);
f.render_widget(stats, mid[0]);

Expand All @@ -311,7 +304,8 @@ impl Monitor {
let stats2 = Paragraph::new(stats2_text).block(
Block::default()
.title("Status code distribution")
.borders(Borders::ALL),
.title_style(Style::default().bold())
.style(Style::default().bg(Color::from(colors::BACKGROUND))),
);
f.render_widget(stats2, mid[1]);

Expand All @@ -324,8 +318,9 @@ impl Monitor {
.collect::<Vec<_>>();
let errors = Paragraph::new(errors_text).block(
Block::default()
.style(Style::default().bg(Color::from(colors::BACKGROUND)))
.title("Error distribution")
.borders(Borders::ALL),
.title_style(Style::default().bold()),
);
f.render_widget(errors, row4[2]);

Expand All @@ -345,8 +340,8 @@ impl Monitor {
.title(Span::raw(title))
.style(
Style::default()
.fg(colors.green.unwrap_or(Color::Reset))
.bg(Color::Reset),
.fg(Color::from(colors::REQUEST_HISTOGRAM))
.bg(Color::from(colors::BACKGROUND)),
)
.borders(Borders::ALL),
)
Expand Down Expand Up @@ -392,9 +387,10 @@ impl Monitor {
.title("Response time histogram")
.style(
Style::default()
.fg(colors.yellow.unwrap_or(Color::Reset))
.bg(Color::Reset),
.fg(Color::from(colors::RESPONSE_HISTOGRAM))
.bg(Color::from(colors::BACKGROUND)),
)
.title_style(Style::default().bold())
.borders(Borders::ALL),
)
.data(resp_histo_data_str.as_slice())
Expand Down Expand Up @@ -434,7 +430,7 @@ impl Monitor {
}) => {
drop(raw_mode);
let _ = crate::printer::print_result(
&mut std::io::stdout(),
&mut io::stdout(),
self.print_mode,
self.start,
&all,
Expand Down
25 changes: 13 additions & 12 deletions src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
io::Write,
time::{Duration, Instant},
};
use crate::colors;

#[derive(Clone, Copy)]
struct StyleScheme {
Expand All @@ -24,33 +25,33 @@ impl StyleScheme {
fn success_rate(self, text: &str, success_rate: f64) -> StyledContent<&str> {
if self.color_enabled {
if success_rate >= 100.0 {
text.green().bold()
text.with(colors::SUCCESS).bold()
} else if success_rate >= 99.0 {
text.yellow().bold()
text.with(colors::WARNING).bold()
} else {
text.red().bold()
text.with(colors::ERROR).bold()
}
} else {
self.no_color(text).bold()
}
}
fn fastest(self, text: &str) -> StyledContent<&str> {
if self.color_enabled {
text.green()
text.with(colors::FASTEST)
} else {
self.no_color(text)
}
}
fn slowest(self, text: &str) -> StyledContent<&str> {
if self.color_enabled {
text.yellow()
text.with(colors::SLOWEST)
} else {
self.no_color(text)
}
}
fn average(self, text: &str) -> StyledContent<&str> {
if self.color_enabled {
text.cyan()
text.with(colors::AVERAGE)
} else {
self.no_color(text)
}
Expand All @@ -59,11 +60,11 @@ impl StyleScheme {
fn latency_distribution(self, text: &str, label: f64) -> StyledContent<&str> {
if self.color_enabled {
if label <= 0.3 {
text.green()
text.with(colors::FASTEST)
} else if label <= 0.8 {
text.yellow()
text.with(colors::AVERAGE)
} else {
text.red()
text.with(colors::SLOWEST)
}
} else {
self.no_color(text)
Expand All @@ -73,11 +74,11 @@ impl StyleScheme {
fn status_distribution(self, text: &str, status: StatusCode) -> StyledContent<&str> {
if self.color_enabled {
if status.is_success() {
text.green()
text.with(colors::SUCCESS)
} else if status.is_client_error() {
text.yellow()
text.with(colors::WARNING)
} else if status.is_server_error() {
text.red()
text.with(colors::ERROR)
} else {
text.white()
}
Expand Down

0 comments on commit a720010

Please sign in to comment.