summaryrefslogtreecommitdiff
path: root/.config/wmii-hg/util.sh
blob: 71dbc41e9059ef643190fb6399cd79062711fad5 (plain)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/bash

# I moved "fixes" into a separate file because it isn't so much configuration...
. fixes.sh

################################################################################
# Added shell features                                                         #
################################################################################

##
# Usage: dquote STRING
# Safely double-quotes a string.
# It escapes ways to execute code, but not variables.
##
dquote() {
	local str=$1
	str="${str//\\/\\\\}" # backslash
	str="${str//\"/\\\"}" # dquote
	str="${str//\$(/\\\$(}" # $(...)
	str="${str//\`/\\\`}" # backtick
	printf '"%s"\n' "$str"
}

##
# Usage: expand_variables
# Expands variables read from /dev/stdin
##
expand_variables() {
	while read; do
		eval printf "'%s\n'" "$(dquote "$REPLY")"
	done
}

is_mounted() {
	local dir="$(readlink -m $1)"
	local mntpnt="$(cut -d' ' -f2 /proc/mounts|grep -Fx -- "$dir")"
	[[ $dir = "$mntpnt" ]]
	return $?
}

################################################################################
# PATH manipulation                                                            #
################################################################################

##
# Usage: path_ls PATH
# List executables in PATH (PATH is delimited by `:')
##
path_ls() {
	local dirs
	IFS=: dirs=($@)
	find -L "${dirs[@]}" -maxdepth 1 -type f -executable -printf '%f\n' 2>/dev/null | sort -u
}

##
# Usage: path_which PATH PROGRAM
# Find the full path of PROGRAM by searching PATH
##
path_which() {
	local mypath=$1
	local prog=$2
	local which=$(which which)
	PATH="$mypath" "$which" -- "$prog" 2>/dev/null
}

################################################################################
# wmii convenience functions                                                   #
################################################################################

##
# Usage: lstags
# Lists wmii tags
##
lstags() {
	ls "$WMII_DIR/tag" | sed -e 's@/$@@' -e '/^sel$/d'
}

################################################################################
# X11 functions                                                                #
################################################################################

##
# Usage: connected_to_x_server
# Return status indicates whether there is an X server at $DISPLAY
##
connected_to_x_server() {
	xdpyinfo &>/dev/null
	return $?
}

################################################################################
# My wmii configuration                                                        #
################################################################################

##
# Usage: scansection [SECTION]
# Reads the doc comments from a section of wmiirc.
# If SECTION is not given, it reads all doc comments.
##
scansection() {
	local file=$(conffile config.sh)
	local sec=$1
	local tmp=$(mktemp --tmpdir wmii-scansecion.XXXXXXXXXX)
	# Isolate the sections we want.
	if [ -n "$sec" ]; then
		# Find the section
		< "$file" sed -n "/^\s*$sec\s*()/,/##\s*End $sec/p" | sed '1d;$d'> "$tmp"
	else
		# Remove extra lines that mark the end of a section
		< "$file" sed "/\s*}\s*##\s*End\s/d" > "$tmp"
	fi
	< "$tmp" sed -n '/##/p' | while read; do
		var="$(echo "$REPLY" | sed -nr 's/^\s*(.*)\)\s*##.*/\1/p')"
		comment="$(echo "$REPLY" | sed -r 's/.*## ?//')"
		if [ -z "$var" ]; then
			printf '%s\n' "$comment"
		else
			printf '\t%s\t%s\n' "$(echo "$var"|expand_variables)" "$comment"
		fi
	done
	rm -- "$tmp"
}

##
# Usage: conffile FILE
##
conffile() {
	echo "$HOME/.wmii-hg/$1"
}