Skip to content

Commit f748999

Browse files
committed
feat(tui): add -s/--session flag and show restore hint on exit (#19, #20)
- Add -s/--session CLI flag to restore a session by id, skipping the start page and loading directly into the chat screen - Print restore command to stderr on exit when a session is active - Derive binary name from clap instead of hardcoding closes #19, #20
1 parent f1858ee commit f748999

1 file changed

Lines changed: 56 additions & 1 deletion

File tree

src/main.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,11 +535,15 @@ mod external_editor_tests {
535535
}
536536

537537
#[derive(Parser)]
538-
#[command(name = "qmt-tui", about = "querymt terminal interface")]
538+
#[command(about = "querymt terminal interface")]
539539
struct Cli {
540540
/// Server address (e.g. 127.0.0.1:3030). Overrides the value in ~/.qmt/tui.toml.
541541
#[arg(long)]
542542
server: Option<String>,
543+
544+
/// Restore a session by id.
545+
#[arg(short = 's', long)]
546+
session: Option<String>,
543547
}
544548

545549
fn detect_launch_cwd() -> Option<String> {
@@ -704,6 +708,10 @@ async fn main() -> anyhow::Result<()> {
704708
let mut app = App::new();
705709
app.launch_cwd = detect_launch_cwd();
706710
app.show_thinking = cfg.show_thinking.unwrap_or(true);
711+
if let Some(session_id) = cli.session {
712+
app.session_id = Some(session_id);
713+
app.screen = Screen::Chat;
714+
}
707715
// Hydrate session effort cache from disk.
708716
config::TuiCache::load().hydrate_app(&mut app);
709717
let result = run_loop(&mut terminal, &mut app, &mut srv_rx, &mut conn_rx, &cmd_tx).await;
@@ -713,9 +721,19 @@ async fn main() -> anyhow::Result<()> {
713721
execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
714722
terminal.show_cursor()?;
715723

724+
if let Some(session_id) = &app.session_id {
725+
eprintln!("{}", restore_hint(session_id));
726+
}
727+
716728
result
717729
}
718730

731+
fn restore_hint(session_id: &str) -> String {
732+
use clap::CommandFactory;
733+
let bin = Cli::command().get_name().to_string();
734+
format!("{bin} -s {session_id}")
735+
}
736+
719737
async fn connection_manager(
720738
url: String,
721739
srv_tx: mpsc::UnboundedSender<RawServerMsg>,
@@ -2934,3 +2952,40 @@ mod reasoning_effort_integration_tests {
29342952
assert_eq!(cms.effort, None);
29352953
}
29362954
}
2955+
2956+
#[cfg(test)]
2957+
mod cli_tests {
2958+
use super::*;
2959+
use clap::{CommandFactory, Parser};
2960+
2961+
fn bin() -> String {
2962+
Cli::command().get_name().to_string()
2963+
}
2964+
2965+
#[test]
2966+
fn cli_session_short_flag() {
2967+
let b = bin();
2968+
let cli = Cli::try_parse_from([b.as_str(), "-s", "abc123"]).unwrap();
2969+
assert_eq!(cli.session, Some("abc123".into()));
2970+
}
2971+
2972+
#[test]
2973+
fn cli_session_long_flag() {
2974+
let b = bin();
2975+
let cli = Cli::try_parse_from([b.as_str(), "--session", "abc123"]).unwrap();
2976+
assert_eq!(cli.session, Some("abc123".into()));
2977+
}
2978+
2979+
#[test]
2980+
fn cli_no_session_defaults_to_none() {
2981+
let b = bin();
2982+
let cli = Cli::try_parse_from([b.as_str()]).unwrap();
2983+
assert_eq!(cli.session, None);
2984+
}
2985+
2986+
#[test]
2987+
fn restore_hint_formats_correctly() {
2988+
let hint = restore_hint("abc-123-def");
2989+
assert_eq!(hint, format!("{} -s abc-123-def", bin()));
2990+
}
2991+
}

0 commit comments

Comments
 (0)