-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathsavecmdhist.pl
72 lines (64 loc) · 1.95 KB
/
savecmdhist.pl
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
use strict;
use warnings;
use Irssi 20171006;
use Irssi::UI;
our $VERSION = '1.0'; # 7775fccf37d60a5
our %IRSSI = (
authors => 'Nei',
url => "http://anti.teamidiot.de/",
name => 'savecmdhist',
description => 'Saves the commands you typed in the input prompt to a history file, so that they persist across /upgrade and restart.',
license => 'ISC',
);
# Usage
# =====
# Put the script in autorun if you want the history to be loaded on
# start.
my $histfile = Irssi::get_irssi_dir()."/cmdhistory";
sub loadcmdhist {
my %R = ("n" => "\n", "\\" => "\\", ";" => ";");
my $fh;
unless (open $fh, '<', $histfile) {
warn "Could not open history file ($histfile) for readin: $!\n"
if -e $histfile;
return;
}
my @hist;
while (defined(my $line = <$fh>)) {
chomp $line;
if ($line =~ /^: (\d*):(\d*):(.*?) :;(.*)$/) {
push @hist, +{
time => $1,
window => length $2 ? $2 : undef,
history => length $3 ? $3 : undef,
};
$hist[-1]{text} = $4 =~ s/\\(\\|;|n)/$R{$1}/gmsr;
}
}
my $he1 = @{[Irssi::UI::Window::get_history_entries(undef)]};
Irssi::UI::Window::load_history_entries(undef, @hist) if @hist && $he1 <= 1;
}
sub savecmdhist {
my %R = ("\n" => "\\n", "\\" => "\\\\", " :;" => " :\\;");
my $old_umask =
umask 0077;
my $fh;
unless (open $fh, '>', $histfile) {
umask $old_umask;
warn "Could not open history file ($histfile) for writing: $!\n";
return;
}
umask $old_umask;
no warnings 'uninitialized';
for my $hist (Irssi::UI::Window::get_history_entries(undef)) {
my $text = $hist->{text} =~ s/(\n|\\| :;)/$R{$1}/gmsr;
print $fh ": $hist->{time}:$hist->{window}:$hist->{history} :;$text\n";
}
close $fh;
}
Irssi::signal_add 'gui exit' => sub {
savecmdhist() if Irssi::settings_get_bool('settings_autosave');
};
Irssi::signal_add 'command save' => 'savecmdhist';
loadcmdhist();