kaycxx-term
C++ terminal library
Loading...
Searching...
No Matches
terminal.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Klaus Reimer <k@ailis.de>
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
11#include <ios>
12#include <iostream>
13#include <memory>
14#include <ostream>
15#include <string>
16#include <string_view>
17
26#include <kaycxx/term/sgr.hpp>
28
29namespace kaycxx::term {
30
31namespace detail {
32class input_mode_stack;
33}
34
40class terminal {
41public:
51 explicit terminal(std::ostream& stream = std::cout, ansi_mode mode = ansi_mode::automatic);
52
55
56 terminal(terminal const&) = delete;
57 terminal& operator=(terminal const&) = delete;
58 terminal(terminal&&) = delete;
59 terminal& operator=(terminal&&) = delete;
60
71 template <typename T>
72 terminal& operator<<(T const& value) {
73 stream_ << value;
74 return *this;
75 }
76
84
92
100
107 terminal& operator<<(std::ostream& (*manipulator)(std::ostream&));
108
115 terminal& operator<<(std::ios& (*manipulator)(std::ios&));
116
123 terminal& operator<<(std::ios_base& (*manipulator)(std::ios_base&));
124
129
134
139
144
149
154
159
165 void push_title(std::string_view title);
166
170 void pop_title();
171
176
181
186
191
198
207 void move_to(int column, int row);
208
217 void move_by(int columns, int rows);
218
224 void move_to_column(int column);
225
230
235
240
245
252
260
267
276 std::string read_key();
277
282
290 void insert_lines(int lines);
291
296
304 void delete_lines(int lines);
305
312
321 void clear_cells(int cells);
322
329
337 void insert_cells(int cells);
338
345
353 void delete_cells(int cells);
354
363 void set_scroll_region(int top, int bottom);
364
369
373 void scroll_up();
374
382 void scroll_up(int lines);
383
388
396 void scroll_down(int lines);
397
405 bool formatting_enabled() const noexcept;
406
414 bool commands_enabled() const noexcept;
415
416private:
418 std::ostream& stream_;
419
421 bool formatting_enabled_;
422
424 bool commands_enabled_;
425
427 std::unique_ptr<detail::input_mode_stack> input_modes_;
428
435 void write_color_value(basic_color value, color_style style);
436
443 void write_color_value(indexed_color value, color_style style);
444
451 void write_color_value(rgb_color value, color_style style);
452};
453
454} // namespace kaycxx::term
Defines ANSI control sequence handling modes.
ansi_mode
Selects which ANSI/VT formatting and terminal commands are emitted.
Definition ansi_mode.hpp:16
Defines the basic 16-color terminal palette.
Terminal output and input helper.
Definition terminal.hpp:40
void clear_cell()
Clears a cell at the cursor position.
void delete_cells(int cells)
Deletes cells at the cursor position.
void clear_cells(int cells)
Clears cells at the cursor position.
std::string read_key()
Reads one key from the terminal.
void delete_cell()
Deletes a cell at the cursor position.
void insert_line()
Inserts a single line at the cursor row.
bool commands_enabled() const noexcept
Checks whether ANSI/VT terminal commands are enabled for this terminal.
void clear_line_to_start()
Clears from the beginning of the current line to the cursor position.
void reset_scroll_region()
Resets the vertical scrolling region to the whole screen.
void clear_line_to_end()
Clears from the cursor position to the end of the current line.
void delete_line()
Deletes a single line at the cursor row.
void scroll_down(int lines)
Scrolls the current scrolling region down by multiple lines.
void leave_alternate_screen()
Leaves the alternate screen buffer.
void clear_screen_to_start()
Clears from the beginning of the screen to the cursor position.
void disable_wrap()
Disables terminal autowrap mode.
terminal & operator<<(std::ios_base &(*manipulator)(std::ios_base &))
Writes an ios_base manipulator.
terminal & operator<<(std::ios &(*manipulator)(std::ios &))
Writes an ios manipulator.
void clear_screen_to_end()
Clears from the cursor position to the end of the screen.
terminal(std::ostream &stream=std::cout, ansi_mode mode=ansi_mode::automatic)
Creates a terminal helper for an output stream.
void scroll_down()
Scrolls the current scrolling region down by one line.
terminal & operator<<(std::ostream &(*manipulator)(std::ostream &))
Writes an ostream manipulator.
void hide_cursor()
Hides the terminal cursor.
void clear_screen()
Clears the visible screen without moving the cursor.
bool pop_input_mode()
Pops and restores the previously pushed input state.
void insert_cell()
Inserts a blank cell at the cursor position.
void delete_lines(int lines)
Deletes multiple lines at the cursor row.
void set_cursor_style(cursor_style style)
Sets the terminal cursor style.
void pop_title()
Pops the previously pushed terminal title.
void move_by(int columns, int rows)
Moves the cursor relative to its current position.
terminal & operator<<(color_style value)
Writes a foreground or background color style.
void clear_scrollback()
Clears the terminal scrollback buffer when supported by the terminal.
terminal & operator<<(T const &value)
Writes a raw stream value.
Definition terminal.hpp:72
void move_to(int column, int row)
Moves the cursor to an absolute position.
void insert_cells(int cells)
Inserts blank cells at the cursor position.
~terminal()
Destroys the terminal helper.
void enable_wrap()
Enables terminal autowrap mode.
void enter_alternate_screen()
Enters the alternate screen buffer.
terminal & operator<<(hyperlink const &link)
Writes a terminal hyperlink.
void clear_line()
Clears the current line without moving the cursor.
terminal_size size() const
Returns the current terminal size.
void set_scroll_region(int top, int bottom)
Sets the vertical scrolling region.
void save_cursor()
Saves the current cursor position.
void scroll_up()
Scrolls the current scrolling region up by one line.
void restore_cursor()
Restores the previously saved cursor position.
terminal & operator<<(sgr value)
Writes an SGR style command.
void move_to_column(int column)
Moves the cursor to a column on the current row.
void push_title(std::string_view title)
Pushes the current terminal title and sets a new title.
void show_cursor()
Shows the terminal cursor.
void scroll_up(int lines)
Scrolls the current scrolling region up by multiple lines.
bool push_input_mode(input_mode mode)
Pushes the current input state and applies a new input mode.
bool formatting_enabled() const noexcept
Checks whether ANSI/VT formatting is enabled for this terminal.
void insert_lines(int lines)
Inserts multiple lines at the cursor row.
Defines foreground and background color styling helpers.
Defines terminal cursor style values.
cursor_style
Terminal cursor display style.
Definition cursor_style.hpp:16
Defines indexed 256-color terminal colors.
Defines terminal input modes.
input_mode
Terminal input mode.
Definition input_mode.hpp:16
Defines RGB true-color terminal colors.
Defines SGR terminal style commands.
Basic terminal palette color.
Definition basic_color.hpp:21
Color style command written to a terminal.
Definition color_style.hpp:18
Indexed terminal palette color.
Definition indexed_color.hpp:18
RGB terminal color.
Definition rgb_color.hpp:18
Select Graphic Rendition command.
Definition sgr.hpp:16
Terminal dimensions in character cells.
Definition terminal_size.hpp:16
Defines terminal size values.