aboutsummaryrefslogtreecommitdiff
path: root/ponysay
blob: 0d84cd80211920c83405e15d221be43f0e1c6f8d (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env bash

version=1.0
SYSTEMPONIES="/usr/share/ponysay/ponies"
HOMEPONIES="${HOME}/.local/share/ponysay/ponies"
SCRIPTDIR="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # Get bash script directory
pony="*"
wrap=""

if [ "$TERM" = "linux" ]; then
	SYSTEMPONIES="/usr/share/ponysay/ttyponies"
	HOMEPONIES="${HOME}/.local/share/ponysay/ttyponies"
fi

if [[ "$PONYSAY_COWSAY" = "" ]]; then
	cmd=cowsay
	customcmd=0
else
	cmd="$PONYSAY_COWSAY"
	customcmd=1
fi

[[ ${0} == *ponythink ]] &&
if [[ "$PONYSAY_COWTHINK" = "" ]]; then
	cmd=cowthink
	customcmd=0
else
	cmd="$PONYSAY_COWTHINK"
	customcmd=1
fi

version() {
	echo "ponysay v$version"
}

scrw=`(stty size <&2 || echo 0 0) | cut -d ' ' -f 2`
listcmd="${SCRIPTDIR}/../lib/ponysay/list.pl"
truncatercmd="${SCRIPTDIR}/../lib/ponysay/truncater"

haspq=1
if [[ -f './pq4ps-list' ]]; then
	function qlist {
		./pq4ps-list
	}
elif [[ -f '/usr/bin/pq4ps-list' ]]; then
	function qlist {
		/usr/bin/pq4ps-list
	}
else
	haspq=0
	function qlist {
		cat
	}
fi

list() {
	echo -e "\\e[01mponyfiles located in $SYSTEMPONIES:\\e[21m"
	perl $listcmd $scrw $(ls --color=no $SYSTEMPONIES | sed -e 's/\.pony$//' | sort) | qlist
	
	if [[ -d $HOMEPONIES ]]; then
		echo -e "\\e[01mponyfiles located in $HOMEPONIES:\\e[21m"
		perl $listcmd $scrw $(ls --color=no $HOMEPONIES | sed -e 's/\.pony$//' | sort) | qlist
	fi
}

_linklist() {
	echo -e "\\e[01mponyfiles located in $1:\\e[21m"
	files=$(ls --color=no $1 | sed -e 's/\.pony$//' | sort)
	
	args=""
	
	for file in $files; do
		target=$(readlink $1"/"$file".pony")
		
		if [[ $target = "" ]]; then
			target=$file
		else
			target=$(echo $target | sed -e 's/^\.\///g' -e 's/\.pony$//g')
		fi
		
		args=$(echo $args $file $target)
	done
	
	scrw=`(stty size <&2 || echo 0 0) | cut -d ' ' -f 2`
	
	perl '/dev/stdin' $(echo $args) <<EOF | sed -e 's/ /_/g' > '/dev/shm/.ponysay~'
#!/usr/bin/perl

#Author: Mattias Andrée (maandree@kth.se)

%hash = ();
\$argc = @ARGV;

\$i = 0;
while (\$i < \$argc)
{
    \$source = \$ARGV[\$i];
    \$i += 1;
    \$target = \$ARGV[\$i];
    \$i += 1;
    if (\$source eq \$target)
    {
        \$hash{\$source} = [ () ];
    }
}

\$i = 0;
while (\$i < \$argc)
{
    \$source = \$ARGV[\$i];
    \$i += 1;
    \$target = \$ARGV[\$i];
    \$i += 1;
    unless (\$source eq \$target)
    {
        push @{ \$hash{\$target} }, \$source;
    }
}

\$i = 0;
while (\$i < \$argc)
{
    \$source = \$ARGV[\$i];
    \$i += 1;
    \$target = \$ARGV[\$i];
    \$i += 1;
    if (\$source eq \$target)
    {
        @list = @{ \$hash{\$source} };
        \$first = 1;
        print \$source;
        foreach \$link (@list)
        {
            if (\$first eq 1)
            {
                print " (".\$link;
                \$first = 0;
            }
            else
            {
                print " ".\$link;
            }
        }
        if (\$first eq 0)
        {
            print ")";
        }
        print "\n";
    }
}
EOF
	
	perl $listcmd $scrw $(cat "/dev/shm/.ponysay~") | sed -e 's/_/ /g' | qlist
	rm '/dev/shm/.ponysay~'
}

linklist() {
	_linklist $SYSTEMPONIES
	
	if [[ -d $HOMEPONIES ]]; then
		_linklist $HOMEPONIES
	fi
}

usage() {
	version
	cat <<EOF

Usage:
${0##*/} [options] [message]

If [message] is not provided, reads the message from STDIN.

Options:
  -v          Show version and exit.
  -h          Show this help and exit.
  -l          List pony files.
  -L          List pony files with synonyms inside brackets.
EOF
	
	if [[ $haspq = 1 ]]; then
		cat <<EOF
  -q          Use the pony quote feature.
EOF
	fi
	
	cat <<EOF
  -f[name]    Select a pony (either a file name or a pony name.)
  -W[column]  The screen column where the message should be wrapped.

See man ponysay(6) for more information.
EOF
}

# if no stdin and no arguments then print usage and exit
if [[ -t 0 && $# == 0 ]]; then
	usage
	exit
fi

say() {
	# Ponies use UTF-8 drawing characters. Prevent a Perl warning.
	export PERL_UNICODE=S
	
	if [ "$TERM" = "linux" ]; then
		echo -ne '\e[H\e[2J'
	fi
	
	function wtrunc {
		if [ "$PONYSAY_FULL_WIDTH" = 'yes' ] || [ "$PONYSAY_FULL_WIDTH" = 'y' ] || [ "$PONYSAY_FULL_WIDTH" = '1' ]; then
			cat
		else
			WIDTH=`(stty size <&2 || echo 0 0) | cut -d ' ' -f 2`
			$truncatercmd $WIDTH 2>/dev/null || cat
		fi
	}
	
	if [ "$PONYSAY_SHELL_LINES" = "" ]; then
		PONYSAY_SHELL_LINES=2
	fi
	
	function htrunchead {
		head --lines=$(( `stty size <&2 | cut -d ' ' -f 1` - $PONYSAY_SHELL_LINES ))
	}
	
	function htrunctail {
		tail --lines=$(( `stty size <&2 | cut -d ' ' -f 1` - $PONYSAY_SHELL_LINES ))
	}
	
	if [[ $customcmd = 0 ]]; then
		function cowcmd {
			pcmd='#!/usr/bin/perl\nuse utf8;'
			ccmd=$(for c in $(echo $PATH":" | sed -e 's/:/\/'"$cmd"' /g'); do if [[ -f $c ]]; then echo $c; break; fi done)
			
			if [[ ${0} == *ponythink ]]; then
				cat <(echo -e $pcmd) $ccmd > "/dev/shm/.ponythink"
				perl '/dev/shm/.ponythink' "$@"
				rm '/dev/shm/.ponythink'
			else
				perl <(cat <(echo -e $pcmd) $ccmd) "$@"
			fi
		}
	else
		function cowcmd	{
			echo "custom $cmd"
			$cmd "$@"
		}
	fi
	
	if [ "$TERM" = "linux" ] || [ "$PONYSAY_TRUNCATE_HEIGHT" = 'yes' ] || [ "$PONYSAY_TRUNCATE_HEIGHT" = 'y' ] || [ "$PONYSAY_TRUNCATE_HEIGHT" = '1' ]; then
		if [ "$PONYSAY_BOTTOM" = 'yes' ] || [ "$PONYSAY_BOTTOM" = 'y' ] || [ "$PONYSAY_BOTTOM" = '1' ]; then
			cowcmd -f "$pony" "${wrap:+-W$wrap}" | wtrunc | htrunctail
		else
			cowcmd -f "$pony" "${wrap:+-W$wrap}" | wtrunc | htrunchead
		fi
	else
		cowcmd -f "$pony" "${wrap:+-W$wrap}" | wtrunc
	fi
}

ponies=()

opts="f:W:Llhv"
if [[ $haspq ]]; then
	opts=$opts"q"
fi

usepq=0
while getopts $opts OPT; do
	case ${OPT} in
		v)  version; exit ;;
		h)  usage; exit ;;
		f)  ponies+=($(echo $OPTARG | sed -e 's/ //g')) ;;
		l)  list; exit ;;
		L)  linklist; exit ;;
		W)  wrap="$OPTARG" ;;
		q)  usepq=1 ;;
		\?) usage >&2; exit 1 ;;
	esac
