From 669b23ac0a62fa1c9ca13b6307469acf1f0ac34f Mon Sep 17 00:00:00 2001 From: Peter Elliott Date: Wed, 6 Oct 2021 00:58:09 -0600 Subject: [PATCH] Kernel: Handle backspace for tab character in TTY cooked mode Before, serenity would only backspace one character for a tab. This is the only feature that my OS has and serenity doesn't. --- Kernel/TTY/TTY.cpp | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/Kernel/TTY/TTY.cpp b/Kernel/TTY/TTY.cpp index fd553c8af3..86ea95cd94 100644 --- a/Kernel/TTY/TTY.cpp +++ b/Kernel/TTY/TTY.cpp @@ -280,14 +280,32 @@ bool TTY::can_do_backspace() const return false; } +static size_t length_with_tabs(CircularDeque const& line) +{ + size_t length = 0; + for (auto& ch : line) { + length += (ch == '\t') ? 8 - (length % 8) : 1; + } + return length; +} + void TTY::do_backspace() { if (can_do_backspace()) { - m_input_buffer.dequeue_end(); - // We deliberately don't process the output here. - echo(8); - echo(' '); - echo(8); + auto ch = m_input_buffer.dequeue_end(); + size_t to_delete = 1; + + if (ch == '\t') { + auto length = length_with_tabs(m_input_buffer); + to_delete = 8 - (length % 8); + } + + for (size_t i = 0; i < to_delete; ++i) { + // We deliberately don't process the output here. + echo('\b'); + echo(' '); + echo('\b'); + } evaluate_block_conditions(); }