Skip to content

Commit

Permalink
Fix separator's meaningless sort #42
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Feb 25, 2020
1 parent 977f831 commit fa58421
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased](https://github.com/dalance/procs/compare/v0.9.11...Unreleased) - ReleaseDate

* [Fixed] separator's meaningless sort [#42](https://github.com/dalance/procs/issues/42)

## [v0.9.11](https://github.com/dalance/procs/compare/v0.9.10...v0.9.11) - 2020-02-16

## [v0.9.10](https://github.com/dalance/procs/compare/v0.9.9...v0.9.10) - 2020-02-16
Expand Down
4 changes: 4 additions & 0 deletions src/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ pub trait Column {
true
}

fn sortable(&self) -> bool {
true
}

fn display_header(
&self,
align: &ConfigColumnAlign,
Expand Down
4 changes: 4 additions & 0 deletions src/columns/separator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,9 @@ impl Column for Separator {
self.raw_contents.insert(proc.pid, raw_content);
}

fn sortable(&self) -> bool {
false
}

column_default!(String);
}
26 changes: 26 additions & 0 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,30 @@ impl View {

#[cfg(target_os = "windows")]
fn pager(_config: &Config) {}

pub fn inc_sort_column(&mut self) -> usize {
let current = self.sort_info.idx;
let max_idx = self.columns.len();

for i in 1..max_idx {
let idx = (current + i) % max_idx;
if self.columns[idx].column.sortable() {
return idx;
}
}
return current;
}

pub fn dec_sort_column(&mut self) -> usize {
let current = self.sort_info.idx;
let max_idx = self.columns.len();

for i in 1..max_idx {
let idx = (current + max_idx - i) % max_idx;
if self.columns[idx].column.sortable() {
return idx;
}
}
return current;
}
}
9 changes: 4 additions & 5 deletions src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl Watcher {
let term_info = TermInfo::new(false);
term_info.clear_screen()?;

let mut sort_offset = 0;
let mut sort_idx = None;
let mut sort_order = None;
let mut min_widths = HashMap::new();
let mut prev_term_width = 0;
Expand All @@ -108,9 +108,8 @@ impl Watcher {
let mut view = View::new(opt, config, true);

// Override sort_info by key
let max_idx = view.columns.len();
if !opt.tree {
view.sort_info.idx = (view.sort_info.idx + sort_offset) % max_idx;
view.sort_info.idx = sort_idx.unwrap_or(view.sort_info.idx);
view.sort_info.order = sort_order.clone().unwrap_or(view.sort_info.order);
}

Expand Down Expand Up @@ -147,8 +146,8 @@ impl Watcher {
view.term_info.clear_screen()?;
break 'outer;
}
Command::Next => sort_offset = (sort_offset + 1) % max_idx,
Command::Prev => sort_offset = (sort_offset + max_idx - 1) % max_idx,
Command::Next => sort_idx = Some(view.inc_sort_column()),
Command::Prev => sort_idx = Some(view.dec_sort_column()),
Command::Ascending => sort_order = Some(ConfigSortOrder::Ascending),
Command::Descending => sort_order = Some(ConfigSortOrder::Descending),
_ => (),
Expand Down

0 comments on commit fa58421

Please sign in to comment.