Skip to content

Commit 35a29cf

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 633195e commit 35a29cf

File tree

5 files changed

+67
-49
lines changed

5 files changed

+67
-49
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ron = "0.8"
2525
serde = { version = "1", features = ["serde_derive"] }
2626
shlex = "1"
2727
tokio = { version = "1", features = ["sync"] }
28-
rustix = { version = "0.38", features = ["termios"] }
28+
rustix = { version = "1", features = ["process", "termios"] }
2929
# CLI arguments
3030
clap_lex = "0.7"
3131
# Internationalization

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: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,12 @@ pub enum Message {
378378
ProfileName(ProfileId, String),
379379
ProfileNew,
380380
ProfileOpen(ProfileId),
381+
ProfileOpenInCWD(ProfileId, bool),
381382
ProfileRemove(ProfileId),
382383
ProfileSyntaxTheme(ProfileId, ColorSchemeKind, usize),
383384
ProfileTabTitle(ProfileId, String),
384385
Surface(surface::Action),
385386
SelectAll(Option<segmented_button::Entity>),
386-
SetOpenInCWD(bool),
387387
ShowAdvancedFontSettings(bool),
388388
ShowHeaderBar(bool),
389389
SyntaxTheme(ColorSchemeKind, usize),
@@ -1042,6 +1042,13 @@ impl App {
10421042
])
10431043
.align_y(Alignment::Center)
10441044
.padding([0, space_s]),
1045+
)
1046+
.add(
1047+
widget::settings::item::builder(fl!("open-in-cwd"))
1048+
.description(fl!("open-in-cwd-description"))
1049+
.toggler(profile.open_in_cwd, move |open_in_cwd| {
1050+
Message::ProfileOpenInCWD(profile_id, open_in_cwd)
1051+
}),
10451052
);
10461053

10471054
let padding = Padding {
@@ -1249,18 +1256,11 @@ impl App {
12491256
.toggler(self.config.focus_follow_mouse, Message::FocusFollowMouse),
12501257
);
12511258

1252-
let advanced_section = widget::settings::section()
1253-
.title(fl!("advanced"))
1254-
.add(
1255-
widget::settings::item::builder(fl!("show-headerbar"))
1256-
.description(fl!("show-header-description"))
1257-
.toggler(self.config.show_headerbar, Message::ShowHeaderBar),
1258-
)
1259-
.add(
1260-
widget::settings::item::builder(fl!("open-in-cwd"))
1261-
.description(fl!("open-in-cwd-description"))
1262-
.toggler(self.config.open_in_cwd, Message::SetOpenInCWD),
1263-
);
1259+
let advanced_section = widget::settings::section().title(fl!("advanced")).add(
1260+
widget::settings::item::builder(fl!("show-headerbar"))
1261+
.description(fl!("show-header-description"))
1262+
.toggler(self.config.show_headerbar, Message::ShowHeaderBar),
1263+
);
12641264

12651265
widget::settings::view_column(vec![
12661266
appearance_section.into(),
@@ -1302,28 +1302,6 @@ impl App {
13021302
let (options, tab_title_override) = match self.startup_options.take() {
13031303
Some(options) => (options, None),
13041304
None => {
1305-
// Current working directory of the selected tab/terminal
1306-
#[cfg(not(windows))]
1307-
let cwd = self
1308-
.config
1309-
.open_in_cwd
1310-
.then(|| {
1311-
tab_model.active_data::<Mutex<Terminal>>().and_then(
1312-
|terminal| {
1313-
terminal
1314-
.lock()
1315-
.unwrap()
1316-
.current_working_directory()
1317-
},
1318-
)
1319-
})
1320-
.flatten();
1321-
// Or default to the profile working directory
1322-
#[cfg(windows)]
1323-
let cwd: Option<
1324-
std::path::PathBuf,
1325-
> = None;
1326-
13271305
match profile_id_opt.and_then(|profile_id| {
13281306
self.config.profiles.get(&profile_id)
13291307
}) {
@@ -1335,6 +1313,25 @@ impl App {
13351313
shell = Some(tty::Shell::new(command, args));
13361314
}
13371315
}
1316+
// Current working directory of the selected tab/terminal
1317+
#[cfg(not(windows))]
1318+
let cwd = profile
1319+
.open_in_cwd
1320+
.then(|| {
1321+
tab_model
1322+
.active_data::<Mutex<Terminal>>()
1323+
.and_then(|terminal| {
1324+
terminal
1325+
.lock()
1326+
.unwrap()
1327+
.current_working_directory()
1328+
})
1329+
})
1330+
.flatten();
1331+
// Or default to the profile working directory
1332+
#[cfg(windows)]
1333+
let cwd: Option<std::path::PathBuf> = None;
1334+
13381335
let working_directory = cwd.or_else(|| {
13391336
Some(profile.working_directory.clone().into())
13401337
});
@@ -1355,7 +1352,17 @@ impl App {
13551352
None => {
13561353
let mut options =
13571354
self.startup_options.take().unwrap_or_default();
1358-
options.working_directory = cwd;
1355+
#[cfg(not(windows))]
1356+
{
1357+
options.working_directory = tab_model
1358+
.active_data::<Mutex<Terminal>>()
1359+
.and_then(|terminal| {
1360+
terminal
1361+
.lock()
1362+
.unwrap()
1363+
.current_working_directory()
1364+
});
1365+
}
13591366
(options, None)
13601367
}
13611368
}
@@ -2253,6 +2260,13 @@ impl Application for App {
22532260
return self
22542261
.create_and_focus_new_terminal(self.pane_model.focused(), Some(profile_id));
22552262
}
2263+
Message::ProfileOpenInCWD(profile_id, open_in_cwd) => {
2264+
#[cfg(not(windows))]
2265+
if let Some(profile) = self.config.profiles.get_mut(&profile_id) {
2266+
profile.open_in_cwd = open_in_cwd;
2267+
return self.save_profiles();
2268+
}
2269+
}
22562270
Message::ProfileRemove(profile_id) => {
22572271
// Reset matching terminals to default profile
22582272
for (_pane, tab_model) in self.pane_model.panes.iter() {
@@ -2311,12 +2325,6 @@ impl Application for App {
23112325
}
23122326
return self.update_focus();
23132327
}
2314-
Message::SetOpenInCWD(open_in_cwd) => {
2315-
if open_in_cwd != self.config.open_in_cwd {
2316-
self.config.open_in_cwd = open_in_cwd;
2317-
return self.update_config();
2318-
}
2319-
}
23202328
Message::ShowHeaderBar(show_headerbar) => {
23212329
if show_headerbar != self.config.show_headerbar {
23222330
config_set!(show_headerbar, show_headerbar);

0 commit comments

Comments
 (0)