Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve time values on the horizontal axis of the chart #641

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All Sniffnet releases with the relative changes are documented in this file.
- Chinese ([#575](https://github.com/GyulyVGC/sniffnet/pull/575))
- Korean ([#604](https://github.com/GyulyVGC/sniffnet/pull/604))
- Turkish ([#608](https://github.com/GyulyVGC/sniffnet/pull/608))
- Improve time values on the horizontal axis of the chart ([#641](https://github.com/GyulyVGC/sniffnet/pull/641) — fixes [#619](https://github.com/GyulyVGC/sniffnet/issues/619))
- Migrate to Iced 0.13 ([#618](https://github.com/GyulyVGC/sniffnet/pull/618))
- Added support for Linux `loongarch64` (fixes [#592](https://github.com/GyulyVGC/sniffnet/issues/592))

Expand Down
6 changes: 5 additions & 1 deletion src/chart/types/traffic_chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::gui::styles::types::palette::to_rgb_color;
use crate::gui::types::message::Message;
use crate::networking::types::traffic_direction::TrafficDirection;
use crate::translations::translations::{incoming_translation, outgoing_translation};
use crate::utils::formatted_strings::get_formatted_num_seconds;
use crate::{ByteMultiple, ChartType, Language, StyleType};

/// Struct defining the chart to be displayed in gui run page
Expand Down Expand Up @@ -217,7 +218,10 @@ impl Chart<Message> for TrafficChart {
&|bytes| ByteMultiple::formatted_string(bytes.abs() as u128)
})
.x_labels(min(6, x_labels))
.x_label_formatter(&std::string::ToString::to_string)
.x_label_formatter(
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
&|seconds| get_formatted_num_seconds(seconds.abs() as u128),
)
.draw()
.unwrap();

Expand Down
9 changes: 2 additions & 7 deletions src/gui/styles/scrollbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,12 @@ impl ScrollbarType {
}

#[allow(clippy::unused_self)]
fn hovered(
&self,
style: &StyleType,
is_mouse_over_x_scrollbar: bool,
is_mouse_over_y_scrollbar: bool,
) -> Style {
fn hovered(&self, style: &StyleType, is_mouse_over_x: bool, is_mouse_over_y: bool) -> Style {
let colors = style.get_palette();
let ext = style.get_extension();

let [horizontal_rail, vertical_rail] =
[is_mouse_over_x_scrollbar, is_mouse_over_y_scrollbar].map(|is_over| Rail {
[is_mouse_over_x, is_mouse_over_y].map(|is_over| Rail {
background: Some(Background::Color(Color {
a: ext.alpha_round_borders,
..ext.buttons_color
Expand Down
49 changes: 49 additions & 0 deletions src/utils/formatted_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,52 @@ pub fn get_path_termination_string(full_path: &str, i: usize) -> String {
]
.concat()
}

pub fn get_formatted_num_seconds(num_seconds: u128) -> String {
match num_seconds {
0..3600 => format!("{:02}:{:02}", num_seconds / 60, num_seconds % 60),
_ => format!(
"{:02}:{:02}:{:02}",
num_seconds / 3600,
(num_seconds % 3600) / 60,
num_seconds % 60
),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_formatted_num_seconds() {
assert_eq!(get_formatted_num_seconds(0), "00:00");
assert_eq!(get_formatted_num_seconds(1), "00:01");
assert_eq!(get_formatted_num_seconds(28), "00:28");
assert_eq!(get_formatted_num_seconds(59), "00:59");
assert_eq!(get_formatted_num_seconds(60), "01:00");
assert_eq!(get_formatted_num_seconds(61), "01:01");
assert_eq!(get_formatted_num_seconds(119), "01:59");
assert_eq!(get_formatted_num_seconds(120), "02:00");
assert_eq!(get_formatted_num_seconds(121), "02:01");
assert_eq!(get_formatted_num_seconds(3500), "58:20");
assert_eq!(get_formatted_num_seconds(3599), "59:59");
assert_eq!(get_formatted_num_seconds(3600), "01:00:00");
assert_eq!(get_formatted_num_seconds(3601), "01:00:01");
assert_eq!(get_formatted_num_seconds(3661), "01:01:01");
assert_eq!(get_formatted_num_seconds(7139), "01:58:59");
assert_eq!(get_formatted_num_seconds(7147), "01:59:07");
assert_eq!(get_formatted_num_seconds(7199), "01:59:59");
assert_eq!(get_formatted_num_seconds(7200), "02:00:00");
assert_eq!(get_formatted_num_seconds(9999), "02:46:39");
assert_eq!(get_formatted_num_seconds(36000), "10:00:00");
assert_eq!(get_formatted_num_seconds(36001), "10:00:01");
assert_eq!(get_formatted_num_seconds(36061), "10:01:01");
assert_eq!(get_formatted_num_seconds(86400), "24:00:00");
assert_eq!(get_formatted_num_seconds(123456789), "34293:33:09");
assert_eq!(
get_formatted_num_seconds(u128::MAX),
"94522879700260684295381835397713392:04:15"
);
}
}