kakoune/src/terminal_ui.cc

1561 lines
51 KiB
C++
Raw Normal View History

2019-11-16 11:27:06 +01:00
#include "terminal_ui.hh"
#include "buffer_utils.hh"
#include "display_buffer.hh"
2013-04-09 20:05:40 +02:00
#include "event_manager.hh"
#include "exception.hh"
#include "file.hh"
2014-11-12 22:27:07 +01:00
#include "keys.hh"
#include "ranges.hh"
#include "string_utils.hh"
#include "diff.hh"
#include <algorithm>
#include <fcntl.h>
#include <csignal>
#include <sys/ioctl.h>
#include <unistd.h>
#include <strings.h>
namespace Kakoune
{
2013-10-17 19:48:12 +02:00
using std::min;
using std::max;
static String fix_atom_text(StringView str)
{
String res;
auto pos = str.begin();
for (auto it = str.begin(), end = str.end(); it != end; ++it)
{
unsigned char c = *it;
if (c <= 0x1F)
{
res += StringView{pos, it};
res += String{Codepoint{(uint32_t)(0x2400 + c)}};
pos = it+1;
}
}
res += StringView{pos, str.end()};
return res;
}
struct TerminalUI::Window::Line
{
struct Atom
{
String text;
ColumnCount skip = 0;
Face face;
2019-12-01 01:09:01 +01:00
ColumnCount length() const { return text.column_length() + skip; }
void resize(ColumnCount size)
{
auto it = text.begin(), end = text.end();
while (it != end and size > 0)
size -= codepoint_width(utf8::read_codepoint(it, end));
if (size < 0) // possible if resizing to the middle of a double-width codepoint
{
kak_assert(size == -1);
utf8::to_previous(it, text.begin());
skip = 1;
}
else
skip = size;
text.resize(it - text.begin(), 0);
}
friend bool operator==(const Atom& lhs, const Atom& rhs) = default;
friend size_t hash_value(const Atom& atom) { return hash_values(atom.text, atom.skip, atom.face); }
};
void append(StringView text, ColumnCount skip, Face face)
2019-12-01 01:09:01 +01:00
{
if (not atoms.empty() and atoms.back().face == face and (atoms.back().skip == 0 or text.empty()))
{
atoms.back().text += fix_atom_text(text);
atoms.back().skip += skip;
}
2019-12-01 01:09:01 +01:00
else
atoms.push_back({fix_atom_text(text), skip, face});
2019-12-01 01:09:01 +01:00
}
void resize(ColumnCount width)
{
auto it = atoms.begin();
ColumnCount column = 0;
for (; it != atoms.end() and column < width; ++it)
column += it->length();
if (column < width)
append({}, width - column, atoms.empty() ? Face{} : atoms.back().face);
else
{
atoms.erase(it, atoms.end());
if (column > width)
atoms.back().resize(atoms.back().length() - (column - width));
}
}
Vector<Atom>::iterator erase_range(ColumnCount pos, ColumnCount len)
{
struct Pos{ Vector<Atom>::iterator it; ColumnCount column; };
auto find_col = [pos=0_col, it=atoms.begin(), end=atoms.end()](ColumnCount col) mutable {
for (; it != end; ++it)
{
auto atom_len = it->length();
if (pos + atom_len >= col)
return Pos{it, col - pos};
pos += atom_len;
}
return Pos{it, 0_col};
};
Pos begin = find_col(pos);
Pos end = find_col(pos+len);
auto make_tail = [](const Atom& atom, ColumnCount from) {
auto it = atom.text.begin(), end = atom.text.end();
while (it != end and from > 0)
from -= codepoint_width(utf8::read_codepoint(it, end));
if (from < 0) // can happen if tail starts in the middle of a double-width codepoint
{
kak_assert(from == -1);
return Atom{" " + StringView{it, end}, atom.skip, atom.face};
}
return Atom{{it, end}, atom.skip - from, atom.face};
};
if (begin.it == end.it)
{
Atom tail = make_tail(*begin.it, end.column);
begin.it->resize(begin.column);
return (tail.text.empty() and tail.skip == 0) ? begin.it+1 : atoms.insert(begin.it+1, tail);
}
begin.it->resize(begin.column);
if (end.column > 0)
{
if (end.column == end.it->length())
++end.it;
else
*end.it = make_tail(*end.it, end.column);
}
return atoms.erase(begin.it+1, end.it);
}
Vector<Atom> atoms;
};
void TerminalUI::Window::create(const DisplayCoord& p, const DisplayCoord& s)
{
kak_assert(p.line >= 0 and p.column >= 0);
kak_assert(s.line >= 0 and s.column >= 0);
pos = p;
size = s;
lines.reset(new Line[(int)size.line]);
}
void TerminalUI::Window::destroy()
{
pos = DisplayCoord{};
size = DisplayCoord{};
lines.reset();
}
void TerminalUI::Window::blit(Window& target)
{
kak_assert(pos.line < target.size.line);
LineCount line_index = pos.line;
for (auto& line : ArrayView{lines.get(), (size_t)size.line})
{
line.resize(size.column);
auto& target_line = target.lines[(size_t)line_index];
target_line.resize(target.size.column);
target_line.atoms.insert(target_line.erase_range(pos.column, size.column), line.atoms.begin(), line.atoms.end());
if (++line_index == target.size.line)
break;
}
}
void TerminalUI::Window::draw(DisplayCoord pos,
ConstArrayView<DisplayAtom> atoms,
2020-10-08 11:56:39 +02:00
const Face& default_face)
2019-12-01 01:09:01 +01:00
{
if (pos.line >= size.line) // We might receive an out of date draw command after a resize
2020-07-03 13:04:08 +02:00
return;
lines[(size_t)pos.line].resize(pos.column);
2019-12-01 01:09:01 +01:00
for (const DisplayAtom& atom : atoms)
{
StringView content = atom.content();
if (content.empty())
continue;
auto face = merge_faces(default_face, atom.face);
if (content.back() == '\n')
lines[(int)pos.line].append(content.substr(0, content.length()-1), 1, face);
2019-12-01 01:09:01 +01:00
else
lines[(int)pos.line].append(content, 0, face);
pos.column += content.column_length();
2019-12-01 01:09:01 +01:00
}
if (pos.column < size.column)
lines[(int)pos.line].append({}, size.column - pos.column, default_face);
2019-12-01 01:09:01 +01:00
}
Fix crash after multiple terminal resizes When Kakoune's terminal is shown on my laptop monitor and I plug in my external monitor, the terminal's workspace will move to that external monitor. When this happens, Kakoune may segfault. There are multiple resize events (SIGWINCH) in quick succession; it crashes because we handle SIGWINCH during rendering. The problem happens during execution of "TerminalUI::Screen::output" (frame #18). When we receive SIGWINCH while writing to stdout, write(2) fails with EAGAIN, prompting us to handle pending events (See ae001a1f9 (Run EventManager whenever writing to a file descriptor would block, 2022-05-10)). We update the screen size in check_resize() here: #4 Kakoune::TerminalUI::check_resize (force=<optimized out>) at terminal_ui.cc:683 #5 Kakoune::TerminalUI::get_next_key () at terminal_ui.cc:719 #6 operator() (__closure=0x555555984198) at terminal_ui.cc:484 #7 std::__invoke_impl<void, Kakoune::TerminalUI::TerminalUI()::<lambda(Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode)>&, Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode> (__f=...) at /usr/include/c++/12.2.1/bits/invoke.h:61 #8 std::__invoke_r<void, Kakoune::TerminalUI::TerminalUI()::<lambda(Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode)>&, Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode> (__fn=...) at /usr/include/c++/12.2.1/bits/invoke.h:111 #9 std::_Function_handler<void(Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode), Kakoune::TerminalUI::TerminalUI()::<lambda(Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode)> >::_M_invoke(const std::_Any_data &, Kakoune::FDWatcher &, Kakoune::FdEvents &&, Kakoune::EventMode &&) (__functor=..., __args#0=..., __args#1=<optimized out>, __args#2=<optimized out>) at /usr/include/c++/12.2.1/bits/std_function.h:290 #10 std::function<void (Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode)>::operator()(Kakoune::FDWatcher&, Kakoune::FdEvents, Kakoune::EventMode) const (__args#2=<optimized out>, __args#1=<optimized out>, __args#0=...) at /usr/include/c++/12.2.1/bits/std_function.h:591 #11 Kakoune::FDWatcher::run (mode=Kakoune::EventMode::Urgent, events=<optimized out>) at event_manager.cc:28 #12 Kakoune::EventManager::handle_next_events (mode=mode@entry=Kakoune::EventMode::Urgent, sigmask=sigmask@entry=0x0, block=<optimized out>, block@entry=false) at event_manager.cc:143 #13 Kakoune::write (fd=1, data=...) at file.cc:273 #14 Kakoune::BufferedWriter<4096>::flush () at string.hh:236 #15 Kakoune::BufferedWriter<4096>::write (data="t file.hh:145 #16 Kakoune::TerminalUI::Screen::set_face (face=..., writer=...) at terminal_ui.cc:255 #17 operator() (line=..., __closure=<synthetic pointer>) at terminal_ui.cc:326 #18 Kakoune::TerminalUI::Screen::output (force=force@entry=true, synchronized=<optimized out>, writer=...) at terminal_ui.cc:402 #19 Kakoune::TerminalUI::redraw (force=force@entry=true) at terminal_ui.cc:571 #20 Kakoune::TerminalUI::refresh (force=<optimized out>) at terminal_ui.cc:592 #21 Kakoune::Client::redraw_ifn () at client.cc:282 #22 Kakoune::ClientManager::redraw_clients () at client_manager.cc:232 #23 Kakoune::run_server (session=..., server_init=..., client_init=..., init_buffer="fish-rust/src/ast.rs", init_coord=..., flags=Kakoune::ServerFlags::None, ui_type=Kakoune::UIType::Terminal, debug_flags=<optimized out>, files=ArrayView<Kakoune::StringView> = {...}) at main.cc:893 #24 main (argc=<optimized out>, argv=<optimized out>) at main.cc:1243 Thereafter, "TerminalUI::Screen::output" resumes and crashes due to a buffer overflow in "lines" which has been resized.
2023-03-12 09:25:55 +01:00
struct Writer : BufferedWriter<true>
{
using Writer::BufferedWriter::BufferedWriter;
~Writer() noexcept(false) = default;
};
template<typename... Args>
static void format_with(Writer& writer, StringView format, Args&&... args)
{
format_with([&](StringView s) { writer.write(s); }, format, std::forward<Args>(args)...);
}
void TerminalUI::Screen::set_face(const Face& face, Writer& writer)
{
static constexpr int fg_table[]{ 39, 30, 31, 32, 33, 34, 35, 36, 37, 90, 91, 92, 93, 94, 95, 96, 97 };
static constexpr int bg_table[]{ 49, 40, 41, 42, 43, 44, 45, 46, 47, 100, 101, 102, 103, 104, 105, 106, 107 };
static constexpr int ul_table[]{ 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
static constexpr const char* attr_table[]{ "0", "4", "4:3", "7", "5", "1", "2", "3", "9" };
auto set_color = [&](bool fg, const Color& color, bool join) {
if (join)
writer.write(";");
if (color.isRGB())
format_with(writer, "{};2;{};{};{}", fg ? 38 : 48, color.r, color.g, color.b);
else
format_with(writer, "{}", (fg ? fg_table : bg_table)[(int)(char)color.color]);
};
if (m_active_face == face)
return;
writer.write("\033[");
bool join = false;
if (face.attributes != m_active_face.attributes)
{
2020-11-02 20:55:15 +01:00
for (int i = 0; i < std::size(attr_table); ++i)
{
if (face.attributes & (Attribute)(1 << i))
format_with(writer, ";{}", attr_table[i]);
}
m_active_face.fg = m_active_face.bg = m_active_face.underline = Color::Default;
join = true;
}
if (m_active_face.fg != face.fg)
{
set_color(true, face.fg, join);
join = true;
}
if (m_active_face.bg != face.bg)
{
set_color(false, face.bg, join);
join = true;
}
if (m_active_face.underline != face.underline)
{
if (join)
writer.write(";");
if (face.underline != Color::Default)
{
if (face.underline.isRGB())
format_with(writer, "58:2::{}:{}:{}", face.underline.r, face.underline.g, face.underline.b);
else
format_with(writer, "58:5:{}", ul_table[(int)(char)face.underline.color]);
}
else
format_with(writer, "59");
}
writer.write("m");
m_active_face = face;
}
void TerminalUI::Screen::output(bool force, bool synchronized, Writer& writer)
{
if (not lines)
return;
if (force)
{
std::fill_n(hashes.get(), (size_t)size.line, 0);
writer.write("\033[m");
m_active_face = Face{};
}
auto hash_line = [](const Line& line) {
return (hash_value(line.atoms) << 1) | 1; // ensure non-zero
};
auto output_line = [&](const Line& line) {
ColumnCount pending_move = 0;
for (auto& [text, skip, face] : line.atoms)
{
if (text.empty() and skip == 0)
continue;
if (pending_move != 0)
{
format_with(writer, "\033[{}C", (int)pending_move);
pending_move = 0;
}
set_face(face, writer);
writer.write(text);
if (skip > 3 and face.attributes == Attribute{})
2019-12-01 01:09:01 +01:00
{
writer.write("\033[K");
pending_move = skip;
2019-12-01 01:09:01 +01:00
}
else if (skip > 0)
writer.write(String{' ', skip});
}
};
if (synchronized)
{
writer.write("\033[?2026h"); // begin synchronized update
struct Change { int keep; int add; int del; };
Vector<Change> changes{Change{}};
auto new_hashes = ArrayView{lines.get(), (size_t)size.line} | transform(hash_line);
for_each_diff(hashes.get(), (int)size.line,
new_hashes.begin(), (int)size.line,
[&changes](DiffOp op, int len) mutable {
switch (op)
{
case DiffOp::Keep:
changes.push_back({len, 0, 0});
break;
case DiffOp::Add:
changes.back().add += len;
break;
case DiffOp::Remove:
changes.back().del += len;
break;
}
});
std::copy(new_hashes.begin(), new_hashes.end(), hashes.get());
int line = 0;
for (auto& change : changes)
{
line += change.keep;
if (int del = change.del - change.add; del > 0)
{
format_with(writer, "\033[{}H\033[{}M", line + 1, del);
line -= del;
}
line += change.del;
}
line = 0;
for (auto& change : changes)
{
line += change.keep;
for (int i = 0; i < change.add; ++i)
{
if (int add = change.add - change.del; i == 0 and add > 0)
format_with(writer, "\033[{}H\033[{}L", line + 1, add);
else
format_with(writer, "\033[{}H", line + 1);
output_line(lines[line++]);
}
}
writer.write("\033[?2026l"); // end synchronized update
}
else
{
for (int line = 0; line < (int)size.line; ++line)
{
auto hash = hash_line(lines[line]);
if (hash == hashes[line])
continue;
hashes[line] = hash;
format_with(writer, "\033[{}H", line + 1);
output_line(lines[line]);
}
}
}
2019-11-16 11:27:06 +01:00
constexpr int TerminalUI::default_shift_function_key;
static constexpr StringView assistant_cat[] =
2015-03-25 14:53:13 +01:00
{ R"( ___ )",
R"( (__ \ )",
R"( / / ╭)",
R"( .' '·. │)",
R"( ' ” │)",
R"( ╰ /\_/| │)",
R"( | . \ │)",
R"( ╰_J` | | | ╯)",
R"( ' \__- _/ )",
R"( \_\ \_\ )",
R"( )"};
static constexpr StringView assistant_clippy[] =
{ " ╭──╮ ",
" │ │ ",
" @ @ ╭",
" ││ ││ │",
" ││ ││ ╯",
" │╰─╯│ ",
" ╰───╯ ",
" " };
static constexpr StringView assistant_dilbert[] =
{ R"( დოოოოოდ )",
R"( | | )",
R"( | | ╭)",
R"( |-ᱛ ᱛ-| │)",
R"( Ͼ Ͽ │)",
R"( | | ╯)",
R"( ˏ`-.ŏ.-´ˎ )",
R"( @ )",
R"( @ )",
R"( )"};
2013-10-17 19:48:12 +02:00
template<typename T> T sq(T x) { return x * x; }
static sig_atomic_t resize_pending = 0;
static sig_atomic_t terminal_hungup = 0;
template<sig_atomic_t* signal_flag>
static void signal_handler(int)
{
*signal_flag = 1;
EventManager::instance().force_signal(0);
}
2012-09-05 00:49:59 +02:00
2019-11-16 11:27:06 +01:00
TerminalUI::TerminalUI()
: m_cursor{CursorMode::Buffer, {}},
m_stdin_watcher{STDIN_FILENO, FdEvents::Read, EventMode::Urgent,
2018-05-17 14:55:53 +02:00
[this](FDWatcher&, FdEvents, EventMode) {
if (not m_on_key)
return;
while (auto key = get_next_key())
{
if (*key == ctrl('z'))
kill(0, SIGTSTP); // We suspend at this line
else if (*key != Key::Invalid)
m_on_key(*key);
}
}},
2019-02-09 05:41:09 +01:00
m_assistant(assistant_clippy)
2012-09-05 00:49:59 +02:00
{
if (not isatty(1))
throw runtime_error("stdout is not a tty");
tcgetattr(STDIN_FILENO, &m_original_termios);
setup_terminal();
set_raw_mode();
2019-09-09 11:39:53 +02:00
enable_mouse(true);
set_signal_handler(SIGWINCH, &signal_handler<&resize_pending>);
set_signal_handler(SIGHUP, &signal_handler<&terminal_hungup>);
2019-11-16 11:27:06 +01:00
set_signal_handler(SIGTSTP, [](int){ TerminalUI::instance().suspend(); });
2015-06-30 01:31:26 +02:00
check_resize(true);
redraw(false);
2012-09-05 00:49:59 +02:00
}
2019-11-16 11:27:06 +01:00
TerminalUI::~TerminalUI()
2012-09-05 00:49:59 +02:00
{
if (not terminal_hungup) {
enable_mouse(false);
restore_terminal();
tcsetattr(STDIN_FILENO, TCSAFLUSH, &m_original_termios);
}
set_signal_handler(SIGWINCH, SIG_DFL);
set_signal_handler(SIGHUP, SIG_IGN);
2019-09-09 12:11:04 +02:00
set_signal_handler(SIGTSTP, SIG_DFL);
}
2019-11-16 11:27:06 +01:00
void TerminalUI::suspend()
2019-09-09 12:11:04 +02:00
{
bool mouse_enabled = m_mouse_enabled;
enable_mouse(false);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &m_original_termios);
restore_terminal();
2019-09-09 12:11:04 +02:00
auto current = set_signal_handler(SIGTSTP, SIG_DFL);
sigset_t unblock_sigtstp, old_mask;
sigemptyset(&unblock_sigtstp);
sigaddset(&unblock_sigtstp, SIGTSTP);
sigprocmask(SIG_UNBLOCK, &unblock_sigtstp, &old_mask);
raise(SIGTSTP); // suspend here
set_signal_handler(SIGTSTP, current);
sigprocmask(SIG_SETMASK, &old_mask, nullptr);
setup_terminal();
2019-09-09 12:11:04 +02:00
check_resize(true);
set_raw_mode();
2019-09-09 12:11:04 +02:00
enable_mouse(mouse_enabled);
refresh(true);
2012-09-05 00:49:59 +02:00
}
2019-11-16 11:27:06 +01:00
void TerminalUI::set_raw_mode() const
2019-09-09 11:39:53 +02:00
{
termios attr = m_original_termios;
attr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
attr.c_oflag &= ~OPOST;
attr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
attr.c_lflag |= NOFLSH;
attr.c_cflag &= ~(CSIZE | PARENB);
attr.c_cflag |= CS8;
attr.c_cc[VMIN] = attr.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &attr);
2019-09-09 11:39:53 +02:00
}
2019-11-16 11:27:06 +01:00
void TerminalUI::redraw(bool force)
{
m_window.blit(m_screen);
if (m_menu.columns != 0 or m_menu.pos.column > m_status_len)
m_menu.blit(m_screen);
m_info.blit(m_screen);
Writer writer{STDOUT_FILENO};
m_screen.output(force, (bool)m_synchronized, writer);
2020-10-08 11:56:39 +02:00
auto set_cursor_pos = [&](DisplayCoord c) {
format_with(writer, "\033[{};{}H", (int)c.line + 1, (int)c.column + 1);
2020-10-08 11:56:39 +02:00
};
if (m_cursor.mode == CursorMode::Prompt)
set_cursor_pos({m_status_on_top ? 0 : m_dimensions.line, m_cursor.coord.column});
else
set_cursor_pos(m_cursor.coord + content_line_offset());
}
2019-11-16 11:27:06 +01:00
void TerminalUI::set_cursor(CursorMode mode, DisplayCoord coord)
{
m_cursor = Cursor{mode, coord};
}
2019-11-16 11:27:06 +01:00
void TerminalUI::refresh(bool force)
{
if (m_dirty or force)
redraw(force);
m_dirty = false;
}
static const DisplayLine empty_line = { String(" "), {} };
2019-11-16 11:27:06 +01:00
void TerminalUI::draw(const DisplayBuffer& display_buffer,
2020-10-08 11:56:39 +02:00
const Face& default_face,
const Face& padding_face)
{
check_resize();
const DisplayCoord dim = dimensions();
const LineCount line_offset = content_line_offset();
LineCount line_index = line_offset;
for (const DisplayLine& line : display_buffer.lines())
m_window.draw(line_index++, line.atoms(), default_face);
auto face = merge_faces(default_face, padding_face);
DisplayAtom padding{String{m_padding_char, m_padding_fill ? dim.column : 1}};
while (line_index < dim.line + line_offset)
m_window.draw(line_index++, padding, face);
m_dirty = true;
}
2019-11-16 11:27:06 +01:00
void TerminalUI::draw_status(const DisplayLine& status_line,
2020-10-08 11:56:39 +02:00
const DisplayLine& mode_line,
const Face& default_face)
{
const LineCount status_line_pos = m_status_on_top ? 0 : m_dimensions.line;
m_window.draw(status_line_pos, status_line.atoms(), default_face);
const auto mode_len = mode_line.length();
m_status_len = status_line.length();
const auto remaining = m_dimensions.column - m_status_len;
if (mode_len < remaining)
{
ColumnCount col = m_dimensions.column - mode_len;
m_window.draw({status_line_pos, col}, mode_line.atoms(), default_face);
}
else if (remaining > 2)
{
DisplayLine trimmed_mode_line = mode_line;
trimmed_mode_line.trim(mode_len + 2 - remaining, remaining - 2);
trimmed_mode_line.insert(trimmed_mode_line.begin(), { "", {} });
kak_assert(trimmed_mode_line.length() == remaining - 1);
ColumnCount col = m_dimensions.column - remaining + 1;
m_window.draw({status_line_pos, col}, trimmed_mode_line.atoms(), default_face);
}
if (m_set_title)
{
Writer writer{STDOUT_FILENO};
constexpr char suffix[] = " - Kakoune\007";
writer.write("\033]2;");
// Fill title escape sequence buffer, removing non ascii characters
for (auto& atom : mode_line)
{
const auto str = atom.content();
for (auto it = str.begin(), end = str.end(); it != end; utf8::to_next(it, end))
writer.write((*it >= 0x20 and *it <= 0x7e) ? *it : '?');
}
writer.write(suffix);
}
m_dirty = true;
}
2019-11-16 11:27:06 +01:00
void TerminalUI::check_resize(bool force)
{
2015-06-30 01:31:26 +02:00
if (not force and not resize_pending)
return;
resize_pending = 0;
const int fd = open("/dev/tty", O_RDWR);
if (fd < 0)
return;
auto close_fd = on_scope_end([fd]{ ::close(fd); });
DisplayCoord terminal_size{24_line, 80_col};
if (winsize ws; ioctl(fd, TIOCGWINSZ, &ws) == 0 and ws.ws_row > 0 and ws.ws_col > 0)
terminal_size = {ws.ws_row, ws.ws_col};
2015-06-30 01:31:26 +02:00
const bool info = (bool)m_info;
const bool menu = (bool)m_menu;
if (m_window) m_window.destroy();
if (info) m_info.destroy();
if (menu) m_menu.destroy();
2015-06-30 01:31:26 +02:00
m_window.create({0, 0}, terminal_size);
m_screen.create({0, 0}, terminal_size);
m_screen.hashes.reset(new size_t[(int)terminal_size.line]{});
kak_assert(m_window);
2015-10-13 14:52:02 +02:00
m_dimensions = terminal_size - 1_line;
// if (char* csr = tigetstr((char*)"csr"))
// putp(tparm(csr, 0, ws.ws_row));
if (menu)
2019-09-09 14:36:58 +02:00
menu_show(Vector<DisplayLine>(std::move(m_menu.items)),
m_menu.anchor, m_menu.fg, m_menu.bg, m_menu.style);
if (info)
info_show(m_info.title, m_info.content, m_info.anchor, m_info.face, m_info.style);
2015-06-30 01:31:26 +02:00
set_resize_pending();
}
2019-11-16 11:27:06 +01:00
Optional<Key> TerminalUI::get_next_key()
{
if (terminal_hungup)
{
set_signal_handler(SIGWINCH, SIG_DFL);
set_signal_handler(SIGHUP, SIG_IGN);
2019-09-17 11:24:19 +02:00
if (m_window)
m_window.destroy();
m_stdin_watcher.disable();
return {};
}
check_resize();
if (m_resize_pending)
{
m_resize_pending = false;
return resize(dimensions());
}
static auto get_char = []() -> Optional<unsigned char> {
if (not fd_readable(STDIN_FILENO))
return {};
if (unsigned char c = 0; read(STDIN_FILENO, &c, 1) == 1)
return c;
terminal_hungup = 1;
return {};
};
const auto c = get_char();
if (not c)
return {};
2020-10-08 11:56:39 +02:00
static constexpr auto control = [](char c) { return c & 037; };
static auto convert = [this](Codepoint c) -> Codepoint {
if (c == control('m') or c == control('j'))
2020-05-12 00:50:27 +02:00
return Key::Return;
if (c == control('i'))
2020-05-12 00:50:27 +02:00
return Key::Tab;
if (c == ' ')
return Key::Space;
2020-10-08 11:56:39 +02:00
if (c == m_original_termios.c_cc[VERASE])
2020-05-12 00:50:27 +02:00
return Key::Backspace;
if (c == 127) // when it's not backspace
return Key::Delete;
2021-02-11 10:14:19 +01:00
if (c == 27)
return Key::Escape;
2020-05-12 00:50:27 +02:00
return c;
};
static auto parse_key = [](unsigned char c) -> Key {
2020-05-12 00:50:27 +02:00
if (Codepoint cp = convert(c); cp > 255)
return Key{cp};
// Special case: you can type NUL with Ctrl-2 or Ctrl-Shift-2 or
// Ctrl-Backtick, but the most straightforward way is Ctrl-Space.
if (c == 0)
return ctrl(Key::Space);
// Represent Ctrl-letter combinations in lower-case, to be clear
// that Shift is not involved.
if (c < 27)
2020-05-12 00:50:27 +02:00
return ctrl(c - 1 + 'a');
// Represent Ctrl-symbol combinations in "upper-case", as they are
// traditionally-rendered.
// Note that Escape is handled elsewhere.
if (c < 32)
return ctrl(c - 1 + 'A');
struct Sentinel{};
struct CharIterator
{
unsigned char operator*() { if (not c) c = get_char().value_or((unsigned char)0); return *c; }
CharIterator& operator++() { c.reset(); return *this; }
bool operator==(const Sentinel&) const { return false; }
Optional<unsigned char> c;
};
return Key{utf8::codepoint(CharIterator{c}, Sentinel{})};
};
static auto parse_mask = [](int mask) {
Key::Modifiers mod = Key::Modifiers::None;
if (mask & 1)
mod |= Key::Modifiers::Shift;
if (mask & 2)
mod |= Key::Modifiers::Alt;
if (mask & 4)
mod |= Key::Modifiers::Control;
return mod;
};
auto parse_csi = [this]() -> Optional<Key> {
auto next_char = [] { return get_char().value_or((unsigned char)0xff); };
2021-10-30 14:21:51 +02:00
int params[16][4] = {};
auto c = next_char();
char private_mode = 0;
if (c == '?' or c == '<' or c == '=' or c == '>')
{
private_mode = c;
c = next_char();
}
2021-10-30 14:21:51 +02:00
for (int count = 0, subcount = 0; count < 16 and c >= 0x30 && c <= 0x3f; c = next_char())
{
if (isdigit(c))
2021-10-30 14:21:51 +02:00
params[count][subcount] = params[count][subcount] * 10 + c - '0';
else if (c == ':' && subcount < 3)
++subcount;
else if (c == ';')
2021-10-30 14:21:51 +02:00
{
++count;
2021-10-30 14:21:51 +02:00
subcount = 0;
}
else
return {};
}
if (c != '$' and (c < 0x40 or c > 0x7e))
return {};
auto mouse_button = [this](Key::Modifiers mod, Key::MouseButton button, Codepoint coord, bool release) {
auto mask = 1 << (int)button;
if (not release)
{
mod |= (m_mouse_state & mask) ? Key::Modifiers::MousePos : Key::Modifiers::MousePress;
m_mouse_state |= mask;
}
else
{
mod |= Key::Modifiers::MouseRelease;
m_mouse_state &= ~mask;
}
return Key{mod | Key::to_modifier(button), coord};
};
auto mouse_scroll = [this](Key::Modifiers mod, bool down) -> Key {
return {mod | Key::Modifiers::Scroll,
(Codepoint)((down ? 1 : -1) * m_wheel_scroll_amount)};
};
2021-10-30 14:21:51 +02:00
auto masked_key = [&](Codepoint key, Codepoint shifted_key = 0) {
int mask = std::max(params[1][0] - 1, 0);
Key::Modifiers modifiers = parse_mask(mask);
2021-10-30 14:21:51 +02:00
if (shifted_key != 0 and (modifiers & Key::Modifiers::Shift))
{
modifiers &= ~Key::Modifiers::Shift;
2021-10-30 14:21:51 +02:00
key = shifted_key;
}
return Key{modifiers, key};
};
switch (c)
{
case '$':
if (private_mode == '?' and next_char() == 'y') // DECRPM
{
2021-10-30 14:21:51 +02:00
if (params[0][0] == 2026)
m_synchronized.supported = (params[1][0] == 1 or params[1][0] == 2);
return Key{Key::Invalid};
}
2021-10-30 14:21:51 +02:00
switch (params[0][0])
{
case 23: case 24:
2021-10-30 14:21:51 +02:00
return Key{Key::Modifiers::Shift, Key::F11 + params[0][0] - 23}; // rxvt style
}
return {};
case 'A': return masked_key(Key::Up);
case 'B': return masked_key(Key::Down);
case 'C': return masked_key(Key::Right);
case 'D': return masked_key(Key::Left);
case 'E': return masked_key('5'); // Numeric keypad 5
case 'F': return masked_key(Key::End); // PC/xterm style
case 'H': return masked_key(Key::Home); // PC/xterm style
2020-03-04 09:06:46 +01:00
case 'P': return masked_key(Key::F1);
case 'Q': return masked_key(Key::F2);
case 'R': return masked_key(Key::F3);
case 'S': return masked_key(Key::F4);
case '~':
2021-10-30 14:21:51 +02:00
switch (params[0][0])
{
case 1: return masked_key(Key::Home); // VT220/tmux style
case 2: return masked_key(Key::Insert);
case 3: return masked_key(Key::Delete);
case 4: return masked_key(Key::End); // VT220/tmux style
case 5: return masked_key(Key::PageUp);
case 6: return masked_key(Key::PageDown);
case 7: return masked_key(Key::Home); // rxvt style
case 8: return masked_key(Key::End); // rxvt style
case 11: case 12: case 13: case 14: case 15:
2021-10-30 14:21:51 +02:00
return masked_key(Key::F1 + params[0][0] - 11);
case 17: case 18: case 19: case 20: case 21:
2021-10-30 14:21:51 +02:00
return masked_key(Key::F6 + params[0][0] - 17);
case 23: case 24:
2021-10-30 14:21:51 +02:00
return masked_key(Key::F11 + params[0][0] - 23);
2019-12-14 23:17:34 +01:00
case 25: case 26:
2021-10-30 14:21:51 +02:00
return Key{Key::Modifiers::Shift, Key::F3 + params[0][0] - 25}; // rxvt style
2019-12-14 23:17:34 +01:00
case 28: case 29:
2021-10-30 14:21:51 +02:00
return Key{Key::Modifiers::Shift, Key::F5 + params[0][0] - 28}; // rxvt style
2019-12-14 23:17:34 +01:00
case 31: case 32:
2021-10-30 14:21:51 +02:00
return Key{Key::Modifiers::Shift, Key::F7 + params[0][0] - 31}; // rxvt style
2019-12-14 23:17:34 +01:00
case 33: case 34:
2021-10-30 14:21:51 +02:00
return Key{Key::Modifiers::Shift, Key::F9 + params[0][0] - 33}; // rxvt style
case 200:
m_paste_buffer = String{};
return Key{Key::Invalid};
case 201:
if (m_paste_buffer)
{
m_on_paste(*m_paste_buffer);
m_paste_buffer.reset();
}
return Key{Key::Invalid};
}
return {};
case 'u':
{
Codepoint key;
switch (params[0][0])
{
// Treat numpad keys the same as their non-numpad counterparts. Could add a numpad modifier here.
case 57414: key = Key::Return; break;
case 57417: key = Key::Left; break;
case 57418: key = Key::Right; break;
case 57419: key = Key::Up; break;
case 57420: key = Key::Down; break;
case 57421: key = Key::PageUp; break;
case 57422: key = Key::PageDown; break;
case 57423: key = Key::Home; break;
case 57424: key = Key::End; break;
case 57425: key = Key::Insert; break;
case 57426: key = Key::Delete; break;
default: key = convert(static_cast<Codepoint>(params[0][0])); break;
}
return masked_key(key, convert(static_cast<Codepoint>(params[0][1])));
}
case 'Z': return shift(Key::Tab);
case 'I': return {Key::FocusIn};
case 'O': return {Key::FocusOut};
case 'M': case 'm':
const bool sgr = private_mode == '<';
if (not sgr and c != 'M')
return {};
2021-10-30 14:21:51 +02:00
const Codepoint b = sgr ? params[0][0] : next_char() - 32;
const int x = (sgr ? params[1][0] : next_char() - 32) - 1;
const int y = (sgr ? params[2][0] : next_char() - 32) - 1;
auto coord = encode_coord({y - content_line_offset(), x});
Key::Modifiers mod = parse_mask((b >> 2) & 0x7);
switch (const int code = b & 0x43; code)
{
case 0: case 1: case 2:
return mouse_button(mod, Key::MouseButton{code}, coord, c == 'm');
case 3:
if (sgr)
return {};
else if (int guess = ffs(m_mouse_state) - 1; 0 <= guess and guess < 3)
return mouse_button(mod, Key::MouseButton{guess}, coord, true);
break;
case 64: return mouse_scroll(mod, false);
case 65: return mouse_scroll(mod, true);
}
return Key{Key::Modifiers::MousePos, coord};
}
return {};
};
static auto parse_ss3 = []() -> Optional<Key> {
int raw_mask = 0;
char code = '0';
do {
raw_mask = raw_mask * 10 + (code - '0');
code = get_char().value_or((unsigned char)0xff);
} while (code >= '0' and code <= '9');
Key::Modifiers mod = parse_mask(std::max(raw_mask - 1, 0));
switch (code)
{
case ' ': return Key{mod, Key::Space};
case 'A': return Key{mod, Key::Up};
case 'B': return Key{mod, Key::Down};
case 'C': return Key{mod, Key::Right};
case 'D': return Key{mod, Key::Left};
case 'F': return Key{mod, Key::End};
case 'H': return Key{mod, Key::Home};
case 'I': return Key{mod, Key::Tab};
case 'M': return Key{mod, Key::Return};
case 'P': return Key{mod, Key::F1};
case 'Q': return Key{mod, Key::F2};
case 'R': return Key{mod, Key::F3};
case 'S': return Key{mod, Key::F4};
case 'X': return Key{mod, '='};
case 'j': return Key{mod, '*'};
case 'k': return Key{mod, '+'};
case 'l': return Key{mod, ','};
case 'm': return Key{mod, '-'};
case 'n': return Key{mod, '.'};
case 'o': return Key{mod, '/'};
case 'p': return Key{mod, '0'};
case 'q': return Key{mod, '1'};
case 'r': return Key{mod, '2'};
case 's': return Key{mod, '3'};
case 't': return Key{mod, '4'};
case 'u': return Key{mod, '5'};
case 'v': return Key{mod, '6'};
case 'w': return Key{mod, '7'};
case 'x': return Key{mod, '8'};
case 'y': return Key{mod, '9'};
default: return {};
}
};
if (*c == 27)
{
if (auto next = get_char())
{
if (*next == '[') // potential CSI
return parse_csi().value_or(alt('['));
if (*next == 'O') // potential SS3
return parse_ss3().value_or(alt('O'));
return alt(parse_key(*next));
}
else if (not m_paste_buffer)
return Key{Key::Escape};
}
if (m_paste_buffer)
{
auto paste_convert = [](char c) {
switch (c)
{
case '\r': return '\n';
default: return c;
}
};
m_paste_buffer->push_back(paste_convert(*c));
return Key{Key::Invalid};
}
return parse_key(*c);
}
template<typename T>
T div_round_up(T a, T b)
{
return (a - T(1)) / b + T(1);
}
2019-11-16 11:27:06 +01:00
void TerminalUI::draw_menu()
{
// menu show may have not created the window if it did not fit.
// so be tolerant.
if (not m_menu)
return;
const int item_count = (int)m_menu.items.size();
if (m_menu.columns == 0)
{
const auto win_width = m_menu.size.column - 4;
kak_assert(m_menu.size.line == 1);
ColumnCount pos = 0;
m_menu.draw({0, 0}, DisplayAtom(m_menu.first_item > 0 ? "< " : ""), m_menu.bg);
int i = m_menu.first_item;
for (; i < item_count and pos < win_width; ++i)
{
const DisplayLine& item = m_menu.items[i];
const ColumnCount item_width = item.length();
auto& face = i == m_menu.selected_item ? m_menu.fg : m_menu.bg;
m_menu.draw({0, pos+2}, item.atoms(), face);
if (pos + item_width >= win_width)
m_menu.draw({0, win_width+2}, DisplayAtom(""), m_menu.bg);
pos += item_width + 1;
}
if (i != item_count)
m_menu.draw({0, win_width+3}, DisplayAtom(">"), m_menu.bg);
m_dirty = true;
return;
}
const LineCount menu_lines = div_round_up(item_count, m_menu.columns);
const LineCount win_height = m_menu.size.line;
2013-10-17 19:48:12 +02:00
kak_assert(win_height <= menu_lines);
const ColumnCount column_width = (m_menu.size.column - 1) / m_menu.columns;
2013-10-17 19:48:12 +02:00
const LineCount mark_height = min(div_round_up(sq(win_height), menu_lines),
win_height);
const int menu_cols = div_round_up(item_count, (int)m_menu.size.line);
const int first_col = m_menu.first_item / (int)m_menu.size.line;
2018-08-10 19:14:48 +02:00
const LineCount mark_line = (win_height - mark_height) * first_col / max(1, menu_cols - m_menu.columns);
2013-10-17 19:48:12 +02:00
for (auto line = 0_line; line < win_height; ++line)
{
for (int col = 0; col < m_menu.columns; ++col)
{
2018-08-10 19:14:48 +02:00
int item_idx = (first_col + col) * (int)m_menu.size.line + (int)line;
auto& face = item_idx < item_count and item_idx == m_menu.selected_item ? m_menu.fg : m_menu.bg;
2019-11-13 12:10:42 +01:00
auto atoms = item_idx < item_count ? m_menu.items[item_idx].atoms() : ConstArrayView<DisplayAtom>{};
m_menu.draw({line, col * column_width}, atoms, face);
}
const bool is_mark = line >= mark_line and line < mark_line + mark_height;
m_menu.draw({line, m_menu.size.column - 1}, DisplayAtom(is_mark ? "" : ""), m_menu.bg);
}
m_dirty = true;
}
static LineCount height_limit(MenuStyle style)
{
switch (style)
{
case MenuStyle::Inline: return 10_line;
case MenuStyle::Prompt: return 10_line;
case MenuStyle::Search: return 3_line;
}
kak_assert(false);
return 0_line;
}
2019-11-16 11:27:06 +01:00
void TerminalUI::menu_show(ConstArrayView<DisplayLine> items,
2020-10-08 11:56:39 +02:00
DisplayCoord anchor, Face fg, Face bg,
MenuStyle style)
{
if (m_menu)
{
m_menu.destroy();
m_dirty = true;
}
2012-11-21 19:01:13 +01:00
m_menu.fg = fg;
m_menu.bg = bg;
m_menu.style = style;
m_menu.anchor = anchor;
if (m_dimensions.column <= 2)
2013-05-16 21:44:58 +02:00
return;
2012-11-21 19:01:13 +01:00
2013-10-17 19:48:12 +02:00
const int item_count = items.size();
m_menu.items.clear(); // make sure it is empty
m_menu.items.reserve(item_count);
const auto longest = accumulate(items | transform(&DisplayLine::length),
1_col, [](auto&& lhs, auto&& rhs) { return std::max(lhs, rhs); });
const ColumnCount max_width = m_dimensions.column - 1;
const bool is_inline = style == MenuStyle::Inline;
const bool is_search = style == MenuStyle::Search;
m_menu.columns = is_search ? 0 : (is_inline ? 1 : max((int)(max_width / (longest+1)), 1));
const LineCount max_height = min(height_limit(style), max(anchor.line, m_dimensions.line - anchor.line - 1));
const LineCount height = is_search ?
1 : (min<LineCount>(max_height, div_round_up(item_count, m_menu.columns)));
if (height == 0)
return;
const ColumnCount maxlen = (m_menu.columns > 1 and item_count > 1) ?
max_width / m_menu.columns - 1 : max_width;
2013-10-17 19:48:12 +02:00
for (auto& item : items)
2012-08-31 14:14:16 +02:00
{
m_menu.items.push_back(item);
m_menu.items.back().trim(0, maxlen);
kak_assert(m_menu.items.back().length() <= maxlen);
2012-08-31 14:14:16 +02:00
}
if (is_inline)
anchor.line += content_line_offset();
LineCount line = anchor.line + 1;
ColumnCount column = std::max(0_col, std::min(anchor.column, m_dimensions.column - longest - 1));
if (is_search)
{
line = m_status_on_top ? 0_line : m_dimensions.line;
column = m_dimensions.column / 2;
}
else if (not is_inline)
line = m_status_on_top ? 1_line : m_dimensions.line - height;
else if (line + height > m_dimensions.line and anchor.line >= height)
line = anchor.line - height;
2013-10-17 19:48:12 +02:00
const auto width = is_search ? m_dimensions.column - m_dimensions.column / 2
: (is_inline ? min(longest+1, m_dimensions.column)
: m_dimensions.column);
m_menu.create({line, column}, {height, width});
m_menu.selected_item = item_count;
m_menu.first_item = 0;
draw_menu();
if (m_info)
info_show(m_info.title, m_info.content,
m_info.anchor, m_info.face, m_info.style);
}
2019-11-16 11:27:06 +01:00
void TerminalUI::menu_select(int selected)
{
const int item_count = m_menu.items.size();
2013-10-17 19:48:12 +02:00
if (selected < 0 or selected >= item_count)
{
m_menu.selected_item = -1;
m_menu.first_item = 0;
}
else if (m_menu.columns == 0) // Do not columnize
{
m_menu.selected_item = selected;
const ColumnCount width = m_menu.size.column - 3;
int first = 0;
ColumnCount item_col = 0;
for (int i = 0; i <= selected; ++i)
{
const ColumnCount item_width = m_menu.items[i].length() + 1;
if (item_col + item_width > width)
{
first = i;
item_col = item_width;
}
else
item_col += item_width;
}
m_menu.first_item = first;
}
2012-09-05 19:02:06 +02:00
else
{
m_menu.selected_item = selected;
2018-08-10 19:14:48 +02:00
const int menu_cols = div_round_up(item_count, (int)m_menu.size.line);
const int first_col = m_menu.first_item / (int)m_menu.size.line;
const int selected_col = m_menu.selected_item / (int)m_menu.size.line;
if (selected_col < first_col)
m_menu.first_item = selected_col * (int)m_menu.size.line;
if (selected_col >= first_col + m_menu.columns)
m_menu.first_item = min(selected_col, menu_cols - m_menu.columns) * (int)m_menu.size.line;
}
draw_menu();
2012-09-05 19:02:06 +02:00
}
2019-11-16 11:27:06 +01:00
void TerminalUI::menu_hide()
2012-09-05 19:02:06 +02:00
{
if (not m_menu)
2012-09-05 19:02:06 +02:00
return;
m_menu.items.clear();
m_menu.destroy();
m_dirty = true;
// Recompute info as it does not have to avoid the menu anymore
if (m_info)
info_show(m_info.title, m_info.content, m_info.anchor, m_info.face, m_info.style);
}
static DisplayCoord compute_pos(DisplayCoord anchor, DisplayCoord size,
2019-11-16 11:27:06 +01:00
TerminalUI::Rect rect, TerminalUI::Rect to_avoid,
bool prefer_above)
2012-12-15 18:32:56 +01:00
{
DisplayCoord pos;
if (prefer_above)
{
pos = anchor - DisplayCoord{size.line};
if (pos.line < 0)
prefer_above = false;
}
auto rect_end = rect.pos + rect.size;
if (not prefer_above)
{
pos = anchor + DisplayCoord{1_line};
if (pos.line + size.line >= rect_end.line)
pos.line = max(rect.pos.line, anchor.line - size.line);
}
if (pos.column + size.column >= rect_end.column)
pos.column = max(rect.pos.column, rect_end.column - size.column);
if (to_avoid.size != DisplayCoord{})
{
DisplayCoord to_avoid_end = to_avoid.pos + to_avoid.size;
DisplayCoord end = pos + size;
// check intersection
if (not (end.line < to_avoid.pos.line or end.column < to_avoid.pos.column or
pos.line > to_avoid_end.line or pos.column > to_avoid_end.column))
{
pos.line = min(to_avoid.pos.line, anchor.line) - size.line;
// if above does not work, try below
if (pos.line < 0)
pos.line = max(to_avoid_end.line, anchor.line);
}
}
2012-12-15 18:32:56 +01:00
return pos;
}
static DisplayLineList wrap_lines(const DisplayLineList& lines, ColumnCount max_width)
{
DisplayLineList result;
for (auto line : lines)
{
ColumnCount column = 0;
for (auto it = line.begin(); it != line.end(); )
{
auto length = it->length();
column += length;
if (column > max_width)
{
auto content = it->content().substr(0, length - (column - max_width));
auto pos = find_if(content | reverse(), [](char c) { return not is_word(c); });
if (pos != content.rend())
content = {content.begin(), pos.base()};
if (not content.empty())
it = ++line.split(it, content.column_length());
result.push_back(AtomList(std::make_move_iterator(line.begin()),
std::make_move_iterator(it)));
it = line.erase(line.begin(), it);
column = 0;
}
else
++it;
}
result.push_back(std::move(line));
}
return result;
}
2019-11-16 11:27:06 +01:00
void TerminalUI::info_show(const DisplayLine& title, const DisplayLineList& content,
2020-10-08 11:56:39 +02:00
DisplayCoord anchor, Face face, InfoStyle style)
{
info_hide();
m_info.title = title;
m_info.content = content;
m_info.anchor = anchor;
m_info.face = face;
m_info.style = style;
const bool framed = style == InfoStyle::Prompt or style == InfoStyle::Modal;
const bool assisted = style == InfoStyle::Prompt and m_assistant.size() != 0;
DisplayCoord max_size = m_dimensions;
if (style == InfoStyle::MenuDoc)
max_size.column = std::max(m_dimensions.column - (m_menu.pos.column + m_menu.size.column),
m_menu.pos.column);
else if (style != InfoStyle::Modal)
max_size.line -= m_menu.size.line;
const auto max_content_width = (m_info_max_width > 0 ? std::min(max_size.column, m_info_max_width) : max_size.column) -
(framed ? 4 : 2) -
(assisted ? m_assistant[0].column_length() : 0);
if (max_content_width <= 0)
return;
auto compute_size = [](const DisplayLineList& lines) -> DisplayCoord {
return {(int)lines.size(), accumulate(lines, 0_col, [](ColumnCount c, const DisplayLine& l) { return std::max(c, l.length()); })};
};
DisplayCoord content_size = compute_size(content);
const bool wrap = content_size.column > max_content_width;
DisplayLineList wrapped_content;
if (wrap)
{
wrapped_content = wrap_lines(content, max_content_width);
content_size = compute_size(wrapped_content);
}
const auto& lines = wrap ? wrapped_content : content;
DisplayCoord size{content_size.line, std::max(content_size.column, title.length() + (framed ? 2 : 0))};
if (framed)
size += {2, 4};
if (assisted)
size = {std::max(LineCount{(int)m_assistant.size()-1}, size.line), size.column + m_assistant[0].column_length()};
size = {std::min(max_size.line, size.line), std::min(max_size.column, size.column)};
if ((framed and size.line < 3) or size.line <= 0)
return;
const Rect rect = {content_line_offset(), m_dimensions};
if (style == InfoStyle::Prompt)
{
anchor = DisplayCoord{m_status_on_top ? 0 : m_dimensions.line, m_dimensions.column-1};
anchor = compute_pos(anchor, size, rect, m_menu, style == InfoStyle::InlineAbove);
}
else if (style == InfoStyle::Modal)
{
auto half = [](const DisplayCoord& c) { return DisplayCoord{c.line / 2, c.column / 2}; };
anchor = rect.pos + half(rect.size) - half(size);
}
else if (style == InfoStyle::MenuDoc)
{
const auto right_max_width = m_dimensions.column - (m_menu.pos.column + m_menu.size.column);
const auto left_max_width = m_menu.pos.column;
anchor.line = m_menu.pos.line;
if (size.column <= right_max_width or right_max_width >= left_max_width)
anchor.column = m_menu.pos.column + m_menu.size.column;
else
anchor.column = m_menu.pos.column - size.column;
}
else
{
anchor = compute_pos(anchor, size, rect, m_menu, style == InfoStyle::InlineAbove);
anchor.line += content_line_offset();
}
m_info.create(anchor, size);
for (auto line = 0_line; line < size.line; ++line)
{
auto draw_atoms = [&, this, pos=DisplayCoord{line}](auto&&... args) mutable {
auto draw = overload(
[&](ColumnCount padding) {
pos.column += padding;
},
[&](String str) {
auto len = str.column_length();
m_info.draw(pos, DisplayAtom{std::move(str)}, face);
pos.column += len;
},
[&](const DisplayLine& atoms) {
m_info.draw(pos, atoms.atoms(), face);
pos.column += atoms.length();
});
(draw(args), ...);
};
constexpr Codepoint dash{L''};
constexpr Codepoint dotted_dash{L''};
if (assisted)
{
const auto assistant_top_margin = (size.line - m_assistant.size()+1) / 2;
StringView assistant_line = (line >= assistant_top_margin) ?
m_assistant[(int)min(line - assistant_top_margin, LineCount{(int)m_assistant.size()}-1)]
: m_assistant[(int)m_assistant.size()-1];
draw_atoms(assistant_line.str());
}
if (not framed)
draw_atoms(lines[(int)line]);
else if (line == 0)
{
if (title.atoms().empty() or content_size.column < 2)
draw_atoms("╭─" + String{dash, content_size.column} + "─╮");
else
{
auto trimmed_title = title;
trimmed_title.trim(0, content_size.column - 2);
auto dash_count = content_size.column - trimmed_title.length() - 2;
String left{dash, dash_count / 2};
String right{dash, dash_count - dash_count / 2};
draw_atoms("╭─" + left + "", trimmed_title, "" + right +"─╮");
}
}
else if (line < size.line - 1 and line <= lines.size())
{
auto info_line = lines[(int)line - 1];
const bool trimmed = info_line.trim(0, content_size.column);
const ColumnCount padding = content_size.column - info_line.length();
draw_atoms("", info_line, padding, (trimmed ? "…│" : ""));
}
else if (line == std::min<LineCount>((int)lines.size() + 1, size.line - 1))
draw_atoms("╰─", String(line > lines.size() ? dash : dotted_dash, content_size.column), "─╯");
}
m_dirty = true;
}
2019-11-16 11:27:06 +01:00
void TerminalUI::info_hide()
{
if (not m_info)
return;
m_info.destroy();
m_dirty = true;
}
2019-11-16 11:27:06 +01:00
void TerminalUI::set_on_key(OnKeyCallback callback)
{
m_on_key = std::move(callback);
EventManager::instance().force_signal(0);
}
void TerminalUI::set_on_paste(OnPasteCallback callback)
{
m_on_paste = std::move(callback);
}
2019-11-16 11:27:06 +01:00
DisplayCoord TerminalUI::dimensions()
{
return m_dimensions;
}
2019-11-16 11:27:06 +01:00
LineCount TerminalUI::content_line_offset() const
{
return m_status_on_top ? 1 : 0;
}
2019-11-16 11:27:06 +01:00
void TerminalUI::set_resize_pending()
{
m_resize_pending = true;
EventManager::instance().force_signal(0);
}
2019-11-16 11:27:06 +01:00
void TerminalUI::setup_terminal()
2013-04-12 01:28:22 +02:00
{
write(STDOUT_FILENO,
"\033[?1049h" // enable alternative screen buffer
"\033[?1004h" // enable focus notify
"\033[>4;1m" // request CSI u style key reporting
2021-10-30 14:21:51 +02:00
"\033[>5u" // kitty progressive enhancement - report shifted key codes
"\033[22t" // save the current window title
"\033[?25l" // hide cursor
"\033=" // set application keypad mode, so the keypad keys send unique codes
"\033[?2004h" // force enable bracketed-paste events
);
}
2019-11-16 11:27:06 +01:00
void TerminalUI::restore_terminal()
{
write(STDOUT_FILENO,
"\033>"
"\033[?25h"
"\033[23t"
2021-10-30 14:21:51 +02:00
"\033[<u"
"\033[>4;0m"
"\033[?1004l"
"\033[?1049l"
"\033[?2004l"
"\033[m" // set the terminal output back to default colours and style
);
2013-04-12 01:28:22 +02:00
}
2019-11-16 11:27:06 +01:00
void TerminalUI::enable_mouse(bool enabled)
2015-10-02 14:52:41 +02:00
{
if (enabled == m_mouse_enabled)
return;
m_mouse_enabled = enabled;
if (enabled)
{
write(STDOUT_FILENO,
"\033[?1006h" // force SGR mode
"\033[?1000h" // enable mouse
"\033[?1002h" // force enable report mouse position
);
2015-10-02 14:52:41 +02:00
}
else
{
write(STDOUT_FILENO,
"\033[?1002l"
"\033[?1000l"
"\033[?1006l"
);
2015-10-02 14:52:41 +02:00
}
}
2019-11-16 11:27:06 +01:00
void TerminalUI::set_ui_options(const Options& options)
{
auto find = [&](StringView name) -> Optional<StringView> {
if (auto it = options.find(name); it != options.end())
return StringView{it->value};
return {};
};
auto assistant = find("terminal_assistant").value_or("clippy");
if (assistant == "clippy")
m_assistant = assistant_clippy;
else if (assistant == "cat")
m_assistant = assistant_cat;
else if (assistant == "dilbert")
m_assistant = assistant_dilbert;
else if (assistant == "none" or assistant == "off")
m_assistant = ConstArrayView<StringView>{};
auto to_bool = [](StringView s) { return s == "yes" or s == "true"; };
m_status_on_top = find("terminal_status_on_top").map(to_bool).value_or(false);
m_set_title = find("terminal_set_title").map(to_bool).value_or(true);
auto synchronized = find("terminal_synchronized").map(to_bool);
m_synchronized.set = (bool)synchronized;
m_synchronized.requested = synchronized.value_or(false);
if (not m_synchronized.queried and not m_synchronized.set) {
write(STDOUT_FILENO, "\033[?2026$p");
m_synchronized.queried = true;
}
2015-10-02 14:52:41 +02:00
m_shift_function_key = find("terminal_shift_function_key").map(str_to_int_ifp).value_or(default_shift_function_key);
enable_mouse(find("terminal_enable_mouse").map(to_bool).value_or(true));
m_wheel_scroll_amount = find("terminal_wheel_scroll_amount").map(str_to_int_ifp).value_or(3);
m_padding_char = find("terminal_padding_char").map([](StringView s) { return s.column_length() < 1 ? ' ' : s[0_char]; }).value_or(Codepoint{'~'});
m_padding_fill = find("terminal_padding_fill").map(to_bool).value_or(false);
m_info_max_width = find("terminal_info_max_width").map(str_to_int_ifp).value_or(0);
}
}