lib.sh RawHistoryFolder
1# shellcheck shell=bash disable=SC2034
2# Shared helpers for the bundled modules. Source this file: . "$(dirname "$0")/lib.sh"
3#
4# The bar gives every script its colors, each as #rrggbb, so a script never
5# hard-codes one:
6# NEFERBAR_BACKGROUND the bar's own background
7# NEFERBAR_FOREGROUND the bar's text color
8# NEFERBAR_ACCENT the highlight color: bar.accent in the config picks
9# which of the 16 theme colors it is
10# NEFERBAR_COLOR0..15 the theme's 16 ANSI colors
11
12: "${NEFERBAR_BACKGROUND:=#26263a}" "${NEFERBAR_FOREGROUND:=#cdd6f4}"
13: "${NEFERBAR_COLOR0:=#45475a}" "${NEFERBAR_COLOR2:=#a6e3a1}" "${NEFERBAR_COLOR3:=#f9e2af}"
14: "${NEFERBAR_COLOR4:=#89b4fa}" "${NEFERBAR_COLOR15:=#a6adc8}"
15: "${NEFERBAR_ACCENT:=$NEFERBAR_COLOR4}"
16
17RESET=$'\e[0m' BOLD=$'\e[1m'
18
19# Nerd Font icons as raw UTF-8 bytes. Unlike \u escapes, these do not depend on
20# the locale of the process that runs the script.
21ICON_DOT=$'\xef\x84\x91' # U+F111 circle
22ICON_STASH=$'\xef\x86\x87' # U+F187 archive
23ICON_APP=$'\xef\x8b\x90' # U+F2D0 window
24
25# rgb "#rrggbb" -> "r;g;b"
26rgb() {
27 local h=${1#\#}
28 printf '%d;%d;%d' "0x${h:0:2}" "0x${h:2:2}" "0x${h:4:2}"
29}
30fg() { printf '\e[38;2;%sm' "$(rgb "$1")"; }
31bg() { printf '\e[48;2;%sm' "$(rgb "$1")"; }
32
33# mix "#a" "#b" PERCENT -> "#rrggbb", PERCENT of the way from a to b
34mix() {
35 local a=${1#\#} b=${2#\#} p=$3 i out=#
36 for i in 0 2 4; do
37 out+=$(printf '%02x' $(((0x${a:i:2} * (100 - p) + 0x${b:i:2} * p) / 100)))
38 done
39 printf '%s' "$out"
40}
41
42# fade FROM TO -> four blank cells that blend FROM into TO
43fade() {
44 local i
45 for i in 20 40 60 80; do printf '%s ' "$(bg "$(mix "$1" "$2" "$i")")"; done
46}
47
48BAR=$NEFERBAR_BACKGROUND
49ACCENT=$NEFERBAR_ACCENT
50DIM=$(mix "$BAR" "$NEFERBAR_FOREGROUND" 45)
51
52# ink "#accent" -> the text color for a block of that color: the theme's black
53# on a light accent, its white on a dark one.
54ink() {
55 local h=${1#\#}
56 if (((0x${h:0:2} * 299 + 0x${h:2:2} * 587 + 0x${h:4:2} * 114) / 1000 > 140)); then
57 printf '%s' "$NEFERBAR_COLOR0"
58 else
59 printf '%s' "$NEFERBAR_COLOR15"
60 fi
61}
62
63# state_changed_loop CMD... runs CMD now and again whenever $NEFERWL_STATE
64# (NeferWL's state file) changes. NeferWL replaces the file on every change, so
65# a newer modification time is enough, and "read -t" waits without forking.
66state_changed_loop() {
67 local stamp tick
68 stamp=$(mktemp) && trap 'rm -f "$stamp"' EXIT
69 exec {tick}<> <(:)
70 "$@"
71 touch -r "$NEFERWL_STATE" "$stamp" 2>/dev/null
72 while read -r -t 0.1 -u "$tick" || true; do
73 if [[ $NEFERWL_STATE -nt $stamp ]]; then
74 touch -r "$NEFERWL_STATE" "$stamp"
75 "$@"
76 fi
77 done
78}