Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 44 additions & 6 deletions crates/telemetry/src/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,11 @@ impl App {
match self.focus_slot() {
Some(slot) => self.focus_on(slot),
None => {
if let Some(first) = shown.first() {
let first = shown
.iter()
.find(|id| self.draws_chart(**id))
.or(shown.first());
if let Some(first) = first {
self.focus_on(*first);
}
}
Expand All @@ -606,14 +610,21 @@ impl App {
self.focus_on(shown[(at + by).rem_euclid(shown.len() as isize) as usize]);
}

/// Whether the focusable slot draws a chart. The Models, Breakdown, Live
/// and Sessions panels are tables, and so is Spend on its daily view.
fn draws_chart(&self, id: PanelId) -> bool {
match id.panel() {
Some(Panel::Spend) => self.spend_view_of(id) != SpendView::Daily,
Some(panel) => panel == Panel::Tokens,
None => false,
}
}

/// The slot whose chart the cursor keys move: the focused Spend slot when
/// it charts, else the first slot of the tab that charts `panel`. A Spend
/// slot on the daily table has no chart.
fn chart_slot(&self, panel: Panel) -> Option<PanelId> {
let charts = |id: &PanelId| {
id.panel() == Some(panel)
&& (panel != Panel::Spend || self.spend_view_of(*id) != SpendView::Daily)
};
let charts = |id: &PanelId| id.panel() == Some(panel) && self.draws_chart(*id);
self.focus_slot()
.filter(charts)
.or_else(|| self.tab_ids().iter().copied().find(charts))
Expand Down Expand Up @@ -1742,7 +1753,7 @@ mod tests {
let mut app = charted(Range::LastDays(30), 30);
app.layout = crate::tui::layout::Layout::parse("A=spend.daily,spend.total").unwrap();
app.go_tab(0);
app.focus = Panel::Spend;
app.focus_on(PanelId::SpendDaily);
let on_table = app.is_table(Panel::Spend);
app.key(press(KeyCode::Left));
let cursor_on_table = app.cursor;
Expand Down Expand Up @@ -2222,4 +2233,31 @@ mod tests {

assert_eq!(app.sort(Panel::Live), Panel::Live.default_sort());
}

#[test]
fn a_fresh_start_on_the_default_layout_focuses_a_panel_that_draws_a_chart() {
let home = crate::util::scratch_dir("fresh-focus");
let mut app = App::new(Range::All);
let (layout, warning) = super::super::ui_state::load_layout(&home);
app.layout = layout;

app.apply_ui_state(&super::super::ui_state::load(&home));

assert_eq!(warning, None);
assert_eq!(app.tab, 0);
assert_eq!(app.focus, Panel::Tokens);
assert_eq!(app.focus_slot(), Some(PanelId::Tokens));
assert!(app.draws_chart(PanelId::Tokens));
assert!(!app.draws_chart(PanelId::SpendDaily));
}

#[test]
fn a_tab_of_tables_still_focuses_its_first_panel() {
let mut app = App::new(Range::All);
app.layout = crate::tui::layout::Layout::parse("A=models,live").unwrap();

app.apply_ui_state(&super::super::ui_state::UiState::default());

assert_eq!(app.focus_slot(), Some(PanelId::Models));
}
}
2 changes: 1 addition & 1 deletion crates/telemetry/src/tui/screenshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ struct View {
/// layout, so a tab shot sets the layout first.
fn tabs(app: &mut App, tab: usize) {
app.layout = super::layout::Layout::default();
app.tab = tab;
app.go_tab(tab as isize);
}

fn charts(app: &mut App) {
Expand Down
4 changes: 2 additions & 2 deletions crates/telemetry/src/tui/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1909,7 +1909,7 @@ mod tests {

app.key(KeyEvent::new(KeyCode::Char('6'), KeyModifiers::NONE));

assert_eq!(app.focus, crate::tui::sort::Panel::Spend);
assert_eq!(app.focus, crate::tui::sort::Panel::Tokens);
}

/// The rows of the header box, from its top border to its bottom one.
Expand Down Expand Up @@ -2017,7 +2017,7 @@ mod tests {

app.apply_ui_state(&crate::tui::ui_state::UiState::default());

assert_eq!(app.focus, crate::tui::sort::Panel::Spend);
assert_eq!(app.focus, crate::tui::sort::Panel::Tokens);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion docs/images/pbtop/breakdown-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/images/pbtop/breakdown-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading