-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzsh-ansimotd.plugin.zsh
executable file
·85 lines (71 loc) · 2.71 KB
/
zsh-ansimotd.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Project: zsh-animotd
# File: /zsh-ansimotd.plugin.zsh
# Created: 2021-11-21 13:05:00
# Author: Clint Plummer
set -o pipefail
export ANSI_MOTD_ART_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/ansimotd"
[ -d "$ANSI_MOTD_ART_DIR" ] || mkdir -p "$ANSI_MOTD_ART_DIR"
# recursively download all zip's from the supplied url into our config dir
# then unpack all displayable art
# eg. ansi_art_download "http://artscene.textfiles.com/artpacks/1996/"
function ansi_art_download {
wget --directory-prefix "$ANSI_MOTD_ART_DIR" --recursive \
--no-verbose --no-clobber --no-parent --level 1 --accept zip "$1"
cd "$ANSI_MOTD_ART_DIR"
for file in **/*.zip
do
# UnZip 6.00
# no long flag options unfortunately
# so run with quiet mode, never overwite, case insensitive case match for the filename
# and extract to a directory of the zip's filename (minus extension)
unzip -q -n -C "$file" '*.ans' '*.img' '*.asc' -d "$ANSI_MOTD_ART_DIR/$file:r"
done
}
# find a random piece of ansi art to display
function ansi_art_random_file {
# if fd is installed let's use that, it's faster (at least in my testing)
# https://github.com/sharkdp/fd
if (( $+commands[fd] )); then
fd --extension ans \
--extension img \
--extension asc \
--absolute-path \
--type f \
--search-path "$ANSI_MOTD_ART_DIR" |\
shuf -n 1
else
find "$ANSI_MOTD_ART_DIR" -type f \
-iname '*.ans' -or \
-iname '*.img' -or \
-iname '*.asc' |\
shuf -n 1
fi;
}
function ansi_art_random {
ansi_filename="$(ansi_art_random_file)"
# optional use pv to limit rate we output
viewer=(cat)
if (( $+commands[pv] )); then
viewer=(pv --quiet --rate-limit ${ANSI_MOTD_RATE_LIMIT_OUTPUT:-1T})
fi
if [ -n "$ansi_filename" ]; then
# turn off automatic margins (a.k.a. line wrapping) if we've been told too
# this is so it'll still render something usable even if the terminal is too narrow
if [ -n "$ANSI_MOTD_DISABLE_LINE_WRAPPING" ]; then print -n '\e[?7l'; fi;
# convert from the original character set (Code page 437)
# see https://en.wikipedia.org/wiki/Code_page_437
iconv -f 437 < $ansi_filename | ${viewer}
# restore automatic margins if we've been told too
if [ -n "$ANSI_MOTD_DISABLE_LINE_WRAPPING" ]; then print -n '\e[?7h'; fi;
# record the filename in this session incase the user wants to find it later
export ANSI_MOTD_FILENAME="$ansi_filename"
else
echo "\
zsh-ansimotd.plugin.zsh:
I couldn't find any ansi art to display, I tried looking in '$ANSI_MOTD_ART_DIR' 😢
For help on getting some checkout the README available at
https://github.com/yuhonas/zsh-ansimotd#getting-some-awesome-ansi-art-to-display
" >&2
fi;
}
ansi_art_random