Skip to content

Commit ef82213

Browse files
committed
feat(chat): add /changedir (/cd) slash command to change workdir mid-session
Allows changing the working directory without restarting the session: /changedir /path/to/project /cd ~/project/wezterm /cd (goes to $HOME) After changing directory, the user is prompted to run /code init to reinitialize code intelligence for the new directory.
1 parent 25f7c8a commit ef82213

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use std::path::PathBuf;
2+
3+
use clap::Args;
4+
use crossterm::{
5+
execute,
6+
style,
7+
};
8+
9+
use crate::cli::chat::{
10+
ChatError,
11+
ChatSession,
12+
ChatState,
13+
};
14+
use crate::theme::StyledText;
15+
16+
#[derive(Debug, PartialEq, Args)]
17+
/// Arguments for the changedir command.
18+
pub struct ChangedirArgs {
19+
/// The directory to switch to. Defaults to $HOME if not provided.
20+
pub path: Option<PathBuf>,
21+
}
22+
23+
impl ChangedirArgs {
24+
pub async fn execute(self, session: &mut ChatSession) -> Result<ChatState, ChatError> {
25+
let target = match self.path {
26+
Some(p) => p,
27+
None => dirs::home_dir().ok_or_else(|| ChatError::Custom("Could not determine home directory".into()))?,
28+
};
29+
30+
let target = if target.is_relative() {
31+
std::env::current_dir()
32+
.map_err(|e| ChatError::Custom(e.to_string().into()))?
33+
.join(&target)
34+
} else {
35+
target
36+
};
37+
38+
std::env::set_current_dir(&target)
39+
.map_err(|e| ChatError::Custom(format!("Failed to change directory: {e}").into()))?;
40+
41+
execute!(
42+
session.stderr,
43+
StyledText::success_fg(),
44+
style::Print(format!("Working directory changed to: {}\n", target.display())),
45+
style::Print("Run /code init to reinitialize code intelligence for this directory.\n"),
46+
StyledText::reset(),
47+
)?;
48+
49+
Ok(ChatState::PromptUser {
50+
skip_printing_tools: true,
51+
})
52+
}
53+
}

crates/chat-cli/src/cli/chat/cli/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::theme::StyledText;
22
pub mod changelog;
3+
pub mod changedir;
34
pub mod checkpoint;
45
pub mod clear;
56
pub mod compact;
@@ -23,6 +24,7 @@ pub mod tools;
2324
pub mod usage;
2425

2526
use changelog::ChangelogArgs;
27+
use changedir::ChangedirArgs;
2628
use clap::Parser;
2729
use clear::ClearArgs;
2830
use compact::CompactArgs;
@@ -65,6 +67,9 @@ pub enum SlashCommand {
6567
Quit,
6668
/// Clear the conversation history
6769
Clear(ClearArgs),
70+
/// Change the working directory for this session
71+
#[command(name = "changedir", alias = "cd")]
72+
Changedir(ChangedirArgs),
6873
/// Manage agents
6974
#[command(subcommand)]
7075
Agent(AgentSubcommand),
@@ -134,6 +139,7 @@ impl SlashCommand {
134139
match self {
135140
Self::Quit => Ok(ChatState::Exit),
136141
Self::Clear(args) => args.execute(session).await,
142+
Self::Changedir(args) => args.execute(session).await,
137143
Self::Agent(subcommand) => subcommand.execute(os, session).await,
138144
Self::Profile => {
139145
use crossterm::{
@@ -203,6 +209,7 @@ impl SlashCommand {
203209
match self {
204210
Self::Quit => "quit",
205211
Self::Clear(_) => "clear",
212+
Self::Changedir(_) => "changedir",
206213
Self::Agent(_) => "agent",
207214
Self::Profile => "profile",
208215
Self::Context(_) => "context",

0 commit comments

Comments
 (0)