Skip to content

Commit

Permalink
feat: make env & cwd configurable for command widget
Browse files Browse the repository at this point in the history
addresses #52
  • Loading branch information
dj95 committed May 8, 2024
1 parent 3f91eda commit fba3477
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ cached = { version = "0.46.1", features = ["wasm"] }
console = "0.15.8"
tracing-subscriber = "0.3.18"
tracing = "0.1.40"
kdl = "4.6.0"

[dev-dependencies]
criterion = { version = "0.5.1", default-features = false, features = [
Expand Down
7 changes: 6 additions & 1 deletion plugin-dev-workspace.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ layout {
command_1_format "#[fg=blue,reverse,bg=default,us=red,blink,dim,strikethrough] {exit_code} {stdout} "
command_1_interval "1"

command_git_branch_command "git rev-parse --abbrev-ref HEAD"
command_git_branch_command "bash -c \"echo $FOO\""
command_git_branch_cwd "/Users/daniel"
command_git_branch_env {
FOO "1"
BAR "foo"
}
command_git_branch_format "#[fg=red] {stdout} "
command_git_branch_interval "2"

Expand Down
18 changes: 10 additions & 8 deletions src/bin/zjstatus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ use uuid::Uuid;
use zjstatus::{
config::{self, ModuleConfig, UpdateEventMask, ZellijState},
frames, pipe,
widgets::command::{CommandResult, CommandWidget},
widgets::datetime::DateTimeWidget,
widgets::mode::ModeWidget,
widgets::notification::NotificationWidget,
widgets::session::SessionWidget,
widgets::swap_layout::SwapLayoutWidget,
widgets::tabs::TabsWidget,
widgets::widget::Widget,
widgets::{
command::{CommandResult, CommandWidget},
datetime::DateTimeWidget,
mode::ModeWidget,
notification::NotificationWidget,
session::SessionWidget,
swap_layout::SwapLayoutWidget,
tabs::TabsWidget,
widget::Widget,
},
};

#[derive(Default)]
Expand Down
4 changes: 2 additions & 2 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl FormattedPart {
let mut format_content_split = format.split(']').collect::<Vec<&str>>();

if format_content_split.len() == 1 {
result.content = format.to_owned();
format.clone_into(&mut result.content);

return result;
}
Expand Down Expand Up @@ -225,7 +225,7 @@ impl FormattedPart {
}

let res = self.format_string(&output);
self.cached_content = res.clone();
self.cached_content.clone_from(&res);

res
}
Expand Down
63 changes: 58 additions & 5 deletions src/widgets/command.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use kdl::{KdlDocument, KdlError};
use lazy_static::lazy_static;
use std::{
collections::BTreeMap,
fs::{remove_file, File},
ops::Sub,
path::Path,
path::{Path, PathBuf},
};

use chrono::{DateTime, Duration, Local};
use regex::Regex;
#[cfg(not(feature = "bench"))]
use zellij_tile::shim::run_command;
use zellij_tile::shim::{run_command, run_command_with_env_variables_and_cwd};

use crate::render::{formatted_parts_from_string_cached, FormattedPart};

Expand All @@ -32,6 +33,8 @@ enum RenderMode {
struct CommandConfig {
command: String,
format: FormattedPart,
env: Option<BTreeMap<String, String>>,
cwd: Option<PathBuf>,
interval: i64,
render_mode: RenderMode,
}
Expand Down Expand Up @@ -137,6 +140,19 @@ fn run_command_if_needed(command_config: CommandConfig, name: &str, state: &Zell
#[allow(unused_variables)]
let command = commandline_parser(&command_config.command);
tracing::debug!("Running command: {:?}", command);

#[cfg(not(feature = "bench"))]
if command_config.env.is_some() || command_config.cwd.is_some() {
run_command_with_env_variables_and_cwd(
&command.iter().map(|x| x.as_str()).collect::<Vec<&str>>(),
command_config.env.unwrap(),
command_config.cwd.unwrap(),
context,
);

return;
}

#[cfg(not(feature = "bench"))]
run_command(
&command.iter().map(|x| x.as_str()).collect::<Vec<&str>>(),
Expand All @@ -161,6 +177,8 @@ fn parse_config(zj_conf: &BTreeMap<String, String>) -> BTreeMap<String, CommandC
let mut command_conf = CommandConfig {
command: "".to_owned(),
format: FormattedPart::default(),
cwd: None,
env: None,
interval: 1,
render_mode: RenderMode::Static,
};
Expand All @@ -170,7 +188,22 @@ fn parse_config(zj_conf: &BTreeMap<String, String>) -> BTreeMap<String, CommandC
}

if key.ends_with("command") {
command_conf.command = zj_conf.get(&key).unwrap().to_owned().clone();
command_conf.command.clone_from(&zj_conf.get(&key).unwrap().to_owned())
}

if key.ends_with("env") {
let doc: Result<KdlDocument, KdlError> = zj_conf.get(&key).unwrap().parse();

if let Ok(doc) = doc {
command_conf.env = Some(get_env_vars(doc));
}
}

if key.ends_with("cwd") {
let mut cwd = PathBuf::new();
cwd.push(zj_conf.get(&key).unwrap().to_owned().clone());

command_conf.cwd = Some(cwd);
}

if key.ends_with("format") {
Expand Down Expand Up @@ -199,6 +232,26 @@ fn parse_config(zj_conf: &BTreeMap<String, String>) -> BTreeMap<String, CommandC
config
}

fn get_env_vars(doc: KdlDocument) -> BTreeMap<String, String> {
let mut output = BTreeMap::new();

for n in doc.nodes() {
let children = n.entries();
if children.len() != 1 {
continue;
}

let value = match children.first().unwrap().value().as_string() {
Some(value) => value,
None => continue,
};

output.insert(n.name().value().to_string(), value.to_string());
}

output
}

fn get_timestamp_from_event_or_default(
name: &str,
state: &ZellijState,
Expand Down Expand Up @@ -276,7 +329,7 @@ fn commandline_parser(input: &str) -> Vec<String> {
is_in_group = false;
found_special_char = '\0';
output.push(buffer.clone());
buffer = "".to_owned();
"".clone_into(&mut buffer);
continue;
}

Expand All @@ -288,7 +341,7 @@ fn commandline_parser(input: &str) -> Vec<String> {

if character == ' ' && !is_in_group {
output.push(buffer.clone());
buffer = "".to_owned();
"".clone_into(&mut buffer);
continue;
}

Expand Down

0 comments on commit fba3477

Please sign in to comment.