1 /***************************************************************************
2 * Copyright (C) 2008-2014 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
26 #include <readline/history.h>
27 #include <readline/readline.h>
29 #include <sys/select.h>
32 #include "utility/string.h"
33 #include "utility/wide_string.h"
38 struct ScopedWindowTimeout
40 ScopedWindowTimeout(WINDOW
*w
, int init_timeout
, int term_timeout
)
41 : m_w(w
), m_term_timeout(term_timeout
) {
42 wtimeout(w
, init_timeout
);
44 ~ScopedWindowTimeout() {
45 wtimeout(m_w
, m_term_timeout
);
72 if (w
->runPromptHook(rl_line_buffer
, &done
))
79 w
->goToXY(x
, start_y
);
82 result
= w
->readKey();
83 if (!w
->FDCallbacksListEmpty())
85 w
->goToXY(x
, start_y
);
89 while (result
== ERR
);
95 auto print_char
= [](wchar_t wc
) {
101 auto print_string
= [](wchar_t *ws
, size_t len
) {
103 for (size_t i
= 0; i
< len
; ++i
)
108 auto narrow_to_wide
= [](wchar_t *dest
, const char *src
, size_t n
) {
110 // convert the string and substitute invalid multibyte chars with dots.
111 for (size_t i
= 0; i
< n
;)
113 int ret
= mbtowc(&dest
[result
], &src
[i
], n
-i
);
126 throw std::runtime_error("mbtowc: unexpected return value");
131 // copy the part of the string that is before the cursor to pre_pos
132 char pt
= rl_line_buffer
[rl_point
];
133 rl_line_buffer
[rl_point
] = 0;
134 wchar_t pre_pos
[rl_point
+1];
135 pre_pos
[narrow_to_wide(pre_pos
, rl_line_buffer
, rl_point
)] = 0;
136 rl_line_buffer
[rl_point
] = pt
;
138 int pos
= wcswidth(pre_pos
, rl_point
);
142 // clear the area for the string
143 mvwhline(w
->raw(), start_y
, start_x
, ' ', width
+1);
145 w
->goToXY(start_x
, start_y
);
146 if (size_t(pos
) <= width
)
148 // if the current position in the string is not bigger than allowed
149 // width, print the part of the string before cursor position...
151 print_string(pre_pos
, pos
);
153 // ...and then print the rest char-by-char until there is no more area
154 wchar_t post_pos
[rl_end
-rl_point
+1];
155 post_pos
[narrow_to_wide(post_pos
, rl_line_buffer
+rl_point
, rl_end
-rl_point
)] = 0;
158 for (wchar_t *c
= post_pos
; *c
!= 0; ++c
)
177 // if the current position in the string is bigger than allowed
178 // width, we always keep the cursor at the end of the line (it
179 // would be nice to have more flexible scrolling, but for now
180 // let's stick to that) by cutting the beginning of the part
181 // of the string before the cursor until it fits the area.
183 wchar_t *mod_pre_pos
= pre_pos
;
184 while (*mod_pre_pos
!= 0)
187 int n
= wcwidth(*mod_pre_pos
);
192 if (size_t(pos
) <= width
)
195 print_string(mod_pre_pos
, pos
);
197 w
->goToXY(start_x
+pos
, start_y
);
202 rl_insert_text(base
);
211 const short Color::transparent
= -1;
212 const short Color::previous
= -2;
214 Color
Color::Default(0, 0, true, false);
215 Color
Color::Black(COLOR_BLACK
, Color::transparent
);
216 Color
Color::Red(COLOR_RED
, Color::transparent
);
217 Color
Color::Green(COLOR_GREEN
, Color::transparent
);
218 Color
Color::Yellow(COLOR_YELLOW
, Color::transparent
);
219 Color
Color::Blue(COLOR_BLUE
, Color::transparent
);
220 Color
Color::Magenta(COLOR_MAGENTA
, Color::transparent
);
221 Color
Color::Cyan(COLOR_CYAN
, Color::transparent
);
222 Color
Color::White(COLOR_WHITE
, Color::transparent
);
223 Color
Color::End(0, 0, false, true);
225 int Color::pairNumber() const
230 else if (previousBackground())
231 throw std::logic_error("color depends on the previous background value");
233 throw std::logic_error("'end' doesn't have a corresponding pair number");
236 // colors start with 0, but pairs start with 1. additionally
237 // first pairs are for transparent background, which has a
238 // value of -1, so we need to add 1 to both foreground and
240 result
= background() + 1;
242 result
+= foreground() + 1;
247 std::istream
&operator>>(std::istream
&is
, Color
&c
)
249 auto get_single_color
= [](const std::string
&s
, bool background
) {
252 result
= COLOR_BLACK
;
255 else if (s
== "green")
256 result
= COLOR_GREEN
;
257 else if (s
== "yellow")
258 result
= COLOR_YELLOW
;
259 else if (s
== "blue")
261 else if (s
== "magenta")
262 result
= COLOR_MAGENTA
;
263 else if (s
== "cyan")
265 else if (s
== "white")
266 result
= COLOR_WHITE
;
267 else if (background
&& s
== "previous")
268 result
= NC::Color::previous
;
269 else if (std::all_of(s
.begin(), s
.end(), isdigit
))
271 result
= atoi(s
.c_str());
272 if (result
< 1 || result
> 256)
283 else if (sc
== "end")
287 short value
= get_single_color(sc
, false);
289 c
= Color(value
, NC::Color::transparent
);
292 size_t underscore
= sc
.find('_');
293 if (underscore
!= std::string::npos
)
295 short fg
= get_single_color(sc
.substr(0, underscore
), false);
296 short bg
= get_single_color(sc
.substr(underscore
+1), true);
297 if (fg
!= -1 && bg
!= -1)
300 is
.setstate(std::ios::failbit
);
303 is
.setstate(std::ios::failbit
);
313 bool mouseEnabled
= false;
321 // save old highlight mouse tracking
322 std::printf("\e[?1001s");
323 // enable mouse tracking
324 std::printf("\e[?1000h");
325 // try to enable extended (urxvt) mouse tracking
326 std::printf("\e[?1015h");
334 // disable extended (urxvt) mouse tracking
335 std::printf("\e[?1015l");
336 // disable mouse tracking
337 std::printf("\e[?1000l");
338 // restore old highlight mouse tracking
339 std::printf("\e[?1001r");
340 mouseEnabled
= false;
345 void initScreen(bool enable_colors
, bool enable_mouse
)
348 if (has_colors() && enable_colors
)
351 use_default_colors();
353 for (int bg
= -1; bg
< COLORS
; ++bg
)
355 for (int fg
= 0; npair
< COLOR_PAIRS
&& fg
< COLORS
; ++fg
, ++npair
)
356 init_pair(npair
, fg
, bg
);
368 // initialize readline (needed, otherwise we get segmentation
369 // fault on SIGWINCH). also, initialize first as doing this
370 // later erases keys bound with rl_bind_key for some users.
372 // disable autocompletion
373 rl_attempted_completion_function
= [](const char *, int, int) -> char ** {
374 rl_attempted_completion_over
= 1;
377 auto abort_prompt
= [](int, int) -> int {
382 // if ctrl-c or ctrl-g is pressed, abort the prompt
383 rl_bind_key('\3', abort_prompt
);
384 rl_bind_key('\7', abort_prompt
);
385 // do not change the state of the terminal
386 rl_prep_term_function
= nullptr;
387 rl_deprep_term_function
= nullptr;
388 // do not catch signals
389 rl_catch_signals
= 0;
390 // overwrite readline callbacks
391 rl_getc_function
= rl::read_key
;
392 rl_redisplay_function
= rl::display_string
;
393 rl_startup_hook
= rl::add_base
;
403 Window::Window(size_t startx
,
415 m_window_timeout(-1),
418 m_border(std::move(border
)),
420 m_title(std::move(title
)),
421 m_escape_terminal_sequences(true),
423 m_underline_counter(0),
424 m_reverse_counter(0),
425 m_alt_charset_counter(0)
427 if (m_start_x
> size_t(COLS
)
428 || m_start_y
> size_t(LINES
)
429 || m_width
+m_start_x
> size_t(COLS
)
430 || m_height
+m_start_y
> size_t(LINES
))
431 throw std::logic_error("constructed window doesn't fit into the terminal");
440 if (!m_title
.empty())
446 m_window
= newpad(m_height
, m_width
);
451 Window::Window(const Window
&rhs
)
452 : m_window(dupwin(rhs
.m_window
))
453 , m_start_x(rhs
.m_start_x
)
454 , m_start_y(rhs
.m_start_y
)
455 , m_width(rhs
.m_width
)
456 , m_height(rhs
.m_height
)
457 , m_window_timeout(rhs
.m_window_timeout
)
458 , m_color(rhs
.m_color
)
459 , m_base_color(rhs
.m_base_color
)
460 , m_border(rhs
.m_border
)
461 , m_prompt_hook(rhs
.m_prompt_hook
)
462 , m_title(rhs
.m_title
)
463 , m_color_stack(rhs
.m_color_stack
)
464 , m_input_queue(rhs
.m_input_queue
)
466 , m_escape_terminal_sequences(rhs
.m_escape_terminal_sequences
)
467 , m_bold_counter(rhs
.m_bold_counter
)
468 , m_underline_counter(rhs
.m_underline_counter
)
469 , m_reverse_counter(rhs
.m_reverse_counter
)
470 , m_alt_charset_counter(rhs
.m_alt_charset_counter
)
474 Window::Window(Window
&&rhs
)
475 : m_window(rhs
.m_window
)
476 , m_start_x(rhs
.m_start_x
)
477 , m_start_y(rhs
.m_start_y
)
478 , m_width(rhs
.m_width
)
479 , m_height(rhs
.m_height
)
480 , m_window_timeout(rhs
.m_window_timeout
)
481 , m_color(rhs
.m_color
)
482 , m_base_color(rhs
.m_base_color
)
483 , m_border(rhs
.m_border
)
484 , m_prompt_hook(rhs
.m_prompt_hook
)
485 , m_title(std::move(rhs
.m_title
))
486 , m_color_stack(std::move(rhs
.m_color_stack
))
487 , m_input_queue(std::move(rhs
.m_input_queue
))
488 , m_fds(std::move(rhs
.m_fds
))
489 , m_escape_terminal_sequences(rhs
.m_escape_terminal_sequences
)
490 , m_bold_counter(rhs
.m_bold_counter
)
491 , m_underline_counter(rhs
.m_underline_counter
)
492 , m_reverse_counter(rhs
.m_reverse_counter
)
493 , m_alt_charset_counter(rhs
.m_alt_charset_counter
)
495 rhs
.m_window
= nullptr;
498 Window
&Window::operator=(Window rhs
)
500 std::swap(m_window
, rhs
.m_window
);
501 std::swap(m_start_x
, rhs
.m_start_x
);
502 std::swap(m_start_y
, rhs
.m_start_y
);
503 std::swap(m_width
, rhs
.m_width
);
504 std::swap(m_height
, rhs
.m_height
);
505 std::swap(m_window_timeout
, rhs
.m_window_timeout
);
506 std::swap(m_color
, rhs
.m_color
);
507 std::swap(m_base_color
, rhs
.m_base_color
);
508 std::swap(m_border
, rhs
.m_border
);
509 std::swap(m_prompt_hook
, rhs
.m_prompt_hook
);
510 std::swap(m_title
, rhs
.m_title
);
511 std::swap(m_color_stack
, rhs
.m_color_stack
);
512 std::swap(m_input_queue
, rhs
.m_input_queue
);
513 std::swap(m_fds
, rhs
.m_fds
);
514 std::swap(m_escape_terminal_sequences
, rhs
.m_escape_terminal_sequences
);
515 std::swap(m_bold_counter
, rhs
.m_bold_counter
);
516 std::swap(m_underline_counter
, rhs
.m_underline_counter
);
517 std::swap(m_reverse_counter
, rhs
.m_reverse_counter
);
518 std::swap(m_alt_charset_counter
, rhs
.m_alt_charset_counter
);
527 void Window::setColor(Color c
)
531 if (c
!= Color::Default
)
533 if (c
.previousBackground())
534 c
= Color(c
.foreground(), m_color
.background());
535 wcolor_set(m_window
, c
.pairNumber(), nullptr);
538 wcolor_set(m_window
, m_base_color
.pairNumber(), nullptr);
539 m_color
= std::move(c
);
542 void Window::setBaseColor(Color c
)
544 m_base_color
= std::move(c
);
547 void Window::setBorder(Border border
)
549 if (!border
&& m_border
)
555 recreate(m_width
, m_height
);
557 else if (border
&& !m_border
)
563 recreate(m_width
, m_height
);
568 void Window::setTitle(const std::string
&new_title
)
570 if (!new_title
.empty() && m_title
.empty())
574 recreate(m_width
, m_height
);
576 else if (new_title
.empty() && !m_title
.empty())
580 recreate(m_width
, m_height
);
585 void Window::recreate(size_t width
, size_t height
)
588 m_window
= newpad(height
, width
);
589 setTimeout(m_window_timeout
);
593 void Window::moveTo(size_t new_x
, size_t new_y
)
602 if (!m_title
.empty())
606 void Window::adjustDimensions(size_t width
, size_t height
)
613 if (!m_title
.empty())
619 void Window::resize(size_t new_width
, size_t new_height
)
621 adjustDimensions(new_width
, new_height
);
622 recreate(m_width
, m_height
);
625 void Window::refreshBorder() const
629 size_t start_x
= getStartX(), start_y
= getStarty();
630 size_t width
= getWidth(), height
= getHeight();
631 color_set(m_border
->pairNumber(), nullptr);
632 attron(A_ALTCHARSET
);
634 mvaddch(start_y
, start_x
, 'l');
635 mvaddch(start_y
, start_x
+width
-1, 'k');
636 mvaddch(start_y
+height
-1, start_x
, 'm');
637 mvaddch(start_y
+height
-1, start_x
+width
-1, 'j');
639 mvhline(start_y
, start_x
+1, 'q', width
-2);
640 mvhline(start_y
+height
-1, start_x
+1, 'q', width
-2);
641 mvvline(start_y
+1, start_x
, 'x', height
-2);
642 mvvline(start_y
+1, start_x
+width
-1, 'x', height
-2);
643 if (!m_title
.empty())
645 mvaddch(start_y
+2, start_x
, 't');
646 mvaddch(start_y
+2, start_x
+width
-1, 'u');
648 attroff(A_ALTCHARSET
);
651 color_set(m_base_color
.pairNumber(), nullptr);
652 if (!m_title
.empty())
655 mvhline(m_start_y
-2, m_start_x
, ' ', m_width
);
657 mvaddstr(m_start_y
-2, m_start_x
, m_title
.c_str());
660 mvhline(m_start_y
-1, m_start_x
, 0, m_width
);
666 void Window::display()
672 void Window::refresh()
674 prefresh(m_window
, 0, 0, m_start_y
, m_start_x
, m_start_y
+m_height
-1, m_start_x
+m_width
-1);
682 void Window::bold(bool bold_state
) const
684 (bold_state
? wattron
: wattroff
)(m_window
, A_BOLD
);
687 void Window::underline(bool underline_state
) const
689 (underline_state
? wattron
: wattroff
)(m_window
, A_UNDERLINE
);
692 void Window::reverse(bool reverse_state
) const
694 (reverse_state
? wattron
: wattroff
)(m_window
, A_REVERSE
);
697 void Window::altCharset(bool altcharset_state
) const
699 (altcharset_state
? wattron
: wattroff
)(m_window
, A_ALTCHARSET
);
702 void Window::setTimeout(int timeout
)
704 if (timeout
!= m_window_timeout
)
706 m_window_timeout
= timeout
;
707 wtimeout(m_window
, timeout
);
711 void Window::addFDCallback(int fd
, void (*callback
)())
713 m_fds
.push_back(std::make_pair(fd
, callback
));
716 void Window::clearFDCallbacksList()
721 bool Window::FDCallbacksListEmpty() const
723 return m_fds
.empty();
726 Key::Type
Window::getInputChar(int key
)
728 if (!m_escape_terminal_sequences
|| key
!= Key::Escape
)
730 auto define_mouse_event
= [this](int type
) {
734 m_mouse_event
.bstate
= BUTTON1_PRESSED
;
737 m_mouse_event
.bstate
= BUTTON2_PRESSED
;
740 m_mouse_event
.bstate
= BUTTON3_PRESSED
;
743 m_mouse_event
.bstate
= BUTTON4_PRESSED
;
746 m_mouse_event
.bstate
= BUTTON5_PRESSED
;
752 m_mouse_event
.bstate
|= BUTTON_SHIFT
;
754 m_mouse_event
.bstate
|= BUTTON_ALT
;
756 m_mouse_event
.bstate
|= BUTTON_CTRL
;
757 if (m_mouse_event
.x
< 0 || m_mouse_event
.x
>= COLS
)
759 if (m_mouse_event
.y
< 0 || m_mouse_event
.y
>= LINES
)
763 auto get_xterm_modifier_key
= [](int ch
) {
768 modifier
= Key::Shift
;
774 modifier
= Key::Alt
| Key::Shift
;
777 modifier
= Key::Ctrl
;
780 modifier
= Key::Ctrl
| Key::Shift
;
783 modifier
= Key::Alt
| Key::Ctrl
;
786 modifier
= Key::Alt
| Key::Ctrl
| Key::Shift
;
789 modifier
= Key::None
;
793 auto parse_number
= [this](int &result
) {
797 x
= wgetch(m_window
);
800 result
= result
*10 + x
- '0';
803 ScopedWindowTimeout
swt(m_window
, 0, m_window_timeout
);
804 key
= wgetch(m_window
);
808 return Key::Shift
| Key::Tab
;
809 case 'O': // ctrl+arrows in rxvt, F1 to F4 in xterm
810 key
= wgetch(m_window
);
814 return Key::Ctrl
| Key::Up
;
816 return Key::Ctrl
| Key::Down
;
818 return Key::Ctrl
| Key::Right
;
820 return Key::Ctrl
| Key::Left
;
833 key
= wgetch(m_window
);
837 return Key::Shift
| Key::Up
;
839 return Key::Shift
| Key::Down
;
841 return Key::Shift
| Key::Right
;
843 return Key::Shift
| Key::Left
;
856 case 'M': // standard mouse event
858 key
= wgetch(m_window
);
859 int raw_x
= wgetch(m_window
);
860 int raw_y
= wgetch(m_window
);
861 // support coordinates up to 255
862 m_mouse_event
.x
= (raw_x
- 33) & 0xff;
863 m_mouse_event
.y
= (raw_y
- 33) & 0xff;
864 return define_mouse_event(key
);
867 return Key::Shift
| Key::Tab
;
868 case '[': // F1 to F5 in tty
869 key
= wgetch(m_window
);
885 case '1': case '2': case '3':
886 case '4': case '5': case '6':
887 case '7': case '8': case '9':
890 int delim
= parse_number(key
);
891 if (key
>= 2 && key
<= 8)
897 modifier
= Key::Null
;
900 modifier
= Key::Ctrl
;
903 modifier
= Key::Shift
;
906 modifier
= Key::Ctrl
| Key::Shift
;
908 case ';': // xterm insert/delete/page up/page down
910 int local_key
= wgetch(m_window
);
911 modifier
= get_xterm_modifier_key(local_key
);
912 local_key
= wgetch(m_window
);
913 if (local_key
!= '~' || (key
!= 2 && key
!= 3 && key
!= 5 && key
!= 6))
923 return modifier
| Key::Insert
;
925 return modifier
| Key::Delete
;
927 return modifier
| Key::End
;
929 return modifier
| Key::PageUp
;
931 return modifier
| Key::PageDown
;
933 return modifier
| Key::Home
;
935 return modifier
| Key::End
;
937 std::cerr
<< "Unreachable code, aborting.\n";
959 case 17: // not a typo
969 case 23: // not a typo
982 key
= wgetch(m_window
);
983 Key::Type modifier
= get_xterm_modifier_key(key
);
984 if (modifier
== Key::None
)
986 key
= wgetch(m_window
);
990 return modifier
| Key::Up
;
992 return modifier
| Key::Down
;
994 return modifier
| Key::Right
;
996 return modifier
| Key::Left
;
998 return modifier
| Key::End
;
1000 return modifier
| Key::Home
;
1005 default: // urxvt mouse
1006 m_mouse_event
.x
= 0;
1007 delim
= parse_number(m_mouse_event
.x
);
1010 m_mouse_event
.y
= 0;
1011 delim
= parse_number(m_mouse_event
.y
);
1016 return define_mouse_event(key
);
1027 default: // alt + something
1029 auto key_prim
= getInputChar(key
);
1030 if (key_prim
!= Key::None
)
1031 return Key::Alt
| key_prim
;
1037 Key::Type
Window::readKey()
1040 // if there are characters in input queue,
1041 // get them and return immediately.
1042 if (!m_input_queue
.empty())
1044 result
= m_input_queue
.front();
1045 m_input_queue
.pop();
1051 FD_SET(STDIN_FILENO
, &fdset
);
1052 timeval timeout
= { m_window_timeout
/1000, (m_window_timeout
%1000)*1000 };
1054 int fd_max
= STDIN_FILENO
;
1055 for (FDCallbacks::const_iterator it
= m_fds
.begin(); it
!= m_fds
.end(); ++it
)
1057 if (it
->first
> fd_max
)
1059 FD_SET(it
->first
, &fdset
);
1062 if (select(fd_max
+1, &fdset
, 0, 0, m_window_timeout
< 0 ? 0 : &timeout
) > 0)
1064 if (FD_ISSET(STDIN_FILENO
, &fdset
))
1065 result
= getInputChar(wgetch(m_window
));
1069 for (FDCallbacks::const_iterator it
= m_fds
.begin(); it
!= m_fds
.end(); ++it
)
1070 if (FD_ISSET(it
->first
, &fdset
))
1078 void Window::pushChar(const Key::Type ch
)
1080 m_input_queue
.push(ch
);
1083 std::string
Window::prompt(const std::string
&base
, size_t width
, bool encrypted
)
1087 rl::aborted
= false;
1089 getyx(m_window
, rl::start_y
, rl::start_x
);
1090 rl::width
= std::min(m_width
-rl::start_x
-1, width
-1);
1091 rl::encrypted
= encrypted
;
1092 rl::base
= base
.c_str();
1096 m_escape_terminal_sequences
= false;
1097 char *input
= readline(nullptr);
1098 m_escape_terminal_sequences
= true;
1101 if (input
!= nullptr)
1103 if (!encrypted
&& input
[0] != 0)
1110 throw PromptAborted(std::move(result
));
1115 void Window::goToXY(int x
, int y
)
1117 wmove(m_window
, y
, x
);
1122 return getcurx(m_window
);
1127 return getcury(m_window
);
1130 bool Window::hasCoords(int &x
, int &y
)
1132 return wmouse_trafo(m_window
, &y
, &x
, 0);
1135 bool Window::runPromptHook(const char *arg
, bool *done
) const
1139 bool continue_
= m_prompt_hook(arg
);
1140 if (done
!= nullptr)
1148 size_t Window::getWidth() const
1156 size_t Window::getHeight() const
1158 size_t height
= m_height
;
1161 if (!m_title
.empty())
1166 size_t Window::getStartX() const
1174 size_t Window::getStarty() const
1176 size_t starty
= m_start_y
;
1179 if (!m_title
.empty())
1184 const std::string
&Window::getTitle() const
1189 const Color
&Window::getColor() const
1194 const Border
&Window::getBorder() const
1199 int Window::getTimeout() const
1201 return m_window_timeout
;
1204 const MEVENT
&Window::getMouseEvent()
1206 return m_mouse_event
;
1209 void Window::scroll(Scroll where
)
1212 scrollok(m_window
, 1);
1219 wscrl(m_window
, -1);
1221 case Scroll::PageUp
:
1222 wscrl(m_window
, m_width
);
1224 case Scroll::PageDown
:
1225 wscrl(m_window
, -m_width
);
1231 scrollok(m_window
, 0);
1235 Window
&Window::operator<<(const Color
&c
)
1239 while (!m_color_stack
.empty())
1240 m_color_stack
.pop();
1241 setColor(m_base_color
);
1245 if (!m_color_stack
.empty())
1246 m_color_stack
.pop();
1247 if (!m_color_stack
.empty())
1248 setColor(m_color_stack
.top());
1250 setColor(m_base_color
);
1255 m_color_stack
.push(c
);
1260 Window
&Window::operator<<(Format format
)
1265 bold((m_bold_counter
= 0));
1266 reverse((m_reverse_counter
= 0));
1267 altCharset((m_alt_charset_counter
= 0));
1270 bold(++m_bold_counter
);
1272 case Format::NoBold
:
1273 if (--m_bold_counter
<= 0)
1274 bold((m_bold_counter
= 0));
1276 case Format::Underline
:
1277 underline(++m_underline_counter
);
1279 case Format::NoUnderline
:
1280 if (--m_underline_counter
<= 0)
1281 underline((m_underline_counter
= 0));
1283 case Format::Reverse
:
1284 reverse(++m_reverse_counter
);
1286 case Format::NoReverse
:
1287 if (--m_reverse_counter
<= 0)
1288 reverse((m_reverse_counter
= 0));
1290 case Format::AltCharset
:
1291 altCharset(++m_alt_charset_counter
);
1293 case Format::NoAltCharset
:
1294 if (--m_alt_charset_counter
<= 0)
1295 altCharset((m_alt_charset_counter
= 0));
1301 Window
&Window::operator<<(TermManip tm
)
1305 case TermManip::ClearToEOL
:
1307 auto x
= getX(), y
= getY();
1308 mvwhline(m_window
, y
, x
, ' ', m_width
-x
);
1316 Window
&Window::operator<<(const XY
&coords
)
1318 goToXY(coords
.x
, coords
.y
);
1322 Window
&Window::operator<<(const char *s
)
1324 waddstr(m_window
, s
);
1328 Window
&Window::operator<<(char c
)
1330 // waddchr doesn't display non-ascii multibyte characters properly
1331 waddnstr(m_window
, &c
, 1);
1335 Window
&Window::operator<<(const wchar_t *ws
)
1337 # ifdef NCMPCPP_UNICODE
1338 waddwstr(m_window
, ws
);
1340 wprintw(m_window
, "%ls", ws
);
1341 # endif // NCMPCPP_UNICODE
1345 Window
&Window::operator<<(wchar_t wc
)
1347 # ifdef NCMPCPP_UNICODE
1348 waddnwstr(m_window
, &wc
, 1);
1350 wprintw(m_window
, "%lc", wc
);
1351 # endif // NCMPCPP_UNICODE
1355 Window
&Window::operator<<(int i
)
1357 wprintw(m_window
, "%d", i
);
1361 Window
&Window::operator<<(double d
)
1363 wprintw(m_window
, "%f", d
);
1367 Window
&Window::operator<<(const std::string
&s
)
1369 waddnstr(m_window
, s
.c_str(), s
.length());
1373 Window
&Window::operator<<(const std::wstring
&ws
)
1375 # ifdef NCMPCPP_UNICODE
1376 waddnwstr(m_window
, ws
.c_str(), ws
.length());
1378 wprintw(m_window
, "%lc", ws
.c_str());
1379 # endif // NCMPCPP_UNICODE
1383 Window
&Window::operator<<(size_t s
)
1385 wprintw(m_window
, "%zu", s
);