diff options
author | Tom Feist <shabble@metavore.org> | 2010-10-09 15:39:29 +0000 |
---|---|---|
committer | Tom Feist <shabble@metavore.org> | 2010-10-09 15:39:29 +0000 |
commit | 90352618f680723a1a6a6cf59aec93917d64dbb1 (patch) | |
tree | 9de05efa2915f6707ebb326fc4c64a237f3326d8 /prompt_info | |
parent | Merge remote branch 'origin/dev' (diff) | |
download | irssi-scripts-90352618f680723a1a6a6cf59aec93917d64dbb1.tar.gz irssi-scripts-90352618f680723a1a6a6cf59aec93917d64dbb1.zip |
factored all the visual stuff out of prompt_replace into its own file.
Diffstat (limited to 'prompt_info')
-rw-r--r-- | prompt_info/prompt_replace.pl | 171 | ||||
-rw-r--r-- | prompt_info/visual.pl | 184 |
2 files changed, 196 insertions, 159 deletions
diff --git a/prompt_info/prompt_replace.pl b/prompt_info/prompt_replace.pl index 20fb7b1..c9bfcc8 100644 --- a/prompt_info/prompt_replace.pl +++ b/prompt_info/prompt_replace.pl @@ -25,41 +25,10 @@ sub DEBUG () { 1 } my $prompt_data = undef; my $prompt_item = undef; -my $region_active = 0; - -my ($term_w, $term_h) = (0, 0); - -# visual region selected. -my ($region_start, $region_end) = (0, 0); -my $region_content = ''; - my $prompt_format = ''; init(); -sub update_terminal_size { - - my @stty_data = qx/stty -a/; - my $line = $stty_data[0]; - - # linux - # speed 38400 baud; rows 36; columns 126; line = 0; - if ($line =~ m/rows (\d+); columns (\d+);/) { - $term_h = $1; - $term_w = $2; - # osx - # speed 9600 baud; 40 rows; 235 columns; - } elsif ($line =~ m/(\d+) rows; (\d+) columns;/) { - $term_h = $1; - $term_w = $2; - } else { - # guess? - $term_h = 24; - $term_w = 80; - } - - print "Terminal detected as $term_w cols by $term_h rows" if DEBUG; -} sub prompt_subcmd_handler { my ($data, $server, $item) = @_; @@ -67,57 +36,21 @@ sub prompt_subcmd_handler { Irssi::command_runsub('prompt', $data, $server, $item); } -sub visual_subcmd_handler { - my ($data, $server, $item) = @_; - $data =~ s/\s+$//g; # strip trailing whitespace. - Irssi::command_runsub('visual', $data, $server, $item); -} - sub init { - Irssi::statusbar_item_register ('uberprompt', 0, 'uberprompt_draw'); + Irssi::statusbar_item_register('uberprompt', 0, 'uberprompt_draw'); Irssi::settings_add_str('uberprompt', 'uberprompt_format', '[$*] '); - Irssi::command_bind("prompt", \&prompt_subcmd_handler); - Irssi::command_bind('prompt on', \&replace_prompt_items); + Irssi::command_bind("prompt", \&prompt_subcmd_handler); + Irssi::command_bind('prompt on', \&replace_prompt_items); Irssi::command_bind('prompt off', \&restore_prompt_items); Irssi::command_bind('prompt set', sub { Irssi::signal_emit 'change prompt', shift; }); Irssi::command_bind('prompt clear', sub { Irssi::signal_emit 'change prompt', '$p'; }); - # misc faff - Irssi::command_bind('visual', \&visual_subcmd_handler); - Irssi::command_bind('visual toggle', \&cmd_toggle_visual); - Irssi::command_bind('visual clear', \&cmd_clear_visual); - - Irssi::command("^BIND ^F /visual toggle"); - Irssi::command("^BIND ^G /visual clear"); - - Irssi::command_bind 'print_test', - sub { - Irssi::gui_printtext(0, 0, '%8hello there%n'); - }; - - # redraw interception - Irssi::signal_add_last('command redraw', \&augment_redraw); - Irssi::signal_add_first('gui key pressed', \&ctrl_l_intercept); - - # for updating the overlay. - Irssi::signal_add_last ('gui key pressed', \&key_pressed); - - # things to refresh the overlay for. - Irssi::signal_add('window changed', \&uberprompt_refresh); - Irssi::signal_add('window name changed', \&uberprompt_refresh); - Irssi::signal_add('window changed automatic', \&uberprompt_refresh); - Irssi::signal_add('window item changed', \&uberprompt_refresh); - - Irssi::signal_add('terminal resized', \&update_terminal_size); - Irssi::signal_add('setup changed', \&reload_settings); - - # so we know where the bottom line is - update_terminal_size(); + Irssi::signal_add('setup changed', \&reload_settings); # intialise the prompt format. reload_settings(); @@ -128,13 +61,12 @@ sub init { # the actual API signal. Irssi::signal_register({'change prompt' => [qw/string/]}); Irssi::signal_add('change prompt' => \&change_prompt_sig); - - } sub change_prompt_sig { my ($text) = @_; + # TODO: mroe intelligence about where to insert $p? $text = '$p' . $text; print "Got prompt change sig with: $text" if DEBUG; @@ -149,9 +81,9 @@ sub change_prompt_sig { } } - sub UNLOAD { # remove uberprompt and return the original ones. + print "Removing uberprompt and restoring original"; restore_prompt_items(); } @@ -187,19 +119,19 @@ sub uberprompt_draw { } else { $p_copy = $default_prompt; } + $p_copy =~ s/\$/\$\$/g; # escape all $ symbols + $p_copy =~ s/\\/\\\\/g; # escape backslashes. + print "Redrawing with: $p_copy, size-only: $get_size_only" if DEBUG; $prompt_item = $sb_item; - $sb_item->default_handler($get_size_only, $p_copy, '', 0); -} + my $ret = $sb_item->default_handler($get_size_only, $p_copy, '', 0); -sub augment_redraw { - print "Redraw called" if DEBUG; - uberprompt_refresh(); - Irssi::timeout_add_once(10, \&refresh_visual_overlay, 0); + return $ret; } + sub uberprompt_refresh { Irssi::statusbar_items_redraw('uberprompt'); } @@ -211,82 +143,6 @@ sub cmd_clear_visual { Irssi::statusbar_items_redraw('input'); } -sub cmd_toggle_visual { - - $region_active = not $region_active; - - if ($region_active) { - $region_start = _pos(); - $region_end = 0; # reset end marker. - print "visual mode started at $region_start" if DEBUG; - } else { - $region_end = _pos(); - print "Visual mode ended at $region_end" if DEBUG; - - if ($region_end > $region_start) { - my $input = Irssi::parse_special('$L', 0, 0); - my $str = substr($input, $region_start, $region_end - $region_start); - print "Region selected: $str" if DEBUG; - } else { - print "Invalid region selection: [ $region_start - $region_end ]" - if DEBUG; - $region_start = $region_end = 0; - } - cmd_clear_visual(); - } -} - -sub ctrl_l_intercept { - my $key = shift; - - if ($key == 12) { # C-l - print "C-l pressed" if DEBUG; - Irssi::command("redraw"); - Irssi::signal_stop(); - } elsif ($key == 10) { # RET - _clear_visual_region(); - } -} - -sub key_pressed { - # this handler needs to be last so the actual character is printed by irssi - # before we overlay on it. Otherwise things are all a bit off-by-1 - return unless $region_active; - - refresh_visual_overlay(); -} - -sub _clear_visual_region { - print "Clearing Region markers" if DEBUG; - $region_end = 0; - $region_start = 0; -} - - -sub refresh_visual_overlay { - - my $end_pos = $region_end; - $end_pos ||= _pos(); # if not set, take current position as end. - - my $len = $end_pos - $region_start; - return unless $len; # no point drawing an empty overlay - - my $input = Irssi::parse_special('$L'); - my $offset = $prompt_item->{size} + $region_start; - - my $text = substr($input, $region_start, $len); - - print "printing '$text' at $offset [$region_start, $end_pos] ($len)" if DEBUG; - - $text = '%8' . $text . '%8'; - _draw_overlay($offset, $text, $len); - -} - -sub _draw_overlay { - my ($offset, $text, $len) = @_; - Irssi::gui_printtext($offset, $term_h, $text); -} sub replace_prompt_items { # remove existing ones. @@ -332,9 +188,6 @@ sub _sbar_command { Irssi::command($command); } -sub _pos { - return Irssi::gui_input_get_pos(); -} # bit of fakery so things don't complain about the lack of prompt_info (hoepfully) diff --git a/prompt_info/visual.pl b/prompt_info/visual.pl new file mode 100644 index 0000000..5941fe0 --- /dev/null +++ b/prompt_info/visual.pl @@ -0,0 +1,184 @@ +use strict; +use warnings; + +use Irssi; +use Irssi::TextUI; # for sbar_items_redraw +use Data::Dumper; + + + +our $VERSION = "0.1"; +our %IRSSI = + ( + authors => "shabble", + contact => 'shabble+irssi@metavore.org, shabble@#irssi/Freenode', + name => "prompt_info", + description => "Helper script for dynamically adding text " + . "into the input-bar prompt.", + license => "Public Domain", + changed => "24/7/2010" + ); + +sub DEBUG () { 1 } +#sub DEBUG () { 0 } + +my $region_active = 0; + +my ($term_w, $term_h) = (0, 0); + +# visual region selected. +my ($region_start, $region_end) = (0, 0); +my $region_content = ''; + +sub update_terminal_size { + + my @stty_data = qx/stty -a/; + my $line = $stty_data[0]; + + # linux + # speed 38400 baud; rows 36; columns 126; line = 0; + if ($line =~ m/rows (\d+); columns (\d+);/) { + $term_h = $1; + $term_w = $2; + # osx + # speed 9600 baud; 40 rows; 235 columns; + } elsif ($line =~ m/(\d+) rows; (\d+) columns;/) { + $term_h = $1; + $term_w = $2; + } else { + # guess? + $term_h = 24; + $term_w = 80; + } + + print "Terminal detected as $term_w cols by $term_h rows" if DEBUG; +} + +sub visual_subcmd_handler { + my ($data, $server, $item) = @_; + $data =~ s/\s+$//g; # strip trailing whitespace. + Irssi::command_runsub('visual', $data, $server, $item); +} + +sub init { + + # misc faff + Irssi::command_bind('visual', \&visual_subcmd_handler); + Irssi::command_bind('visual toggle', \&cmd_toggle_visual); + Irssi::command_bind('visual clear', \&cmd_clear_visual); + + Irssi::command("^BIND ^F /visual toggle"); + Irssi::command("^BIND ^G /visual clear"); + + Irssi::command_bind 'print_test', + sub { + Irssi::gui_printtext(0, 0, '%8hello there%n'); + }; + + # redraw interception + Irssi::signal_add_last('command redraw', \&augment_redraw); + Irssi::signal_add_first('gui key pressed', \&ctrl_l_intercept); + + # for updating the overlay. + Irssi::signal_add_last ('gui key pressed', \&key_pressed); + + # things to refresh the overlay for. + Irssi::signal_add('window changed', \&uberprompt_refresh); + Irssi::signal_add('window name changed', \&uberprompt_refresh); + Irssi::signal_add('window changed automatic', \&uberprompt_refresh); + Irssi::signal_add('window item changed', \&uberprompt_refresh); + + Irssi::signal_add('terminal resized', \&update_terminal_size); + + # so we know where the bottom line is + update_terminal_size(); + + +} +sub augment_redraw { + print "Redraw called" if DEBUG; + uberprompt_refresh(); + Irssi::timeout_add_once(10, \&refresh_visual_overlay, 0); +} + + +sub cmd_toggle_visual { + + $region_active = not $region_active; + + if ($region_active) { + $region_start = _pos(); + $region_end = 0; # reset end marker. + print "visual mode started at $region_start" if DEBUG; + } else { + $region_end = _pos(); + print "Visual mode ended at $region_end" if DEBUG; + + if ($region_end > $region_start) { + my $input = Irssi::parse_special('$L', 0, 0); + my $str = substr($input, $region_start, $region_end - $region_start); + print "Region selected: $str" if DEBUG; + } else { + print "Invalid region selection: [ $region_start - $region_end ]" + if DEBUG; + $region_start = $region_end = 0; + } + cmd_clear_visual(); + } +} + +sub ctrl_l_intercept { + my $key = shift; + + if ($key == 12) { # C-l + print "C-l pressed" if DEBUG; + Irssi::command("redraw"); + Irssi::signal_stop(); + } elsif ($key == 10) { # RET + _clear_visual_region(); + } +} + +sub key_pressed { + # this handler needs to be last so the actual character is printed by irssi + # before we overlay on it. Otherwise things are all a bit off-by-1 + return unless $region_active; + + refresh_visual_overlay(); +} + +sub _clear_visual_region { + print "Clearing Region markers" if DEBUG; + $region_end = 0; + $region_start = 0; +} + + +sub refresh_visual_overlay { + + my $end_pos = $region_end; + $end_pos ||= _pos(); # if not set, take current position as end. + + my $len = $end_pos - $region_start; + return unless $len; # no point drawing an empty overlay + + my $input = Irssi::parse_special('$L'); + my $offset = $prompt_item->{size} + $region_start; + + my $text = substr($input, $region_start, $len); + + print "printing '$text' at $offset [$region_start, $end_pos] ($len)" if DEBUG; + + $text = '%8' . $text . '%8'; + _draw_overlay($offset, $text, $len); + +} + +sub _draw_overlay { + my ($offset, $text, $len) = @_; + Irssi::gui_printtext($offset, $term_h, $text); +} + +sub _pos { + return Irssi::gui_input_get_pos(); +} |