Skip to content

Commit

Permalink
Add option to toggle open in CWD to Profiles
Browse files Browse the repository at this point in the history
The option is enabled by default on Unix but settable per profile.

Windows is currently unsupported and defaults to the old behavior.
  • Loading branch information
joshuamegnauth54 committed Aug 22, 2024
1 parent 8aa1ce7 commit 3e33977
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 44 deletions.
4 changes: 2 additions & 2 deletions i18n/en/cosmic_term.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ make-default = Make default
working-directory = Working directory
hold = Hold
remain-open = Remain open after child process exits.
open-in-cwd = Use parent CWD
open-in-cwd-description = Open new terminals using the focused tab's working directory.
## Settings
settings = Settings
Expand Down Expand Up @@ -60,8 +62,6 @@ focus-follow-mouse = Typing focus follows mouse
advanced = Advanced
show-headerbar = Show header
show-header-description = Reveal the header from the right-click menu.
open-in-cwd = Use parent CWD
open-in-cwd-description = Start new terms using the focused tab's working directory.
# Find
find-placeholder = Find...
Expand Down
16 changes: 13 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ pub struct Profile {
pub tab_title: String,
#[serde(default)]
pub working_directory: String,
/// Open new terminal with the current working directory of the focused term
#[serde(default = "cwd_default")]
pub open_in_cwd: bool,
#[serde(default)]
pub hold: bool,
}
Expand All @@ -211,11 +214,21 @@ impl Default for Profile {
syntax_theme_light: COSMIC_THEME_LIGHT.to_string(),
tab_title: String::new(),
working_directory: String::new(),
open_in_cwd: true,
hold: true,
}
}
}

#[cfg(not_windows)]
const fn cwd_default() -> bool {
true
}
#[cfg(windows)]
const fn cwd_default() -> bool {
false
}

