Skip to content

Commit

Permalink
Remove 8 bit control codes (#259)
Browse files Browse the repository at this point in the history
* Terminals no longer write 8-bit control codes.

Due to a misunderstanding with the VT100 protocol, Terminal++ was sending
the code for activating 8-bit mode, but this should be sent from the
terminal and not the application.  A further change should allow the
reception of this code in order to switch the terminal mode.

For now, sending 8-bit control codes is removed, although received 8-bit
codes still parse correctly.

Fixes #255.

* Updated Readme and examples for updated terminal constructor.
  • Loading branch information
KazDragon authored Mar 8, 2021
1 parent 51da084 commit 3fab0a2
Show file tree
Hide file tree
Showing 19 changed files with 42 additions and 193 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,6 @@ target_sources(terminalpp_tester
test/screen_test.cpp
test/string_test.cpp
test/string_manip_test.cpp
test/terminal_init_test.cpp
test/terminal_cursor_test.cpp
test/terminal_erase_test.cpp
test/terminal_read_test.cpp
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ int main()
using namespace terminalpp::literals;
terminalpp::string text = "Hello, world!\n"_ts;

terminalpp::terminal terminal{write_to_console};
terminalpp::terminal terminal;
terminal.write(write_to_console) << text;
}

Expand All @@ -112,7 +112,7 @@ int main()
using namespace terminalpp::literals;
terminalpp::string text = "\\[1Hello, \\[2World! \\x\\U263A\n"_ets;
terminalpp::terminal terminal{write_to_console};
terminalpp::terminal terminal;
terminal.write(write_to_console) << text;
}
```
Expand Down Expand Up @@ -140,7 +140,7 @@ void write_to_console(terminalpp::bytes data)
int main()
{
using namespace terminalpp::literals;
terminalpp::terminal terminal{write_to_console};
terminalpp::terminal terminal;

terminal.write(write_to_console)
<< terminalpp::save_cursor_position()
Expand Down Expand Up @@ -195,7 +195,7 @@ void write_to_console(terminalpp::bytes data)

int main()
{
terminalpp::terminal terminal{write_to_console};
terminalpp::terminal terminal;
terminalpp::screen screen;
terminalpp::canvas canvas({80, 24});

Expand Down
2 changes: 1 addition & 1 deletion examples/encoded_hello_world/src/encoded_hello_world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ int main()
using namespace terminalpp::literals;
terminalpp::string text = "\\[1Hello, \\[2World! \\x\\U263A\n"_ets;

terminalpp::terminal terminal{write_to_console};
terminalpp::terminal terminal;
terminal.write(write_to_console) << text;
}
2 changes: 1 addition & 1 deletion examples/hello_world/src/hello_world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ int main()
using namespace terminalpp::literals;
terminalpp::string text = "Hello, world!\n"_ts;

terminalpp::terminal terminal{write_to_console};
terminalpp::terminal terminal;
terminal.write(write_to_console) << text;
}
2 changes: 1 addition & 1 deletion examples/positioned_smiley/src/positioned_smiley.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ void write_to_console(terminalpp::bytes data)
int main()
{
using namespace terminalpp::literals;
terminalpp::terminal terminal{write_to_console};
terminalpp::terminal terminal;

terminal.write(write_to_console)
<< terminalpp::save_cursor_position()
Expand Down
2 changes: 1 addition & 1 deletion examples/shocking_pink/src/shocking_pink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void write_to_console(terminalpp::bytes data)

int main()
{
terminalpp::terminal terminal{write_to_console};
terminalpp::terminal terminal;
terminalpp::screen screen;
terminalpp::canvas canvas({80, 24});

Expand Down
2 changes: 1 addition & 1 deletion examples/tprint/src/tprint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int main(int argc, char **argv)
}
else
{
terminalpp::terminal terminal{write_to_console};
terminalpp::terminal terminal;
terminal.write(write_to_console)
<< terminalpp::encode(argv[1])
<< "\n";
Expand Down
15 changes: 1 addition & 14 deletions include/terminalpp/behaviour.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ namespace terminalpp {
struct TERMINALPP_EXPORT behaviour
{
constexpr behaviour()
: can_use_eight_bit_control_codes(false),
uses_eight_bit_control_codes_by_default(false),
supports_cha(true),
: supports_cha(true),
supports_cha_default(true),
supports_vpa(true),
supports_vpa_default(true),
Expand All @@ -28,17 +26,6 @@ struct TERMINALPP_EXPORT behaviour
{
}

// Eight bit control codes save on a character each time an ANSI
// control code is used. This could amount to quite the saving over
// time.
bool can_use_eight_bit_control_codes : 1;

// If a terminal can use eight bit control codes, it may require a
// code to enable that sequence. Some terminals may default to using
// eight bit control codes, but not understand the enable sequence, so
// it should only be used if actually required.
bool uses_eight_bit_control_codes_by_default : 1;

// True if the terminal supports Cursor Horizontal Absolute
bool supports_cha : 1;

Expand Down
39 changes: 9 additions & 30 deletions include/terminalpp/detail/element_difference.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,9 @@ namespace terminalpp { namespace detail {
template <class WriteContinuation>
void csi(behaviour const &terminal_behaviour, WriteContinuation &&wc)
{
static byte_storage const csi7 = {
terminalpp::ansi::control7::csi[0],
terminalpp::ansi::control7::csi[1],
};

static byte_storage const csi8 = {
terminalpp::ansi::control8::csi
};

wc(terminal_behaviour.can_use_eight_bit_control_codes ? csi8 : csi7);
wc({
std::cbegin(ansi::control7::csi),
std::cend(ansi::control7::csi)});
}

//* =========================================================================
Expand All @@ -40,16 +33,9 @@ void csi(behaviour const &terminal_behaviour, WriteContinuation &&wc)
template <class WriteContinuation>
void osc(behaviour const &terminal_behaviour, WriteContinuation &&wc)
{
static byte_storage const osc7 = {
terminalpp::ansi::control7::osc[0],
terminalpp::ansi::control7::osc[1],
};

static byte_storage const osc8 = {
terminalpp::ansi::control8::osc
};

wc(terminal_behaviour.can_use_eight_bit_control_codes ? osc8 : osc7);
wc({
std::cbegin(ansi::control7::osc),
std::cend(ansi::control7::osc)});
}

//* =========================================================================
Expand All @@ -58,16 +44,9 @@ void osc(behaviour const &terminal_behaviour, WriteContinuation &&wc)
template <class WriteContinuation>
void st(behaviour const &terminal_behaviour, WriteContinuation &&wc)
{
static byte_storage const st7 = {
terminalpp::ansi::control7::st[0],
terminalpp::ansi::control7::st[1],
};

static byte_storage const st8 = {
terminalpp::ansi::control8::st
};

wc(terminal_behaviour.can_use_eight_bit_control_codes ? st8 : st7);
wc({
std::cbegin(ansi::control7::st),
std::cend(ansi::control7::st)});
}

//* =========================================================================
Expand Down
25 changes: 0 additions & 25 deletions include/terminalpp/detail/terminal_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,6 @@

namespace terminalpp { namespace detail {

//* =========================================================================
/// \brief A manipulator that sets up the initial required state of a
/// terminal.
//* =========================================================================
struct TERMINALPP_EXPORT initialise_terminal
{
//* =====================================================================
/// \brief Write the initializer for the 8-bit control mode if necessary.
//* =====================================================================
template <class WriteContinuation>
void operator()(
terminalpp::behaviour const &beh,
terminalpp::terminal_state &state,
WriteContinuation &&cont) const
{
if (beh.can_use_eight_bit_control_codes
&& !beh.uses_eight_bit_control_codes_by_default)
{
cont({
std::cbegin(terminalpp::ansi::control8::enable),
std::cend(terminalpp::ansi::control8::enable)});
}
}
};

//* =========================================================================
/// \brief A manipulator that initializes the attributes to the default if it
/// has not yet been done.
Expand Down
18 changes: 2 additions & 16 deletions include/terminalpp/terminal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,15 @@ class TERMINALPP_EXPORT terminal
public:
//* =====================================================================
/// \brief Constructor.
///
/// \tparam WriteContinuation is a callable that matches the
/// signature \code void (bytes) \endcode.
//* =====================================================================
template <class WriteContinuation>
explicit terminal(
WriteContinuation &&cont,
behaviour const &beh = behaviour{})
: behaviour_(beh)
{
write(std::forward<WriteContinuation>(cont))
<< detail::initialise_terminal();
}
explicit terminal(behaviour const &beh = behaviour{});

//* =====================================================================
/// \brief Sets the size of the terminal.
/// This is used to determine cursor locations when writing text that
/// wraps at the end of the line, etc.
//* =====================================================================
void set_size(terminalpp::extent size)
{
state_.terminal_size_ = size;
}
void set_size(extent size);

//* =====================================================================
/// \brief Write to the terminal.
Expand Down
16 changes: 16 additions & 0 deletions src/terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,20 @@ namespace terminalpp {

terminal_state::terminal_state() = default;

// ==========================================================================
// CONSTRUCTOR
// ==========================================================================
terminal::terminal(behaviour const &beh)
: behaviour_(beh)
{
}

// ==========================================================================
// SET_SIZE
// ==========================================================================
void terminal::set_size(extent size)
{
state_.terminal_size_ = size;
}

}
2 changes: 1 addition & 1 deletion test/palette_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ TEST_P(colour_attribute_strings, output_the_correct_ansi_data)
result.append(std::cbegin(data), std::cend(data));
};

terminalpp::terminal terminal{discard_result};
terminalpp::terminal terminal;

// First write a space in the default attribute. Thereafter, we write
// only what is not default about the palette-based element.
Expand Down
3 changes: 1 addition & 2 deletions test/screen_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class a_screen : public a_terminal
public:
a_screen()
: size_{5, 5},
canvas_(size_),
reference_terminal_(discard_result)
canvas_(size_)
{
terminal_.set_size(size_);
reference_terminal_.set_size(size_);
Expand Down
65 changes: 0 additions & 65 deletions test/terminal_init_test.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion test/terminal_read_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class terminal_read_test_base
{
public:
terminal_read_test_base(terminalpp::behaviour const &behaviour = terminalpp::behaviour{})
: terminal_([](terminalpp::bytes) {}, behaviour)
: terminal_(behaviour)
{
}

Expand Down
27 changes: 0 additions & 27 deletions test/terminal_settings_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,33 +134,6 @@ TEST_F(a_terminal_with_support_for_window_title_st, sends_window_title_with_bel)
expect_sequence("\x1B]2;title\x1B\\"_tb, result_);
}

namespace {

class a_terminal_with_support_for_window_title_st_and_8bit_control_codes : public a_terminal
{
public:
a_terminal_with_support_for_window_title_st_and_8bit_control_codes()
: a_terminal(
[]
{
terminalpp::behaviour beh;
beh.supports_window_title_st = true;
beh.can_use_eight_bit_control_codes = true;
beh.uses_eight_bit_control_codes_by_default = true;
return beh;
}())
{
}
};

}

TEST_F(a_terminal_with_support_for_window_title_st_and_8bit_control_codes, sends_window_title_with_bel)
{
terminal_.write(append_to_result) << terminalpp::set_window_title("title");
expect_sequence("\x9D"_tb "2;title\x9C"_tb, result_);
}

TEST_F(a_terminal, activating_normal_screen_buffer_sends_use_normal_screen_buffer_codes)
{
terminal_.write(append_to_result) << terminalpp::use_normal_screen_buffer();
Expand Down
2 changes: 1 addition & 1 deletion test/terminal_string_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ TEST(a_terminal_that_supports_unicode_in_all_charsets, skips_charset_switch_befo
result.append(data.begin(), data.end());
};

terminalpp::terminal terminal{discard_result, behaviour};
terminalpp::terminal terminal{behaviour};
terminal.write(append_result) << "\\cU\\C205\\U0057"_ets;

expect_sequence("\x1B[0m\x1B(U\xCD\x1B%GW"_tb, result);
Expand Down
Loading

0 comments on commit 3fab0a2

Please sign in to comment.