aboutsummaryrefslogtreecommitdiff
path: root/ponysay
blob: b3c31b883c3b9b6a2d540236a2ca20819065dfed (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
#!/usr/bin/env bash

version=0.9
SYSTEMPONIES=/usr/share/ponysay/ponies
HOMEPONIES="${HOME}/.ponies"
pony="*"
wrap=

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

cmd=cowsay
[[ ${0} == *ponythink ]] && cmd=cowthink

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

list() {
    echo "ponyfiles located in $SYSTEMPONIES:"
    ls -1 $SYSTEMPONIES | sed "s/.pony//"
    if [[ -d $HOMEPONIES ]]; then
	echo "ponyfiles located in $HOMEPONIES:"
	ls -1 $HOMEPONIES | sed "s/.pony//"
    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 ponyfiles.
  -f[name]    Select a pony (Either a filename or a pony name)
  -W[column]  The screen column where the message should be wrapped
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 '\ec'
    fi
    
    function wtrunc
    {
	ponysaytruncater `tput cols || echo 0` 2>/dev/null                    ||
	${HOME}/.local/bin/ponysaytruncater `tput cols || echo 0` 2>/dev/null ||
	./ponysaytruncater `tput cols || echo 0` 2>/dev/null                  ||
	cat
    }
    
    function htrunc
    {
	head --lines=$(( $(stty size <&2 | cut -d ' ' -f 1) - 2 ))
    }
    
    if [ "$TERM" = "linux" ]; then
	if [ "$PONYSAY_BOTTOM" = 'yes' ] || [ "$PONYSAY_BOTTOM" = 'y' ] || [ "$PONYSAY_BOTTOM" = '1' ]; then
	    exec "$cmd" -f "$pony" "${wrap:+-W$wrap}" | wtrunc | tac | htrunc | tac
	else
	    exec "$cmd" -f "$pony" "${wrap:+-W$wrap}" | wtrunc | htrunc
	fi
    else
	exec "$cmd" -f "$pony" "${wrap:+-W$wrap}" | wtrunc
    fi
}

while getopts f:W:lhv OPT
do
    case ${OPT} in
        v)  version; exit ;;
        h)  usage; exit ;;
        f)  pony="$OPTARG" ;;
	l)  list; exit ;;
        W)  wrap="$OPTARG" ;;
        \?) usage >&2; exit 1 ;;
    esac
done
shift $((OPTIND - 1))

if ! hash $cmd &>/dev/null; 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 [[ ! -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