#[derive(Clone, CosmicConfigEntry, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Config {
pub app_theme: AppTheme,
Expand All @@ -229,8 +242,6 @@ pub struct Config {
pub font_stretch: u16,
pub font_size_zoom_step_mul_100: u16,
pub opacity: u8,
/// Open new terminal with the current working directory of the focused term
pub open_in_cwd: bool,
pub profiles: BTreeMap<ProfileId, Profile>,
pub show_headerbar: bool,
pub use_bright_bold: bool,
Expand All @@ -255,7 +266,6 @@ impl Default for Config {
font_stretch: Stretch::Normal.to_number(),
font_weight: Weight::NORMAL.0,
opacity: 100,
open_in_cwd: true,
profiles: BTreeMap::new(),
show_headerbar: true,
syntax_theme_dark: COSMIC_THEME_DARK.to_string(),
Expand Down
94 changes: 55 additions & 39 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,11 @@ pub enum Message {
ProfileName(ProfileId, String),
ProfileNew,
ProfileOpen(ProfileId),
ProfileOpenInCWD(ProfileId, bool),
ProfileRemove(ProfileId),
ProfileSyntaxTheme(ProfileId, ColorSchemeKind, usize),
ProfileTabTitle(ProfileId, String),
SelectAll(Option<segmented_button::Entity>),
SetOpenInCWD(bool),
ShowAdvancedFontSettings(bool),
ShowHeaderBar(bool),
SyntaxTheme(ColorSchemeKind, usize),
Expand Down Expand Up @@ -477,7 +477,6 @@ impl App {
.sort_by(|a, b| LANGUAGE_SORTER.compare(a, b));
}


fn reset_terminal_panes_zoom(&mut self) {
for (_pane, tab_model) in self.pane_model.panes.iter() {
for entity in tab_model.iter() {
Expand Down Expand Up @@ -534,8 +533,12 @@ impl App {
let mut terminal = terminal.lock().unwrap();
let current_zoom_adj = terminal.zoom_adj();
match zoom_message {
Message::ZoomIn => terminal.set_zoom_adj(current_zoom_adj.saturating_add(1)),
Message::ZoomOut => terminal.set_zoom_adj(current_zoom_adj.saturating_sub(1)),
Message::ZoomIn => {
terminal.set_zoom_adj(current_zoom_adj.saturating_add(1))
}
Message::ZoomOut => {
terminal.set_zoom_adj(current_zoom_adj.saturating_sub(1))
}
_ => {}
}
terminal.set_config(&self.config, &self.themes);
Expand Down Expand Up @@ -989,6 +992,13 @@ impl App {
])
.align_items(Alignment::Center)
.padding([0, space_s]),
)
.add(
widget::settings::item::builder(fl!("open-in-cwd"))
.description(fl!("open-in-cwd-description"))
.toggler(profile.open_in_cwd, move |open_in_cwd| {
Message::ProfileOpenInCWD(profile_id, open_in_cwd)
}),
);

let padding = Padding {
Expand Down Expand Up @@ -1194,17 +1204,11 @@ impl App {
.toggler(self.config.focus_follow_mouse, Message::FocusFollowMouse),
);

let advanced_section = widget::settings::view_section(fl!("advanced"))
.add(
widget::settings::item::builder(fl!("show-headerbar"))
.description(fl!("show-header-description"))
.toggler(self.config.show_headerbar, Message::ShowHeaderBar),
)
.add(
widget::settings::item::builder(fl!("open-in-cwd"))
.description(fl!("open-in-cwd-description"))
.toggler(self.config.open_in_cwd, Message::SetOpenInCWD),
);
let advanced_section = widget::settings::view_section(fl!("advanced")).add(
widget::settings::item::builder(fl!("show-headerbar"))
.description(fl!("show-header-description"))
.toggler(self.config.show_headerbar, Message::ShowHeaderBar),
);

widget::settings::view_column(vec![
appearance_section.into(),
Expand Down Expand Up @@ -1242,22 +1246,6 @@ impl App {
Some(colors) => {
let current_pane = self.pane_model.focus;
if let Some(tab_model) = self.pane_model.active_mut() {
// Current working directory of the selected tab/terminal
#[cfg(not(windows))]
let cwd = self
.config
.open_in_cwd
.then(|| {
tab_model.active_data::<Mutex<Terminal>>().and_then(
|terminal| {
terminal.lock().unwrap().current_working_directory()
},
)
})
.flatten();
#[cfg(windows)]
let cwd: Option<std::path::PathBuf> = None;

// Use the profile options, startup options, or defaults
let (options, tab_title_override) = match profile_id_opt
.and_then(|profile_id| self.config.profiles.get(&profile_id))
Expand All @@ -1270,8 +1258,27 @@ impl App {
shell = Some(tty::Shell::new(command, args));
}
}
let working_directory = cwd

#[cfg(not(windows))]
let working_directory = profile
.open_in_cwd
// Evaluate current working working directory based on
// selected tab/terminal
.then(|| {
tab_model.active_data::<Mutex<Terminal>>().and_then(
|terminal| {
terminal
.lock()
.unwrap()
.current_working_directory()
},
)
})
.flatten()
.or_else(|| Some(profile.working_directory.clone().into()));
#[cfg(windows)]
let working_directory = (!profile.working_directory.is_empty())
.then(|| profile.working_directory.clone().into());

let options = tty::Options {
shell,
Expand All @@ -1289,7 +1296,15 @@ impl App {
None => {
let mut options =
self.startup_options.take().unwrap_or_default();
options.working_directory = cwd;
#[cfg(not(windows))]
{
// Eval CWD since it's the default option
options.working_directory = tab_model
.active_data::<Mutex<Terminal>>()
.and_then(|terminal| {
terminal.lock().unwrap().current_working_directory()
});
}
(options, None)
}
};
Expand Down Expand Up @@ -2158,6 +2173,13 @@ impl Application for App {
Message::ProfileOpen(profile_id) => {
return self.create_and_focus_new_terminal(self.pane_model.focus, Some(profile_id));
}
Message::ProfileOpenInCWD(profile_id, open_in_cwd) => {
#[cfg(not(windows))]
if let Some(profile) = self.config.profiles.get_mut(&profile_id) {
profile.open_in_cwd = open_in_cwd;
return self.save_profiles();
}
}
Message::ProfileRemove(profile_id) => {
// Reset matching terminals to default profile
for (_pane, tab_model) in self.pane_model.panes.iter() {
Expand Down Expand Up @@ -2216,12 +2238,6 @@ impl Application for App {
}
return self.update_focus();
}
Message::SetOpenInCWD(open_in_cwd) => {
if open_in_cwd != self.config.open_in_cwd {
self.config.open_in_cwd = open_in_cwd;
return self.save_config();
}
}
Message::ShowHeaderBar(show_headerbar) => {
if show_headerbar != self.config.show_headerbar {
config_set!(show_headerbar, show_headerbar);
Expand Down

0 comments on commit 3e33977

Please sign in to comment.