x1b 0.4.0

State tracker for 0x1b terminal escape codes. Also gives random characters names.
Documentation
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use std::io::{self, Write};
bitflags! {
	pub flags TextAttr: u8 {
		const TA_BOLD = 1,
		const TA_DIM = 2,
		const TA_UNDER = 4,
		const TA_BLINK = 8,
		const TA_REV = 16,
	}
}

const TA_CHARS: [(TextAttr, char); 5] = [
	(TA_BOLD, '1'),
	(TA_DIM, '2'),
	(TA_UNDER, '4'),
	(TA_BLINK, '5'),
	(TA_REV, '7')];

impl TextAttr {
	pub fn clear(&mut self) -> bool {
		let ret = self.bits != 0;
		self.bits = 0;
		ret
	}
}

pub trait RGB {
	fn fg(&self, buf: &mut String);
	fn bg(&self, buf: &mut String);
}

#[derive(Copy, Clone, Eq, PartialEq)]
pub enum RGB4 {
	Default,
	Black,
	Red,
	Green,
	Yellow,
	Blue,
	Magenta,
	Cyan,
	LightGray,
	DarkGray,
	LightRed,
	LightGreen,
	LightYellow,
	LightBlue,
	LightMagenta,
	LightCyan,
	White,
}

impl Default for RGB4 {
	fn default() -> Self {
		RGB4::Default
	}
}

impl RGB for RGB4 {
	fn fg(&self, buf: &mut String) {
		buf.push_str(match *self {
			RGB4::Default => "\x1b[49m",
			RGB4::Black => "\x1b[40m",
			RGB4::Red => "\x1b[41m",
			RGB4::Green => "\x1b[42m",
			RGB4::Yellow => "\x1b[43m",
			RGB4::Blue => "\x1b[44m",
			RGB4::Magenta => "\x1b[45m",
			RGB4::Cyan => "\x1b[46m",
			RGB4::LightGray => "\x1b[47m",
			RGB4::DarkGray => "\x1b[40;1m",
			RGB4::LightRed => "\x1b[41;1m",
			RGB4::LightGreen => "\x1b[42;1m",
			RGB4::LightYellow => "\x1b[43;1m",
			RGB4::LightBlue => "\x1b[44;1m",
			RGB4::LightMagenta => "\x1b[45;1m",
			RGB4::LightCyan => "\x1b[46;1m",
			RGB4::White => "\x1b[47;1m",
		});
	}
	fn bg(&self, buf: &mut String) {
		buf.push_str(match *self {
			RGB4::Default => "\x1b[39m",
			RGB4::Black => "\x1b[30m",
			RGB4::Red => "\x1b[31m",
			RGB4::Green => "\x1b[32m",
			RGB4::Yellow => "\x1b[33m",
			RGB4::Blue => "\x1b[34m",
			RGB4::Magenta => "\x1b[35m",
			RGB4::Cyan => "\x1b[36m",
			RGB4::LightGray => "\x1b[37m",
			RGB4::DarkGray => "\x1b[30;1m",
			RGB4::LightRed => "\x1b[31;1m",
			RGB4::LightGreen => "\x1b[32;1m",
			RGB4::LightYellow => "\x1b[33;1m",
			RGB4::LightBlue => "\x1b[34;1m",
			RGB4::LightMagenta => "\x1b[35;1m",
			RGB4::LightCyan => "\x1b[36;1m",
			RGB4::White => "\x1b[37;1m",
		});
	}
}

impl RGB for () {
	fn fg(&self, _buf: &mut String) { }
	fn bg(&self, _buf: &mut String) { }
}

pub struct RGB8;
impl RGB8 {
	pub fn rgb4(c: RGB4) -> u8 {
		match c {
			RGB4::Default => 0,
			RGB4::Black => 0,
			RGB4::Red => 1,
			RGB4::Green => 2,
			RGB4::Yellow => 3,
			RGB4::Blue => 4,
			RGB4::Magenta => 5,
			RGB4::Cyan => 6,
			RGB4::LightGray => 7,
			RGB4::DarkGray => 8,
			RGB4::LightRed => 9,
			RGB4::LightGreen => 10,
			RGB4::LightYellow => 11,
			RGB4::LightBlue => 12,
			RGB4::LightMagenta => 13,
			RGB4::LightCyan => 14,
			RGB4::White => 15,
		}
	}

