aboutsummaryrefslogtreecommitdiff
path: root/dunicode
blob: ad1915b93b9acadffc45139797d0ca2476ce6f67 (plain) (blame)
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
90
91
92
93
#!/bin/sh
set -e

data_dir="${XDG_DATA_HOME:-$HOME/.local/share}/unicode"
icon="󰞅 "
menu="wmenu"

note()
{
	: "${1:?}"

	command -v notify-send > /dev/null &&
		notify-send "$icon  dunicode" "$1"
}

die()
{
	: "${1:?}"

	note "$1"
	printf "\033[31;1merr: %b\033[0m\n" "$1" 1>&2
	exit "${2:-1}"
}

dep_check()
{
	: "${1:?}"

	for dep; do
		command -v "$dep" 1>/dev/null ||
			die "$dep not found, please install it" 127
	done

	unset dep
}

get_glyph()
{
	for glyph in "$data_dir"/*
	do
		[ -s "$glyph" ] &&
			str="${str}${glyph##*/}\n"
	done

	[ -z "$str" ] &&
		die "no glyphs available, put them in $data_dir"

	# shellcheck disable=SC2059
	[ -n "${str#*\n}" ] &&
		str="$(printf "$str" | "$menu" -p "$icon")"

	str="$("$menu" -l 25 -p "$icon " < "${data_dir}/${str}")"
	echo "${str%%[[:space:]]*}"
}

########
# MAIN #
########

[ -z "$WAYLAND_DISPLAY" ] && menu="dmenu"

case "$1" in
-h)
	printf "usage: %s %s" "${0##*/}" "[-ct]"
	;;
-c)
	glyph="$(get_glyph)"

	if [ -z "$WAYLAND_DISPLAY" ]
	then
		dep_check "xclip"
		printf "%s" "$glyph" | xclip -selection clipboard
	else
		dep_check "wl-copy"
		printf "%s" "$glyph" | wl-copy
	fi &&
		note "$glyph coppied to clipboard"
	;;
-t|"")
	glyph="$(get_glyph)"

	if [ -z "$WAYLAND_DISPLAY" ]
	then
		dep_check "xdotool"
		xdotool type --delay 20 "$glyph"
	else
		dep_check "wtype"
		wtype -d 20 "$glyph"
	fi
	;;
*)
	die "invalid usage: ${0##*/} $*"
esac