Added build files to turn the bible into a book
This commit is contained in:
@@ -1,78 +1,150 @@
|
||||
# Information about the terminal
|
||||
# Conversion
|
||||
|
||||
## Get the terminal size in lines and columns (*from a script*)
|
||||
|
||||
This is handy when writing scripts in pure bash and `stty`/`tput` can’t be
|
||||
called.
|
||||
## Convert a hex color to RGB
|
||||
|
||||
**Example Function:**
|
||||
|
||||
```sh
|
||||
get_term_size() {
|
||||
# Usage: get_term_size
|
||||
hex_to_rgb() {
|
||||
# Usage: hex_to_rgb "#FFFFFF"
|
||||
((r=16#${1:1:2}))
|
||||
((g=16#${1:3:2}))
|
||||
((b=16#${1:5:6}))
|
||||
|
||||
# (:;:) is a micro sleep to ensure the variables are
|
||||
# exported immediately.
|
||||
shopt -s checkwinsize; (:;:)
|
||||
printf '%s\n' "$LINES $COLUMNS"
|
||||
printf '%s\n' "$r $g $b"
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
# Output: LINES COLUMNS
|
||||
$ get_term_size
|
||||
15 55
|
||||
$ hex_to_rgb "#FFFFFF"
|
||||
255 255 255
|
||||
```
|
||||
|
||||
## Get the terminal size in pixels
|
||||
|
||||
**CAVEAT**: This does not work in some terminal emulators.
|
||||
## Convert an RGB color to hex
|
||||
|
||||
**Example Function:**
|
||||
|
||||
```sh
|
||||
get_window_size() {
|
||||
# Usage: get_window_size
|
||||
printf '%b' "${TMUX:+\\ePtmux;\\e}\\e[14t${TMUX:+\\e\\\\}"
|
||||
IFS=';t' read -d t -t 0.05 -sra term_size
|
||||
printf '%s\n' "${term_size[1]}x${term_size[2]}"
|
||||
rgb_to_hex() {
|
||||
# Usage: rgb_to_hex "r" "g" "b"
|
||||
printf '#%02x%02x%02x\n' "$1" "$2" "$3"
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
|
||||
```shell
|
||||
# Output: WIDTHxHEIGHT
|
||||
$ get_window_size
|
||||
1200x800
|
||||
|
||||
# Output (fail):
|
||||
$ get_window_size
|
||||
x
|
||||
$ rgb_to_hex "255" "255" "255"
|
||||
#FFFFFF
|
||||
```
|
||||
|
||||
## Get the current cursor position
|
||||
|
||||
This is useful when creating a TUI in pure bash.
|
||||
# Code Golf
|
||||
|
||||
**Example Function:**
|
||||
## Shorter `for` loop syntax
|
||||
|
||||
```sh
|
||||
get_cursor_pos() {
|
||||
# Usage: get_cursor_pos
|
||||
IFS='[;' read -p $'\e[6n' -d R -rs _ y x _
|
||||
printf '%s\n' "$x $y"
|
||||
```shell
|
||||
# Tiny C Style.
|
||||
for((;i++<10;)){ echo "$i";}
|
||||
|
||||
# Undocumented method.
|
||||
for i in {1..10};{ echo "$i";}
|
||||
|
||||
# Expansion.
|
||||
for i in {1..10}; do echo "$i"; done
|
||||
|
||||
# C Style.
|
||||
for((i=0;i<=10;i++)); do echo "$i"; done
|
||||
```
|
||||
|
||||
## Shorter infinite loops
|
||||
|
||||
```shell
|
||||
# Normal method
|
||||
while :; do echo hi; done
|
||||
|
||||
# Shorter
|
||||
for((;;)){ echo hi;}
|
||||
```
|
||||
|
||||
## Shorter function declaration
|
||||
|
||||
```shell
|
||||
# Normal method
|
||||
f(){ echo hi;}
|
||||
|
||||
# Using a subshell
|
||||
f()(echo hi)
|
||||
|
||||
# Using arithmetic
|
||||
# You can use this to assign integer values.
|
||||
# Example: f a=1
|
||||
# f a++
|
||||
f()(($1))
|
||||
|
||||
# Using tests, loops etc.
|
||||
# NOTE: You can also use ‘while’, ‘until’, ‘case’, ‘(())’, ‘[[]]’.
|
||||
f()if true; then echo "$1"; fi
|
||||
f()for i in "$@"; do echo "$i"; done
|
||||
```
|
||||
|
||||
## Shorter `if` syntax
|
||||
|
||||
```shell
|
||||
# One line
|
||||
# Note: The 3rd statement may run when the 1st is true
|
||||
[[ "$var" == hello ]] && echo hi || echo bye
|
||||
[[ "$var" == hello ]] && { echo hi; echo there; } || echo bye
|
||||
|
||||
# Multi line (no else, single statement)
|
||||
# Note: The exit status may not be the same as with an if statement
|
||||
[[ "$var" == hello ]] && \
|
||||
echo hi
|
||||
|
||||
# Multi line (no else)
|
||||
[[ "$var" == hello ]] && {
|
||||
echo hi
|
||||
# ...
|
||||
}
|
||||
```
|
||||
|
||||
**Example Usage:**
|
||||
## Simpler `case` statement to set variable
|
||||
|
||||
We can use the `:` builtin to avoid repeating `variable=` in a case
|
||||
statement. The `$_` variable stores the last argument of the last
|
||||
successful command. `:` always succeeds so we can abuse it to store the
|
||||
variable value.
|
||||
|
||||
```shell
|
||||
# Output: X Y
|
||||
$ get_cursor_pos
|
||||
1 8
|
||||
# Modified snippet from Neofetch.
|
||||
case "$OSTYPE" in
|
||||
"darwin"*)
|
||||
: "MacOS"
|
||||
;;
|
||||
|
||||
"linux"*)
|
||||
: "Linux"
|
||||
;;
|
||||
|
||||
*"bsd"* | "dragonfly" | "bitrig")
|
||||
: "BSD"
|
||||
;;
|
||||
|
||||
"cygwin" | "msys" | "win32")
|
||||
: "Windows"
|
||||
;;
|
||||
|
||||
*)
|
||||
printf '%s\n' "Unknown OS detected, aborting..." >&2
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# Finally, set the variable.
|
||||
os="$_"
|
||||
```
|
||||
|
||||
<!-- CHAPTER END -->
|
||||
|
||||
Reference in New Issue
Block a user