forked from lukesampson/cowsay-psh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcowsay.ps1
159 lines (134 loc) · 3.79 KB
/
cowsay.ps1
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
##
## Cowsay 3.03
##
## Original cowsay (c) 1999-2000 Tony Monroe.
## http://www.nog.net/~tony/warez/cowsay-3.03.tar.gz
##
## Powershell port by Luke Sampson 2013
##
. "$psscriptroot\lib\opts.ps1"
function display_usage {
"
cow{say,think} version $version, (c) 1999 Tony Monroe
Usage: $progname [-bdgpstwy] [-h] [-e eyes] [-f cowfile]
[-l] [-n] [-T tongue] [-W wrapcolumn] [message]
"
}
function list_cowfiles($path) {
echo "Cow files in $($path -replace [regex]::escape($home), "~") `:"
gci $path -filter "*.cow" |
sort name |
% { " $($_.name -replace '\.cow$', '')" }
}
function slurp_input($in, $ar) {
if(!$ar) {
$in
} else {
if($opts.n) { display_usage; exit 1 }
[string]::join(' ', ($ar | % {
if($_ -is [array]) { [string]::join(', ', $_ ) }
else { $_ }
}) )
}
}
function maxlength($msg) {
$l= 0; $m = -1
$msg | % {
$l = $_.length
if($l -gt $m) { $m = $l }
}
return $m
}
function construct_balloon($msg, $think) {
$balloon_lines = @()
$thoughts = " "
if(!$msg) { return $balloon_lines, $thoughts }
$max = maxlength $msg
$max2 = $max + 2 # border space fudge
$format = "{0} {1,-$max} {2}"
$border = @() # up-left, up-right, down-left, down-right, left, right
if($think) {
$thoughts = 'o'
$border = '()()()'.tochararray()
} elseif ($msg.length -lt 2) {
$thoughts = '\'
$border = '<>'.tochararray()
} else {
$thoughts = '\'
$border = '/\\/||'.tochararray()
}
$middle = if($msg.length -lt 3) { $null } else {
$msg[1..($msg.length-2)] | % { [string]::format($format, $border[4], $_, $border[5]) }
}
$last = if($msg.length -lt 2) { $null } else {
[string]::format($format, $border[2], $msg[-1], $border[3])
}
$balloon_lines +=
" $('-'*$max2) ",
[string]::format($format, $border[0], $msg[0], $border[1])
if($middle) { $balloon_lines += $middle }
$balloon_lines +=
$last,
" $('-'*$max2) "
($balloon_lines | ? { $_ -ne $null }), $thoughts
}
function get_cow($f, $path, $vars) {
if(!$f.endsWith('.cow')) { $f += ".cow" }
$fpath = "$path\$f"
if(!(test-path $fpath)) { "$script:progname: could not find $f cowfile!"; exit 1 }
$script = gc -raw $fpath
$the_cow = ""
iex $script
$the_cow
}
$version = "3.03";
$progname = $myInvocation.myCommand.name
if($myInvocation.pscommandpath) {
$progname = (split-path $myInvocation.pscommandpath -leaf)
}
$progname = $progname -replace '\.[^\.]+$', ''
$eyes = "oo";
$tongue = " ";
$cowpath = "$psscriptroot\cows"
$opts = @{
'e' = 'oo'
'f' = 'default.cow'
'n' = 0
'T' = ' '
'W' = 40
}
try {
$opts, $args = getopts $args 'bde:f:ghlLnNpstT:wW:y' $opts
} catch {
"$progname`: $_"; display_usage; exit 1;
}
if($opts.h) { display_usage; exit 0 }
if($opts.l) { list_cowfiles $cowpath; exit 0 }
$borg = $opts.b
$dead = $opts.d
$greedy = $opts.g
$paranoid = $opts.p
$stoned = $opts.s
$tired = $opts.t
$wired = $opts.w
$young = $opts.y
$eyes = $opts.e.substring(0, 2)
$tongue = $opts.T.substring(0, 2)
$message = slurp_input $input $args
if($message -is [string]) {
$message = $message.split("`n")
}
# todo: wrap
$balloon_lines, $thoughts = construct_balloon $message ($progname -eq 'cowthink')
# construct_face (original sub uses vars from parent scope)
if ($borg) { $eyes = "==" }
if ($dead) { $eyes = "xx"; $tongue = "U " }
if ($greedy) { $eyes = '$$' }
if ($paranoid) { $eyes = "@@" }
if ($stoned) { $eyes = "**"; $tongue = "U " }
if ($tired) { $eyes = "--" }
if ($wired) { $eyes = "OO" }
if ($young) { $eyes = ".." }
$the_cow = get_cow $opts.f $cowpath
echo ([string]::join("`n", $balloon_lines))
echo $the_cow