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

Allow sorting by any column #183

Merged
merged 8 commits into from
Aug 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix bug w/ movement, refactor and add reflection
  • Loading branch information
ClementTsang committed Aug 15, 2020
commit dae8751b43605e9538e34d664b994ec27db679f1
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- [179](https://github.com/ClementTsang/bottom/pull/179): Show full command/process path as an option.

- [183](https://github.com/ClementTsang/bottom/pull/183): Added sorting capabilities to any column.

### Changes

- Added `WASD` as an alternative widget movement system.
Expand All @@ -25,6 +27,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Bug Fixes

- [183](https://github.com/ClementTsang/bottom/pull/183): Fixed bug in basic mode where the battery widget was placed incorrectly.

## [0.4.5] - 2020-07-08

- No changes here, just an uptick for Crates.io using the wrong Cargo.lock.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ Run using `btm`.
| `Tab` | Group/un-group processes with the same name |
| `Ctrl-f`, `/` | Open process search widget |
| `P` | Toggle between showing the full path or just the process name |
| `s, F6` | Open process column sort widget |
| `s, F6` | Open process sort widget |
| `I` | Invert current sort |

#### Process search bindings
Expand Down
656 changes: 310 additions & 346 deletions src/app.rs

Large diffs are not rendered by default.

30 changes: 29 additions & 1 deletion src/app/layout_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ impl BottomLayout {
.children(vec![
BottomColRow::builder()
.canvas_handle_height(true)
.total_widget_ratio(3)
.children(vec![
BottomWidget::builder()
.canvas_handle_width(true)
Expand All @@ -559,6 +560,7 @@ impl BottomLayout {
.down_neighbour(Some(DEFAULT_WIDGET_ID + 1))
.left_neighbour(Some(4))
.right_neighbour(Some(DEFAULT_WIDGET_ID))
.width_ratio(1)
.build(),
BottomWidget::builder()
.canvas_handle_width(true)
Expand All @@ -567,7 +569,8 @@ impl BottomLayout {
.up_neighbour(Some(100))
.down_neighbour(Some(DEFAULT_WIDGET_ID + 1))
.left_neighbour(Some(DEFAULT_WIDGET_ID + 2))
.right_neighbour(Some(8))
.right_neighbour(Some(7))
.width_ratio(2)
.build(),
])
.build(),
Expand Down Expand Up @@ -809,6 +812,25 @@ pub struct BottomColRow {
pub flex_grow: bool,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum WidgetDirection {
Left,
Right,
Up,
Down,
}

impl WidgetDirection {
pub fn is_opposite(&self, other_direction: &WidgetDirection) -> bool {
match &self {
WidgetDirection::Left => *other_direction == WidgetDirection::Right,
WidgetDirection::Right => *other_direction == WidgetDirection::Left,
WidgetDirection::Up => *other_direction == WidgetDirection::Down,
WidgetDirection::Down => *other_direction == WidgetDirection::Up,
}
}
}

/// Represents a single widget.
#[derive(Debug, Default, Clone, TypedBuilder)]
pub struct BottomWidget {
Expand All @@ -830,11 +852,17 @@ pub struct BottomWidget {
#[builder(default = None)]
pub down_neighbour: Option<u64>,

/// If set to true, the canvas will override any ratios.
#[builder(default = false)]
pub canvas_handle_width: bool,

/// Whether we want this widget to take up all available room (and ignore any ratios).
#[builder(default = false)]
pub flex_grow: bool,

/// The value is the direction to bounce, as well as the parent offset.
#[builder(default = None)]
pub parent_reflector: Option<(WidgetDirection, u64)>,
}

#[derive(Debug, Clone, Eq, PartialEq, Hash)]
Expand Down
2 changes: 1 addition & 1 deletion src/canvas/widgets/basic_table_arrows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl BasicTableArrows for Painter {
.map(|right_widget| {
if right_widget.widget_type == BottomWidgetType::ProcSort {
right_widget
.left_neighbour
.right_neighbour
.map(|right_right_widget_id| {
app_state.widget_map.get(&right_right_widget_id).map(
|right_right_widget| {
Expand Down
6 changes: 3 additions & 3 deletions src/canvas/widgets/cpu_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::borrow::Cow;
use std::cmp::max;

use crate::{
app::App,
app::{layout_manager::WidgetDirection, App},
canvas::{
drawing_utils::{get_start_position, get_variable_intrinsic_widths},
Painter,
Expand Down Expand Up @@ -57,9 +57,9 @@ impl CpuGraphWidget for Painter {
// Skip drawing legend
if app_state.current_widget.widget_id == (widget_id + 1) {
if app_state.app_config_fields.left_legend {
app_state.move_widget_selection_right();
app_state.move_widget_selection(&WidgetDirection::Right);
} else {
app_state.move_widget_selection_left();
app_state.move_widget_selection(&WidgetDirection::Left);
}
}
self.draw_cpu_graph(f, app_state, draw_loc, widget_id);
Expand Down
2 changes: 1 addition & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub const PROCESS_HELP_TEXT: [&str; 11] = [
"Tab Group/un-group processes with the same name",
"Ctrl-f, / Open process search widget",
"P Toggle between showing the full path or just the process name",
"s, F6 Open process column sort widget",
"s, F6 Open process sort widget",
"I Invert current sort",
];

Expand Down
18 changes: 9 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use tui::{backend::CrosstermBackend, Terminal};

use app::{
data_harvester::{self, processes::ProcessSorting},
layout_manager::UsedWidgets,
layout_manager::{UsedWidgets, WidgetDirection},
App,
};
use constants::*;
Expand Down Expand Up @@ -310,10 +310,10 @@ fn handle_key_event_or_break(

match event.code {
KeyCode::Char('f') => app.on_slash(),
KeyCode::Left => app.move_widget_selection_left(),
KeyCode::Right => app.move_widget_selection_right(),
KeyCode::Up => app.move_widget_selection_up(),
KeyCode::Down => app.move_widget_selection_down(),
KeyCode::Left => app.move_widget_selection(&WidgetDirection::Left),
KeyCode::Right => app.move_widget_selection(&WidgetDirection::Right),
KeyCode::Up => app.move_widget_selection(&WidgetDirection::Up),
KeyCode::Down => app.move_widget_selection(&WidgetDirection::Down),
KeyCode::Char('r') => {
if reset_sender.send(ResetEvent::Reset).is_ok() {
app.reset();
Expand All @@ -333,10 +333,10 @@ fn handle_key_event_or_break(
}
} else if let KeyModifiers::SHIFT = event.modifiers {
match event.code {
KeyCode::Left => app.move_widget_selection_left(),
KeyCode::Right => app.move_widget_selection_right(),
KeyCode::Up => app.move_widget_selection_up(),
KeyCode::Down => app.move_widget_selection_down(),
KeyCode::Left => app.move_widget_selection(&WidgetDirection::Left),
KeyCode::Right => app.move_widget_selection(&WidgetDirection::Right),
KeyCode::Up => app.move_widget_selection(&WidgetDirection::Up),
KeyCode::Down => app.move_widget_selection(&WidgetDirection::Down),
KeyCode::Char(caught_char) => app.on_char_key(caught_char),
_ => {}
}
Expand Down
4 changes: 2 additions & 2 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,11 @@ pub fn get_widget_layout(
total_row_height_ratio: total_height_ratio,
};

// debug!("Bottom layout: {:#?}", ret_bottom_layout);

// Confirm that we have at least ONE widget - if not, error out!
if iter_id > 0 {
ret_bottom_layout.get_movement_mappings();
// debug!("Bottom layout: {:#?}", ret_bottom_layout);

ret_bottom_layout
} else {
return Err(error::BottomError::ConfigError(
Expand Down
34 changes: 32 additions & 2 deletions src/options/layout_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl Row {
.widget_type(BottomWidgetType::CpuLegend)
.widget_id(*iter_id)
.canvas_handle_width(true)
.parent_reflector(Some((
WidgetDirection::Right,
1,
)))
.build(),
BottomWidget::builder()
.width_ratio(17)
Expand All @@ -90,6 +94,10 @@ impl Row {
.widget_type(BottomWidgetType::CpuLegend)
.widget_id(*iter_id)
.canvas_handle_width(true)
.parent_reflector(Some((
WidgetDirection::Left,
1,
)))
.build(),
])
.build()]
Expand All @@ -110,19 +118,26 @@ impl Row {
.widget_type(BottomWidgetType::ProcSort)
.widget_id(*iter_id)
.canvas_handle_width(true)
.parent_reflector(Some((
WidgetDirection::Right,
2,
)))
.width_ratio(1)
.build(),
BottomWidget::builder()
.widget_type(BottomWidgetType::Proc)
.widget_id(proc_id)
.width_ratio(2)
.build(),
])
.total_widget_ratio(2)
.total_widget_ratio(3)
.flex_grow(true)
.build(),
BottomColRow::builder()
.children(vec![BottomWidget::builder()
.widget_type(BottomWidgetType::ProcSearch)
.widget_id(proc_search_id)
.parent_reflector(Some((WidgetDirection::Up, 1)))
.build()])
.canvas_handle_height(true)
.build(),
Expand Down Expand Up @@ -187,6 +202,10 @@ impl Row {
.widget_type(BottomWidgetType::CpuLegend)
.widget_id(*iter_id)
.canvas_handle_width(true)
.parent_reflector(Some((
WidgetDirection::Right,
1,
)))
.build(),
BottomWidget::builder()
.width_ratio(17)
Expand Down Expand Up @@ -214,6 +233,10 @@ impl Row {
.widget_type(BottomWidgetType::CpuLegend)
.widget_id(*iter_id)
.canvas_handle_width(true)
.parent_reflector(Some((
WidgetDirection::Left,
1,
)))
.build(),
])
.build(),
Expand All @@ -232,14 +255,20 @@ impl Row {
.widget_type(BottomWidgetType::ProcSort)
.widget_id(*iter_id)
.canvas_handle_width(true)
.parent_reflector(Some((
WidgetDirection::Right,
2,
)))
.width_ratio(1)
.build(),
BottomWidget::builder()
.widget_type(BottomWidgetType::Proc)
.widget_id(proc_id)
.width_ratio(2)
.build(),
])
.col_row_height_ratio(col_row_height_ratio)
.total_widget_ratio(2)
.total_widget_ratio(3)
.build(),
);
col_row_children.push(
Expand All @@ -248,6 +277,7 @@ impl Row {
.children(vec![BottomWidget::builder()
.widget_type(BottomWidgetType::ProcSearch)
.widget_id(proc_search_id)
.parent_reflector(Some((WidgetDirection::Up, 1)))
.build()])
.canvas_handle_height(true)
.build(),
Expand Down
1 change: 1 addition & 0 deletions tests/widget_movement_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO: See if we can mock widget movements and test if they work