-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathemoji-prompt.sh
89 lines (80 loc) · 3.17 KB
/
emoji-prompt.sh
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
86
87
88
89
# Return a list of repeated shuffles of the input list by building and
# running a python script. The length of the result is the first argument
# to the fn. This funcion is only used to build the list of emoji at the
# start of a new terminal, when this file is sourced. From then on, emoji
# are simply chosen from those pre-shuffled lists.
SHUFFLE_LIST() {
local num=$(( 0 + $1 )) # error here, not python
shift
local things=($@)
if (( ${#things[@]} == 0 )); then
echo ''
else
local PY
read -d '' PY <<EOF
# -*- coding: UTF-8 -*-
from __future__ import print_function
import random
e='${things[@]}'.strip().split()
all=[]
while len(all) < $num:
random.shuffle(e)
all += e
print(' '.join(all[:$num]))
EOF
echo $(echo "$PY" | python)
fi
}
# Pick an element based on time & pid of current shell ($$). It changes every
# hour, and is different for each shell.
RAND_ELEMENT_BY_TIME () {
# Put the args into a named list, so we can use it as an array
local things=($@)
local len=${#things[@]}
if (( $len == 0 )); then
echo ''
else
# Index by hour, day, month, to avoid loops. But that's best if list is
# long or the repeat-sequence is jittered. Use bash to eval the
# expression, cuz some remote systems don't have bc.
local hour_of_year=$[$(date '+ ( %_H + 24*(%_d-1) + 24*31*(%_m-1) )')]
local index=$(( $hour_of_year % $len ))
echo "${things[$index]}"
fi
}
# NOTE: it's important that emojis are separated by space, and the last one
# isn't followed by a space.
HOURS_IN_MONTH=$(( 24 * 31 ))
MORNING_EMOJI=$(SHUFFLE_LIST $HOURS_IN_MONTH 🌄 ☕️ 🍳 🍞 🐓 🐔)
DAY_EMOJI=$(SHUFFLE_LIST $HOURS_IN_MONTH 🌲 🌳 🌴 🌵 🌷 🌺 🌸 🌹 🌻 🌼 💐 🌾 🌿 🍀 🍁 🍂 🍃 🍄 ⛅️ ☔️ 🌈 🌊 🗻 🌍 🌞 💻 🚽 📚 🔪 )
FOOD_EMOJI=$(SHUFFLE_LIST $HOURS_IN_MONTH 🍔 🍕 🍖 🍗 🍘 🍙 🍚 🍛 🍜 🍝 🍞 🍟 🍣 🍤 🍥 🍱 🍲 🍳 🍴)
SNACK_EMOJI=$(SHUFFLE_LIST $HOURS_IN_MONTH 🍏 🍇 🍉 🍊 🍌 🍍 🍑 🍒 🍓 🍡 🍢 🍦 🍧 🍨 🍩 🍪 🍫 🍬 🍭 🍮 🍰)
DRINK_EMOJI=$(SHUFFLE_LIST $HOURS_IN_MONTH 🍷 🍸 🍶 🍹 🍺 🍻)
NIGHT_EMOJI=$(SHUFFLE_LIST $HOURS_IN_MONTH 😴 🌠 🌑 🌒 🌔 🌖 🌘 🌚 🌝 🌛 🌜 ⛺️ 🌃 🌉 🌌)
EMOJI_SUFFIX='\[\033[0;30m\]¸\[\033[0m\]'
# This is the function to use if you only want the emoji itself
CURRENT_EMOJI_RAW() {
local now=$(date +%k%M)
local TE="$NIGHT_EMOJI"
if [ $now -lt 600 ]; then
TE="$NIGHT_EMOJI"
elif [ $now -lt 1000 ]; then
TE="$MORNING_EMOJI"
elif [ $now -lt 1200 ]; then
TE="$DAY_EMOJI"
elif [ $now -lt 1330 ]; then
TE="$FOOD_EMOJI"
elif [ $now -lt 1700 ]; then
TE="$DAY_EMOJI"
elif [ $now -lt 1900 ]; then
TE="$SNACK_EMOJI"
elif [ $now -lt 2200 ]; then
TE="$DRINK_EMOJI"
fi
echo "$(RAND_ELEMENT_BY_TIME $TE)"
}
# This is probbaly the function a prompt should use to get the current emoji,
# including a smal black character, to help with width alignment.
CURRENT_EMOJI() {
echo -e "$(CURRENT_EMOJI_RAW)$EMOJI_SUFFIX"
}