Skip to content

Commit f6f7ea5

Browse files
committed
fix(cortex-cli): add --cwd option to sessions command
Fixes bounty issue #1661 The sessions command now supports a --cwd option to filter sessions by a specific directory. Previously, users could only see sessions from their current working directory or use --all to see all sessions. Now they can filter sessions by any directory without changing their current location. Usage: cortex sessions --cwd /path/to/project
1 parent 5579873 commit f6f7ea5

1 file changed

Lines changed: 34 additions & 11 deletions

File tree

cortex-cli/src/main.rs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,10 @@ struct SessionsCommand {
304304
/// Show all sessions including from other directories
305305
#[arg(long)]
306306
all: bool,
307+
308+
/// Filter sessions by a specific directory
309+
#[arg(long)]
310+
cwd: Option<PathBuf>,
307311
}
308312

309313
/// Config command.
@@ -478,7 +482,9 @@ async fn main() -> Result<()> {
478482
}
479483
},
480484
Some(Commands::Resume(resume_cli)) => run_resume(resume_cli).await,
481-
Some(Commands::Sessions(sessions_cli)) => list_sessions(sessions_cli.all).await,
485+
Some(Commands::Sessions(sessions_cli)) => {
486+
list_sessions(sessions_cli.all, sessions_cli.cwd).await
487+
}
482488
Some(Commands::Export(export_cli)) => export_cli.run().await,
483489
Some(Commands::Import(import_cli)) => import_cli.run().await,
484490
Some(Commands::Config(config_cli)) => show_config(config_cli).await,
@@ -676,7 +682,7 @@ async fn run_resume(resume_cli: ResumeCommand) -> Result<()> {
676682
Ok(())
677683
}
678684

679-
async fn list_sessions(show_all: bool) -> Result<()> {
685+
async fn list_sessions(show_all: bool, filter_cwd: Option<PathBuf>) -> Result<()> {
680686
let config = cortex_engine::Config::default();
681687
let sessions = cortex_engine::list_sessions(&config.cortex_home)?;
682688

@@ -689,14 +695,23 @@ async fn list_sessions(show_all: bool) -> Result<()> {
689695
return Ok(());
690696
}
691697

692-
let current_dir = std::env::current_dir().ok();
693-
let filtered: Vec<_> = if show_all {
694-
sessions.iter().collect()
698+
// Determine which directory to filter by:
699+
// - If --cwd is provided, use that directory
700+
// - If --all is provided, show all sessions
701+
// - Otherwise, use the current working directory
702+
let filter_dir = if show_all {
703+
None
704+
} else if let Some(ref cwd) = filter_cwd {
705+
// Canonicalize the provided path for consistent comparison
706+
Some(cwd.canonicalize().unwrap_or_else(|_| cwd.clone()))
695707
} else {
696-
sessions
697-
.iter()
698-
.filter(|s| current_dir.as_ref().is_none_or(|cwd| s.cwd == *cwd))
699-
.collect()
708+
std::env::current_dir().ok()
709+
};
710+
711+
let filtered: Vec<_> = if let Some(ref dir) = filter_dir {
712+
sessions.iter().filter(|s| s.cwd == *dir).collect()
713+
} else {
714+
sessions.iter().collect()
700715
};
701716

702717
let display_sessions = if filtered.is_empty() {
@@ -705,7 +720,14 @@ async fn list_sessions(show_all: bool) -> Result<()> {
705720
&filtered.iter().map(|s| (*s).clone()).collect()
706721
};
707722

708-
println!("Recent Sessions{}:", if show_all { " (all)" } else { "" });
723+
let header_suffix = if show_all {
724+
" (all)".to_string()
725+
} else if let Some(ref dir) = filter_dir {
726+
format!(" ({})", dir.display())
727+
} else {
728+
String::new()
729+
};
730+
println!("Recent Sessions{}:", header_suffix);
709731
println!("{:-<80}", "");
710732

711733
for session in display_sessions.iter().take(15) {
@@ -738,8 +760,9 @@ async fn list_sessions(show_all: bool) -> Result<()> {
738760

739761
println!("\nTo resume: Cortex resume <session-id>");
740762
println!(" Cortex resume --last");
741-
if !show_all {
763+
if !show_all && filter_cwd.is_none() {
742764
println!(" Cortex sessions --all (show all directories)");
765+
println!(" Cortex sessions --cwd <dir> (filter by directory)");
743766
}
744767
Ok(())
745768
}

0 commit comments

Comments
 (0)