Skip to content

Commit 6082f3b

Browse files
Add option to toggle open in CWD to Profiles
The option is enabled by default on Unix but settable per profile. Windows is currently unsupported and defaults to the old behavior.
1 parent dc000a2 commit 6082f3b

File tree

3 files changed

+64
-41
lines changed

3 files changed

+64
-41
lines changed

i18n/en/cosmic_term.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ make-default = Make default
2626
working-directory = Working directory
2727
hold = Hold
2828
remain-open = Remain open after child process exits.
29+
open-in-cwd = Use parent CWD
30+
open-in-cwd-description = Open new terminals using the focused tab's working directory.
2931
3032
## Settings
3133
settings = Settings
@@ -60,8 +62,6 @@ focus-follow-mouse = Typing focus follows mouse
6062
advanced = Advanced
6163
show-headerbar = Show header
6264
show-header-description = Reveal the header from the right-click menu.
63-
open-in-cwd = Use parent CWD
64-
open-in-cwd-description = Start new terms using the focused tab's working directory.
6565
6666
# Find
6767
find-placeholder = Find...

src/config.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,9 @@ pub struct Profile {
198198
pub tab_title: String,
199199
#[serde(default)]
200200
pub working_directory: String,
201+
/// Open new terminal with the current working directory of the focused term
202+
#[serde(default = "cwd_default")]
203+
pub open_in_cwd: bool,
201204
#[serde(default)]
202205
pub hold: bool,
203206
}
@@ -212,10 +215,20 @@ impl Default for Profile {
212215
tab_title: String::new(),
213216
working_directory: String::new(),
214217
hold: false,
218+
open_in_cwd: cwd_default(),
215219
}
216220
}
217221
}
218222