	pub fn rgb(r: u8, g: u8, b: u8) -> u8 {
		16 + r*36 + g*6 + b
	}

	pub fn gray(s: u8) -> u8 {
		232 + s
	}
}

impl RGB for u8 {
	fn fg(&self, buf: &mut String) {
		buf.push_str(&format!("\x1b[38;5;{}m", *self))
	}
	fn bg(&self, buf: &mut String) {
		buf.push_str(&format!("\x1b[48;5;{}m", *self))
	}
}

impl RGB for (u8, u8, u8) {
	fn fg(&self, buf: &mut String) {
		buf.push_str(&format!("\x1b[{};2;{};{};{}m", 38, self.0, self.1, self.2));
	}
	fn bg(&self, buf: &mut String) {
		buf.push_str(&format!("\x1b[{};2;{};{};{}m", 48, self.0, self.1, self.2));
	}
}

pub struct Cursor<TColor: RGB> {
	pub buf: String,
	pub fg: TColor,
	pub bg: TColor,
	pub attr: TextAttr,
	pub x: u16,
	pub y: u16,
}

impl<TColor: RGB + Default> Default for Cursor<TColor> {
	fn default() -> Self {
		Cursor::<TColor> {
			buf: String::new(),
			attr: TextAttr::empty(),
			fg: Default::default(),
			bg: Default::default(),
			x: 1,
			y: 1,
		}
	}
}

impl<TColor: RGB + Eq> Cursor<TColor> {
	pub fn new(fg: TColor, bg: TColor) -> Self {
		Cursor::<TColor> {
			buf: String::new(),
			attr: TextAttr::empty(),
			fg: fg,
			bg: bg,
			x: 1,
			y: 1,
		}
	}

