From 4613a14a9f8e7f9b915fcd149728e0e5beb28b7b Mon Sep 17 00:00:00 2001 From: Juerd Waalboer Date: Tue, 1 Nov 2022 04:48:49 +0100 Subject: [PATCH] pager: don't scroll down for non-log + some code moved around in TextEditor --- lib/RevBank/TextEditor.pm | 80 +++++++++++++++++++++------------------ plugins/users | 2 +- 2 files changed, 45 insertions(+), 37 deletions(-) diff --git a/lib/RevBank/TextEditor.pm b/lib/RevBank/TextEditor.pm index de9cd8f..ea76bc3 100644 --- a/lib/RevBank/TextEditor.pm +++ b/lib/RevBank/TextEditor.pm @@ -114,48 +114,51 @@ sub _editor($title, $origdata, $readonly = 0) { -wrapping => 0, -hscrollbar => 0, -vscrollbar => 0, - -pos => ($readonly ? length($origdata) : 0), + -pos => ($readonly == 2 ? length($origdata) : 0), #-readonly => !!$readonly # does not support -pos ); my $return; - my @keys = ( - [ Curses::KEY_HOME() => 'cursor-scrlinestart' ], - [ Curses::KEY_END() => 'cursor-scrlineend' ], - [ "\cK" => 'delete-line' ], # nano (can't do meta/alt for M-m) - [ "\cU" => 'paste' ], # nano - [ "\c[" => sub { } ], - [ "\cL" => sub { $cui->draw } ], - [ "\c^" => sub { $editor->pos(0) } ], - [ "\c_" => sub { $editor->pos(length($editor->get)) } ], - [ "\cI" => sub { $editor->add_string(" " x ($tab - ($editor->{-xpos} % $tab))) } ], - [ "\cS" => sub { $cui->dialog("Enable flow control :)") } ], - [ "\cQ" => sub {} ], - [ "\cC" => sub { $editor->{-pastebuffer} = $editor->getline_at_ypos($editor->{-ypos}) } ], - [ "\cF" => sub { _find($win) } ], - [ "\cX" => sub { - if ($editor->get ne $origdata) { - my $answer = $cui->dialog( - -message => "Save changes?", - -buttons => [ - { -label => "[Save]", -value => 1 }, - { -label => "[Discard]", -value => 0 }, - { -label => "[Cancel]", -value => -1 }, - ], - -values => [ 1, 0 ], - ); - $return = $editor->get if $answer == 1; - $cui->mainloopExit if $answer >= 0; - } else { - $cui->mainloopExit; - } - } ], - ); + if ($readonly) { + $editor->readonly(1); # must be before bindings + $editor->set_binding(sub { $cui->mainloopExit }, "q") if $readonly; + } else { + my @keys = ( + [ Curses::KEY_HOME() => 'cursor-scrlinestart' ], + [ Curses::KEY_END() => 'cursor-scrlineend' ], + [ "\cK" => 'delete-line' ], # nano (can't do meta/alt for M-m) + [ "\cU" => 'paste' ], # nano + [ "\c[" => sub { } ], + [ "\cL" => sub { $cui->draw } ], + [ "\c^" => sub { $editor->pos(0) } ], + [ "\c_" => sub { $editor->pos(length($editor->get)) } ], + [ "\cI" => sub { $editor->add_string(" " x ($tab - ($editor->{-xpos} % $tab))) } ], + [ "\cS" => sub { $cui->dialog("Enable flow control :)") } ], + [ "\cQ" => sub {} ], + [ "\cC" => sub { $editor->{-pastebuffer} = $editor->getline_at_ypos($editor->{-ypos}) } ], + [ "\cF" => sub { _find($win) } ], + [ "\cX" => sub { + if ($editor->get ne $origdata) { + my $answer = $cui->dialog( + -message => "Save changes?", + -buttons => [ + { -label => "[Save]", -value => 1 }, + { -label => "[Discard]", -value => 0 }, + { -label => "[Cancel]", -value => -1 }, + ], + -values => [ 1, 0 ], + ); + $return = $editor->get if $answer == 1; + $cui->mainloopExit if $answer >= 0; + } else { + $cui->mainloopExit; + } + } ], + ); - $editor->readonly(1) if $readonly; # must be before bindings - $editor->set_binding(reverse @$_) for @keys; - $editor->set_binding(sub { $cui->mainloopExit }, "q") if $readonly; + $editor->set_binding(reverse @$_) for @keys; + } $editor->focus(); $cui->mainloop; @@ -187,4 +190,9 @@ sub pager($title, $data) { _editor($title, $data, 1); } +sub logpager($title, $data) { + _require(); + _editor($title, $data, 2); +} + 1; diff --git a/plugins/users b/plugins/users index 906a872..26d3128 100644 --- a/plugins/users +++ b/plugins/users @@ -66,7 +66,7 @@ sub log_for($self, $cart, $input, @) { my @lines = _grep($user); require RevBank::TextEditor; - RevBank::TextEditor::pager("RevBank log for $user", join("", @lines, "(end)")); + RevBank::TextEditor::logpager("RevBank log for $user", join("", @lines, "(end)")); return ACCEPT; }