Merge pull request #49 from dylanaraps/stdout2

Add 'stdout' mode which allows you print the output in a lemonbar compatible format.
This commit is contained in:
Dylan Araps 2016-02-06 10:55:05 +11:00
commit 916c9c2cb1
4 changed files with 156 additions and 5 deletions

37
1.1.md
View File

@ -69,6 +69,43 @@ the distro ascii art and the automatic config creation.
- Fetch now has a Gentoo/Funtoo e-build courtesy of @z1lt0id - 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<br \>
with lemonbar and in your scripts. You can use it by launching fetch with `--stdout` to print all functions enabled<br \>
in your `printinfo` function. You can selectively print functions by passing arguments to `--stdout`<br \>
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<br \>
as a seperator between the output.
- Added `--stdout_title` and `$stdout_title` which allow you to toggle the `title@hostname`<br \>
from appearing in the output.
- Added `--stdout_subtitles` which allow you to toggle the `Info:` titles from appearing in<br \>
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 ### Info
- You can now display info without a subtitle. eg. `info memory` - You can now display info without a subtitle. eg. `info memory`

View File

@ -297,6 +297,15 @@ alias fetch2="fetch \
--ascii_distro distro Which Distro\'s ascii art to print --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: Screenshot:
--scrot /path/to/img Take a screenshot, if path is left empty the screen- --scrot /path/to/img Take a screenshot, if path is left empty the screen-
shot function will use \$scrot_dir and \$scrot_name. shot function will use \$scrot_dir and \$scrot_name.

View File

@ -330,4 +330,20 @@ config="on"
config_file="$HOME/.config/fetch/config" 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"
# }}} # }}}

95
fetch
View File

@ -350,6 +350,22 @@ config="on"
config_file="$HOME/.config/fetch/config" 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"
# }}} # }}}
@ -1222,8 +1238,8 @@ getbattery () {
else else
# If there's only a single battery and it's battery 0, # If there's only a single battery and it's battery 0,
# don't number the subtitle. # don't number the subtitle.
if [ "${#batteries[@]}" == 1 ] && [ "$battery_num" == 0 ]; then if [ "${#batteries[@]}" == 1 ]; then
prin "${title}: ${batteries[0]}%" battery="${batteries[0]}%"
return return
fi fi
@ -1782,6 +1798,11 @@ info () {
string="${bold}${title_color}${output}" string="${bold}${title_color}${output}"
string="${string/@/${at_color}@${title_color}}" string="${string/@/${at_color}@${title_color}}"
length=${#output} length=${#output}
# Hide the title in stdout mode
[ "$stdout" == "on" ] && \
[ "$stdout_title" == "off" ] &&\
string=""
;; ;;
underline) underline)
@ -1799,7 +1820,22 @@ info () {
[ -z "$2" ] && string=${string/*: } [ -z "$2" ] && string=${string/*: }
# Print the string # Print the string
if [ "$stdout" == "on" ]; then
# Unset the vars containg escape codes as lemonbar doesn't
# support them.
unset -v bold subtitle_color clear colon_color info_color \
underline_color title_color at_color
# 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}" printf "%b%s\n" "${padding}${string}${clear}"
fi
} }
# }}} # }}}
@ -1823,7 +1859,34 @@ prin () {
;; ;;
esac esac
# Print the info
if [ "$stdout" == "on" ]; then
printf "%s" "${string}${stdout_seperator}"
else
printf "%b%s\n" "${padding}${string}${clear}" printf "%b%s\n" "${padding}${string}${clear}"
fi
}
# }}}
# Stdout {{{
stdout () {
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
}
} }
# }}} # }}}
@ -2038,11 +2101,21 @@ usage () { cat << EOF
--ascii_distro distro Which Distro\'s ascii art to print --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: Screenshot:
--scrot /path/to/img Take a screenshot, if path is left empty the screen- --scrot /path/to/img Take a screenshot, if path is left empty the screen-
shot function will use \$scrot_dir and \$scrot_name. shot function will use \$scrot_dir and \$scrot_name.
--scrot_cmd cmd Screenshot program to launch --scrot_cmd cmd Screenshot program to launch
Other: Other:
--config /path/to/config Specify a path to a custom config file --config /path/to/config Specify a path to a custom config file
--config none Launch the script without a config file --config none Launch the script without a config file
@ -2149,6 +2222,22 @@ while [ "$1" ]; do
--scrot | -s) scrot="on"; [ "$2" ] && scrot_path="$2" ;; --scrot | -s) scrot="on"; [ "$2" ] && scrot_path="$2" ;;
--scrot_cmd) scrot_cmd="$2" ;; --scrot_cmd) scrot_cmd="$2" ;;
# Stdout
--stdout_title) stdout_title="$2" ;;
--stdout_seperator) stdout_seperator="$2" ;;
--stdout_subtitles) stdout_subtitles="$2" ;;
--stdout)
case "$2" in
"--"* | "") stdout="on" ;;
*) stdout="on"; stdout_args="on"; args=("$@"); stdout ;;
esac
unset info_color colors
underline="off"
image="off"
color_blocks="off"
;;
# Other # Other
--config) --config)
case "$2" in case "$2" in
@ -2213,7 +2302,7 @@ fi
[ "$image" != "off" ] && printf "\033[0H" [ "$image" != "off" ] && printf "\033[0H"
# Get colors / bold # Get colors / bold
colors colors 2>/dev/null
bold bold
# Print the info # Print the info