	pub fn esc(&mut self, s: &str){
		self.buf.push('\x1b');
		self.buf.push('[');
		self.buf.push_str(s)
	}
	pub fn escch(&mut self, c: char){
		self.buf.push('\x1b');
		self.buf.push('[');
		self.buf.push(c)
	}
	pub fn clearattr(&mut self){
		self.attr.clear();
		self.escch('m')
	}
	pub fn hasallattr(&self, ta: TextAttr) -> bool{
		self.attr.contains(ta)
	}
	pub fn hasanyattr(&self, ta: TextAttr) -> bool{
		self.attr.intersects(ta)
	}
	pub fn setattr(&mut self, ta: TextAttr){
		if ta == self.attr { return }
		self.buf.push('\x1b');
		self.buf.push('[');
		if ta.contains(self.attr) {
			for &(attr, code) in TA_CHARS.iter() {
				if ta.contains(attr) && !self.attr.contains(attr) {
					self.buf.push(code);
					self.buf.push(';')
				}
			}
		} else {
			self.escch('m');
			for &(attr, code) in TA_CHARS.iter() {
				if ta.contains(attr) {
					self.buf.push(code);
					self.buf.push(';')
				}
			}
		}
		unsafe { *(self.buf.as_mut_vec().last_mut().unwrap()) = b'm' }
		self.attr = ta;
	}
	pub fn setbold(&mut self){
		self.attr.insert(TA_BOLD);
		self.esc("1m")
	}
	pub fn setdim(&mut self){
		self.attr.insert(TA_DIM);
		self.esc("2m")
	}
	pub fn setunder(&mut self){
		self.attr.insert(TA_UNDER);
		self.esc("4m")
	}
	pub fn setblink(&mut self){
		self.attr.insert(TA_BLINK);
		self.esc("5m")
	}
	pub fn setrev(&mut self){
		self.attr.insert(TA_REV);
		self.esc("7m")
	}
	pub fn unsetbold(&mut self){
		self.attr.remove(TA_BOLD);
		self.esc("21m")
	}
	pub fn unsetrev(&mut self){
		self.attr.remove(TA_REV);
		self.esc("27m")
	}
	pub fn wrapon(&mut self){
		self.buf.push_str("\x1b7h")
	}
	pub fn wrapoff(&mut self){
		self.buf.push_str("\x1b7l")
	}
	pub fn up1(&mut self){
		self.y -= 1;
		self.escch('A')
	}
	pub fn down1(&mut self){
		self.y += 1;
		self.escch('B')
	}
	pub fn right1(&mut self){
		self.x -= 1;
		self.escch('C')
	}
	pub fn left1(&mut self){
		self.x += 1;
		self.escch('D')
	}
	pub fn up(&mut self, n: u16){
		self.y -= n;
		self.esc(&format!("{}A", n))
	}
	pub fn down(&mut self, n: u16){
		self.y += n;
		self.esc(&format!("{}B", n))
	}
	pub fn right(&mut self, n: u16){
		self.x -= n;
		self.esc(&format!("{}C", n))
	}
	pub fn left(&mut self, n: u16){
		self.x += n;
		self.esc(&format!("{}D", n))
	}
	pub fn x1down(&mut self, n: u16){
		self.x = 1;
		self.y += n;
		self.esc(&format!("{}E", n))
	}
	pub fn x1up(&mut self, n: u16){
		self.x = 1;
		self.y -= n;
		self.esc(&format!("{}F", n))
	}
	pub fn setx(&mut self, x: u16){
		self.x = x;
		self.esc(&format!("{}G", x))
	}
	pub fn sety(&mut self, y: u16){
		self.y = y;
		self.esc(&format!("{}d", y))
	}
	pub fn resetxy(&mut self){
		self.x = 1;
		self.y = 1;
		self.escch('H')
	}
	pub fn mv(&mut self, x: u16, y: u16){
		if self.x != x && self.y != y {
			self.x = x;
			self.y = y;
			self.esc(&format!("{};{}H",y,x));
		}
	}
	pub fn erasebelow(&mut self){
		self.escch('J')
	}
	pub fn eraseabove(&mut self){
		self.esc("1J")
	}
	pub fn eraseall(&mut self){
		self.esc("2J")
	}
	pub fn eraseleft(&mut self){
		self.escch('K')
	}
	pub fn eraseright(&mut self){
		self.esc("1K")
	}
	pub fn eraseline(&mut self){
		self.esc("2K")
	}
	pub fn delln(&mut self){
		self.escch('M')
	}
	pub fn dellns(&mut self, n: u16){
		self.esc(&format!("{}M", n))
	}
	pub fn delch(&mut self){
		self.escch('S')
	}
	pub fn delchs(&mut self, n: u16){
		self.esc(&format!("{}S", n))
	}
	pub fn getattr(&self) -> TextAttr{
		self.attr
	}
	pub fn showcur(&mut self){
		self.esc("?23h")
	}
	pub fn hidecur(&mut self){
		self.esc("?25l")
	}
	pub fn spame(&mut self){
		self.buf.push_str("\x1b#8")
	}
	pub fn getxy(&self) -> (u16, u16){
		(self.x, self.y)
	}
	pub fn setfg(&mut self, rgb: TColor) {
		if self.fg != rgb {
			self.fg = rgb;
			self.fg.fg(&mut self.buf);
		}
	}
	pub fn setbg(&mut self, rgb: TColor) {
		if self.bg != rgb {
			self.bg = rgb;
			self.bg.bg(&mut self.buf);
		}
	}
	pub fn prchr(&mut self, c: char){
		self.x += 1;
		self.buf.push(c)
	}
	pub fn print(&mut self, s: &str){
		let mut rsp = s.rsplit('\n');
		let last = rsp.next().unwrap();
		let lines = rsp.count();
		self.x += last.len() as u16;
		self.y += lines as u16;
		self.buf.push_str(s)
	}
	pub fn clear(&mut self) -> io::Result<()> {
		self.buf.clear();
		self.x = 1;
		self.y = 1;
		Cursor::<TColor>::dropclear()
	}
	pub fn dropclear() -> io::Result<()> {
		io::stdout().write_all(b"\x1bc")
	}
	pub fn flush(&mut self) -> io::Result<()> {
		let mut out = io::stdout();
		try!(out.write_all(self.buf.as_bytes()));
		try!(out.flush());
		Ok(self.buf.clear())
	}
}