Refactor output, handle controls

This commit is contained in:
Přemysl Eric Janouch 2017-06-30 07:01:45 +02:00
parent 82764010f9
commit 182ba280dd
Signed by: p
GPG Key ID: B715679E3A361BE6
1 changed files with 30 additions and 33 deletions

63
sdn.cpp
View File

@ -110,46 +110,44 @@ fun decode_mode (mode_t m) -> wstring {
}; };
} }
// XXX: maybe we should try to remove this function
fun print (const wstring &wide, int limit) -> int {
int total_width = 0;
for (wchar_t w : wide) {
// TODO: controls as ^X, show in inverse
if (!isprint (w))
w = L'?';
int width = wcwidth (w);
if (total_width + width > limit)
break;
cchar_t c = {};
c.chars[0] = w;
add_wch (&c);
total_width += width;
}
return total_width;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
using ncstring = basic_string<cchar_t>; using ncstring = basic_string<cchar_t>;
fun cchar (chtype attrs, wchar_t c) -> cchar_t {
cchar_t ch {};
setcchar (&ch, &c, attrs, 0, nullptr);
return ch;
}
fun apply_attrs (const wstring &w, attr_t attrs) -> ncstring { fun apply_attrs (const wstring &w, attr_t attrs) -> ncstring {
ncstring res; ncstring res;
for (auto c : w) for (auto c : w)
res.push_back ({attrs, {c}}); res += cchar (attrs, c);
return res; return res;
} }
fun print_nc (const ncstring &nc, int limit) -> int { fun sanitize_char (chtype attrs, wchar_t c) -> ncstring {
int total_width = 0; if (c < 32)
for (cchar_t c : nc) { return {cchar (attrs | A_REVERSE, L'^'),
// TODO: controls as ^X, show in inverse cchar (attrs | A_REVERSE, c + 64)};
auto &w = c.chars[0]; if (!iswprint (c))
if (!isprint (w)) return {cchar (attrs | A_REVERSE, L'?')};
w = L'?'; return {cchar (attrs, c)};
}
int width = wcwidth (w); fun sanitize (const ncstring &nc) -> ncstring {
ncstring out;
for (cchar_t c : nc)
for (size_t i = 0; i < CCHARW_MAX && c.chars[i]; i++)
out += sanitize_char (c.attr, c.chars[i]);
return out;
}
fun print (const ncstring &nc, int limit) -> int {
int total_width = 0;
for (cchar_t c : sanitize (nc)) {
int width = wcwidth (c.chars[0]);
if (total_width + width > limit) if (total_width + width > limit)
break; break;
@ -240,8 +238,8 @@ fun update () {
move (available - used + i, 0); move (available - used + i, 0);
auto limit = COLS, used = 0; auto limit = COLS, used = 0;
for (auto &i : row) { for (auto &i : row) {
used += print_nc (i, limit - used); used += print (i, limit - used);
used += print (L" ", limit - used); used += print (apply_attrs (L" ", 0), limit - used);
} }
hline (' ', limit - used); hline (' ', limit - used);
} }
@ -256,7 +254,7 @@ fun update () {
move (LINES - 1, 0); move (LINES - 1, 0);
wchar_t prefix[] = { g.editor, L' ', L'\0' }; wchar_t prefix[] = { g.editor, L' ', L'\0' };
addwstr (prefix); addwstr (prefix);
move (LINES - 1, print (g.editor_line, COLS - 3) + 2); move (LINES - 1, print (apply_attrs (g.editor_line, 0), COLS - 3) + 2);
curs_set (1); curs_set (1);
} else } else
curs_set (0); curs_set (0);
@ -487,7 +485,6 @@ int main (int argc, char *argv[]) {
cerr << "cannot initialize screen" << endl; cerr << "cannot initialize screen" << endl;
return 1; return 1;
} }
reload (); reload ();
auto start_dir = g.cwd; auto start_dir = g.cwd;