Skip to content
This repository was archived by the owner on Jul 30, 2026. It is now read-only.
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
7 changes: 6 additions & 1 deletion crates/tokscale-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ pub(crate) enum Commands {
pub(crate) struct TuiArgs {
#[arg(long, value_enum, help = "Open a specific tab")]
pub(crate) tab: Option<Tab>,
#[arg(short, long, value_parser = parse_theme_arg)]
#[arg(
short,
long,
value_parser = parse_theme_arg,
help = "Use a complete TUI semantic color theme"
)]
pub(crate) theme: Option<String>,
#[arg(
short,
Expand Down
77 changes: 55 additions & 22 deletions crates/tokscale-cli/src/tui/actions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

use super::app::{App, HourlyViewMode, SortField, Tab};
use super::presentation::Presentation;
use super::presentation::{Presentation, SubscriptionPresentation};
use super::view_state::ViewState;

/// A capability exposed by the current TUI view.
Expand All @@ -17,7 +17,6 @@ pub(crate) enum Action {
Sort(SortField),
OpenDetails,
Back,
JumpToday,
ToggleView,
Clients,
GroupBy,
Expand All @@ -41,9 +40,11 @@ pub(crate) struct ActionSet {

impl ActionSet {
pub(crate) fn for_view(app: &App, state: &ViewState, presentation: Presentation) -> Self {
if app.current_tab == Tab::Usage {
return Self::for_usage(app);
if let Presentation::Subscription(subscription) = presentation {
debug_assert_eq!(app.current_tab, Tab::Usage);
return Self::for_usage(app, subscription);
}
debug_assert_ne!(app.current_tab, Tab::Usage);

let installed = app.has_installed_generation();
let empty = presentation.is_empty();
Expand Down Expand Up @@ -146,7 +147,6 @@ impl ActionSet {
KeyCode::Char('c') => Some(Action::Sort(SortField::Cost)),
KeyCode::Enter => Some(Action::OpenDetails),
KeyCode::Esc | KeyCode::Backspace => Some(Action::Back),
KeyCode::Char('j') => Some(Action::JumpToday),
KeyCode::Char('h') if app.current_tab == Tab::Overview => Some(Action::ToggleView),
KeyCode::Char('v') if matches!(app.current_tab, Tab::Daily | Tab::Hourly) => {
Some(Action::ToggleView)
Expand All @@ -168,22 +168,15 @@ impl ActionSet {
}
}

fn for_usage(app: &App) -> Self {
let mut actions = vec![Action::Scroll, Action::PreviousTab, Action::NextTab];
actions.extend([Action::Theme, Action::ToggleAutoRefresh]);
actions.extend([
Action::IncreaseRefreshInterval,
Action::DecreaseRefreshInterval,
]);
if !app.background_loading {
actions.push(Action::RefreshLocal);
fn for_usage(app: &App, presentation: SubscriptionPresentation) -> Self {
let mut actions = Vec::new();
if matches!(presentation, SubscriptionPresentation::Results { .. }) {
actions.push(Action::Scroll);
}
if app.has_enabled_subscription_providers() {
actions.extend([Action::PreviousTab, Action::NextTab, Action::Theme]);
if app.has_enabled_subscription_providers() && !presentation.is_refreshing() {
actions.push(Action::RefreshSubscription);
}
if app.has_installed_generation() {
actions.push(Action::Export);
}
actions.push(Action::Quit);
Self {
actions,
Expand Down Expand Up @@ -231,7 +224,7 @@ fn view_actions(app: &App, state: &ViewState) -> Vec<Action> {
Tab::Monthly | Tab::Weekly => vec![Action::OpenDetails],
Tab::Daily if app.is_daily_detail_active() => vec![Action::Back],
Tab::Daily if state.daily_profile_active() => vec![Action::ToggleView],
Tab::Daily => vec![Action::OpenDetails, Action::JumpToday, Action::ToggleView],
Tab::Daily => vec![Action::OpenDetails, Action::ToggleView],
Tab::Hourly => vec![Action::ToggleView],
Tab::Sessions if state.session_detail_active() => vec![Action::Back],
Tab::Sessions => state
Expand All @@ -257,8 +250,8 @@ mod tests {
use super::*;
use crate::tui::app::{ProjectionBackend, SortDirection, TuiConfig};
use crate::tui::data::{
ContributionDay, DailyClientInfo, DailyModelInfo, DailyUsage, GraphData, HourlyModelInfo,
HourlyUsage, TokenBreakdown,
ContributionDay, ContributionGrade, DailyClientInfo, DailyModelInfo, DailyUsage, GraphData,
HourlyModelInfo, HourlyUsage, TokenBreakdown,
};
use crate::tui::session_data::SessionSnapshot;
use crate::tui::settings::Settings;
Expand Down Expand Up @@ -433,6 +426,42 @@ mod tests {
assert!(!set.contains(Action::GroupBy));
}

#[test]
fn usage_actions_are_subscription_or_shell_scoped() {
let mut app = make_app(Tab::Usage, true);
app.set_subscription_provider_ids_for_test(vec![
crate::tui::subscription_usage::UsageProviderId::Codex,
]);
app.subscription_usage_errors = vec![crate::tui::subscription_usage::UsageProviderError {
provider: "Claude".to_string(),
message: "credential expired".to_string(),
}];

let set = action_set(&app, &ViewState::default());

for action in [
Action::Scroll,
Action::PreviousTab,
Action::NextTab,
Action::Theme,
Action::RefreshSubscription,
Action::Quit,
] {
assert!(set.contains(action), "missing {action:?}");
}
for action in [
Action::RefreshLocal,
Action::ToggleAutoRefresh,
Action::IncreaseRefreshInterval,
Action::DecreaseRefreshInterval,
Action::Export,
Action::Clients,
Action::GroupBy,
] {
assert!(!set.contains(action), "unexpected {action:?}");
}
}

#[test]
fn populated_daily_and_stats_expose_only_their_real_actions() {
let mut daily = make_app(Tab::Daily, true);
Expand All @@ -449,7 +478,7 @@ mod tests {
date: NaiveDate::from_ymd_opt(2026, 7, 22).unwrap(),
tokens: 0,
cost: 0.0,
intensity: 0.0,
grade: ContributionGrade::Empty,
})]],
};
daily.current_tab = Tab::Stats;
Expand Down Expand Up @@ -580,5 +609,9 @@ mod tests {
ActionSet::action_for_key(&overview, &key(KeyCode::Char('v'))),
None
);
assert_eq!(
ActionSet::action_for_key(&daily, &key(KeyCode::Char('j'))),
None
);
}
}
Loading
Loading