Skip to content

Commit

Permalink
scroll to new output line
Browse files Browse the repository at this point in the history
  • Loading branch information
dzhou121 committed Apr 7, 2024
1 parent c7c533d commit 29190fa
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
5 changes: 5 additions & 0 deletions examples/example_tiron_project/main.rcl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
remote_user = "dz",
actions = [
{ action = "copy", params = {src = "/tmp/test.rcl", dest = "/tmp/test.conf" }},
{ action = "job", params = {name = "job1"} },
{ action = "job", params = {name = "job1"} },
{ action = "job", params = {name = "job1"} },
{ action = "job", params = {name = "job1"} },
{ action = "job", params = {name = "job1"} },
],
},
]
19 changes: 18 additions & 1 deletion tiron-tui/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::SystemTime;

use anyhow::{anyhow, Result};
use crossbeam_channel::{Receiver, Sender};
use ratatui::{
Expand Down Expand Up @@ -207,10 +209,25 @@ impl App {
action.success(success);
}
ActionMessage::NodeShutdown { success } => {
host.success = Some(success);
host.success = Some((
success,
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0),
));
run.sort_hosts();
}
ActionMessage::NodeStartFailed { reason } => {
host.start_failed = Some(reason);
host.success = Some((
false,
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0),
));
run.sort_hosts();
}
}
Ok(())
Expand Down
24 changes: 22 additions & 2 deletions tiron-tui/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct HostSection {
// or screen size changed
pub content_height: Option<usize>,
pub viewport_height: usize,
pub success: Option<bool>,
pub success: Option<(bool, u64)>,
pub start_failed: Option<String>,
}

Expand Down Expand Up @@ -96,6 +96,7 @@ impl HostSection {
);

let mut y = 0;
let mut running_bottom = 0;

let stop_if_outside_area = self.content_height.is_some();
if let Some(reason) = &self.start_failed {
Expand All @@ -115,13 +116,18 @@ impl HostSection {
for action in &self.actions {
action.render(area, buf, &mut y, self.scroll, stop_if_outside_area);
y += 1;
if action.output.started {
running_bottom = y;
}
if stop_if_outside_area && y >= area.height + self.scroll {
break;
}
}

if self.content_height.is_none() {
self.content_height = Some(y as usize);
self.scroll = running_bottom.saturating_sub(area.height);
self.scroll_state = self.scroll_state.position(self.scroll as usize);
}
self.viewport_height = area.height as usize;

Expand Down Expand Up @@ -214,7 +220,7 @@ impl RunPanel {
Some(Color::Red)
} else {
host.success
.map(|success| if success { Color::Green } else { Color::Red })
.map(|(success, _)| if success { Color::Green } else { Color::Red })
};
if let Some(color) = color {
host.host.clone().fg(color)
Expand All @@ -226,6 +232,20 @@ impl RunPanel {
.block(Block::default().borders(Borders::RIGHT))
.render(area, buf, &mut self.hosts_state);
}

pub fn sort_hosts(&mut self) {
let active_id = self.get_active_host().ok().map(|h| h.id);
self.hosts.sort_by_key(|h| h.success);
let active = if let Some(id) = active_id {
self.hosts.iter().position(|h| h.id == id)
} else {
None
};
if let Some(active) = active {
self.active = active;
self.hosts_state.select(Some(active));
}
}
}

const fn get_line_offset(line_width: u16, text_area_width: u16, alignment: Alignment) -> u16 {
Expand Down

0 comments on commit 29190fa

Please sign in to comment.