- size, usize, position
- root_window
- other functions
- grid
- window
- component
- scroll_bar
- form
- button
- compact_button
- label
- entrybox
- checkbox
- radio_button
- radio_button_collection
- scale
- textbox
- textbox_reflowed
- The
size
struct template is a simple 2D size class that can hold width and height values, usesint
to store the values. - The
usize
same as size but usesunsigned int
since some newt api require it. - The
position
struct has the same interfaces assize
andusize
butwidth
andheight
are calledleft
andtop
. It's used to express the position of the component instead of it's size.
int width { 0 }
int height { 0 }
size(const int, const int)
size(const size_base<other_number>)
-
A constructor that initializes the width and height to the specified values.
-
A templated constructor that initializes the size from another size_base object that may have a different integral type. The constructor uses static_cast to convert the other object's values to the integral type of this object.
size operator+(const int) const
size operator-(const int) const
size operator*(const int) const
size operator/(const int) const
size operator+(const size) const
size operator-(const size) const
size operator*(const size) const
size operator/(const size) const
-
Returns a new
size
object that is the result of applying the operator with the specifiedconst int
value to both thewidth
andheight
of the current object. -
Returns a new
size
object that is the result of applying the operator with the correspondent othersize.widht
andsize.height
to the current object.
The root_window
class provides a set of static methods to manipulate the root window of the newt Terminal User Interface (TUI). The root_window
class cannot be instantiated or copied, as it only contains static methods.
static void init() noexcept
Initializes the newt library and clears the root window.
static void finish() noexcept
Shuts down the newt library.
static void draw_text(const position, const std::string_view)
Draws the given text string on the root window at the given position.
static void push_help_line(const std::string_view)
Pushes the given text string as a help line on the root window.
static void push_default_help_line()
Pushes the default help line on the root window.
static void clear_help_line()
Clears the help line on the root window.
static void pop_help_line()
Removes the last help line pushed onto the root window.
root_window::init();
root_window::draw_text(position(2, 2), "Hello World!");
root_window::push_help_line("Press any key to quit");
newt::wait_for_key();
root_window::pop_help_line();
root_window::finish();
void refresh()
Refreshes the screen.
void bell()
Makes the terminal bell.
size get_screen_size()
Returns the size
of the terminal screen as a size object, which contains the width and height of the screen.
usize get_screen_usize()
Same as get_screen_size()
but retruns usize
.
void clear_key_buffer()
Clears the keyboard buffer.
void wait_for_key()
Waits for a key press before continuing execution.
void reflow_text(std::string&, const int)
Reflows a given std::string
to fit within the specified width. It takes a reference to the string to be reflowed and the desired width as const int
.
std::string compute_filler(const std::string_view OLD, const std::string_view NEW)
Computes the filler string required to fill the gap between two strings of different lengths. It takes two string views as arguments, the old and new strings, and returns a string view containing the filler characters.
void inline resize_screen(const bool)
Resizes the terminal screen to the specified dimensions. The const bool
argument specifies whether the screen should be redrawn after the resize.
void inline delay(const unsigned int)
Pauses program execution for the specified number of microseconds.
void inline cursor_on()
Turns on the cursor display.
void inline cursor_off()
Turns off the cursor display.
template <component_or_collection... components_and_collections_t>
[[nodiscard]] inline std::pair<exit_info, form> fast_run(const int COLS, const int ROWS, const std::string_view TITLE, components_and_collections_t&... components)
This is a function template called fast_run
that takes as template arguments a variadic list of types that can be either a component
or a component_collection
. The function returns a std::pair
of exit_info
and form
.
The function takes several parameters:
COLS
: representing the number of columns in the grid.ROWS
: representing the number of rows in the grid.TITLE
: representing the title of the window.components_and_collections_t&... components
: a variadic list of references to objects of the types specified in the template parameter list. These objects are used to populate the grid in the form.
Inside the function, a grid
object is created using the specified number of rows and columns and the provided components and collections. A window
object is created using the created grid and the provided title, the window is centered. A form
object is created using the provided components and collections.
Finally, the run
method of the form
object is called, which will display the form to the user and wait for input. The function returns a std::pair
containing the exit status of the form and the form
object itself (this is mainly to allow the callee to access the components since the form would free them), which can be used for further processing.
The grid
class represents a grid-based layout of user interface elements in the Newt library.
explicit grid(const int COLS, const int ROWS) noexcept
template <generic_component... component_types> explicit
grid(const int COLS, const int ROWS, const component_types&...) noexcept
-
Constructs an empty
grid
with the specified number of columns and rows. -
Constructs a
grid
with the specified number ofcolumns
androws
, and fills it with the specifiedcomponents
.
void set_field(const int COL, const int ROW, const component_t& COMPONENT, const padding PADDING = {}, const int ANCHOR = anchor::NOWHERE, const int GROW = grow::NO)
Sets the specified component at the given column and row, with optional padding, anchor, and grow parameters.
std::pair<int, int> get_cols_rows()
Returns a std::pair
containing the number of cols
and rows
in the grid
.
template <generic_component... component_types>
void set_fields(const component_types&...)
TODO: UPDATE Fills the grid
with the specified components.
explicit operator newtGrid() const
Returns the underlying newtGrid
data type.
The grid
class utilizes the following data structures and enum:
struct padding {
int left { 0 };
int top { 0 };
int right { 0 };
int bottom { 0 };
};
The padding
struct defines the amount of padding on each side of a component within the grid
.
enum anchor {
NOWHERE = 0,
LEFT = NEWT_ANCHOR_LEFT,
RIGHT = NEWT_ANCHOR_RIGHT,
TOP = NEWT_ANCHOR_TOP,
BOTTOM = NEWT_ANCHOR_BOTTOM,
};
The anchor
enum defines the anchor position for a component within the grid
.
enum grow {
NO = 0,
X = NEWT_GRID_FLAG_GROWX,
Y = NEWT_GRID_FLAG_GROWY,
};
The grow
enum defines whether a component
can grow in a certain direction within the grid
.
The window
class allows the user to create a window. It provides several constructors to create a new window with a specified size
and title, or to wrap an existing grid
in a window.
window(const usize, const std::string_view = std::string_view {}) noexcept
window(const position, const usize, const std::string_view = std::string_view {}) noexcept
explicit window(const grid&, const std::string_view = {}) noexcept
-
Creates a new window with the specified
usize
and optional title. The window is centered on the screen. -
Creates a new window with the specified
usize
,position
, and optional title. The window is positioned at the specifiedposition
. -
Wraps an existing
grid
in a window with the optional title.
The component
class is a wrapper around a newtComponent
object that provides ownership management and convenience methods for working with components.
This class is the base class for all the components this library offers and should not be used directly by the user.
However an istance of this class is returned when a form gets runned since the newt api doesn't give a way to deduce the component type.
component(newtComponent) noexcept
component(component&) = default
component(component&&) = default
-
Constructs a
component
object that owns the givennewtComponent
. -
Constructs a
component
object that takes ownership from the othercomponent
. -
Move constructs a compoennt object that takes owership from the other
component
.
component& operator=(component&&) noexcept = default
Move assignment operator, takes ownership from the other component
.
operator newtComponent() const
Conversion operator that returns the underlying newtComponent
.
newtComponent get_owneship()
Returns the underlying newtComponent
and releases ownership from the component
object.
bool operator==(const component_t&) const
Comparison operator that checks if two component
objects have the same underlying newtComponent
.
The scroll_bar
class is a derived class of the component
class that represents a scroll bar component.
scroll_bar(const position POS, const int HEIGHT, const int NORMAL_COLOR_SET, const int THUMB_COLOR_SET) noexcept
Constructs a scroll_bar
object with the specified position, height, and colors for the normal and thumb states of the scrollbar. The scroll_bar
object takes ownership of a newtVerticalScrollbar
object constructed with the given parameters.
void set(const int WHERE, const int TOTAL)
Sets the position of the scrollbar to the specified WHERE
value and the total range to the specified TOTAL
value.
void set_color(const int NORMAL, const int THUMB)
Sets the colors of the normal and thumb states of the scrollbar to the specified values.
The form
class is a subclass of component
that represents a form widget in the newt UI library.
explicit form(void* help_tag = nullptr, const int FLAGS = 0) noexcept
explicit form(scroll_bar& bar, void* help_tag = nullptr, const int FLAGS = 0) noexcept
template <component_or_collection... components_and_collections_t>
explicit form(components_and_collections_t&... components) noexcept
-
Constructs a new
form
object with an optionalhelp_tag
andFLAGS
. Thehelp_tag
is an opaque data pointer that can be used to associate help text with the form. -
Constructs a new
form
object that contains the givenscroll_bar
and has an optionalhelp_tag
andFLAGS
. -
Constructs a new
form
object that contains the givencomponents
and/orcollections
.
void add_component(component_t&)
Adds a component
to the form.
void add_component(collection_t&)
Adds a component_collection
to the form.
void add_components(collections_and_components_t&...)
Adds multiple components
and/or component_collection
to the form.
auto run() -> exit_info
Displays the form and waits for user input. Returns an exit_info
object that describes how the user exited the form.
void add_hot_key(const int)
Registers a keyboard shortcut key for the form.
void watch_fd(const int FILE_DESCRIPTOR, const int FLAGS)
Sets up a file descriptor for the form to listen to.
void draw_form()
Draws the form on the screen.
void set_current(const component_t&)
Sets the current component of the form to the given component
.
void set_background(const int)
Sets the background color of the form.
void set_height(const int)
Sets the height of the form.
void set_width(const int)
Sets the width of the form.
component get_current()
Returns the current component of the form as a component
object.
The struct exit_info
contains informations about how the user exited a form
.
struct exit_info {
exit_reason reason;
std::variant<int, component> data;
};
- If the reason is
HOTKEY
thandata
contains the key pressed. - If the reason is
COMPONENT
thandata
contains thecomponent
that caused the exit. - If the reason is
TIMER
thandata
the watch variable. - If the reason is
FDREADY
orERROR
thandata
is empty.
The button
class is a wrapper around a newtButton
object that provides ownership management and convenience methods for working with buttons. It inherits from the component
class.
button(const std::string_view, const position) noexcept
Constructs a button
object with the specified text and position.
The compact_button
class is a wrapper around a newtCompactButton
object that represents a button with a simple text label that can be clicked. It uses less screen space than button.
explicit compact_button(const std::string_view, const position) noexcept
Constructs a compact_button
object with the specified text and position.
The label
class is a derived class of the component
class and represents a text label on a form in the Newt library.
explicit label(const std::string_view, const position) noexcept
Constructs a label
object with the given text and position.
void set_text(const std::string_view)
Sets the text of the label to the given text.
void set_colors(const int)
Sets the colors of the label.
The entrybox
class is a wrapper around a newtEntry
object that provides ownership management and convenience methods for working with text input fields.
explicit entrybox(const int WIDTH, const position POS = { 0, 0 }, std::string_view INITIAL_VALUE = { "" }, const int FLAGS = NEWT_ENTRY_SCROLL) noexcept
Constructs an entrybox
object with the specified WIDTH
and POS
. INITIAL_VALUE
is the initial text value of the entrybox, and FLAGS
are the display flags for the entrybox.
void set_value(const std::string_view TEXT, const bool CURSOR_AT_END = true)
Sets the text value of the entrybox to TEXT
. If CURSOR_AT_END
is true, the cursor is moved to the end of the text.
std::string_view get_value()
Returns the current text value of the entrybox as a std::string_view
.
// TODO: Add filter api
TODO: Add a filter API for restricting input to the entrybox.
The checkbox
class is a wrapper around a newtCheckbox
object that provides ownership management and convenience methods for working with checkboxes.
checkbox(const std::string_view TEXT, const position POS = { 0, 0 }, const char DEFAULT_VAL = ' ', const std::string_view SEQ = std::string_view {}) noexcept
Constructs a checkbox
object with the given TEXT
, POS
, DEFAULT_VAL
, and SEQ
.
Checkkboxes in newt can cycle on a range of values, but by default there are only two states:
' '
'*'
The SEQ
parameter can be used to define all the possible states for a checkbox.
char get_value()
Returns the value of the checkbox as a char
.
void set_value(const char)
Sets the value of the checkbox.
The radio_button
class is a wrapper around a newtRadiobutton
object that provides ownership management and convenience methods for working with radio buttons.
radio_button()
explicit radio_button(std::string_view TEXT, const position POS = { 0, 0 }, const radio_button& PREVIUS = {}, bool IS_DEFAULT = false) noexcept
-
Constructs a
radio_button
object that does not own anynewtRadiobutton
object. -
Constructs a
radio_button
object that owns thenewtRadiobutton
object created with the given parameters.
If the PREVIUS
parameter is a default constructed radio_button
then created radio_button also creates a new group.
If the PREVIUS
parameter is filled with an owning radio_button
than the created radio_button is added to it's group.
[[nodiscard]] radio_button get_current() const
Returns a new radio_button
object that does not owns the newtRadiobutton
object that is currently selected in the group, or a default constructed radio_button
object if no radio button is selected.
void set_current()
Makes the radio button the selected one in it's group.
The radio_button_collection
class represents a collection of radio_button
objects. It provides methods for accessing and manipulating the collection as a whole, such as setting the current radio button and getting the index of the current radio button.
template <typename... T>
explicit radio_button_collection(T... strings)
Constructs a radio_button_collection
object with a series of string arguments, each of which is used to create a new radio_button
object that is added to the collection.
std::vector<radio_button>& get_components()
Returns a reference to the internal vector of radio_button
objects.
const std::vector<radio_button>& get_components() const
Returns a const reference to the internal vector of radio_button
objects.
size_t get_current_index() const
Returns the index of the current radio_button
object in the collection.
void set_current(const size_t INDEX)
Sets the current radio_button
object to the one at the specified index.
template <typename... T>
void add_radio_button(std::string_view TEXT, const position POS = { 0, 0 }, const bool DEFAULT = false)
Adds a new radio_button
object to the collection with the specified text, position, and default value. If the collection is empty, the new radio button becomes the first radio button in the collection. Otherwise, the new radio button is added after the last radio button in the collection.
The scale
class is a wrapper around a newtScale
object that provides ownership management and a method for setting the value of the scale.
scale(const int WIDTH, const long long FULL_VALUE, const position POS = { 0, 0 }) noexcept
Constructs a scale
object with the given WIDTH
and FULL_VALUE
parameters, and optionally a position
parameter for its position on the form.
void set_value(const unsigned long long VALUE)
Sets the value of the scale to the given VALUE
.
The textbox
class is a wrapper around a newtTextbox
object that provides ownership management and convenience methods for working with textboxes.
explicit textbox(const size, std::string_view, position = { 0, 0 }, bool = true) noexcept
Constructs a textbox
object with the given size, text, and position. If IS_SCROLLABLE
is set to true, the textbox can be scrolled vertically.
void set_text(std::string_view)
Sets the text of the textbox. The text must be the same length or shorter.
void set_height(int)
Sets the height of the textbox.
int get_num_lines() const
Returns the number of lines of text in the textbox.
void set_colors(const int NORMALE,const int ACTIVE)
Sets the colors of the textbox. The NORMAL
parameter specifies the color of the text when the textbox is not active, while the ACTIVE
parameter specifies the color when the textbox is active.
The textbox_reflowed
class is a specialization of the component
class that represents a text box widget that can reflow its content based on the available width.
explicit textbox_reflowed(const int WIDTH, const std::string_view TEXT, const position POS = { 0, 0 }) noexcept
Constructs a textbox_reflowed
object with the given WIDTH
, initial TEXT
and POS
.
void set_text(const std::string_view TEXT)
Sets the content of the text box to the given TEXT
.
void set_height(const int HEIGHT)
Sets the height of the text box to the given HEIGHT
.
int get_num_lines() const
Returns the number of lines of text in the text box.
void set_colors(const int NORMAL, const int ACTIVE)
Sets the colors of the text box in normal and active states.