Utilities: Add half-page scrolling + number modifiers to 'less'

- 'u' and 'd' now scroll up and down half a page
- Typing a number followed by 'j', 'k', 'return', 'up' or 'down' will
  scroll that many lines in the appropriate direction
- Typing a number followed by 'g' or 'G' will scroll directly to the
  line corresponding to that number
This commit is contained in:
Lennon Donaghy 2021-07-28 07:36:20 +01:00 committed by Andreas Kling
parent a071dba1ef
commit e9bd81a6a0
Notes: sideshowbarker 2024-07-18 07:56:42 +09:00

View file

@ -158,9 +158,30 @@ public:
void bottom()
{
while (read_line())
;
down_n(m_lines.size() - m_line);
}
void up_half_page()
{
up_n(m_height / 2);
}
void down_half_page()
{
down_n(m_height / 2);
}
void go_to_line(size_t line_num)
{
if (line_num < m_line) {
up_n(m_line - line_num);
} else {
down_n(line_num - m_line);
}
}
void init()
{
while (m_lines.size() < m_height) {
@ -371,22 +392,46 @@ int main(int argc, char** argv)
pager.init();
StringBuilder modifier_buffer = StringBuilder(10);
for (String sequence;; sequence = get_key_sequence()) {
if (sequence == "" || sequence == "q") {
break;
} else if (sequence == "j" || sequence == "\e[B" || sequence == "\n") {
pager.down();
} else if (sequence == "k" || sequence == "\e[A") {
if (!emulate_more)
pager.up();
} else if (sequence == "g") {
pager.top();
} else if (sequence == "G") {
pager.bottom();
} else if (sequence == " " || sequence == "\e[6~") {
pager.down_page();
} else if (sequence == "\e[5~") {
pager.up_page();
if (sequence.to_uint().has_value()) {
modifier_buffer.append(sequence);
} else {
if (sequence == "" || sequence == "q") {
break;
} else if (sequence == "j" || sequence == "\e[B" || sequence == "\n") {
if (!modifier_buffer.is_empty())
pager.down_n(modifier_buffer.build().to_uint().value_or(1));
else
pager.down();
} else if (sequence == "k" || sequence == "\e[A") {
if (!emulate_more) {
if (!modifier_buffer.is_empty())
pager.up_n(modifier_buffer.build().to_uint().value_or(1));
else
pager.up();
}
} else if (sequence == "g") {
if (!modifier_buffer.is_empty())
pager.go_to_line(modifier_buffer.build().to_uint().value());
else
pager.top();
} else if (sequence == "G") {
if (!modifier_buffer.is_empty())
pager.go_to_line(modifier_buffer.build().to_uint().value());
else
pager.bottom();
} else if (sequence == " " || sequence == "\e[6~") {
pager.down_page();
} else if (sequence == "\e[5~") {
pager.up_page();
} else if (sequence == "d") {
pager.down_half_page();
} else if (sequence == "u") {
pager.up_half_page();
}
modifier_buffer.clear();
}
if (quit_at_eof && pager.at_end())