Merge pull request #662 from adil192/feat/add-working-dir-arg
feat: add `--working-directory` arg
This commit is contained in:
commit
b9062e47d5
1 changed files with 48 additions and 37 deletions
85
src/main.rs
85
src/main.rs
|
|
@ -37,7 +37,9 @@ use std::{
|
||||||
collections::{BTreeMap, BTreeSet, HashMap},
|
collections::{BTreeMap, BTreeSet, HashMap},
|
||||||
env,
|
env,
|
||||||
error::Error,
|
error::Error,
|
||||||
fs, process,
|
fs,
|
||||||
|
path::PathBuf,
|
||||||
|
process,
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
sync::{LazyLock, Mutex, atomic::Ordering},
|
sync::{LazyLock, Mutex, atomic::Ordering},
|
||||||
};
|
};
|
||||||
|
|
@ -96,6 +98,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let mut shell_program_opt = None;
|
let mut shell_program_opt = None;
|
||||||
let mut shell_args = Vec::new();
|
let mut shell_args = Vec::new();
|
||||||
let mut daemonize = true;
|
let mut daemonize = true;
|
||||||
|
let mut working_directory = None;
|
||||||
// Parse the arguments using clap_lex
|
// Parse the arguments using clap_lex
|
||||||
while let Some(arg) = raw_args.next_os(&mut cursor) {
|
while let Some(arg) = raw_args.next_os(&mut cursor) {
|
||||||
match arg.to_str() {
|
match arg.to_str() {
|
||||||
|
|
@ -110,6 +113,14 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
);
|
);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
Some(arg_str @ "--working-directory") | Some(arg_str @ "-w") => {
|
||||||
|
if let Some(dir_arg) = raw_args.next_os(&mut cursor) {
|
||||||
|
working_directory = Some(PathBuf::from(dir_arg));
|
||||||
|
} else {
|
||||||
|
eprintln!("Missing argument for {arg_str}");
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
Some("--no-daemon") => {
|
Some("--no-daemon") => {
|
||||||
daemonize = false;
|
daemonize = false;
|
||||||
}
|
}
|
||||||
|
|
@ -167,15 +178,16 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
let shortcuts_config = shortcuts::ShortcutsConfig::new(config.shortcuts_custom.clone());
|
let shortcuts_config = shortcuts::ShortcutsConfig::new(config.shortcuts_custom.clone());
|
||||||
|
|
||||||
let startup_options = if let Some(shell_program) = shell_program_opt {
|
let shell = if let Some(shell_program) = shell_program_opt {
|
||||||
let options = tty::Options {
|
Some(tty::Shell::new(shell_program, shell_args))
|
||||||
shell: Some(tty::Shell::new(shell_program, shell_args)),
|
|
||||||
..tty::Options::default()
|
|
||||||
};
|
|
||||||
Some(options)
|
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
let startup_options = Some(tty::Options {
|
||||||
|
shell,
|
||||||
|
working_directory,
|
||||||
|
..tty::Options::default()
|
||||||
|
});
|
||||||
|
|
||||||
// Terminal config setup
|
// Terminal config setup
|
||||||
let term_config = term::Config::default();
|
let term_config = term::Config::default();
|
||||||
|
|
@ -213,8 +225,9 @@ Designed for the COSMIC™ desktop environment, cosmic-term is a libcosmic-based
|
||||||
|
|
||||||
Project home page: https://github.com/pop-os/cosmic-term
|
Project home page: https://github.com/pop-os/cosmic-term
|
||||||
Options:
|
Options:
|
||||||
--help Show this message
|
--help Show this message
|
||||||
--version Show the version of cosmic-term"#
|
--version Show the version of cosmic-term
|
||||||
|
-w, --working-directory <dir> Set the working directory for the terminal"#
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1513,40 +1526,38 @@ impl App {
|
||||||
Some(colors) => {
|
Some(colors) => {
|
||||||
let current_pane = self.pane_model.focused();
|
let current_pane = self.pane_model.focused();
|
||||||
if let Some(tab_model) = self.pane_model.active_mut() {
|
if let Some(tab_model) = self.pane_model.active_mut() {
|
||||||
// Use the startup options, profile options, or defaults
|
let (options, tab_title_override) = if let Some(profile) = profile_id_opt
|
||||||
let (options, tab_title_override) = match self.startup_options.take() {
|
.and_then(|profile_id| self.config.profiles.get(&profile_id))
|
||||||
Some(options) => (options, None),
|
{
|
||||||
None => match profile_id_opt
|
// Merge profile and startup options, preferring startup options
|
||||||
.and_then(|profile_id| self.config.profiles.get(&profile_id))
|
let startup_options = self.startup_options.take().unwrap_or_default();
|
||||||
{
|
let options = tty::Options {
|
||||||
Some(profile) => {
|
shell: startup_options.shell.or_else(|| {
|
||||||
let mut shell = None;
|
|
||||||
if let Some(mut args) = shlex::split(&profile.command) {
|
if let Some(mut args) = shlex::split(&profile.command) {
|
||||||
if !args.is_empty() {
|
if !args.is_empty() {
|
||||||
let command = args.remove(0);
|
let command = args.remove(0);
|
||||||
shell = Some(tty::Shell::new(command, args));
|
return Some(tty::Shell::new(command, args));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let working_directory =
|
return None;
|
||||||
(!profile.working_directory.is_empty())
|
}),
|
||||||
.then(|| profile.working_directory.clone().into());
|
working_directory: startup_options.working_directory.or_else(|| {
|
||||||
|
(!profile.working_directory.is_empty())
|
||||||
let options = tty::Options {
|
.then(|| profile.working_directory.clone().into())
|
||||||
shell,
|
}),
|
||||||
working_directory,
|
drain_on_exit: startup_options.drain_on_exit || profile.drain_on_exit,
|
||||||
drain_on_exit: profile.drain_on_exit,
|
..startup_options
|
||||||
env: HashMap::new(),
|
};
|
||||||
};
|
let tab_title_override = if profile.tab_title.is_empty() {
|
||||||
let tab_title_override = if profile.tab_title.is_empty() {
|
None
|
||||||
None
|
} else {
|
||||||
} else {
|
Some(profile.tab_title.clone())
|
||||||
Some(profile.tab_title.clone())
|
};
|
||||||
};
|
(options, tab_title_override)
|
||||||
(options, tab_title_override)
|
} else {
|
||||||
}
|
(self.startup_options.take().unwrap_or_default(), None)
|
||||||
None => (Options::default(), None),
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let entity = tab_model
|
let entity = tab_model
|
||||||
.insert()
|
.insert()
|
||||||
.text(
|
.text(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue