Skip to content

Commit

Permalink
Update tools/termcaps
Browse files Browse the repository at this point in the history
  • Loading branch information
a-n-t-h-o-n-y committed Feb 8, 2024
1 parent 9fb022a commit 6a538be
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 384 deletions.
1 change: 1 addition & 0 deletions include/esc/esc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <esc/area.hpp>
#include <esc/brush.hpp>
#include <esc/color.hpp>
#include <esc/detail/transcode.hpp>
#include <esc/event.hpp>
#include <esc/glyph.hpp>
#include <esc/io.hpp>
Expand Down
52 changes: 37 additions & 15 deletions include/esc/sequence.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <concepts>
#include <string>
#include <string_view>
#include <type_traits>
#include <variant>

#include <esc/brush.hpp>
Expand All @@ -15,18 +17,23 @@ namespace esc {

// MOVE CURSOR -----------------------------------------------------------------

/**
* Type alias to move the cursor to a Point.
*/
using Cursor = Point;

/**
* Tag type to move the cursor to a Point.
*/
struct CursorPosition {
public:
constexpr CursorPosition(int x, int y) : at{.x = x, .y = y} {}
// struct CursorPosition {
// public:
// constexpr CursorPosition(int x, int y) : at{.x = x, .y = y} {}

constexpr CursorPosition(Point p) : at{p} {}
// constexpr CursorPosition(Point p) : at{p} {}

public:
Point at;
};
// public:
// Point at;
// };

/**
* Get the control sequence to move the cursor to the specified Point.
Expand All @@ -37,7 +44,7 @@ struct CursorPosition {
* @param p The Point to move the cursor to.
* @return The control sequence to move the cursor to the specified Point.
*/
[[nodiscard]] auto escape(CursorPosition p) -> std::string;
[[nodiscard]] auto escape(Cursor p) -> std::string;

// CLEAR -----------------------------------------------------------------------

Expand All @@ -49,8 +56,7 @@ struct BlankRow {};
/**
* Get the control sequence to clear the row the cursor is currently at.
*
* @details Write escape(CursorPosition) result before to pick which line to
* clear.
* @details Write escape(Cursor) result before to pick which line to clear.
* @param BlankRow The tag type to clear the row the cursor is currently at.
* @return The control sequence to clear the row the cursor is currently at.
*/
Expand Down Expand Up @@ -217,22 +223,39 @@ struct BlankScreen {};
*/
[[nodiscard]] auto escape(Glyph const& g) -> std::string;

/**
* Get the control sequence to set each Glyph in the GlyphString.
*
* @param gs The GlyphString to set.
* @return The control sequence to set each Glyph in the GlyphString.
*/
template <GlyphString T>
[[nodiscard]] auto escape(T const& gs) -> std::string
{
auto result = std::string{};
for (auto const& g : gs) {
result += escape(g);
}
return result;
}

// CONVENIENCE -----------------------------------------------------------------

/**
* Types that can have a control sequence generated for them.
*/
template <typename T>
concept Escapable = detail::AnyOf<T,
CursorPosition,
Cursor,
BlankRow,
BlankScreen,
Trait,
Traits,
ColorBG,
ColorFG,
Brush,
Glyph>;
Glyph> ||
GlyphString<T>;

/**
* Convenience function to concatenate multiple escapable objects at once.
Expand All @@ -242,11 +265,10 @@ concept Escapable = detail::AnyOf<T,
* @param args... A list of escapable objects.
* @return A single string containing the escape sequences for all args...
*/
template <Escapable... Args>
template <Escapable... Args,
typename = std::enable_if_t<(sizeof...(Args) > 1), void>>
[[nodiscard]] auto escape(Args&&... args) -> std::string
{
static_assert(sizeof...(Args) > 0,
"escape(...): Must have at least one argument.");
return (escape(std::forward<Args>(args)) + ...);
}

Expand Down
12 changes: 6 additions & 6 deletions include/esc/terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ void set(KeyMode x);
/**
* `Show` will display the cursor on screen; `Hide` will not.
*/
enum class Cursor { Show, Hide };
enum class CursorMode { Show, Hide };

/**
* Set the cursor to the given value.
* @details Calls on write internally, but does not call flush().
* @param x The Cursor value to set.
* @param x The CursorMode to set.
*/
void set(Cursor x);
void set(CursorMode x);

// MOUSE MODE ------------------------------------------------------------------

Expand All @@ -125,7 +125,7 @@ concept Setable = detail::AnyOf<T,
InputBuffer,
Signals,
ScreenBuffer,
Cursor,
CursorMode,
MouseMode,
KeyMode>;

Expand Down Expand Up @@ -167,7 +167,7 @@ auto set(Args&&... args) -> void
* Move: Basic, plus Mouse Move Events are generated with or without a
* button pressed.
*
* @param Cursor
* @param CursorMode
* Show: The Cursor will be displayed on screen.
* Hide: The Cursor will not be displayed on screen.
*
Expand Down Expand Up @@ -195,7 +195,7 @@ auto set(Args&&... args) -> void
*/
void initialize_terminal(ScreenBuffer,
MouseMode,
Cursor,
CursorMode,
Echo,
InputBuffer,
Signals,
Expand Down
11 changes: 7 additions & 4 deletions src/sequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
#include <type_traits>
#include <variant>

#include <esc/brush.hpp>
#include <esc/color.hpp>
#include <esc/detail/transcode.hpp>
#include <esc/glyph.hpp>
#include <esc/trait.hpp>

namespace {
Expand Down Expand Up @@ -73,10 +76,10 @@ auto traits_to_int_sequence(esc::Traits traits) -> std::string

namespace esc {

auto escape(CursorPosition p) -> std::string
auto escape(Cursor p) -> std::string
{
return "\033[" + std::to_string(p.at.y + 1) + ';' +
std::to_string(p.at.x + 1) + 'H';
return "\033[" + std::to_string(p.y + 1) + ';' + std::to_string(p.x + 1) +
'H';
}

auto escape(BlankRow) -> std::string
Expand Down Expand Up @@ -181,7 +184,7 @@ auto foreground_color() -> Color { return ::current_foreground; }

auto escape(Brush b) -> std::string
{
return escape(background(b.background)) + escape(foreground(b.foreground)) +
return escape(bg(b.background)) + escape(fg(b.foreground)) +
escape(b.traits);
}

Expand Down
15 changes: 8 additions & 7 deletions src/terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ void set(KeyMode x)
}
}

void set(Cursor x)
void set(CursorMode x)
{
switch (x) {
case Cursor::Show:
case CursorMode::Show:
write(
"\033["
"?25h");
break;
case Cursor::Hide:
case CursorMode::Hide:
write(
"\033["
"?25l");
Expand Down Expand Up @@ -169,7 +169,7 @@ void set(MouseMode x)

void initialize_terminal(ScreenBuffer screen_buffer,
MouseMode mouse_mode,
Cursor cursor,
CursorMode cursor,
Echo echo,
InputBuffer input_buffer,
Signals signals,
Expand Down Expand Up @@ -202,7 +202,7 @@ void initialize_terminal(ScreenBuffer screen_buffer,

void initialize_normal_terminal()
{
initialize_terminal(ScreenBuffer::Normal, MouseMode::Off, Cursor::Show,
initialize_terminal(ScreenBuffer::Normal, MouseMode::Off, CursorMode::Show,
Echo::On, InputBuffer::Canonical, Signals::On,
KeyMode::Normal);
}
Expand All @@ -211,7 +211,7 @@ void initialize_interactive_terminal(MouseMode mouse_mode,
KeyMode key_mode,
Signals signals)
{
initialize_terminal(ScreenBuffer::Alternate, mouse_mode, Cursor::Hide,
initialize_terminal(ScreenBuffer::Alternate, mouse_mode, CursorMode::Hide,
Echo::Off, InputBuffer::Immediate, signals, key_mode);
}

Expand All @@ -220,7 +220,8 @@ void uninitialize_terminal()
// TODO take settings parameter and use that to reset the terminal to
// settings before.
write(turn_on_auto_wrap());
set(ScreenBuffer::Normal, MouseMode::Off, Cursor::Show, KeyMode::Normal);
set(ScreenBuffer::Normal, MouseMode::Off, CursorMode::Show,
KeyMode::Normal);
flush();
::tcsetattr(STDIN_FILENO, TCSAFLUSH, &original_termios);
if (detail::tty_file_descriptor.has_value()) {
Expand Down
Loading

0 comments on commit 6a538be

Please sign in to comment.