Skip to content

Commit

Permalink
feature: support default selection of average CPU graph (#1353)
Browse files Browse the repository at this point in the history
* feature: support default selection of average CPU graph

* test
  • Loading branch information
ClementTsang authored Dec 10, 2023
1 parent 300f667 commit b6f92c2
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 34 deletions.
12 changes: 0 additions & 12 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,3 @@ rustflags = ["-C", "target-feature=+crt-static"]

[target.i686-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]

# [target.arm-unknown-linux-gnueabihf]
# linker = "arm-linux-gnueabihf-gcc"

# [target.armv7-unknown-linux-gnueabihf]
# linker = "arm-linux-gnueabihf-gcc"

# [target.aarch64-unknown-linux-gnu]
# linker = "aarch64-linux-gnu-gcc"

# [target.aarch64-unknown-linux-musl]
# linker = "aarch64-linux-musl-gcc"
11 changes: 11 additions & 0 deletions docs/content/configuration/config-file/cpu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# CPU

## Default CPU Graph Selection

You can configure which CPU graph is shown by default when starting up bottom by setting `cpu.default`.

```toml
[cpu]
# One of "all" (default), "average"/"avg"
default = "average"
```
4 changes: 4 additions & 0 deletions sample_configs/default_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@
#[processes]
#columns = ["PID", "Name", "CPU%", "Mem%", "R/s", "W/s", "T.Read", "T.Write", "User", "State", "GMEM%", "GPU%"]

# [cpu]
# One of "all" (default), "average"/"avg"
# default = "average"

# These are all the components that support custom theming. Note that colour support
# will depend on terminal support.

Expand Down
22 changes: 11 additions & 11 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub struct AppConfigFields {
pub temperature_type: temperature::TemperatureType,
pub use_dot: bool,
pub left_legend: bool,
pub show_average_cpu: bool,
pub show_average_cpu: bool, // TODO: Unify this in CPU options
pub use_current_cpu_total: bool,
pub unnormalized_cpu: bool,
pub use_basic_mode: bool,
Expand Down Expand Up @@ -1910,7 +1910,7 @@ impl App {
.proc_state
.get_mut_widget_state(self.current_widget.widget_id)
{
proc_widget_state.table.set_first();
proc_widget_state.table.to_first();
}
}
BottomWidgetType::ProcSort => {
Expand All @@ -1919,7 +1919,7 @@ impl App {
.proc_state
.get_mut_widget_state(self.current_widget.widget_id - 2)
{
proc_widget_state.sort_table.set_first();
proc_widget_state.sort_table.to_first();
}
}
BottomWidgetType::Temp => {
Expand All @@ -1928,7 +1928,7 @@ impl App {
.temp_state
.get_mut_widget_state(self.current_widget.widget_id)
{
temp_widget_state.table.set_first();
temp_widget_state.table.to_first();
}
}
BottomWidgetType::Disk => {
Expand All @@ -1937,7 +1937,7 @@ impl App {
.disk_state
.get_mut_widget_state(self.current_widget.widget_id)
{
disk_widget_state.table.set_first();
disk_widget_state.table.to_first();
}
}
BottomWidgetType::CpuLegend => {
Expand All @@ -1946,7 +1946,7 @@ impl App {
.cpu_state
.get_mut_widget_state(self.current_widget.widget_id - 1)
{
cpu_widget_state.table.set_first();
cpu_widget_state.table.to_first();
}
}

Expand All @@ -1969,7 +1969,7 @@ impl App {
.proc_state
.get_mut_widget_state(self.current_widget.widget_id)
{
proc_widget_state.table.set_last();
proc_widget_state.table.to_last();
}
}
BottomWidgetType::ProcSort => {
Expand All @@ -1978,7 +1978,7 @@ impl App {
.proc_state
.get_mut_widget_state(self.current_widget.widget_id - 2)
{
proc_widget_state.sort_table.set_last();
proc_widget_state.sort_table.to_last();
}
}
BottomWidgetType::Temp => {
Expand All @@ -1987,7 +1987,7 @@ impl App {
.temp_state
.get_mut_widget_state(self.current_widget.widget_id)
{
temp_widget_state.table.set_last();
temp_widget_state.table.to_last();
}
}
BottomWidgetType::Disk => {
Expand All @@ -1997,7 +1997,7 @@ impl App {
.get_mut_widget_state(self.current_widget.widget_id)
{
if !self.converted_data.disk_data.is_empty() {
disk_widget_state.table.set_last();
disk_widget_state.table.to_last();
}
}
}
Expand All @@ -2007,7 +2007,7 @@ impl App {
.cpu_state
.get_mut_widget_state(self.current_widget.widget_id - 1)
{
cpu_widget_state.table.set_last();
cpu_widget_state.table.to_last();
}
}
_ => {}
Expand Down
16 changes: 12 additions & 4 deletions src/components/data_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub struct DataTable<DataType, Header, S = Unsortable, C = Column<Header>> {
data: Vec<DataType>,
sort_type: S,
first_draw: bool,
first_index: Option<usize>,
_pd: PhantomData<(DataType, S, Header)>,
}

Expand All @@ -55,6 +56,7 @@ impl<DataType: DataToCell<H>, H: ColumnHeader> DataTable<DataType, H, Unsortable
data: vec![],
sort_type: Unsortable,
first_draw: true,
first_index: None,
_pd: PhantomData,
}
}
Expand All @@ -63,14 +65,20 @@ impl<DataType: DataToCell<H>, H: ColumnHeader> DataTable<DataType, H, Unsortable
impl<DataType: DataToCell<H>, H: ColumnHeader, S: SortType, C: DataTableColumn<H>>
DataTable<DataType, H, S, C>
{
/// Sets the default value selected on first initialization, if possible.
pub fn first_draw_index(mut self, first_index: usize) -> Self {
self.first_index = Some(first_index);
self
}

/// Sets the scroll position to the first value.
pub fn set_first(&mut self) {
pub fn to_first(&mut self) {
self.state.current_index = 0;
self.state.scroll_direction = ScrollDirection::Up;
}

/// Sets the scroll position to the last value.
pub fn set_last(&mut self) {
pub fn to_last(&mut self) {
self.state.current_index = self.data.len().saturating_sub(1);
self.state.scroll_direction = ScrollDirection::Down;
}
Expand Down Expand Up @@ -189,11 +197,11 @@ mod test {
let mut table = DataTable::new(columns, props, styling);
table.set_data((0..=4).map(|index| TestType { index }).collect::<Vec<_>>());

table.set_last();
table.to_last();
assert_eq!(table.current_index(), 4);
assert_eq!(table.state.scroll_direction, ScrollDirection::Down);

table.set_first();
table.to_first();
assert_eq!(table.current_index(), 0);
assert_eq!(table.state.scroll_direction, ScrollDirection::Up);

Expand Down
9 changes: 7 additions & 2 deletions src/components/data_table/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,15 @@ where
self.props.table_gap
};

let columns = &self.columns;
if !self.data.is_empty() || !self.first_draw {
self.first_draw = false; // TODO: Doing it this way is fine, but it could be done better (e.g. showing custom no results/entries message)
if self.first_draw {
self.first_draw = false; // TODO: Doing it this way is fine, but it could be done better (e.g. showing custom no results/entries message)
if let Some(first_index) = self.first_index {
self.set_position(first_index);
}
}

let columns = &self.columns;
let rows = {
let num_rows =
usize::from(inner_height.saturating_sub(table_gap + header_height));
Expand Down
1 change: 1 addition & 0 deletions src/components/data_table/sortable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ where
order: props.order,
},
first_draw: true,
first_index: None,
data: vec![],
_pd: PhantomData,
}
Expand Down
4 changes: 4 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,10 @@ pub const CONFIG_TEXT: &str = r#"# This is a default config file for bottom. Al
# PID, Name, CPU%, Mem%, R/s, W/s, T.Read, T.Write, User, State, Time, GMem%, GPU%
#columns = ["PID", "Name", "CPU%", "Mem%", "R/s", "W/s", "T.Read", "T.Write", "User", "State", "GMEM%", "GPU%"]
# [cpu]
# One of "all" (default), "average"/"avg"
# default = "average"
# These are all the components that support custom theming. Note that colour support
# will depend on terminal support.
#[colors] # Uncomment if you want to use custom colors
Expand Down
13 changes: 11 additions & 2 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ use crate::{

pub mod layout_options;

pub mod process_columns;
use self::process_columns::ProcessConfig;
mod process_columns;
pub use process_columns::ProcessConfig;

mod cpu;
pub use cpu::{CpuConfig, CpuDefault};

use anyhow::{Context, Result};

Expand All @@ -43,6 +46,7 @@ pub struct Config {
pub temp_filter: Option<IgnoreList>,
pub net_filter: Option<IgnoreList>,
pub processes: Option<ProcessConfig>,
pub cpu: Option<CpuConfig>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -340,6 +344,11 @@ pub fn build_app(
widget.widget_id,
CpuWidgetState::new(
&app_config_fields,
config
.cpu
.as_ref()
.map(|cfg| cfg.default)
.unwrap_or_default(),
default_time_value,
autohide_timer,
styling,
Expand Down
79 changes: 79 additions & 0 deletions src/options/cpu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use serde::Deserialize;

/// The default selection of the CPU widget. If the given selection is invalid, we will fall back to all.
#[derive(Clone, Copy, Debug, Default, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CpuDefault {
#[default]
All,
#[serde(alias = "avg")]
Average,
}

/// Process column settings.
#[derive(Clone, Debug, Default, Deserialize)]
pub struct CpuConfig {
#[serde(default)]
pub default: CpuDefault,
}

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

#[test]
fn default_cpu_default() {
let config = "";
let generated: CpuConfig = toml_edit::de::from_str(config).unwrap();
match generated.default {
CpuDefault::All => {}
CpuDefault::Average => {
panic!("the default should be all")
}
}
}

#[test]
fn all_cpu_default() {
let config = r#"
default = "all"
"#;
let generated: CpuConfig = toml_edit::de::from_str(config).unwrap();
match generated.default {
CpuDefault::All => {}
CpuDefault::Average => {
panic!("the default should be all")
}
}
}

#[test]
fn avg_cpu_default() {
let config = r#"
default = "avg"
"#;

let generated: CpuConfig = toml_edit::de::from_str(config).unwrap();
match generated.default {
CpuDefault::All => {
panic!("the avg should be set")
}
CpuDefault::Average => {}
}
}

#[test]
fn average_cpu_default() {
let config = r#"
default = "average"
"#;

let generated: CpuConfig = toml_edit::de::from_str(config).unwrap();
match generated.default {
CpuDefault::All => {
panic!("the avg should be set")
}
CpuDefault::Average => {}
}
}
}
15 changes: 12 additions & 3 deletions src/widgets/cpu_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
DataToCell,
},
data_conversion::CpuWidgetData,
options::CpuDefault,
utils::gen_util::truncate_to_text,
};

Expand Down Expand Up @@ -165,8 +166,8 @@ pub struct CpuWidgetState {

impl CpuWidgetState {
pub fn new(
config: &AppConfigFields, current_display_time: u64, autohide_timer: Option<Instant>,
colours: &CanvasStyling,
config: &AppConfigFields, default_selection: CpuDefault, current_display_time: u64,
autohide_timer: Option<Instant>, colours: &CanvasStyling,
) -> Self {
const COLUMNS: [Column<CpuWidgetColumn>; 2] = [
Column::soft(CpuWidgetColumn::CPU, Some(0.5)),
Expand All @@ -183,13 +184,21 @@ impl CpuWidgetState {
};

let styling = DataTableStyling::from_colours(colours);
let mut table = DataTable::new(COLUMNS, props, styling);
match default_selection {
CpuDefault::All => {}
CpuDefault::Average if !config.show_average_cpu => {}
CpuDefault::Average => {
table = table.first_draw_index(1);
}
}

CpuWidgetState {
current_display_time,
is_legend_hidden: false,
show_avg: config.show_average_cpu,
autohide_timer,
table: DataTable::new(COLUMNS, props, styling),
table,
styling: CpuWidgetStyling::from_colours(colours),
}
}
Expand Down

0 comments on commit b6f92c2

Please sign in to comment.