From 49e7673f0d657f2810c4b317fefc99a2e0dcb7c1 Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 5 Feb 2016 16:29:11 +1100 Subject: [PATCH 01/15] Added '--stdout' to print the output in a lemonbar compatible format --- config/config | 16 ++++++++++++++++ fetch | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/config/config b/config/config index 5e22fc4b..7ea5e023 100644 --- a/config/config +++ b/config/config @@ -326,4 +326,20 @@ config="on" config_file="$HOME/.config/fetch/config" +# }}} + +# Other Options {{{ + +# Seperator to use in stdout mode. +# --stdout_seperator string +stdout_seperator=" " + +# Hide/Show the title in stdout mode. +# --stdout_title on/off +stdout_title="off" + +# Hide/Show each info's subtitle in stdout mode. +# --stdout_subtitles on/off +stdout_subtitles="on" + # }}} diff --git a/fetch b/fetch index c2ec6e45..5a8c0dfa 100755 --- a/fetch +++ b/fetch @@ -346,6 +346,22 @@ config="on" config_file="$HOME/.config/fetch/config" +# }}} + +# Other Options {{{ + +# Seperator to use in stdout mode. +# --stdout_seperator string +stdout_seperator=" " + +# Hide/Show the title in stdout mode. +# --stdout_title on/off +stdout_title="off" + +# Hide/Show each info's subtitle in stdout mode. +# --stdout_subtitles on/off +stdout_subtitles="on" + # }}} @@ -1778,10 +1794,12 @@ info () { string="${bold}${title_color}${output}" string="${string/@/${at_color}@${title_color}}" length=${#output} + [ "$stdout_title" == "off" ] && string="" ;; underline) string="${underline_color}${output}" + [ "$stdout" == "on" ] && string="" ;; linebreak | cols) @@ -1795,8 +1813,23 @@ info () { ;; esac + # If there's no subtitle don't print one + [ -z "$2" ] && string=${string/*: } + # Print the string - printf "%b%s\n" "${padding}${string}${clear}" + if [ "$stdout" == "on" ]; then + # Unset the vars containg escape codes as lemonbar doesn't + # support them. + unset subtitle bold subtitle_color clear colon_color info_color \ + underline_color title_color at_color + + # Remove subtitles from output. + [ "$stdout_subtitles" == "off" ] && string=${string/*: } + + [ ! -z "$string" ] && printf "%s" "${string}${stdout_seperator}" + else + printf "%b%s\n" "${padding}${string}${clear}" + fi } # }}} @@ -2140,6 +2173,12 @@ while [ "$1" ]; do --scrot | -s) scrot="on"; [ "$2" ] && scrot_path="$2" ;; --scrot_cmd) scrot_cmd="$2" ;; + # Stdout + --stdout) stdout="on"; image="off"; color_blocks="off" ;; + --stdout_title) stdout_title="$2" ;; + --stdout_seperator) stdout_seperator="$2" ;; + --stdout_subtitles) stdout_subtitles="$2" ;; + # Other --config) case "$2" in From 456c706f1d28c01fe77b6658658acfd678b0db4d Mon Sep 17 00:00:00 2001 From: Dylan Date: Fri, 5 Feb 2016 17:02:53 +1100 Subject: [PATCH 02/15] Fix missing title --- fetch | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/fetch b/fetch index 5a8c0dfa..26afe4ea 100755 --- a/fetch +++ b/fetch @@ -1794,7 +1794,11 @@ info () { string="${bold}${title_color}${output}" string="${string/@/${at_color}@${title_color}}" length=${#output} - [ "$stdout_title" == "off" ] && string="" + + # Hide the title in stdout mode + [ "$stdout" == "on" ] && \ + [ "$stdout_title" == "off" ] &&\ + string="" ;; underline) From 8e6a548f634da4c29d62e53fb5b8a92e0d901678 Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 6 Feb 2016 00:50:45 +1100 Subject: [PATCH 03/15] '--stdout' now takes arguments --- fetch | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/fetch b/fetch index 26afe4ea..80e901d4 100755 --- a/fetch +++ b/fetch @@ -2178,10 +2178,31 @@ while [ "$1" ]; do --scrot_cmd) scrot_cmd="$2" ;; # Stdout - --stdout) stdout="on"; image="off"; color_blocks="off" ;; --stdout_title) stdout_title="$2" ;; --stdout_seperator) stdout_seperator="$2" ;; --stdout_subtitles) stdout_subtitles="$2" ;; + --stdout) + stdout="on" + image="off" + color_blocks="off" + + for func in "$@"; do + case "$func" in + "--stdout") continue ;; + "--"*) break ;; + *) + # Call the function + get$func 2>/dev/null + eval output="\$${func}" + + # Print the output + printf "%s" "$output" + ;; + esac + done + + [ ! -z "$output" ] && exit + ;; # Other --config) From a69b86a22a3794a4335a7b62ad049e1f73688919 Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 6 Feb 2016 01:04:41 +1100 Subject: [PATCH 04/15] Fix battery showing no info in stdout mode --- fetch | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/fetch b/fetch index 80e901d4..4ac40438 100755 --- a/fetch +++ b/fetch @@ -1857,7 +1857,11 @@ prin () { ;; esac - printf "%b%s\n" "${padding}${string}${clear}" + # Print the info + printf "%b%s" "${padding}${string}${clear}" + + # If stdout mode is off, print a newline too. + [ "$stdout" == "off" ] && printf "\n" } # }}} @@ -2196,7 +2200,7 @@ while [ "$1" ]; do eval output="\$${func}" # Print the output - printf "%s" "$output" + printf "%s" "${output}" ;; esac done From 4ad5cf791d1c626be013485be1a2570d7e9316ef Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 6 Feb 2016 01:22:29 +1100 Subject: [PATCH 05/15] Fix weird behaviour with prin and --stdout --- fetch | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/fetch b/fetch index 4ac40438..6e57db5a 100755 --- a/fetch +++ b/fetch @@ -1235,7 +1235,7 @@ getbattery () { # If there's only a single battery and it's battery 0, # don't number the subtitle. if [ "${#batteries[@]}" == 1 ] && [ "$battery_num" == 0 ]; then - prin "${title}: ${batteries[0]}%" + battery="$(prin "${title}: ${batteries[0]}%")" return fi @@ -1858,10 +1858,11 @@ prin () { esac # Print the info - printf "%b%s" "${padding}${string}${clear}" - - # If stdout mode is off, print a newline too. - [ "$stdout" == "off" ] && printf "\n" + if [ "$stdout" == "on" ]; then + printf "%s" "${string}${stdout_seperator}" + else + printf "%b%s\n" "${padding}${string}${clear}" + fi } # }}} From 59cc2f61ce657d8ec26e6c8a07c555b63a1232d3 Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 6 Feb 2016 01:27:17 +1100 Subject: [PATCH 06/15] Remove check for battery num --- fetch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fetch b/fetch index 6e57db5a..b36b6906 100755 --- a/fetch +++ b/fetch @@ -1234,7 +1234,7 @@ getbattery () { else # If there's only a single battery and it's battery 0, # don't number the subtitle. - if [ "${#batteries[@]}" == 1 ] && [ "$battery_num" == 0 ]; then + if [ "${#batteries[@]}" == 1 ]; then battery="$(prin "${title}: ${batteries[0]}%")" return fi From 5e5cf5da2a65bc51d5b160b047c18ff40f496fc4 Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 6 Feb 2016 01:31:13 +1100 Subject: [PATCH 07/15] Fix weird leftover escape codes in stdout mode --- fetch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fetch b/fetch index b36b6906..67070163 100755 --- a/fetch +++ b/fetch @@ -1235,7 +1235,7 @@ getbattery () { # If there's only a single battery and it's battery 0, # don't number the subtitle. if [ "${#batteries[@]}" == 1 ]; then - battery="$(prin "${title}: ${batteries[0]}%")" + battery="${batteries[0]}%" return fi From 8da6f40ce06f8fe3c72089e91660b19ea7742c19 Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 6 Feb 2016 02:25:21 +1100 Subject: [PATCH 08/15] Cleanup of stdout --- fetch | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/fetch b/fetch index 67070163..75a9b812 100755 --- a/fetch +++ b/fetch @@ -1822,15 +1822,19 @@ info () { # Print the string if [ "$stdout" == "on" ]; then + # Unset the vars containg escape codes as lemonbar doesn't # support them. - unset subtitle bold subtitle_color clear colon_color info_color \ + unset bold subtitle_color clear colon_color info_color \ underline_color title_color at_color - # Remove subtitles from output. - [ "$stdout_subtitles" == "off" ] && string=${string/*: } + # Show/Hide subtitles + [ "$stdout_subtitles" == "off" ] && \ + string=${string/*: } + # If the string isn't empty, print it. [ ! -z "$string" ] && printf "%s" "${string}${stdout_seperator}" + else printf "%b%s\n" "${padding}${string}${clear}" fi @@ -2187,22 +2191,19 @@ while [ "$1" ]; do --stdout_seperator) stdout_seperator="$2" ;; --stdout_subtitles) stdout_subtitles="$2" ;; --stdout) + # Disable images and color blocks stdout="on" image="off" color_blocks="off" + # Remove info color escape code. + unset info_color + for func in "$@"; do case "$func" in "--stdout") continue ;; "--"*) break ;; - *) - # Call the function - get$func 2>/dev/null - eval output="\$${func}" - - # Print the output - printf "%s" "${output}" - ;; + *) info "$func" ;; esac done From e6d8fc9c988ab57610585de9b86437b0781cae95 Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 6 Feb 2016 02:26:44 +1100 Subject: [PATCH 09/15] Remove unnecessary comments --- fetch | 3 --- 1 file changed, 3 deletions(-) diff --git a/fetch b/fetch index 75a9b812..cd2884c2 100755 --- a/fetch +++ b/fetch @@ -2191,12 +2191,9 @@ while [ "$1" ]; do --stdout_seperator) stdout_seperator="$2" ;; --stdout_subtitles) stdout_subtitles="$2" ;; --stdout) - # Disable images and color blocks stdout="on" image="off" color_blocks="off" - - # Remove info color escape code. unset info_color for func in "$@"; do From 84b8b04d83e0f1d0b8e42c1305387106ee40c8c2 Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 6 Feb 2016 02:36:00 +1100 Subject: [PATCH 10/15] Remove newline in if statement --- fetch | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/fetch b/fetch index cd2884c2..dcf448f8 100755 --- a/fetch +++ b/fetch @@ -1829,8 +1829,7 @@ info () { underline_color title_color at_color # Show/Hide subtitles - [ "$stdout_subtitles" == "off" ] && \ - string=${string/*: } + [ "$stdout_subtitles" == "off" ] && string=${string/*: } # If the string isn't empty, print it. [ ! -z "$string" ] && printf "%s" "${string}${stdout_seperator}" From 6f69aed92e2f23f897f42b36c0cb6aae14ff1a4f Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 6 Feb 2016 10:04:50 +1100 Subject: [PATCH 11/15] Fix '--stdout_seperator' not working when '--stdout' gets args --- fetch | 53 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/fetch b/fetch index dcf448f8..1edddc15 100755 --- a/fetch +++ b/fetch @@ -1870,6 +1870,26 @@ prin () { # }}} +# Stdout {{{ + +stdout () { + index=1 + for func in "${args[@]}"; do + + case "$func" in + "--stdout") continue ;; + "--"*) break ;; + *) + case "${args[$((index + 1))]}" in "--"*) unset stdout_seperator ;; esac + info "$func" + ;; + esac + index=$((index + 1)) + done +} + +# }}} + # Underline {{{ getunderline () { @@ -2190,20 +2210,15 @@ while [ "$1" ]; do --stdout_seperator) stdout_seperator="$2" ;; --stdout_subtitles) stdout_subtitles="$2" ;; --stdout) - stdout="on" + case "$2" in + "--"* | "") stdout="on" ;; + *) stdout="on"; stdout_args="on" ;; + esac + + unset info_color image="off" color_blocks="off" - unset info_color - - for func in "$@"; do - case "$func" in - "--stdout") continue ;; - "--"*) break ;; - *) info "$func" ;; - esac - done - - [ ! -z "$output" ] && exit + args=("$@") ;; # Other @@ -2269,12 +2284,16 @@ fi # Move cursor to the top [ "$image" != "off" ] && printf "\033[0H" -# Get colors / bold -colors -bold +if [ "$stdout_args" == "on" ]; then + stdout +else + # Get colors / bold + colors + bold -# Print the info -printinfo + # Print the info + printinfo +fi if [ "$image" != "off" ]; then # Get cursor position From 943a5493c39c305ff00c71038706a4752f48513c Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 6 Feb 2016 10:21:12 +1100 Subject: [PATCH 12/15] Stdout mode: Replace the printinfo function instead of disabling it. --- fetch | 47 ++++++++++++++++++++++------------------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/fetch b/fetch index 1edddc15..db9de29c 100755 --- a/fetch +++ b/fetch @@ -1825,7 +1825,7 @@ info () { # Unset the vars containg escape codes as lemonbar doesn't # support them. - unset bold subtitle_color clear colon_color info_color \ + unset -v bold subtitle_color clear colon_color info_color \ underline_color title_color at_color # Show/Hide subtitles @@ -1873,19 +1873,21 @@ prin () { # Stdout {{{ stdout () { - index=1 - for func in "${args[@]}"; do + printinfo () { + index=1 + for func in "${args[@]}"; do - case "$func" in - "--stdout") continue ;; - "--"*) break ;; - *) - case "${args[$((index + 1))]}" in "--"*) unset stdout_seperator ;; esac - info "$func" - ;; - esac - index=$((index + 1)) - done + case "$func" in + "--stdout") continue ;; + "--"*) break ;; + *) + case "${args[$((index + 1))]}" in "--"*) unset stdout_seperator ;; esac + info "$func" + ;; + esac + index=$((index + 1)) + done + } } # }}} @@ -2212,13 +2214,12 @@ while [ "$1" ]; do --stdout) case "$2" in "--"* | "") stdout="on" ;; - *) stdout="on"; stdout_args="on" ;; + *) stdout="on"; stdout_args="on"; args=("$@"); stdout ;; esac - unset info_color + unset info_color colors image="off" color_blocks="off" - args=("$@") ;; # Other @@ -2284,16 +2285,12 @@ fi # Move cursor to the top [ "$image" != "off" ] && printf "\033[0H" -if [ "$stdout_args" == "on" ]; then - stdout -else - # Get colors / bold - colors - bold +# Get colors / bold +colors 2>/dev/null +bold - # Print the info - printinfo -fi +# Print the info +printinfo if [ "$image" != "off" ]; then # Get cursor position From 0507b6a80ed870b7738beee8e7efd543838d08c2 Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 6 Feb 2016 10:27:14 +1100 Subject: [PATCH 13/15] Remove uneeded check --- fetch | 1 - 1 file changed, 1 deletion(-) diff --git a/fetch b/fetch index db9de29c..a816a3bc 100755 --- a/fetch +++ b/fetch @@ -1803,7 +1803,6 @@ info () { underline) string="${underline_color}${output}" - [ "$stdout" == "on" ] && string="" ;; linebreak | cols) From ce3415481b8d8d3b2ae00f1bad5b651f46c10242 Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 6 Feb 2016 10:30:46 +1100 Subject: [PATCH 14/15] Remove underline from stdout output --- fetch | 1 + 1 file changed, 1 insertion(+) diff --git a/fetch b/fetch index 7d7adbdb..f79d8303 100755 --- a/fetch +++ b/fetch @@ -2223,6 +2223,7 @@ while [ "$1" ]; do esac unset info_color colors + underline="off" image="off" color_blocks="off" ;; From f82adabc114e38803e8de0d68e3f33a2ab0965be Mon Sep 17 00:00:00 2001 From: Dylan Date: Sat, 6 Feb 2016 10:49:06 +1100 Subject: [PATCH 15/15] Added usage for stdout mode --- 1.1.md | 37 +++++++++++++++++++++++++++++++++++++ README.md | 9 +++++++++ fetch | 10 ++++++++++ 3 files changed, 56 insertions(+) diff --git a/1.1.md b/1.1.md index b6bc0d81..cbc9debf 100644 --- a/1.1.md +++ b/1.1.md @@ -69,6 +69,43 @@ the distro ascii art and the automatic config creation. - Fetch now has a Gentoo/Funtoo e-build courtesy of @z1lt0id +### Stdout + +- Added `stdout` mode which allows you to fetch info in a plain text format that works
+with lemonbar and in your scripts. You can use it by launching fetch with `--stdout` to print all functions enabled
+in your `printinfo` function. You can selectively print functions by passing arguments to `--stdout`
+like so: + +```sh +# Print the output of all info functions enabled in printinfo +fetch --stdout + +# Print the output of memory +fetch --stdout memory + +# Print the output of memory and disk +fetch --stdout disk + +``` + +- Added `--stdout_seperator` and `$stdout_seperator` which takes a string and adds it
+as a seperator between the output. +- Added `--stdout_title` and `$stdout_title` which allow you to toggle the `title@hostname`
+from appearing in the output. +- Added `--stdout_subtitles` which allow you to toggle the `Info:` titles from appearing in
+the output. + +```sh +# Hiding subtitles +fetch --stdout --stdout_subtitles off + +# Custom seperator +fetch --stdout disk gpu --stdout_seperator " | " + +``` + +**NOTE:** `stdout_subtitles` and `stdout_title` only work when `--stdout` is used on its own. + ### Info - You can now display info without a subtitle. eg. `info memory` diff --git a/README.md b/README.md index b1a904fa..e49eeae1 100644 --- a/README.md +++ b/README.md @@ -297,6 +297,15 @@ alias fetch2="fetch \ --ascii_distro distro Which Distro\'s ascii art to print + Stdout: + --stdout info info Launch fetch in stdout mode which prints the info + in a plain-text format that you can use with + lemonbar etc. + --stdout_title on/off Hide/Show the title in stdout mode. + --stdout_seperator string String to use as a seperator in stdout mode. + --stdout_subtitles on/off Hide/Show the subtitles in stdout mode. + + Screenshot: --scrot /path/to/img Take a screenshot, if path is left empty the screen- shot function will use \$scrot_dir and \$scrot_name. diff --git a/fetch b/fetch index f79d8303..f85576d2 100755 --- a/fetch +++ b/fetch @@ -2101,11 +2101,21 @@ usage () { cat << EOF --ascii_distro distro Which Distro\'s ascii art to print + Stdout: + --stdout info info Launch fetch in stdout mode which prints the info + in a plain-text format that you can use with + lemonbar etc. + --stdout_title on/off Hide/Show the title in stdout mode. + --stdout_seperator string String to use as a seperator in stdout mode. + --stdout_subtitles on/off Hide/Show the subtitles in stdout mode. + + Screenshot: --scrot /path/to/img Take a screenshot, if path is left empty the screen- shot function will use \$scrot_dir and \$scrot_name. --scrot_cmd cmd Screenshot program to launch + Other: --config /path/to/config Specify a path to a custom config file --config none Launch the script without a config file