1 /***************************************************************************
2 * Copyright (C) 2008-2016 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 <sys/select.h>
29 #include "utility/readline.h"
30 #include "utility/string.h"
31 #include "utility/wide_string.h"
55 if (w
->runPromptHook(rl_line_buffer
, &done
))
62 w
->goToXY(x
, start_y
);
65 result
= w
->readKey();
66 if (!w
->FDCallbacksListEmpty())
68 w
->goToXY(x
, start_y
);
72 while (result
== ERR
);
78 auto print_char
= [](wchar_t wc
) {
84 auto print_string
= [](wchar_t *ws
, size_t len
) {
86 for (size_t i
= 0; i
< len
; ++i
)
91 auto narrow_to_wide
= [](wchar_t *dest
, const char *src
, size_t n
) {
93 // convert the string and substitute invalid multibyte chars with dots.
94 for (size_t i
= 0; i
< n
;)
96 int ret
= mbtowc(&dest
[result
], &src
[i
], n
-i
);
109 throw std::runtime_error("mbtowc: unexpected return value");
114 // copy the part of the string that is before the cursor to pre_pos
115 char pt
= rl_line_buffer
[rl_point
];
116 rl_line_buffer
[rl_point
] = 0;
117 wchar_t pre_pos
[rl_point
+1];
118 pre_pos
[narrow_to_wide(pre_pos
, rl_line_buffer
, rl_point
)] = 0;
119 rl_line_buffer
[rl_point
] = pt
;
121 int pos
= wcswidth(pre_pos
, rl_point
);
125 // clear the area for the string
126 mvwhline(w
->raw(), start_y
, start_x
, ' ', width
+1);
128 w
->goToXY(start_x
, start_y
);
129 if (size_t(pos
) <= width
)
131 // if the current position in the string is not bigger than allowed
132 // width, print the part of the string before cursor position...
134 print_string(pre_pos
, pos
);
136 // ...and then print the rest char-by-char until there is no more area
137 wchar_t post_pos
[rl_end
-rl_point
+1];
138 post_pos
[narrow_to_wide(post_pos
, rl_line_buffer
+rl_point
, rl_end
-rl_point
)] = 0;
141 for (wchar_t *c
= post_pos
; *c
!= 0; ++c
)
160 // if the current position in the string is bigger than allowed
161 // width, we always keep the cursor at the end of the line (it
162 // would be nice to have more flexible scrolling, but for now
163 // let's stick to that) by cutting the beginning of the part
164 // of the string before the cursor until it fits the area.
166 wchar_t *mod_pre_pos
= pre_pos
;
167 while (*mod_pre_pos
!= 0)
170 int n
= wcwidth(*mod_pre_pos
);
175 if (size_t(pos
) <= width
)
178 print_string(mod_pre_pos
, pos
);
180 w
->goToXY(start_x
+pos
, start_y
);
185 rl_insert_text(base
);
194 const short Color::transparent
= -1;
195 const short Color::previous
= -2;
197 Color
Color::Default(0, 0, true, false);
198 Color
Color::Black(COLOR_BLACK
, Color::transparent
);
199 Color
Color::Red(COLOR_RED
, Color::transparent
);
200 Color
Color::Green(COLOR_GREEN
, Color::transparent
);
201 Color
Color::Yellow(COLOR_YELLOW
, Color::transparent
);
202 Color
Color::Blue(COLOR_BLUE
, Color::transparent
);
203 Color
Color::Magenta(COLOR_MAGENTA
, Color::transparent
);
204 Color
Color::Cyan(COLOR_CYAN
, Color::transparent
);
205 Color
Color::White(COLOR_WHITE
, Color::transparent
);
206 Color
Color::End(0, 0, false, true);
208 int Color::pairNumber() const
213 else if (previousBackground())
214 throw std::logic_error("color depends on the previous background value");
216 throw std::logic_error("'end' doesn't have a corresponding pair number");
219 // colors start with 0, but pairs start with 1. additionally
220 // first pairs are for transparent background, which has a
221 // value of -1, so we need to add 1 to both foreground and
223 result
= background() + 1;
225 result
+= foreground() + 1;
230 std::istream
&operator>>(std::istream
&is
, Color
&c
)
232 auto get_single_color
= [](const std::string
&s
, bool background
) {
235 result
= COLOR_BLACK
;
238 else if (s
== "green")
239 result
= COLOR_GREEN
;
240 else if (s
== "yellow")
241 result
= COLOR_YELLOW
;
242 else if (s
== "blue")
244 else if (s
== "magenta")
245 result
= COLOR_MAGENTA
;
246 else if (s
== "cyan")
248 else if (s
== "white")
249 result
= COLOR_WHITE
;
250 else if (background
&& s
== "previous")
251 result
= NC::Color::previous
;
252 else if (std::all_of(s
.begin(), s
.end(), isdigit
))
254 result
= atoi(s
.c_str());
255 if (result
< 1 || result
> 256)
266 else if (sc
== "end")
270 short value
= get_single_color(sc
, false);
272 c
= Color(value
, NC::Color::transparent
);
275 size_t underscore
= sc
.find('_');
276 if (underscore
!= std::string::npos
)
278 short fg
= get_single_color(sc
.substr(0, underscore
), false);
279 short bg
= get_single_color(sc
.substr(underscore
+1), true);
280 if (fg
!= -1 && bg
!= -1)
283 is
.setstate(std::ios::failbit
);
286 is
.setstate(std::ios::failbit
);
296 bool supportEnabled
= false;
304 // save old highlight mouse tracking
305 std::printf("\e[?1001s");
306 // enable mouse tracking
307 std::printf("\e[?1000h");
308 // try to enable extended (urxvt) mouse tracking
309 std::printf("\e[?1015h");
310 // send the above to the terminal immediately
318 // disable extended (urxvt) mouse tracking
319 std::printf("\e[?1015l");
320 // disable mouse tracking
321 std::printf("\e[?1000l");
322 // restore old highlight mouse tracking
323 std::printf("\e[?1001r");
324 // send the above to the terminal immediately
330 void initScreen(bool enable_colors
, bool enable_mouse
)
333 if (has_colors() && enable_colors
)
336 use_default_colors();
338 for (int bg
= -1; bg
< COLORS
; ++bg
)
340 for (int fg
= 0; npair
< COLOR_PAIRS
&& fg
< COLORS
; ++fg
, ++npair
)
341 init_pair(npair
, fg
, bg
);
351 Mouse::supportEnabled
= enable_mouse
;
354 // initialize readline (needed, otherwise we get segmentation
355 // fault on SIGWINCH). also, initialize first as doing this
356 // later erases keys bound with rl_bind_key for some users.
358 // disable autocompletion
359 rl_attempted_completion_function
= [](const char *, int, int) -> char ** {
360 rl_attempted_completion_over
= 1;
363 auto abort_prompt
= [](int, int) -> int {
368 // if ctrl-c or ctrl-g is pressed, abort the prompt
369 rl_bind_key('\3', abort_prompt
);
370 rl_bind_key('\7', abort_prompt
);
371 // do not change the state of the terminal
372 rl_prep_term_function
= nullptr;
373 rl_deprep_term_function
= nullptr;
374 // do not catch signals
375 rl_catch_signals
= 0;
376 rl_catch_sigwinch
= 0;
377 // overwrite readline callbacks
378 rl_getc_function
= rl::read_key
;
379 rl_redisplay_function
= rl::display_string
;
380 rl_startup_hook
= rl::add_base
;
390 Window::Window(size_t startx
,
402 m_window_timeout(-1),
405 m_border(std::move(border
)),
407 m_title(std::move(title
)),
408 m_escape_terminal_sequences(true),
410 m_underline_counter(0),
411 m_reverse_counter(0),
412 m_alt_charset_counter(0)
414 if (m_start_x
> size_t(COLS
)
415 || m_start_y
> size_t(LINES
)
416 || m_width
+m_start_x
> size_t(COLS
)
417 || m_height
+m_start_y
> size_t(LINES
))
418 throw std::logic_error("constructed window doesn't fit into the terminal");
427 if (!m_title
.empty())
433 m_window
= newpad(m_height
, m_width
);
438 Window::Window(const Window
&rhs
)
439 : m_window(dupwin(rhs
.m_window
))
440 , m_start_x(rhs
.m_start_x
)
441 , m_start_y(rhs
.m_start_y
)
442 , m_width(rhs
.m_width
)
443 , m_height(rhs
.m_height
)
444 , m_window_timeout(rhs
.m_window_timeout
)
445 , m_color(rhs
.m_color
)
446 , m_base_color(rhs
.m_base_color
)
447 , m_border(rhs
.m_border
)
448 , m_prompt_hook(rhs
.m_prompt_hook
)
449 , m_title(rhs
.m_title
)
450 , m_color_stack(rhs
.m_color_stack
)
451 , m_input_queue(rhs
.m_input_queue
)
453 , m_escape_terminal_sequences(rhs
.m_escape_terminal_sequences
)
454 , m_bold_counter(rhs
.m_bold_counter
)
455 , m_underline_counter(rhs
.m_underline_counter
)
456 , m_reverse_counter(rhs
.m_reverse_counter
)
457 , m_alt_charset_counter(rhs
.m_alt_charset_counter
)
461 Window::Window(Window
&&rhs
)
462 : m_window(rhs
.m_window
)
463 , m_start_x(rhs
.m_start_x
)
464 , m_start_y(rhs
.m_start_y
)
465 , m_width(rhs
.m_width
)
466 , m_height(rhs
.m_height
)
467 , m_window_timeout(rhs
.m_window_timeout
)
468 , m_color(rhs
.m_color
)
469 , m_base_color(rhs
.m_base_color
)
470 , m_border(rhs
.m_border
)
471 , m_prompt_hook(rhs
.m_prompt_hook
)
472 , m_title(std::move(rhs
.m_title
))
473 , m_color_stack(std::move(rhs
.m_color_stack
))
474 , m_input_queue(std::move(rhs
.m_input_queue
))
475 , m_fds(std::move(rhs
.m_fds
))
476 , m_escape_terminal_sequences(rhs
.m_escape_terminal_sequences
)
477 , m_bold_counter(rhs
.m_bold_counter
)
478 , m_underline_counter(rhs
.m_underline_counter
)
479 , m_reverse_counter(rhs
.m_reverse_counter
)
480 , m_alt_charset_counter(rhs
.m_alt_charset_counter
)
482 rhs
.m_window
= nullptr;
485 Window
&Window::operator=(Window rhs
)
487 std::swap(m_window
, rhs
.m_window
);
488 std::swap(m_start_x
, rhs
.m_start_x
);
489 std::swap(m_start_y
, rhs
.m_start_y
);
490 std::swap(m_width
, rhs
.m_width
);
491 std::swap(m_height
, rhs
.m_height
);
492 std::swap(m_window_timeout
, rhs
.m_window_timeout
);
493 std::swap(m_color
, rhs
.m_color
);
494 std::swap(m_base_color
, rhs
.m_base_color
);
495 std::swap(m_border
, rhs
.m_border
);
496 std::swap(m_prompt_hook
, rhs
.m_prompt_hook
);
497 std::swap(m_title
, rhs
.m_title
);
498 std::swap(m_color_stack
, rhs
.m_color_stack
);
499 std::swap(m_input_queue
, rhs
.m_input_queue
);
500 std::swap(m_fds
, rhs
.m_fds
);
501 std::swap(m_escape_terminal_sequences
, rhs
.m_escape_terminal_sequences
);
502 std::swap(m_bold_counter
, rhs
.m_bold_counter
);
503 std::swap(m_underline_counter
, rhs
.m_underline_counter
);
504 std::swap(m_reverse_counter
, rhs
.m_reverse_counter
);
505 std::swap(m_alt_charset_counter
, rhs
.m_alt_charset_counter
);
514 void Window::setColor(Color c
)
518 if (c
!= Color::Default
)
520 if (c
.previousBackground())
521 c
= Color(c
.foreground(), m_color
.background());
522 wcolor_set(m_window
, c
.pairNumber(), nullptr);
525 wcolor_set(m_window
, m_base_color
.pairNumber(), nullptr);
526 m_color
= std::move(c
);
529 void Window::setBaseColor(Color c
)
531 m_base_color
= std::move(c
);
534 void Window::setBorder(Border border
)
536 if (!border
&& m_border
)
542 recreate(m_width
, m_height
);
544 else if (border
&& !m_border
)
550 recreate(m_width
, m_height
);
555 void Window::setTitle(const std::string
&new_title
)
557 if (!new_title
.empty() && m_title
.empty())
561 recreate(m_width
, m_height
);
563 else if (new_title
.empty() && !m_title
.empty())
567 recreate(m_width
, m_height
);
572 void Window::recreate(size_t width
, size_t height
)
575 m_window
= newpad(height
, width
);
576 wtimeout(m_window
, 0);
580 void Window::moveTo(size_t new_x
, size_t new_y
)
589 if (!m_title
.empty())
593 void Window::adjustDimensions(size_t width
, size_t height
)
600 if (!m_title
.empty())
606 void Window::resize(size_t new_width
, size_t new_height
)
608 adjustDimensions(new_width
, new_height
);
609 recreate(m_width
, m_height
);
612 void Window::refreshBorder() const
616 size_t start_x
= getStartX(), start_y
= getStarty();
617 size_t width
= getWidth(), height
= getHeight();
618 color_set(m_border
->pairNumber(), nullptr);
619 attron(A_ALTCHARSET
);
621 mvaddch(start_y
, start_x
, 'l');
622 mvaddch(start_y
, start_x
+width
-1, 'k');
623 mvaddch(start_y
+height
-1, start_x
, 'm');
624 mvaddch(start_y
+height
-1, start_x
+width
-1, 'j');
626 mvhline(start_y
, start_x
+1, 'q', width
-2);
627 mvhline(start_y
+height
-1, start_x
+1, 'q', width
-2);
628 mvvline(start_y
+1, start_x
, 'x', height
-2);
629 mvvline(start_y
+1, start_x
+width
-1, 'x', height
-2);
630 if (!m_title
.empty())
632 mvaddch(start_y
+2, start_x
, 't');
633 mvaddch(start_y
+2, start_x
+width
-1, 'u');
635 attroff(A_ALTCHARSET
);
638 color_set(m_base_color
.pairNumber(), nullptr);
639 if (!m_title
.empty())
642 mvhline(m_start_y
-2, m_start_x
, ' ', m_width
);
644 mvaddstr(m_start_y
-2, m_start_x
, m_title
.c_str());
647 mvhline(m_start_y
-1, m_start_x
, 0, m_width
);
653 void Window::display()
659 void Window::refresh()
661 prefresh(m_window
, 0, 0, m_start_y
, m_start_x
, m_start_y
+m_height
-1, m_start_x
+m_width
-1);
669 void Window::bold(bool bold_state
) const
671 (bold_state
? wattron
: wattroff
)(m_window
, A_BOLD
);
674 void Window::underline(bool underline_state
) const
676 (underline_state
? wattron
: wattroff
)(m_window
, A_UNDERLINE
);
679 void Window::reverse(bool reverse_state
) const
681 (reverse_state
? wattron
: wattroff
)(m_window
, A_REVERSE
);
684 void Window::altCharset(bool altcharset_state
) const
686 (altcharset_state
? wattron
: wattroff
)(m_window
, A_ALTCHARSET
);
689 void Window::setTimeout(int timeout
)
691 m_window_timeout
= timeout
;
694 void Window::addFDCallback(int fd
, void (*callback
)())
696 m_fds
.push_back(std::make_pair(fd
, callback
));
699 void Window::clearFDCallbacksList()
704 bool Window::FDCallbacksListEmpty() const
706 return m_fds
.empty();
709 Key::Type
Window::getInputChar(int key
)
711 if (!m_escape_terminal_sequences
|| key
!= Key::Escape
)
713 auto define_mouse_event
= [this](int type
) {
717 m_mouse_event
.bstate
= BUTTON1_PRESSED
;
720 m_mouse_event
.bstate
= BUTTON2_PRESSED
;
723 m_mouse_event
.bstate
= BUTTON3_PRESSED
;
726 m_mouse_event
.bstate
= BUTTON4_PRESSED
;
729 m_mouse_event
.bstate
= BUTTON5_PRESSED
;
735 m_mouse_event
.bstate
|= BUTTON_SHIFT
;
737 m_mouse_event
.bstate
|= BUTTON_ALT
;
739 m_mouse_event
.bstate
|= BUTTON_CTRL
;
740 if (m_mouse_event
.x
< 0 || m_mouse_event
.x
>= COLS
)
742 if (m_mouse_event
.y
< 0 || m_mouse_event
.y
>= LINES
)
746 auto get_xterm_modifier_key
= [](int ch
) {
751 modifier
= Key::Shift
;
757 modifier
= Key::Alt
| Key::Shift
;
760 modifier
= Key::Ctrl
;
763 modifier
= Key::Ctrl
| Key::Shift
;
766 modifier
= Key::Alt
| Key::Ctrl
;
769 modifier
= Key::Alt
| Key::Ctrl
| Key::Shift
;
772 modifier
= Key::None
;
776 auto parse_number
= [this](int &result
) {
780 x
= wgetch(m_window
);
783 result
= result
*10 + x
- '0';
786 key
= wgetch(m_window
);
790 return Key::Shift
| Key::Tab
;
792 key
= wgetch(m_window
);
811 return Key::Ctrl
| Key::Up
;
813 return Key::Ctrl
| Key::Down
;
815 return Key::Ctrl
| Key::Right
;
817 return Key::Ctrl
| Key::Left
;
831 key
= wgetch(m_window
);
835 return Key::Shift
| Key::Up
;
837 return Key::Shift
| Key::Down
;
839 return Key::Shift
| Key::Right
;
841 return Key::Shift
| Key::Left
;
854 case 'M': // standard mouse event
856 key
= wgetch(m_window
);
857 int raw_x
= wgetch(m_window
);
858 int raw_y
= wgetch(m_window
);
859 // support coordinates up to 255
860 m_mouse_event
.x
= (raw_x
- 33) & 0xff;
861 m_mouse_event
.y
= (raw_y
- 33) & 0xff;
862 return define_mouse_event(key
);
865 return Key::Shift
| Key::Tab
;
866 case '[': // F1 to F5 in tty
867 key
= wgetch(m_window
);
883 case '1': case '2': case '3':
884 case '4': case '5': case '6':
885 case '7': case '8': case '9':
888 int delim
= parse_number(key
);
889 if (key
>= 2 && key
<= 8)
895 modifier
= Key::Null
;
898 modifier
= Key::Ctrl
;
901 modifier
= Key::Shift
;
904 modifier
= Key::Ctrl
| Key::Shift
;
906 case ';': // xterm insert/delete/page up/page down
908 int local_key
= wgetch(m_window
);
909 modifier
= get_xterm_modifier_key(local_key
);
910 local_key
= wgetch(m_window
);
911 if (local_key
!= '~' || (key
!= 2 && key
!= 3 && key
!= 5 && key
!= 6))
921 return modifier
| Key::Insert
;
923 return modifier
| Key::Delete
;
925 return modifier
| Key::End
;
927 return modifier
| Key::PageUp
;
929 return modifier
| Key::PageDown
;
931 return modifier
| Key::Home
;
933 return modifier
| Key::End
;
935 std::cerr
<< "Unreachable code, aborting.\n";
957 case 17: // not a typo
967 case 23: // not a typo
980 key
= wgetch(m_window
);
981 Key::Type modifier
= get_xterm_modifier_key(key
);
982 if (modifier
== Key::None
)
984 key
= wgetch(m_window
);
988 return modifier
| Key::Up
;
990 return modifier
| Key::Down
;
992 return modifier
| Key::Right
;
994 return modifier
| Key::Left
;
996 return modifier
| Key::End
;
998 return modifier
| Key::Home
;
1003 default: // urxvt mouse
1004 m_mouse_event
.x
= 0;
1005 delim
= parse_number(m_mouse_event
.x
);
1008 m_mouse_event
.y
= 0;
1009 delim
= parse_number(m_mouse_event
.y
);
1014 return define_mouse_event(key
);
1025 default: // alt + something
1027 auto key_prim
= getInputChar(key
);
1028 if (key_prim
!= Key::None
)
1029 return Key::Alt
| key_prim
;
1035 Key::Type
Window::readKey()
1038 // if there are characters in input queue,
1039 // get them and return immediately.
1040 if (!m_input_queue
.empty())
1042 result
= m_input_queue
.front();
1043 m_input_queue
.pop();
1049 FD_SET(STDIN_FILENO
, &fds_read
);
1050 timeval timeout
= { m_window_timeout
/1000, (m_window_timeout
%1000)*1000 };
1052 int fd_max
= STDIN_FILENO
;
1053 for (const auto &fd
: m_fds
)
1055 if (fd
.first
> fd_max
)
1057 FD_SET(fd
.first
, &fds_read
);
1060 auto tv_addr
= m_window_timeout
< 0 ? nullptr : &timeout
;
1061 int res
= select(fd_max
+1, &fds_read
, nullptr, nullptr, tv_addr
);
1064 if (FD_ISSET(STDIN_FILENO
, &fds_read
))
1066 int key
= wgetch(m_window
);
1070 result
= getInputChar(key
);
1075 for (const auto &fd
: m_fds
)
1076 if (FD_ISSET(fd
.first
, &fds_read
))
1084 void Window::pushChar(const Key::Type ch
)
1086 m_input_queue
.push(ch
);
1089 std::string
Window::prompt(const std::string
&base
, size_t width
, bool encrypted
)
1093 rl::aborted
= false;
1095 getyx(m_window
, rl::start_y
, rl::start_x
);
1096 rl::width
= std::min(m_width
-rl::start_x
-1, width
-1);
1097 rl::encrypted
= encrypted
;
1098 rl::base
= base
.c_str();
1102 m_escape_terminal_sequences
= false;
1103 char *input
= readline(nullptr);
1104 m_escape_terminal_sequences
= true;
1107 if (input
!= nullptr)
1109 #ifdef HAVE_READLINE_HISTORY_H
1110 if (!encrypted
&& input
[0] != 0)
1112 #endif // HAVE_READLINE_HISTORY_H
1118 throw PromptAborted(std::move(result
));
1123 void Window::goToXY(int x
, int y
)
1125 wmove(m_window
, y
, x
);
1130 return getcurx(m_window
);
1135 return getcury(m_window
);
1138 bool Window::hasCoords(int &x
, int &y
)
1140 return wmouse_trafo(m_window
, &y
, &x
, 0);
1143 bool Window::runPromptHook(const char *arg
, bool *done
) const
1147 bool continue_
= m_prompt_hook(arg
);
1148 if (done
!= nullptr)
1156 size_t Window::getWidth() const
1164 size_t Window::getHeight() const
1166 size_t height
= m_height
;
1169 if (!m_title
.empty())
1174 size_t Window::getStartX() const
1182 size_t Window::getStarty() const
1184 size_t starty
= m_start_y
;
1187 if (!m_title
.empty())
1192 const std::string
&Window::getTitle() const
1197 const Color
&Window::getColor() const
1202 const Border
&Window::getBorder() const
1207 int Window::getTimeout() const
1209 return m_window_timeout
;
1212 const MEVENT
&Window::getMouseEvent()
1214 return m_mouse_event
;
1217 void Window::scroll(Scroll where
)
1220 scrollok(m_window
, 1);
1227 wscrl(m_window
, -1);
1229 case Scroll::PageUp
:
1230 wscrl(m_window
, m_width
);
1232 case Scroll::PageDown
:
1233 wscrl(m_window
, -m_width
);
1239 scrollok(m_window
, 0);
1243 Window
&Window::operator<<(const Color
&c
)
1247 while (!m_color_stack
.empty())
1248 m_color_stack
.pop();
1249 setColor(m_base_color
);
1253 if (!m_color_stack
.empty())
1254 m_color_stack
.pop();
1255 if (!m_color_stack
.empty())
1256 setColor(m_color_stack
.top());
1258 setColor(m_base_color
);
1263 m_color_stack
.push(c
);
1268 Window
&Window::operator<<(Format format
)
1273 bold((m_bold_counter
= 0));
1274 reverse((m_reverse_counter
= 0));
1275 altCharset((m_alt_charset_counter
= 0));
1278 bold(++m_bold_counter
);
1280 case Format::NoBold
:
1281 if (--m_bold_counter
<= 0)
1282 bold((m_bold_counter
= 0));
1284 case Format::Underline
:
1285 underline(++m_underline_counter
);
1287 case Format::NoUnderline
:
1288 if (--m_underline_counter
<= 0)
1289 underline((m_underline_counter
= 0));
1291 case Format::Reverse
:
1292 reverse(++m_reverse_counter
);
1294 case Format::NoReverse
:
1295 if (--m_reverse_counter
<= 0)
1296 reverse((m_reverse_counter
= 0));
1298 case Format::AltCharset
:
1299 altCharset(++m_alt_charset_counter
);
1301 case Format::NoAltCharset
:
1302 if (--m_alt_charset_counter
<= 0)
1303 altCharset((m_alt_charset_counter
= 0));
1309 Window
&Window::operator<<(TermManip tm
)
1313 case TermManip::ClearToEOL
:
1315 auto x
= getX(), y
= getY();
1316 mvwhline(m_window
, y
, x
, ' ', m_width
-x
);
1324 Window
&Window::operator<<(const XY
&coords
)
1326 goToXY(coords
.x
, coords
.y
);
1330 Window
&Window::operator<<(const char *s
)
1332 waddstr(m_window
, s
);
1336 Window
&Window::operator<<(char c
)
1338 // Might cause problem similar to
1339 // https://github.com/arybczak/ncmpcpp/issues/21, enable for testing as the
1340 // code in the ticket supposed to be culprit was rewritten.
1341 waddnstr(m_window
, &c
, 1);
1342 //wprintw(m_window, "%c", c);
1346 Window
&Window::operator<<(const wchar_t *ws
)
1348 #if NCURSES_WADDWSTR
1349 waddwstr(m_window
, ws
);
1351 wprintw(m_window
, "%ls", ws
);
1352 #endif // NCURSES_WADDWSTR
1356 Window
&Window::operator<<(wchar_t wc
)
1358 #if NCURSES_WADDNWSTR
1359 waddnwstr(m_window
, &wc
, 1);
1361 wprintw(m_window
, "%lc", wc
);
1362 #endif // NCURSES_WADDNWSTR
1366 Window
&Window::operator<<(int i
)
1368 wprintw(m_window
, "%d", i
);
1372 Window
&Window::operator<<(double d
)
1374 wprintw(m_window
, "%f", d
);
1378 Window
&Window::operator<<(const std::string
&s
)
1380 waddnstr(m_window
, s
.c_str(), s
.length());
1384 Window
&Window::operator<<(const std::wstring
&ws
)
1386 #if NCURSES_WADDNWSTR
1387 waddnwstr(m_window
, ws
.c_str(), ws
.length());
1389 wprintw(m_window
, "%lc", ws
.c_str());
1390 #endif // NCURSES_WADDNWSTR
1394 Window
&Window::operator<<(size_t s
)
1396 wprintw(m_window
, "%zu", s
);