aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMart Lubbers2016-01-17 18:05:11 +0100
committerCamil Staps2019-12-11 17:29:33 +0100
commit625518acd7c62acde2d6d150272fa17a45eb3ce9 (patch)
tree7142278a1694f3e2fda2276e1277525ee31a7632
parentMerge pull request #373 from zsugabubus/fix-memory (diff)
Add support for custom commandsHEADmaster
Add the cmd option in the config that runs a shell command.
-rw-r--r--Makefile.am1
-rw-r--r--i3status.c13
-rw-r--r--include/i3status.h1
-rw-r--r--man/i3status.man16
-rw-r--r--src/print_cmd.c23
5 files changed, 54 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am
index c2c1c0a..df0db59 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -53,6 +53,7 @@ i3status_SOURCES = \
src/general.c \
src/output.c \
src/print_battery_info.c \
+ src/print_cmd.c \
src/print_cpu_temperature.c \
src/print_cpu_usage.c \
src/print_ddate.c \
diff --git a/i3status.c b/i3status.c
index b02b71a..3ddef09 100644
--- a/i3status.c
+++ b/i3status.c
@@ -506,6 +506,12 @@ int main(int argc, char *argv[]) {
CFG_CUSTOM_SEP_BLOCK_WIDTH_OPT,
CFG_END()};
+ cfg_opt_t cmd_opts[] = {
+ CFG_STR("cmd", "echo 'hello world'", CFGF_NONE),
+ CFG_CUSTOM_ALIGN_OPT,
+ CFG_CUSTOM_MIN_WIDTH_OPT,
+ CFG_END()};
+
cfg_opt_t opts[] = {
CFG_STR_LIST("order", "{}", CFGF_NONE),
CFG_SEC("general", general_opts, CFGF_NONE),
@@ -519,6 +525,7 @@ int main(int argc, char *argv[]) {
CFG_SEC("volume", volume_opts, CFGF_TITLE | CFGF_MULTI),
CFG_SEC("ipv6", ipv6_opts, CFGF_NONE),
CFG_SEC("time", time_opts, CFGF_NONE),
+ CFG_SEC("cmd", cmd_opts, CFGF_TITLE | CFGF_MULTI),
CFG_SEC("tztime", tztime_opts, CFGF_TITLE | CFGF_MULTI),
CFG_SEC("ddate", ddate_opts, CFGF_NONE),
CFG_SEC("load", load_opts, CFGF_NONE),
@@ -828,6 +835,12 @@ int main(int argc, char *argv[]) {
print_file_contents(json_gen, buffer, title, cfg_getstr(sec, "path"), cfg_getstr(sec, "format"), cfg_getstr(sec, "format_bad"), cfg_getint(sec, "max_characters"));
SEC_CLOSE_MAP;
}
+
+ CASE_SEC("cmd") {
+ SEC_OPEN_MAP("cmd");
+ print_cmd(json_gen, buffer, cfg_getstr(sec, "cmd"));
+ SEC_CLOSE_MAP;
+ }
}
if (output_format == O_I3BAR) {
yajl_gen_array_close(json_gen);
diff --git a/include/i3status.h b/include/i3status.h
index 2e314b1..e1a0e8b 100644
--- a/include/i3status.h
+++ b/include/i3status.h
@@ -227,6 +227,7 @@ void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, cons
void print_load(yajl_gen json_gen, char *buffer, const char *format, const char *format_above_threshold, const float max_threshold);
void print_memory(yajl_gen json_gen, char *buffer, const char *format, const char *format_degraded, const char *threshold_degraded, const char *threshold_critical, const char *memory_used_method, const char *unit, const int decimals);
void print_volume(yajl_gen json_gen, char *buffer, const char *fmt, const char *fmt_muted, const char *device, const char *mixer, int mixer_idx);
+void print_cmd(yajl_gen json_gen, char *buffer, const char *cmd);
bool process_runs(const char *path);
int volume_pulseaudio(uint32_t sink_idx, const char *sink_name);
bool description_pulseaudio(uint32_t sink_idx, const char *sink_name, char buffer[MAX_SINK_DESCRIPTION_LEN]);
diff --git a/man/i3status.man b/man/i3status.man
index a36c853..da54d37 100644
--- a/man/i3status.man
+++ b/man/i3status.man
@@ -652,6 +652,22 @@ If the file is not found "no file" will be printed, if the file can't be read
*Example Max_characters*: 255
+=== Custom command
+
+Outputs the first line of the stdout of the given shell command passed to *sh
+-c*. Note that the last newline is automatically removed.
+
+*Example order*: +cmd quodlibet+
+
+*Example cmd*: +pkill -0 quodlibet && quodlibet --print-playing+
+
+*Example configuration*:
+-------------------------------------------------------------
+cmd quodlibet {
+ cmd = "pkill -0 quodlibet && quodlibet --print-playing"
+}
+-------------------------------------------------------------
+
== Universal module options
When using the i3bar output format, there are a few additional options that
diff --git a/src/print_cmd.c b/src/print_cmd.c
new file mode 100644
index 0000000..a7ac575
--- /dev/null
+++ b/src/print_cmd.c
@@ -0,0 +1,23 @@
+// vim:ts=4:sw=4:expandtab
+#include <stdio.h>
+#include <string.h>
+#include <yajl/yajl_gen.h>
+#include <yajl/yajl_version.h>
+
+#include "i3status.h"
+
+void print_cmd(yajl_gen json_gen, char *buffer, const char *cmd) {
+ char *outwalk = buffer;
+ char path[1024];
+ FILE *fp;
+ fp = popen(cmd, "r");
+ fgets(path, sizeof(path) - 1, fp);
+ pclose(fp);
+
+ char *nl = index(path, '\n');
+ if (nl != NULL) {
+ *nl = '\0';
+ }
+ maybe_escape_markup(path, &outwalk);
+ OUTPUT_FULL_TEXT(buffer);
+}