emoji.plugin.zsh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # emoji plugin
  2. #
  3. # Makes emoji support available within ZSH
  4. #
  5. # See the README for documentation.
  6. # Handle $0 according to the standard:
  7. # https://zdharma-continuum.github.io/Zsh-100-Commits-Club/Zsh-Plugin-Standard.html
  8. 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
  9. 0="${${(M)0:#/*}:-$PWD/$0}"
  10. _omz_emoji_plugin_dir="${0:h}"
  11. () {
  12. local LC_ALL=en_US.UTF-8
  13. typeset -gAH emoji_skintone
  14. source "$_omz_emoji_plugin_dir/emoji-char-definitions.zsh"
  15. unset _omz_emoji_plugin_dir
  16. # These additional emoji are not in the definition file, but are useful in conjunction with it
  17. # This is a combining character that can be placed after any other character to surround
  18. # it in a "keycap" symbol.
  19. # The digits 0-9 are already in the emoji table as keycap_digit_<N>, keycap_ten, etc.
  20. # It's unclear whether this should be in the $emoji array, because those characters are all ones
  21. # which can be displayed on their own.
  22. emoji[regional_indicator_symbol_letter_d_regional_indicator_symbol_letter_e]=$'\xF0\x9F\x87\xA9\xF0\x9F\x87\xAA'
  23. emoji[regional_indicator_symbol_letter_g_regional_indicator_symbol_letter_b]=$'\xF0\x9F\x87\xAC\xF0\x9F\x87\xA7'
  24. emoji[regional_indicator_symbol_letter_c_regional_indicator_symbol_letter_n]=$'\xF0\x9F\x87\xA8\xF0\x9F\x87\xB3'
  25. emoji[regional_indicator_symbol_letter_j_regional_indicator_symbol_letter_p]=$'\xF0\x9F\x87\xAF\xF0\x9F\x87\xB5'
  26. emoji[regional_indicator_symbol_letter_k_regional_indicator_symbol_letter_r]=$'\xF0\x9F\x87\xB0\xF0\x9F\x87\xB7'
  27. emoji[regional_indicator_symbol_letter_f_regional_indicator_symbol_letter_r]=$'\xF0\x9F\x87\xAB\xF0\x9F\x87\xB7'
  28. emoji[regional_indicator_symbol_letter_e_regional_indicator_symbol_letter_s]=$'\xF0\x9F\x87\xAA\xF0\x9F\x87\xB8'
  29. emoji[regional_indicator_symbol_letter_i_regional_indicator_symbol_letter_t]=$'\xF0\x9F\x87\xAE\xF0\x9F\x87\xB9'
  30. emoji[regional_indicator_symbol_letter_u_regional_indicator_symbol_letter_s]=$'\xF0\x9F\x87\xBA\xF0\x9F\x87\xB8'
  31. emoji[regional_indicator_symbol_letter_r_regional_indicator_symbol_letter_u]=$'\xF0\x9F\x87\xB7\xF0\x9F\x87\xBA'
  32. # Easier access to skin tone modifiers
  33. emoji_skintone[1_2]=$'\U1F3FB'
  34. emoji_skintone[3]=$'\U1F3FC'
  35. emoji_skintone[4]=$'\U1F3FD'
  36. emoji_skintone[5]=$'\U1F3FE'
  37. emoji_skintone[6]=$'\U1F3FF'
  38. }
  39. # Prints a random emoji character
  40. #
  41. # random_emoji [group]
  42. #
  43. function random_emoji() {
  44. local group=$1
  45. local names
  46. if [[ -z "$group" || "$group" == "all" ]]; then
  47. names=(${(k)emoji})
  48. else
  49. names=(${=emoji_groups[$group]})
  50. fi
  51. local list_size=${#names}
  52. [[ $list_size -eq 0 ]] && return 1
  53. local random_index=$(( ( RANDOM % $list_size ) + 1 ))
  54. local name=${names[$random_index]}
  55. if [[ "$group" == "flags" ]]; then
  56. echo ${emoji_flags[$name]}
  57. else
  58. echo ${emoji[$name]}
  59. fi
  60. }
  61. # Displays a listing of emoji with their names
  62. #
  63. # display_emoji [group]
  64. #
  65. function display_emoji() {
  66. local group=$1
  67. local names
  68. if [[ -z "$group" || "$group" == "all" ]]; then
  69. names=(${(k)emoji})
  70. else
  71. names=(${=emoji_groups[$group]})
  72. fi
  73. # The extra spaces in output here are a hack for readability, since some
  74. # terminals treat these emoji chars as single-width.
  75. local counter=1
  76. for i in $names; do
  77. if [[ "$group" == "flags" ]]; then
  78. printf '%s ' "$emoji_flags[$i]"
  79. else
  80. printf '%s ' "$emoji[$i]"
  81. fi
  82. # New line every 20 emoji, to avoid weirdnesses
  83. if (($counter % 20 == 0)); then
  84. printf "\n"
  85. fi
  86. let counter=$counter+1
  87. done
  88. print
  89. for i in $names; do
  90. if [[ "$group" == "flags" ]]; then
  91. echo "${emoji_flags[$i]} = $i"
  92. else
  93. echo "${emoji[$i]} = $i"
  94. fi
  95. done
  96. }