Skip to content

Commit

Permalink
feat: multiple formats for command widget
Browse files Browse the repository at this point in the history
fixes #69
  • Loading branch information
dj95 committed Jun 20, 2024
1 parent 092ce15 commit d684f41
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 35 deletions.
2 changes: 1 addition & 1 deletion plugin-dev-workspace.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ layout {

command_0_command "echo \"平仮名, ひらがな 📦\""
command_0_clickaction "bash -c \"zellij --session zjstatus-dev pipe 'zjstatus::notify::hello world!' -n zjstatus\""
command_0_format "#[fg=colour80] {exit_code} {stdout} "
command_0_format "#[fg=colour80] {exit_code} #[fg=colour90] {stdout} "
command_0_interval "1"

command_1_command "date"
Expand Down
83 changes: 49 additions & 34 deletions src/widgets/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ lazy_static! {
static ref COMMAND_REGEX: Regex = Regex::new("_[a-zA-Z0-9]+$").unwrap();
}

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
enum RenderMode {
Static,
Dynamic,
Expand All @@ -32,7 +32,7 @@ enum RenderMode {
#[derive(Clone, Debug)]
struct CommandConfig {
command: String,
format: FormattedPart,
format: Vec<FormattedPart>,
env: Option<BTreeMap<String, String>>,
cwd: Option<PathBuf>,
interval: i64,
Expand Down Expand Up @@ -78,37 +78,51 @@ impl Widget for CommandWidget {
}
};

let mut content = command_config.format.content.clone();

if content.contains("{exit_code}") {
content = content.replace(
"{exit_code}",
format!("{}", command_result.exit_code.unwrap_or(-1)).as_str(),
);
}

if content.contains("{stdout}") {
content = content.replace(
"{stdout}",
command_result
.stdout
.strip_suffix('\n')
.unwrap_or(&command_result.stdout),
);
}

if content.contains("{stderr}") {
content = content.replace(
"{stderr}",
command_result
.stderr
.strip_suffix('\n')
.unwrap_or(&command_result.stderr),
);
}
let content = command_config
.format
.iter()
.map(|f| {
let mut content = f.content.clone();

if content.contains("{exit_code}") {
content = content.replace(
"{exit_code}",
format!("{}", command_result.exit_code.unwrap_or(-1)).as_str(),
);
}

if content.contains("{stdout}") {
content = content.replace(
"{stdout}",
command_result
.stdout
.strip_suffix('\n')
.unwrap_or(&command_result.stdout),
);
}

if content.contains("{stderr}") {
content = content.replace(
"{stderr}",
command_result
.stderr
.strip_suffix('\n')
.unwrap_or(&command_result.stderr),
);
}

(f, content)
})
.fold("".to_owned(), |acc, (f, content)| {
if command_config.render_mode == RenderMode::Static {
return format!("{acc}{}", f.format_string(&content));
}

format!("{acc}{}", content)
});

match command_config.render_mode {
RenderMode::Static => command_config.format.format_string(&content),
RenderMode::Static => content,
RenderMode::Dynamic => render_dynamic_formatted_content(&content),
RenderMode::Raw => content,
}
Expand Down Expand Up @@ -208,7 +222,7 @@ fn parse_config(zj_conf: &BTreeMap<String, String>) -> BTreeMap<String, CommandC

let mut command_conf = CommandConfig {
command: "".to_owned(),
format: FormattedPart::default(),
format: Vec::new(),
cwd: None,
env: None,
interval: 1,
Expand Down Expand Up @@ -248,7 +262,8 @@ fn parse_config(zj_conf: &BTreeMap<String, String>) -> BTreeMap<String, CommandC
}

if key.ends_with("format") {
command_conf.format = FormattedPart::from_format_string(zj_conf.get(&key).unwrap());
command_conf.format =
FormattedPart::multiple_from_format_string(zj_conf.get(&key).unwrap());
}

if key.ends_with("interval") {
Expand Down Expand Up @@ -462,7 +477,7 @@ mod test {
let res = run_command_if_needed(
CommandConfig {
command: "echo test".to_owned(),
format: FormattedPart::default(),
format: Vec::new(),
env: None,
cwd: None,
interval,
Expand Down

0 comments on commit d684f41

Please sign in to comment.