Skip to content

Commit

Permalink
profile flag
Browse files Browse the repository at this point in the history
  • Loading branch information
leb-kuchen committed Jun 22, 2024
1 parent 09e2073 commit 3bf1fd2
Showing 1 changed file with 63 additions and 6 deletions.
69 changes: 63 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,46 @@ pub fn icon_cache_get(name: &'static str, size: u16) -> widget::icon::Icon {
icon_cache.get(name, size)
}

fn parse_flag<'a>(arg: &'a str) -> Option<(Arg<'a>, &'a str)> {
if let Some(arg) = arg.strip_prefix("--") {
return arg
.split_once('=')
.filter(|(flag, value)| !value.is_empty() && !flag.is_empty())
.map(|(flag, value)| (Arg::Long(flag), value));
} else if let Some(arg) = arg.strip_prefix('-') {
return arg
.is_char_boundary(1)
.then(|| arg.split_at(1))
.filter(|(_, value)| !value.is_empty())
.map(|(flag, value)| (Arg::Short(flag.as_bytes()[0]), value));
}
return None;
}
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)]
enum Arg<'a> {
Long(&'a str),
Short(u8),
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_flag() {
assert_eq!(parse_flag("--profile=0"), Some((Arg::Long("profile"), "0")));
assert_eq!(parse_flag("-P0"), Some((Arg::Short(b'P'), "0")));
}
}

/// Runs application with these settings
#[rustfmt::skip]
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut shell_program_opt = None;
let mut shell_args = Vec::new();
let mut parse_flags = true;
let mut daemonize = true;
let mut profile = None;
for arg in env::args().skip(1) {
if parse_flags {
match arg.as_str() {
Expand All @@ -85,9 +118,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
"--no-daemon" => {
daemonize = false;
}
_ => {
//TODO: should this throw an error?
log::warn!("ignored argument {:?}", arg);
arg => {
match parse_flag(arg) {
Some((Arg::Short(b'P') | Arg::Long("profile"), value)) =>
profile = Some(value.to_string()),
//TODO: should this throw an error?
_ => eprintln!("ignored argument: {}", arg),
}
}
}
} else if shell_program_opt.is_none() {
Expand All @@ -96,7 +133,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
shell_args.push(arg);
}
}

#[cfg(all(unix, not(target_os = "redox")))]
if daemonize {
match fork::daemon(true, true) {
Expand Down Expand Up @@ -130,6 +166,20 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
};


let mut profile_id = None;
if let Some(profile) = profile {
if let Some((id, _)) = config.profiles.iter().find(|(_, k)|&k.name == &profile) {
profile_id = Some(id);
} else if let Ok(value) = profile.parse::<u64>() {
if let Some(id) = config.profiles.keys().find(|k|k.0 == value) {
profile_id = Some(id)
}
}
}
println!("{:?}", profile_id);


let startup_options = if let Some(shell_program) = shell_program_opt {
let options = tty::Options {
shell: Some(tty::Shell::new(shell_program, shell_args)),
Expand All @@ -151,10 +201,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
settings = settings.size_limits(Limits::NONE.min_width(360.0).min_height(180.0));

let flags = Flags {

startup_profile: profile_id.copied(),
config_handler,
config,
startup_options,
term_config,

};
cosmic::app::run::<App>(settings, flags)?;

Expand All @@ -166,6 +219,7 @@ pub struct Flags {
config_handler: Option<cosmic_config::Config>,
config: Config,
startup_options: Option<tty::Options>,
startup_profile: Option<ProfileId>,
term_config: term::Config,
}

Expand Down Expand Up @@ -399,6 +453,7 @@ pub struct App {
find_search_value: String,
term_event_tx_opt: Option<mpsc::Sender<(pane_grid::Pane, segmented_button::Entity, TermEvent)>>,
startup_options: Option<tty::Options>,
startup_profile: Option<ProfileId>,
term_config: term::Config,
color_scheme_errors: Vec<String>,
color_scheme_expanded: Option<(ColorSchemeKind, Option<ColorSchemeId>)>,
Expand Down Expand Up @@ -1442,6 +1497,7 @@ impl Application for App {
find_search_id: widget::Id::unique(),
find_search_value: String::new(),
startup_options: flags.startup_options,
startup_profile: flags.startup_profile,
term_config: flags.term_config,
term_event_tx_opt: None,
color_scheme_errors: Vec::new(),
Expand Down Expand Up @@ -2255,10 +2311,11 @@ impl Application for App {
return self.update_title(Some(pane));
}
Message::TabNew => {
let profile = self.startup_profile.take();
return self.create_and_focus_new_terminal(
self.pane_model.focus,
self.get_default_profile(),
)
profile.or_else(|| self.get_default_profile()),
);
}
Message::TabNext => {
if let Some(tab_model) = self.pane_model.active() {
Expand Down

0 comments on commit 3bf1fd2

Please sign in to comment.