emotty.plugin.zsh
1932 bytes
1# ------------------------------------------------------------------------------
2# FILE: emotty.plugin.zsh
3# DESCRIPTION: Return an emoji for the current $TTY number.
4# AUTHOR: Alexis Hildebrandt (afh[at]surryhill.net)
5# VERSION: 1.0.0
6# DEPENDS: emoji plugin
7#
8# There are different sets of emoji characters available, to choose a different
9# set export emotty_set to the name of the set you would like to use, e.g.:
10# % export emotty_set=nature
11# ------------------------------------------------------------------------------
12
13# Handle $0 according to the standard:
14# https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
150="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
160="${${(M)0:#/*}:-$PWD/$0}"
17
18typeset -gAH _emotty_sets
19local _emotty_plugin_dir="${0:h}"
20source "$_emotty_plugin_dir/emotty_stellar_set.zsh"
21source "$_emotty_plugin_dir/emotty_floral_set.zsh"
22source "$_emotty_plugin_dir/emotty_zodiac_set.zsh"
23source "$_emotty_plugin_dir/emotty_nature_set.zsh"
24source "$_emotty_plugin_dir/emotty_emoji_set.zsh"
25source "$_emotty_plugin_dir/emotty_love_set.zsh"
26unset _emotty_plugin_dir
27
28emotty_default_set=emoji
29
30function emotty() {
31 # Use emotty set defined by user, fallback to default
32 local emotty=${_emotty_sets[${emotty_set:-$emotty_default_set}]}
33
34 # Parse tty number via prompt expansion. %l equals:
35 # - N if tty = /dev/ttyN
36 # - pts/N if tty = /dev/pts/N
37 local tty=${${(%):-%l}##pts/}
38 # Normalize it to an emotty set index
39 (( tty = (tty % ${#${=emotty}}) + 1 ))
40
41 local character_name=${${=emotty}[tty]}
42 echo "${emoji[${character_name}]}${emoji2[emoji_style]}"
43}
44
45function display_emotty() {
46 local name=${1:-$emotty_set}
47 echo $name
48 for i in ${=_emotty_sets[$name]}; do
49 printf "${emoji[$i]}${emoji2[emoji_style]} "
50 done
51 print
52 for i in ${=_emotty_sets[$name]}; do
53 print "${emoji[$i]}${emoji2[emoji_style]} = $i"
54 done
55}