done
shift $((OPTIND - 1))

if [[ $usepq = 1 ]]; then
	if [[ -f './pq4ps' ]]; then
		function q {
			./pq4ps $@
		}
	elif [[ -f '/usr/bin/pq4ps' ]]; then
		function q {
			/usr/bin/pq4ps $@
		}
	fi
	$0 ${wrap:+-W$wrap} $(q "$*")
	exit
fi

hash $cmd &>/dev/null; if [ $? -ne 0 ]; then
	cat >&2 <<EOF
You don't seem to have the $cmd program.
Please install it in order to use this wrapper.

Alternatively, symlink it to '$cmd' in anywhere in \$PATH
if it actually exists under a different filename.
EOF
	exit 1
fi

if [[ ! ${#ponies[@]} == 0 ]]; then
	pony="${ponies[$RANDOM%${#ponies[@]}]}"
fi

if [[ ! -f $pony ]]; then
	# Pony not a file? Search for it
	
	ponies=()
	[[ -d $SYSTEMPONIES ]] && ponies+=( "$SYSTEMPONIES"/$pony.pony )
	[[ -d $HOMEPONIES ]]   && ponies+=( "$HOMEPONIES"/$pony.pony )
	
	if (( ${#ponies} < 1 )); then
		echo >&2 "All the ponies are missing! Call the Princess!"
		exit 1
	fi
	
	# Choose a random pony
	pony="${ponies[$RANDOM%${#ponies[@]}]}"
fi

if [[ -n "$*" ]]; then
	# Handle a message given via arguments
	say <<<"$*"
else
	say
fi