aboutsummaryrefslogtreecommitdiffstats
path: root/prompt_info/prompt_replace.pl
blob: 5e29f9269908f626f818f46e49efc38631747ecc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use strict;
use warnings;

use Irssi;
use Irssi::TextUI;              # for sbar_items_redraw
use Data::Dumper;
use Term::Size;



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 $prompt_data = undef;
my $prompt_item = undef;

my $vis_enabled = 0;

my ($term_w, $term_h);

my ($region_start, $region_end);

init();

sub update_terminal_size {
    my @stty_lines = qx/stty -a/;
    my $line = $stty_lines[0];
    @stty_lines = (); # don't need the rest.

    if ($line =~ m/\s*(\d+)\s*rows\s*;\s*(\d+)\s*columns\s*;/) {
        $term_h = $1;
        $term_w = $2;
    } else {
        print "Failed to detect terminal size";
    }
}

sub init {

    Irssi::command_bind('prompt_on', \&replace_prompt_items);
    Irssi::command_bind('prompt_off', \&restore_prompt_items);

    Irssi::command_bind('prompt_set',
                        sub {
                            my $data = shift;
                            $prompt_data = $data;
                            refresh_prompt();
                        });

    Irssi::command_bind('prompt_clear',
                        sub {
                            $prompt_data = undef;
                            refresh_prompt();
                        });

    Irssi::command_bind('visual', \&cmd_vis);
    Irssi::command("^BIND meta-l /visual");

    Irssi::statusbar_item_register ('new_prompt', 0, 'new_prompt_render');


    Irssi::signal_add_last('gui key pressed', \&key_pressed);
    Irssi::signal_add('window changed', \&refresh_prompt);
    Irssi::signal_add('window name changed', \&refresh_prompt);
    Irssi::signal_add('window changed automatic', \&refresh_prompt);
    Irssi::signal_add('window item changed', \&refresh_prompt);

    Irssi::signal_add('terminal resized', \&update_terminal_size);

    update_terminal_size();
    replace_prompt_items();
}

# Irssi::signal_add('window changed automatic', \&refresh_prompt);
# Irssi::signal_add('window changed automatic', \&refresh_prompt);


sub UNLOAD {
    restore_prompt_items();
}

sub cmd_vis {
    $vis_enabled = not $vis_enabled;
    if ($vis_enabled) {
        print "visual mode started";
        $region_start = _pos();
    } else {
        print "Visual mode ended";
        $region_end = _pos();

        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";
        } else {
            print "Invalid region selection: [ $region_start - $region_end ]";
            $region_start = $region_end = undef;
        }
    }

}

sub refresh_prompt {
    Irssi::statusbar_items_redraw('new_prompt');
}

my $buf = '';

sub key_pressed {
    my ($key) = @_;
    my $char = chr($key);
    $buf .= $char;
    my $str = Irssi::parse_special('$L');

    return unless $vis_enabled;

    print_to_input($str, $prompt_item->{size});
}

sub print_to_input {
    my ($str, $offset) = @_;
    $str = "%8$str%8";

    #    my ($term_w, $term_h) = Term::Size::chars *STDOUT{IO};


    # my $theme = Irssi::current_theme();
    # my $prompt = $theme->format_expand('{prompt $itemname $winname}',
    #                                    Irssi::EXPAND_FLAG_RECURSIVE_MASK);
    #print "Prompt is $prompt";

    Irssi::gui_printtext($offset, $term_h, $str);
}


  sub new_prompt_render {
      my ($sb_item, $get_size_only) = @_;


      my $default_prompt = '';

      my $window = Irssi::active_win();
      if (scalar( () = $window->items )) {
          $default_prompt = '{prompt $[.15]itemname}';
      } else {
          $default_prompt = '{prompt $winname}';
      }

      my $p_copy = $prompt_data;
      if (defined $prompt_data) {
          # check if we have a marker
          $p_copy =~ s/\$p/$default_prompt/;
      } else {
          $p_copy = $default_prompt;
      }
      print "Redrawing with: $p_copy";

      $prompt_item = $sb_item;

      $sb_item->default_handler($get_size_only, $p_copy, '', 0);
  }

sub replace_prompt_items {
    # remove existing ones.
    print "Removing original prompt" if DEBUG;

    _sbar_command('prompt', 'remove', 'prompt');
    _sbar_command('prompt', 'remove', 'prompt_empty');

    # add the new one.

    _sbar_command('prompt', 'add', 'new_prompt',
                  qw/-alignment left -before input -priority 0/);

}

sub restore_prompt_items {

    _sbar_command('prompt', 'remove', 'new_prompt');

    print "Restoring original prompt" if DEBUG;
    _sbar_command('prompt', 'add', 'prompt',
                  qw/-alignment left -before input -priority 0/);
    _sbar_command('prompt', 'add', 'prompt_empty',
                  qw/-alignment left -after prompt -priority 0/);
}


sub _sbar_command {
    my ($bar, $cmd, $item, @args) = @_;

    my $args_str = join ' ', @args;

    $args_str .= ' ' if length $args_str;

    my $command = sprintf 'STATUSBAR %s %s %s%s',
      $bar, $cmd, $args_str, defined($item)?$item:'';

    print "Running command: $command" if DEBUG;
    Irssi::command($command);
}


sub _pos {
    return Irssi::gui_input_get_pos();
}


# my $prompt_additional_content = '';

# Irssi::expando_create('prompt_additional', \&expando_prompt, {});

# sub expando_prompt {
#     my ($server, $witem, $arg) = @_;
#     return $prompt_additional_content;
#     #return Irssi::current_theme->format_expand("{sb
#     #$prompt_additional_content}", 0x0f);
# }

# sub redraw_prompts {
#     Irssi::statusbar_items_redraw ('prompt');
#     Irssi::statusbar_items_redraw ('prompt_empty');
# }

# sub handle_change_prompt_sig {
#     my ($text) = @_;

#     my $expanded_text = Irssi::parse_special($text);

#     print "Got prompt change sig with: $text -> $expanded_text" if DEBUG;

#     my $changed = ($expanded_text ne $prompt_additional_content);

#     $prompt_additional_content = $expanded_text;

#     if ($changed) {
#         print "Redrawing prompts" if DEBUG;
#         redraw_prompts();
#     }
# }

# sub prompt_additional_cmd {
#     my ($str) = @_;
#     print "Setting prompt to: $str" if DEBUG;
#     Irssi::signal_emit('change prompt', $str);
# }

# test_abstract_setup();
# Irssi::signal_register({'change prompt' => [qw/string/]});
# Irssi::signal_add('change prompt' => \&handle_change_prompt_sig);

# Irssi::command_bind('set_prompt' => \&prompt_additional_cmd);

# sub test_abstract_setup {
#     my $theme = Irssi::current_theme();
#     my $prompt = $theme->format_expand('{prompt}', 0);
#     if ($prompt !~ m/\$prompt_additional/) {
#         print "Prompt_Info: It looks like you haven't modified your theme"
#           . " to include the \$prompt_additional expando.  You will not see"
#             . " any prompt info messages until you do. See script comments"
#               . "for details";
#     }
# }