Skip to content

Commit

Permalink
fixed indexing buggs
Browse files Browse the repository at this point in the history
  • Loading branch information
jacek-kurlit committed Jul 2, 2024
1 parent 9f0a6e3 commit 79e1541
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 41 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ It works like pkill command but search is interactive.

- [ ] Fix all TODO's
- [ ] Fix all FIXME's
- [ ] There is bug, if list of processes is empty TAB key will make index out of boundary exception
- [ ] Empty table is showing 1/0 instead of 0/0
- [ ] On linux Signal::Kill forces process to stop while Signal::Term terminate a process gracefully, add handling to check if os is supporting Term
- [ ] this + 2 is due to '> ' at the beginning, maybe some fix? `f.set_cursor(area.x + app.character_index as u16 + 2, area.y);`

## Optimization

Expand All @@ -35,3 +31,7 @@ It works like pkill command but search is interactive.
- [ ] Add option to search in environment variables - This is doable, maybe we can show it in process details?
- [ ] Add Process details - we can either add it at the bottom or add pop up with details
- [ ] Add option to ask if user wants to kill all processes (???)

## Refactor

- [ ] Consider some ratatui widget that can handle input
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ mod tui;

fn main() -> Result<()> {
let args: Vec<String> = env::args().skip(1).collect();
start_tui_app(args.first().cloned().unwrap_or("".to_string()))
start_tui_app(args.into_iter().next().unwrap_or("".to_string()))
}
6 changes: 5 additions & 1 deletion src/processes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ impl ProcessManager {

pub fn kill_process(&self, pid: u32) {
if let Some(prc) = self.sys.process(Pid::from_u32(pid)) {
prc.kill_with(sysinfo::Signal::Term);
if sysinfo::SUPPORTED_SIGNALS.contains(&sysinfo::Signal::Term) {
prc.kill_with(sysinfo::Signal::Term);
} else {
prc.kill();
}
}
}
}
Expand Down
70 changes: 35 additions & 35 deletions src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,49 +48,38 @@ struct App {

impl App {
fn new(search_criteria: String) -> Result<App> {
let mut process_manager = ProcessManager::new();
let processes = process_manager.find_processes(&search_criteria);
let scroll_size = processes.len().saturating_sub(1);
let mut app = App {
state: TableState::default().with_selected(0),
process_manager,
processes,
scroll_state: ScrollbarState::new(scroll_size),
state: TableState::default(),
process_manager: ProcessManager::new(),
processes: vec![],
scroll_state: ScrollbarState::new(0),
colors: TableColors::new(),
search_criteria,
character_index: 0,
};
app.search_for_processess();
app.move_cursor_to_end();
Ok(app)
}
pub fn next(&mut self) {
let i = match self.state.selected() {
Some(i) => {
if i >= self.processes.len() - 1 {
0
} else {
i + 1
}
let i = self.state.selected().map(|i| {
let mut i = i + 1;
if i >= self.processes.len() {
i = 0
}
None => 0,
};
self.state.select(Some(i));
self.scroll_state = self.scroll_state.position(i);
i
});
self.state.select(i);
self.scroll_state = self.scroll_state.position(i.unwrap_or(0));
}

pub fn previous(&mut self) {
let i = match self.state.selected() {
Some(i) => {
if i == 0 {
self.processes.len().saturating_sub(1)
} else {
i - 1
}
}
None => 0,
};
self.state.select(Some(i));
self.scroll_state = self.scroll_state.position(i);
let previous_index = self.state.selected().map(|i| {
let i = i.wrapping_sub(1);
i.clamp(0, self.processes.len().saturating_sub(1))
});
self.state.select(previous_index);
self.scroll_state = self.scroll_state.position(previous_index.unwrap_or(0));
}

fn move_cursor_left(&mut self) {
Expand Down Expand Up @@ -123,6 +112,14 @@ impl App {

fn search_for_processess(&mut self) {
self.processes = self.process_manager.find_processes(&self.search_criteria);
self.scroll_state = self
.scroll_state
.content_length(self.processes.len().saturating_sub(1));
if self.processes.is_empty() {
self.state.select(None);
} else {
self.state.select(Some(0));
}
}

/// Returns the byte index based on the character position.
Expand Down Expand Up @@ -253,11 +250,15 @@ fn ui(f: &mut Frame, app: &mut App) {
}

fn render_input(f: &mut Frame, app: &mut App, area: Rect) {
let current_input = format!("> {}", app.search_criteria);
//TODO: use loop icon instead of '>'
let prompt = "> ";
let current_input = format!("{}{}", prompt, app.search_criteria);
let input = Paragraph::new(current_input.as_str());
f.render_widget(input, area);
//FIXME: this + 2 is cue to '> ' at the beggining, maybe some fix?
f.set_cursor(area.x + app.character_index as u16 + 2, area.y);
f.set_cursor(
area.x + app.character_index as u16 + prompt.len() as u16,
area.y,
);
}

fn render_table(f: &mut Frame, app: &mut App, area: Rect) {
Expand Down Expand Up @@ -298,10 +299,9 @@ fn render_table(f: &mut Frame, app: &mut App, area: Rect) {
.block(
Block::default()
.title(
//FIXME: for empty table this is howing 1 / 0
Title::from(format!(
" {} / {} ",
app.state.selected().unwrap_or(0) + 1,
app.state.selected().map(|i| i + 1).unwrap_or(0),
app.processes.len()
))
.position(block::Position::Top)
Expand Down

0 comments on commit 79e1541

Please sign in to comment.