-
Notifications
You must be signed in to change notification settings - Fork 0
/
manksay.mank
223 lines (202 loc) · 4.74 KB
/
manksay.mank
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
# ___________
# < Mank say! >
# -----------
# \
# \ _^_ _^_
# \ | x x |
# \ (| ___ |)
# | / \ |
# |_______|
# |
# A fun little terminal app like cowsay
# Things that'd make this code eaiser:
# - For each loops
# - Capture by reference for lambdas
# - for i in <max> abbreviation
const MAX_BOX_WIDTH := 40;
const BLANKS_NEWLINE_THRES := 8;
fun wrap_text: str[] (s: str, max_width: i32) {
# Greedy text wrapping
lines := new_vec@(str)();
words := split_str(s, \c -> {
# These are a little silly (think of a cleaner way -- too sleepy now)
if c == ' ' || c == '\r' {
SplitAction::CONSUME
} else if c == '\n' || c == '\t' {
SplitAction::KEEP
} else {
SplitAction::NOT_DELIM
}
});
current_line := "";
blanks_in_row := 0;
first_word := true;
last_was_newline := false;
for i in 0 .. words.length {
word := words[i];
tab := false;
if word.length == 0 || { tab = word[0] == '\t'; tab } {
blanks_in_row += 1 * if tab { 8 } else { 1 };
continue;
}
# Hacky newline adding -- makes a cleaner looking output (more like cowsay)
add_newline := {
if word[0] == '\n' {
if !last_was_newline {
last_was_newline = true;
false
} else {
true
}
} else if last_was_newline && blanks_in_row >= BLANKS_NEWLINE_THRES {
last_was_newline = false;
true
} else {
false
}
};
if add_newline {
push_back(lines, current_line);
push_back(lines, "");
current_line = "";
first_word = true;
}
if word[0] == '\n' { continue; }
blanks_in_row = 0;
last_was_newline = false;
# FIXME: This is kinda wasteful
if utf8_str_len(current_line + word) > max_width {
push_back(lines, current_line);
current_line = "";
first_word = true;
}
padding := if (!first_word) { " " } else { "" };
current_line += padding + word;
first_word = false;
}
if current_line.length > 0 {
push_back(lines, current_line);
}
lines
}
# Bit of a hack
enum SplitAction {
CONSUME,
KEEP,
NOT_DELIM
}
fun split_str: str[] (s: str, is_delim: \char -> SplitAction) {
# Naive string split (seems to conform to Python's)
out := new_vec@(str)();
current := "";
for i in 0 .. s.length {
c := s[i];
delim_action := is_delim(c);
if delim_action.tag == SplitAction::NOT_DELIM.tag {
current += c as str;
} else {
push_back(out, current);
current = "";
if delim_action.tag == SplitAction::KEEP.tag {
push_back(out, c as str);
}
}
if i == s.length - 1 {
push_back(out, current);
}
}
out
}
fun max_line_len: i32 (lines: str[]) {
max_len := 0;
for i in 0 .. lines.length {
len := utf8_str_len(lines[i]);
if len > max_len {
max_len = len;
}
}
max_len
}
fun utf8_str_len: i32 (s: str) {
# This still can't handle double-width(?) unicode characters
len := 0;
for i in 0 .. s.length {
len += ((s[i] & 0xc0 as char) != 0x80 as char) as i32;
}
len
}
fun repeat_char: str (s: char, count: i32) {
# Not great
out := "";
for i in 0 .. count {
out += s as str;
}
out
}
fun box_lines: str[] (lines: str[]) {
boxed := new_vec@(str)();
max_width := max_line_len(lines);
top := " " + repeat_char('_', max_width + 2) + " ";
push_back(boxed, top);
if lines.length == 1 {
push_back(boxed, "< " + lines[0] + " >");
} else {
for i in 0 .. lines.length {
line := lines[i];
bind (start_char, end_char) = if i == 0 {
('/', '\\')
} else if i == lines.length - 1 {
('\\', '/')
} else {
('|', '|')
};
push_back(boxed, {
start_char as str + " "
+ line + repeat_char(' ', max_width - utf8_str_len(line)) + " "
+ end_char as str
});
}
}
bottom := " " + repeat_char('-', max_width + 2) + " ";
push_back(boxed, bottom);
boxed
}
proc mank_say(s: str) {
mr_mank := (
" \\ \n"
+ " \\ _^_ _^_ \n"
+ " \\ | x x | \n"
+ " \\ (| ___ |) \n"
+ " | / \\ | \n"
+ " |_______| \n"
+ " | \n"
);
wrapped := wrap_text(s, MAX_BOX_WIDTH);
if wrapped.length == 0 {
push_back(wrapped, "");
}
mesage := box_lines(wrapped);
for i in 0 .. mesage.length {
println(mesage[i]);
}
println(mr_mank);
}
proc main (args: str[]) {
mank_say(
if args.length < 2 {
read_raw_input()
} else {
args[1]
})
}
fun read_raw_input: str {
raw_inpit := "";
loop {
c := getchar();
if c == -1 {
break;
}
raw_inpit += c as str;
}
raw_inpit
}