/feature-tests/history-restore.pl

http://github.com/shabble/irssi-scripts · Perl · 186 lines · 81 code · 30 blank · 75 comment · 8 complexity · c39b7c1f21535f0a6a6643a6f39cbf19 MD5 · raw file

  1. =pod
  2. =head1 NAME
  3. template.pl
  4. =head1 DESCRIPTION
  5. A minimalist template useful for basing actual scripts on.
  6. =head1 INSTALLATION
  7. Copy into your F<~/.irssi/scripts/> directory and load with
  8. C</SCRIPT LOAD F<filename>>.
  9. =head1 USAGE
  10. None, since it doesn't actually do anything.
  11. =head1 AUTHORS
  12. Copyright E<copy> 2011 Tom Feist C<E<lt>shabble+irssi@metavore.orgE<gt>>
  13. =head1 LICENCE
  14. Permission is hereby granted, free of charge, to any person obtaining a copy
  15. of this software and associated documentation files (the "Software"), to deal
  16. in the Software without restriction, including without limitation the rights
  17. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. copies of the Software, and to permit persons to whom the Software is
  19. furnished to do so, subject to the following conditions:
  20. The above copyright notice and this permission notice shall be included in
  21. all copies or substantial portions of the Software.
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. THE SOFTWARE.
  29. =head1 BUGS
  30. =head1 TODO
  31. Use this template to make an actual script.
  32. =cut
  33. use strict;
  34. use warnings;
  35. use Irssi;
  36. use Irssi::Irc;
  37. use Irssi::TextUI;
  38. use Data::Dumper;
  39. our $VERSION = '0.1';
  40. our %IRSSI = (
  41. authors => 'shabble',
  42. contact => 'shabble+irssi@metavore.org',
  43. name => 'history-restore',
  44. description => '',
  45. license => 'MIT',
  46. updated => '$DATE'
  47. );
  48. my @fake_history;
  49. # = ( "test", "test2", "test3" );
  50. push @fake_history, "Test Entry $_" for (1..100);
  51. my $buf;
  52. my @hist_queue = ();
  53. sub init {
  54. Irssi::theme_register
  55. ([
  56. verbatim => '[$*]',
  57. script_loaded => 'Loaded script {hilight $0} v$1',
  58. ]);
  59. # definitely not an int for last param.
  60. #Irssi::signal_register({'key down' => [qw/string string int/] });
  61. Irssi::command_bind('dohist', \&cmd_dohist);
  62. Irssi::command_bind('showhist', \&cmd_showhist);
  63. Irssi::printformat(Irssi::MSGLEVEL_CLIENTCRAP, 'script_loaded',
  64. $IRSSI{name}, $VERSION);
  65. }
  66. sub win {
  67. return $_[0] || Irssi::active_win();
  68. }
  69. sub do_next {
  70. if (length $buf) {
  71. my $char = substr($buf, 0, 1, '');
  72. _key($char);
  73. do_next();
  74. #Irssi::timeout_add_once(10,
  75. # sub {
  76. # }, '');
  77. } else {
  78. $buf = shift @hist_queue;
  79. _down();
  80. if ($buf) {
  81. do_next();
  82. } else {
  83. print "Queue empty";
  84. Irssi::timeout_add_once(100,
  85. sub {
  86. print "Done";
  87. Irssi::command("/showhist");
  88. }, '');
  89. }
  90. }
  91. }
  92. sub cmd_dohist {
  93. my ($args, $server, $witem) = @_;
  94. print "Inserting fake history...";
  95. @hist_queue = @fake_history;
  96. do_next();
  97. }
  98. sub _key {
  99. Irssi::signal_emit('gui key pressed', ord($_[0]));
  100. }
  101. sub _down {
  102. _key($_) for ("\e", "[", "B");
  103. }
  104. sub cmd_showhist {
  105. my ($args, $server, $witem) = @_;
  106. #print "Args: " . Dumper(\@_);
  107. dump_history(win($witem));
  108. }
  109. sub dump_history {
  110. my ($win) = @_;
  111. my @history = $win->get_history_lines();
  112. my $i = 0;
  113. print "---------HISTORY-----------";
  114. for (@history) {
  115. $i++;
  116. printf("%02d: %s", $i, $_);
  117. }
  118. print "---------------------------";
  119. }
  120. init();
  121. # my $m = 0;
  122. # for my $entry (@fake_history) {
  123. # my $n = 0;
  124. # $m++;
  125. # for my $char (split '', $entry) {
  126. # $n++;
  127. # Irssi::timeout_add_once(100 * $n * $m,
  128. # sub { print "$char of $entry scheduled at "
  129. # . (100 * $n * $m);
  130. # _key($char)
  131. # }, '');
  132. # }
  133. # Irssi::timeout_add_once(100 * $n * $m,
  134. # sub { print "Down scheduled at "
  135. # . (100 * $n * $m);
  136. # _down();
  137. # }, '');
  138. # }