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
  • Loading branch information
joshuamegnauth54 committed Aug 6, 2024
1 parent c261fb2 commit 7a8f93a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 39 deletions.
11 changes: 8 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,16 @@ 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,
}
}
}

const fn cwd_default() -> bool {
true
}

#[derive(Clone, CosmicConfigEntry, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Config {
pub app_theme: AppTheme,
Expand All @@ -229,8 +237,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 +261,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
81 changes: 45 additions & 36 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,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 @@ -956,6 +956,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 @@ -1161,17 +1168,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 @@ -1209,22 +1210,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 @@ -1237,8 +1222,25 @@ impl App {
shell = Some(tty::Shell::new(command, args));
}
}
let working_directory = cwd

#[cfg(not(windows))]
let working_directory = profile
.open_in_cwd
.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 @@ -1256,7 +1258,14 @@ impl App {
None => {
let mut options =
self.startup_options.take().unwrap_or_default();
options.working_directory = cwd;
#[cfg(not(windows))]
{
options.working_directory = tab_model
.active_data::<Mutex<Terminal>>()
.and_then(|terminal| {
terminal.lock().unwrap().current_working_directory()
});
}
(options, None)
}
};
Expand Down Expand Up @@ -2095,6 +2104,12 @@ 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) => {
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 @@ -2153,12 +2168,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 {
self.config.show_headerbar = show_headerbar;
Expand Down

0 comments on commit 7a8f93a

Please sign in to comment.