1 /**
2  * Consoleur: a package for interaction with character-oriented terminal emulators
3  *
4  * Copyright: Maxim Freck, 2017.
5  * Authors:   Maxim Freck <maxim@freck.pp.ru>
6  * License:   $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
7  */
8 module consoleur.input.util;
9 
10 import consoleur.core;
11 import consoleur.input.types;
12 
13 /*******
14  * Returns: UTF-8 codepoint length
15  *
16  * Params:
17  *  src = The first byte of codepoint
18  */
19 ubyte codepointLength(ubyte src) @safe
20 {
21 	if ((src & 0b11111100) == 0b11111100) return 6;
22 	if ((src & 0b11111000) == 0b11111000) return 5;
23 	if ((src & 0b11110000) == 0b11110000) return 4;
24 	if ((src & 0b11100000) == 0b11100000) return 3;
25 	if ((src & 0b11000000) == 0b11000000) return 2;
26 	if ((src & 0b10000000) == 0b10000000) return 1;
27 	return 0;
28 }
29 
30 package Key readUtf8Char(Key ch) @safe
31 {
32 	foreach (size_t n; 1 .. ch.content.utf[0].codepointLength) {
33 		popStdin(ch.content.utf[n]);
34 	}
35 	return ch;
36 }
37 
38 package string escapeString(string src) @trusted
39 {
40 	import std.format: format;
41 	string str;
42 
43 	foreach (b; src) {
44 		if (b == 0x1b) {
45 			str ~= "⇇";
46 		} else if(b < 0x20) {
47 			str ~= cast(wchar)(b + 0x2400);
48 		} else if(b > 0x7e){
49 			str ~= format("\\x%x", b);
50 		} else {
51 			str ~= b;
52 		}
53 	}
54 
55 	return str;
56 }