Skip to content

Commit 01e605b

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 8aa1ce7 commit 01e605b

File tree

3 files changed

+70
-44
lines changed

3 files changed

+70
-44
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
}
@@ -211,11 +214,21 @@ impl Default for Profile {
211214
syntax_theme_light: COSMIC_THEME_LIGHT.to_string(),
212215
tab_title: String::new(),
213216
working_directory: String::new(),
217+
open_in_cwd: true,
214218
hold: true,
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: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,11 @@ pub enum Message {
331331
ProfileName(ProfileId, String),
332332
ProfileNew,
333333
ProfileOpen(ProfileId),
334+
ProfileOpenInCWD(ProfileId, bool),
334335
ProfileRemove(ProfileId),
335336
ProfileSyntaxTheme(ProfileId, ColorSchemeKind, usize),
336337
ProfileTabTitle(ProfileId, String),
337338
SelectAll(Option<segmented_button::Entity>),
338-
SetOpenInCWD(bool),
339339
ShowAdvancedFontSettings(bool),
340340
ShowHeaderBar(bool),
341341
SyntaxTheme(ColorSchemeKind, usize),
@@ -477,7 +477,6 @@ impl App {
477477
.sort_by(|a, b| LANGUAGE_SORTER.compare(a, b));
478478
}
479479

480-
481480
fn reset_terminal_panes_zoom(&mut self) {
482481
for (_pane, tab_model) in self.pane_model.panes.iter() {
483482
for entity in tab_model.iter() {
@@ -534,8 +533,12 @@ impl App {
534533
let mut terminal = terminal.lock().unwrap();
535534
let current_zoom_adj = terminal.zoom_adj();
536535
match zoom_message {
537-
Message::ZoomIn => terminal.set_zoom_adj(current_zoom_adj.saturating_add(1)),
538-
Message::ZoomOut => terminal.set_zoom_adj(current_zoom_adj.saturating_sub(1)),
536+
Message::ZoomIn => {
537+
terminal.set_zoom_adj(current_zoom_adj.saturating_add(1))
538+
}
539+
Message::ZoomOut => {
540+
terminal.set_zoom_adj(current_zoom_adj.saturating_sub(1))
541+
}
539542
_ => {}
540543
}
541544
terminal.set_config(&self.config, &self.themes);
@@ -989,6 +992,13 @@ impl App {
989992
])
990993
.align_items(Alignment::Center)
991994
.padding([0, space_s]),
995+
)
996+
.add(
997+
widget::settings::item::builder(fl!("open-in-cwd"))
998+
.description(fl!("open-in-cwd-description"))
999+
.toggler(profile.open_in_cwd, move |open_in_cwd| {
1000+
Message::ProfileOpenInCWD(profile_id, open_in_cwd)
1001+
}),
9921002
);
9931003

9941004
let padding = Padding {
@@ -1194,17 +1204,11 @@ impl App {
11941204
.toggler(self.config.focus_follow_mouse, Message::FocusFollowMouse),
11951205
);
11961206

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

12091213
widget::settings::view_column(vec![
12101214
appearance_section.into(),
@@ -1242,22 +1246,6 @@ impl App {
12421246
Some(colors) => {
12431247
let current_pane = self.pane_model.focus;
12441248
if let Some(tab_model) = self.pane_model.active_mut() {
1245-
// Current working directory of the selected tab/terminal
1246-
#[cfg(not(windows))]
1247-
let cwd = self
1248-
.config
1249-
.open_in_cwd
1250-
.then(|| {
1251-
tab_model.active_data::<Mutex<Terminal>>().and_then(
1252-
|terminal| {
1253-
terminal.lock().unwrap().current_working_directory()
1254-
},
1255-
)
1256-
})
1257-
.flatten();
1258-
#[cfg(windows)]
1259-
let cwd: Option<std::path::PathBuf> = None;
1260-
12611249
// Use the profile options, startup options, or defaults
12621250
let (options, tab_title_override) = match profile_id_opt
12631251
.and_then(|profile_id| self.config.profiles.get(&profile_id))
@@ -1270,8 +1258,27 @@ impl App {
12701258
shell = Some(tty::Shell::new(command, args));
12711259
}
12721260
}
1273-
let working_directory = cwd
1261+
1262+
#[cfg(not(windows))]
1263+
let working_directory = profile
1264+
.open_in_cwd
1265+
// Evaluate current working working directory based on
1266+
// selected tab/terminal
1267+
.then(|| {
1268+
tab_model.active_data::<Mutex<Terminal>>().and_then(
1269+
|terminal| {
1270+
terminal
1271+
.lock()
1272+
.unwrap()
1273+
.current_working_directory()
1274+
},
1275+
)
1276+
})
1277+
.flatten()
12741278
.or_else(|| Some(profile.working_directory.clone().into()));
1279+
#[cfg(windows)]
1280+
let working_directory = (!profile.working_directory.is_empty())
1281+
.then(|| profile.working_directory.clone().into());
12751282

12761283
let options = tty::Options {
12771284
shell,
@@ -1289,7 +1296,15 @@ impl App {
12891296
None => {
12901297
let mut options =
12911298
self.startup_options.take().unwrap_or_default();
1292-
options.working_directory = cwd;
1299+
#[cfg(not(windows))]
1300+
{
1301+
// Eval CWD since it's the default option
1302+
options.working_directory = tab_model
1303+
.active_data::<Mutex<Terminal>>()
1304+
.and_then(|terminal| {
1305+
terminal.lock().unwrap().current_working_directory()
1306+
});
1307+
}
12931308
(options, None)
12941309
}
12951310
};
@@ -2158,6 +2173,13 @@ impl Application for App {
21582173
Message::ProfileOpen(profile_id) => {
21592174
return self.create_and_focus_new_terminal(self.pane_model.focus, Some(profile_id));
21602175
}
2176+
Message::ProfileOpenInCWD(profile_id, open_in_cwd) => {
2177+
#[cfg(not(windows))]
2178+
if let Some(profile) = self.config.profiles.get_mut(&profile_id) {
2179+
profile.open_in_cwd = open_in_cwd;
2180+
return self.save_profiles();
2181+
}
2182+
}
21612183
Message::ProfileRemove(profile_id) => {
21622184
// Reset matching terminals to default profile
21632185
for (_pane, tab_model) in self.pane_model.panes.iter() {
@@ -2216,12 +2238,6 @@ impl Application for App {
22162238
}
22172239
return self.update_focus();
22182240
}
2219-
Message::SetOpenInCWD(open_in_cwd) => {
2220-
if open_in_cwd != self.config.open_in_cwd {
2221-
self.config.open_in_cwd = open_in_cwd;
2222-
return self.save_config();
2223-
}
2224-
}
22252241
Message::ShowHeaderBar(show_headerbar) => {
22262242
if show_headerbar != self.config.show_headerbar {
22272243
config_set!(show_headerbar, show_headerbar);

0 commit comments

Comments
 (0)