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
|
#![feature(box_syntax)]
use anyhow::Result;
use blubcat::{
color::Color,
pattern::{Gradient, Rainbow, Sequence, Solid},
transform::{Matrix, Polar, Translate, Transpose},
Sample,
};
use std::{
env,
f64::consts::PI,
fs::File,
io::{stdin, BufRead, BufReader, Read},
process::exit,
};
static HELP: &str = "
Usage: blubcat [OPTION...] [FILE...]
Concatenate FILE(s) to standard output, colorizing the output according to OPTION.
With no FILE, or when FILE is -, the standard input is read.
OPTION can be PATTERN, TRANSFORM or PRESET.
Patterns are stored on a stack.
<pop>,<push> indicates the number of patters popped from the stack and how many are pushed back on.
PATTERN:
0,1 -R --rainbow Sinebow
0,1 -K --const <color> Solid color
0,1 -S --sequence <c1,c2,c3,...> Sequences colors on the X-axis
0,1 -G --gradient <c1,c2,c3,...> Interpolates the colors on the X-axis
TRANSFORM:
1,1 -r --rotate <angle>
1,1 -s --scale <factor>
1,1 -m --matrix <x> <y> <z> <w>
1,1 -t --translate <x> <y>
1,1 -T --transpose
1,1 -p --polar
2,1 --mix
2,1 --add
2,1 --subtract
2,1 --multiply
PRESET:
0,1 --zebra
0,1 --pride --trans
0,1 --ukraine ofc this is needed
COLOR:
<rrggbb>
black white red green blue yellow cyan magenta
";
fn main() -> Result<()> {
let mut args = env::args().skip(1);
let mut arg_extra = None;
let mut pat: Vec<Box<dyn Sample>> = vec![];
loop {
let a = args.next();
let mut arg_next = || args.next();
let mut arg_num = || {
arg_next()
.expect("value expected")
.parse::<f64>()
.expect("not a number")
};
if let Some(a) = a {
if !a.starts_with("-") || a == "--" || a == "-" {
arg_extra = Some(a);
break;
}
let mut push_queue: Vec<Box<dyn Sample>> = vec![];
match a.as_str() {
"-?" | "--help" => {
println!("{}", HELP);
exit(0)
}
/* PRESETS */
"--pride" => pat.push(flag_helper(&[
"e50000", "ff8d00", "ffee00", "028121", "004cff", "770088",
])),
"--zebra" => pat.push(flag_helper(&["000000", "ffffff"])),
"--trans" => pat.push(flag_helper(&[
"5bcffb", "f5abb9", "ffffff", "f5abb9", "5bcffb",
])),
"--ukraine" => pat.push(flag_helper(&["0057B8", "FFD700"])),
/* PATTERNS */
"-R" | "--rainbow" => pat.push(box Rainbow),
"-S" | "--sequence" => pat.push(box Sequence(
arg_next()
.unwrap()
.split(",")
.map(|e| Color::parse(e).expect("color invalid"))
.collect::<Vec<_>>(),
)),
"-G" | "--gradient" => pat.push(box Gradient(
arg_next()
.unwrap()
.split(",")
.map(|e| Color::parse(e).expect("color invalid"))
.collect::<Vec<_>>(),
)),
"-K" | "--const" => pat.push(box Solid(
Color::parse(arg_next().expect("color expected").as_str())
.expect("invalid color"),
)),
/* TRANSFORMS */
"-s" | "--scale" => {
let fac = arg_num();
push_queue.push(box Matrix {
inner: pat.pop().unwrap(),
matrix: ((fac, 0.0), (0.0, fac)),
})
}
"-r" | "--rotate" => {
let angle = arg_num() * PI * 2.0;
push_queue.push(box Matrix {
inner: pat.pop().unwrap(),
matrix: ((angle.cos(), -angle.sin()), (angle.sin(), angle.cos())),
})
}
"-m" | "--matrix" => push_queue.push(box Matrix {
inner: pat.pop().unwrap(),
matrix: ((arg_num(), arg_num()), (arg_num(), arg_num())),
}),
"-p" | "--polar" => push_queue.push(box Polar(pat.pop().unwrap())),
"-t" | "--translate" => {
let (x, y) = (arg_num(), arg_num());
push_queue.push(box Translate {
offset: (x, y),
inner: pat.pop().unwrap(),
})
}
"-T" | "--transpose" => push_queue.push(box Transpose(pat.pop().unwrap())),
_ => panic!("unknown option {}", &a),
}
pat.extend(push_queue.drain(..))
} else {
break;
}
}
let mut inputs = args.collect::<Vec<String>>();
let mut pat = pat.pop().unwrap_or_else(|| box Solid(Color::WHITE));
if let Some(a) = arg_extra {
inputs.insert(0, a)
}
if inputs.len() == 0 {
colorize(stdin(), &mut pat)?
} else {
for f in inputs {
let file = File::open(f)?;
colorize(file, &mut pat)?;
}
}
Ok(())
}
fn flag_helper(colors: &[&str]) -> Box<dyn Sample> {
box Matrix {
matrix: ((1.0 / 12.0, 1.0 / 12.0), (0.0, 0.0)),
inner: box Sequence(
colors
.into_iter()
.map(|e| Color::parse(e).unwrap())
.collect(),
),
}
}
fn colorize(file: impl Read, sampler: &mut Box<dyn Sample>) -> Result<()> {
let mut file = BufReader::new(file);
let mut line = String::new();
for y in 0.. {
if file.read_line(&mut line)? == 0 {
break;
}
for (x, c) in line.chars().enumerate() {
let (r, g, b) = sampler.sample(x as f64, y as f64).as_rgb888();
print!("\x1b[38;2;{r};{g};{b}m{c}")
}
line.clear();
}
Ok(())
}
|