diff options
| author | Paul Buetow <git@mx.buetow.org> | 2021-01-02 09:02:07 +0000 |
|---|---|---|
| committer | Paul Buetow <git@mx.buetow.org> | 2021-01-02 09:02:07 +0000 |
| commit | 5161b18fbb896a987675f68b0605114a918bc23b (patch) | |
| tree | 4d5608a503f91f7f705fe8ae95ce2f292a690c09 | |
initial cli-hive
| -rw-r--r-- | cli-hive.pl | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/cli-hive.pl b/cli-hive.pl new file mode 100644 index 0000000..f0187f2 --- /dev/null +++ b/cli-hive.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Data::Dumper; +use JSON; + +sub tojson { + my $timestamp = shift; + my $lines = shift; + my %lines = ( + 'command' => join '\\n', @$lines, + ); + $lines{'timestamp'} = $timestamp if defined $timestamp; + + print encode_json \%lines; + print "\n"; +} + +sub extract_timestamp { + my $line = shift; + my ($timestamp, $command) = $line =~ /^: (\d+).*?;(.*)/; + + return ($timestamp, $command) if defined $command; + return (undef, $line); +} + +sub entry { + my @lines; + my $timestamp; + + return sub { + my $line = shift; + my ($timestamp_, $command) = extract_timestamp $line; + $timestamp = $timestamp_ if defined $timestamp_; + + if ($command =~ /\\$/) { + chomp $command; + push @lines, $command; + return; + } + chomp $command; + push @lines, $command; + tojson $timestamp, \@lines; + + @lines, $timestamp = (), undef; + }; +} + +sub follow_history { + open my $fd, '<', "$ENV{HOME}/.zsh_history" or die $!; + seek $fd, SEEK_END; + for (;;) { + my $entry = entry; + $entry->($_) while <$fd>; + sleep 1; + } + close $fd; +} + +follow_history; |