223+
#[cfg(not(windows))]
224+
const fn cwd_default() -> bool {
225+
true
226+
}
227+
#[cfg(windows)]
228+
const fn cwd_default() -> bool {
229+
false
230+
}
231+
219232
#[derive(Clone, CosmicConfigEntry, Debug, Deserialize, Eq, PartialEq, Serialize)]
220233
pub struct Config {
221234
pub app_theme: AppTheme,
@@ -229,8 +242,6 @@ pub struct Config {
229242
pub font_stretch: u16,
230243
pub font_size_zoom_step_mul_100: u16,
231244
pub opacity: u8,
232-
/// Open new terminal with the current working directory of the focused term
233-
pub open_in_cwd: bool,
234245
pub profiles: BTreeMap<ProfileId, Profile>,
235246
pub show_headerbar: bool,
236247
pub use_bright_bold: bool,
@@ -255,7 +266,6 @@ impl Default for Config {
255266
font_stretch: Stretch::Normal.to_number(),
256267
font_weight: Weight::NORMAL.0,
257268
opacity: 100,
258-
open_in_cwd: true,
259269
profiles: BTreeMap::new(),
260270
show_headerbar: true,
261271
syntax_theme_dark: COSMIC_THEME_DARK.to_string(),

src/main.rs

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,11 @@ pub enum Message {
334334
ProfileName(ProfileId, String),
335335
ProfileNew,
336336
ProfileOpen(ProfileId),
337+
ProfileOpenInCWD(ProfileId, bool),
337338
ProfileRemove(ProfileId),
338339
ProfileSyntaxTheme(ProfileId, ColorSchemeKind, usize),
339340
ProfileTabTitle(ProfileId, String),
340341
SelectAll(Option<segmented_button::Entity>),
341-
SetOpenInCWD(bool),
342342
ShowAdvancedFontSettings(bool),
343343
ShowHeaderBar(bool),
344344
SyntaxTheme(ColorSchemeKind, usize),
@@ -995,6 +995,13 @@ impl App {
995995
])
996996
.align_items(Alignment::Center)
997997
.padding([0, space_s]),
998+
)
999+
.add(
1000+
widget::settings::item::builder(fl!("open-in-cwd"))
1001+
.description(fl!("open-in-cwd-description"))
1002+
.toggler(profile.open_in_cwd, move |open_in_cwd| {
1003+
Message::ProfileOpenInCWD(profile_id, open_in_cwd)
1004+
}),
9981005
);
9991006

10001007
let padding = Padding {
@@ -1200,17 +1207,11 @@ impl App {
12001207
.toggler(self.config.focus_follow_mouse, Message::FocusFollowMouse),
12011208
);
12021209

1203-
let advanced_section = widget::settings::view_section(fl!("advanced"))
1204-
.add(
1205-
widget::settings::item::builder(fl!("show-headerbar"))
1206-
.description(fl!("show-header-description"))
1207-
.toggler(self.config.show_headerbar, Message::ShowHeaderBar),
1208-
)
1209-
.add(
1210-
widget::settings::item::builder(fl!("open-in-cwd"))
1211-
.description(fl!("open-in-cwd-description"))
1212-
.toggler(self.config.open_in_cwd, Message::SetOpenInCWD),
1213-
);
1210+
let advanced_section = widget::settings::view_section(fl!("advanced")).add(
1211+
widget::settings::item::builder(fl!("show-headerbar"))
1212+
.description(fl!("show-header-description"))
1213+
.toggler(self.config.show_headerbar, Message::ShowHeaderBar),
1214+
);
12141215

12151216
widget::settings::view_column(vec![
12161217
appearance_section.into(),
@@ -1248,22 +1249,6 @@ impl App {
12481249
Some(colors) => {
12491250
let current_pane = self.pane_model.focus;
12501251
if let Some(tab_model) = self.pane_model.active_mut() {
1251-
// Current working directory of the selected tab/terminal
1252-
#[cfg(not(windows))]
1253-
let cwd = self
1254-
.config
1255-
.open_in_cwd
1256-
.then(|| {
1257-
tab_model.active_data::<Mutex<Terminal>>().and_then(
1258-
|terminal| {
1259-
terminal.lock().unwrap().current_working_directory()
1260-
},
1261-
)
1262-
})
1263-
.flatten();
1264-
#[cfg(windows)]
1265-
let cwd: Option<std::path::PathBuf> = None;
1266-
12671252
// Use the profile options, startup options, or defaults
12681253
let (options, tab_title_override) = match profile_id_opt
12691254
.and_then(|profile_id| self.config.profiles.get(&profile_id))
@@ -1276,8 +1261,27 @@ impl App {
12761261
shell = Some(tty::Shell::new(command, args));
12771262
}
12781263
}
1279-
let working_directory = cwd
1264+
1265+
#[cfg(not(windows))]
1266+
let working_directory = profile
1267+
.open_in_cwd
1268+
// Evaluate current working working directory based on
1269+
// selected tab/terminal
1270+
.then(|| {
1271+
tab_model.active_data::<Mutex<Terminal>>().and_then(
1272+
|terminal| {
1273+
terminal
1274+
.lock()
1275+
.unwrap()
1276+
.current_working_directory()
1277+
},
1278+
)
1279+
})
1280+
.flatten()
12801281
.or_else(|| Some(profile.working_directory.clone().into()));
1282+
#[cfg(windows)]
1283+
let working_directory = (!profile.working_directory.is_empty())
1284+
.then(|| profile.working_directory.clone().into());
12811285

12821286
let options = tty::Options {
12831287
shell,
@@ -1295,7 +1299,15 @@ impl App {
12951299
None => {
12961300
let mut options =
12971301
self.startup_options.take().unwrap_or_default();
1298-
options.working_directory = cwd;
1302+
#[cfg(not(windows))]
1303+
{
1304+
// Eval CWD since it's the default option
1305+
options.working_directory = tab_model
1306+
.active_data::<Mutex<Terminal>>()
1307+
.and_then(|terminal| {
1308+
terminal.lock().unwrap().current_working_directory()
1309+
});
1310+
}
12991311
(options, None)
13001312
}
13011313
};
@@ -2181,6 +2193,13 @@ impl Application for App {
21812193
Message::ProfileOpen(profile_id) => {
21822194
return self.create_and_focus_new_terminal(self.pane_model.focus, Some(profile_id));
21832195
}
2196+
Message::ProfileOpenInCWD(profile_id, open_in_cwd) => {
2197+
#[cfg(not(windows))]
2198+
if let Some(profile) = self.config.profiles.get_mut(&profile_id) {
2199+
profile.open_in_cwd = open_in_cwd;
2200+
return self.save_profiles();
2201+
}
2202+
}
21842203
Message::ProfileRemove(profile_id) => {
21852204
// Reset matching terminals to default profile
21862205
for (_pane, tab_model) in self.pane_model.panes.iter() {
@@ -2239,12 +2258,6 @@ impl Application for App {
22392258
}
22402259
return self.update_focus();
22412260
}
2242-
Message::SetOpenInCWD(open_in_cwd) => {
2243-
if open_in_cwd != self.config.open_in_cwd {
2244-
self.config.open_in_cwd = open_in_cwd;
2245-
return self.update_config();
2246-
}
2247-
}
22482261
Message::ShowHeaderBar(show_headerbar) => {
22492262
if show_headerbar != self.config.show_headerbar {
22502263
config_set!(show_headerbar, show_headerbar);

0 commit comments

Comments
 (0)