Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add history to Console (plus some minor tweaks) #256

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ my %WriteMakefileArgs = (
"IO::Async::Timer::Countdown" => 0,
"IO::Async::Timer::Periodic" => 0,
"JMAP::Tester" => 0,
"JSON" => 0,
"JSON::MaybeXS" => 0,
"LWP::Protocol::https" => 0,
"Lingua::EN::Inflect" => 0,
Expand Down Expand Up @@ -170,7 +169,6 @@ my %FallbackPrereqs = (
"IO::Async::Timer::Countdown" => 0,
"IO::Async::Timer::Periodic" => 0,
"JMAP::Tester" => 0,
"JSON" => 0,
"JSON::MaybeXS" => 0,
"LWP::Protocol::https" => 0,
"Lingua::EN::Inflect" => 0,
Expand Down
1 change: 0 additions & 1 deletion cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ requires "IO::Async::Timer::Absolute" => "0";
requires "IO::Async::Timer::Countdown" => "0";
requires "IO::Async::Timer::Periodic" => "0";
requires "JMAP::Tester" => "0";
requires "JSON" => "0";
requires "JSON::MaybeXS" => "0";
requires "LWP::Protocol::https" => "0";
requires "Lingua::EN::Inflect" => "0";
Expand Down
157 changes: 147 additions & 10 deletions lib/Synergy/Channel/Console.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use utf8;

use Moose;
use experimental qw(signatures);
use JSON::MaybeXS;

use Future::AsyncAwait;
use Synergy::Event;
Expand All @@ -16,6 +15,7 @@ use namespace::autoclean;
use List::Util qw(max);

use Term::ANSIColor qw(colored);
use YAML::XS ();

with 'Synergy::Role::Channel';

Expand Down Expand Up @@ -148,6 +148,7 @@ package Synergy::Channel::Console::DiagnosticHandler {

/console - print Console channel configuration
/format - configure Console channel output (see "/help format")
/history - inspect messages previously sent across this channel

/set VAR VALUE - change the default value for one of the following

Expand Down Expand Up @@ -175,6 +176,31 @@ package Synergy::Channel::Console::DiagnosticHandler {
channels.
EOH

$HELP{history} = <<~'EOH';
The history command lets you see messages sent across a Console channel,
assuming you have history logging turned on. Your Console channel will need
a max_message_history setting greater than 1.

It works like this:

/history $message_number $format? $channel_name?

The message number is shown on (chonky-formatted) messages in a Console
channel, if it's logging history. $format defaults to "text" and
$channel_name defaults to the channel on which you're sending this command.
This can be useful for using the Console environment for debugging non-text
alternatives.

So, given this message box:

╭─────┤ term-rw!rjbs #6 ├──────────────────────────────╮
│ I don't know how to search for that!
╰──────────────────────────────────────────────────────╯

You can enter "/history 6" to see the message re-displayed as text, or
"/history 6 alts" to see the non-text alternatives dumped.
EOH

$HELP{events} = <<~'EOH';
You can begin your message with a string inside braces. The string is made
up of instructions separated by spaces. They can be:
Expand Down Expand Up @@ -309,6 +335,69 @@ EOH
return [ box => "Updated $var" ];
}

sub _diagnostic_cmd_history ($self, $rest) {
my ($number, $format, $channel_name) = split /\s+/, $rest, 3;

$format = 'text' unless length $format;

my $channel;

if ($channel_name) {
$channel = $self->hub->channel_named($channel_name);

unless ($channel) {
return [ box => "Unknown channel: $channel_name" ];
}

unless ($channel->DOES('Synergy::Channel::Console')) {
return [ box => "That isn't a Console channel, so this won't work. $channel" ];
}
} else {
$channel = $self->channel;
$channel_name = $channel->name;
}

unless ($channel->max_message_history > 0) {
return [ box => "That reactor does not store message history." ];
}

unless ($number =~ /\A[0-9]+\z/) {
return [ box => "That second argument doesn't look like a number." ];
}

my $message_log = $channel->_message_log;

unless (@$message_log) {
return [ box => "There's no history logged (yet?)." ];
}

my ($message) = grep {; $_->{number} == $number } @$message_log;

unless ($message) {
my $expired = $number < $message_log->[0]{number};
if ($expired) {
return [ box => "I can't find that message in history. It probably expired." ];
}

return [ box => "There's no message in history with that number." ];
}

my %new_message = %$message;

my $content
= $format eq 'text' ? $channel->_format_message_chonky(\%new_message)
: $format eq 'alts' ? YAML::XS::Dump($new_message{alts})
: undef;

unless ($content) {
return [ box => "I don't know how to format things this way: $format" ];
}

my $title = "history: channel=$channel_name item=$number format=$format";

return [ wide_box => $content, $title ];
}

sub _display_notice ($self, $text) {
$self->stream->write($self->_format_notice($self->channel->name, $text));
return;
Expand Down Expand Up @@ -450,32 +539,80 @@ async sub start ($self) {
return;
}

has _next_message_number => (
is => 'rw',
init_arg => undef,
default => 0,
traits => [ 'Counter' ],
handles => { get_next_message_number => 'inc' },
);

has _message_log => (
is => 'ro',
init_arg => undef,
default => sub { [] },
);

has max_message_history => (
is => 'ro',
default => 0,
);

sub _log_message ($self, $message) {
return undef unless $self->max_message_history > 0;

my $i = $self->get_next_message_number;

$message->{number} = $i;

my $log = $self->_message_log;
push @$log, $message;

if (@$log > $self->max_message_history) {
shift @$log;
}
}

sub send_message_to_user ($self, $user, $text, $alts = {}) {
$self->send_message($user->username, $text, $alts);
}

sub _format_message ($self, $name, $address, $text) {
sub _format_message ($self, $message) {
if ($self->message_format eq 'compact') {
return $self->_format_message_compact($name, $address, $text)
return $self->_format_message_compact($message);
}

return $self->_format_message_chonky($name, $address, $text)
return $self->_format_message_chonky($message);
}

sub send_message ($self, $address, $text, $alts = {}) {
my $name = $self->name;

$self->_stream->write(
$self->_format_message($name, $address, $text)
);
my $message = {
name => $name,
address => $address,
text => $text,
alts => $alts,
};

$self->_log_message($message);

$self->_stream->write( $self->_format_message($message) );
}

sub send_ephemeral_message ($self, $conv_address, $to_address, $text) {
my $name = $self->name;

$self->_stream->write(
$self->_format_message($name, $to_address, "[ephemeral] $text")
);
my $message = {
name => $name,
address => $to_address,
text => "[ephemeral] $text",
alts => undef,
};

$self->_log_message($message);

$self->_stream->write( $self->_format_message($message) );
}

sub describe_event ($self, $event) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Synergy/Channel/Discord.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use Synergy::Logger '$Logger';

use namespace::autoclean;

my $JSON = JSON->new->canonical;
my $JSON = JSON::MaybeXS->new->canonical;

with 'Synergy::Role::Channel';

Expand Down
2 changes: 0 additions & 2 deletions lib/Synergy/Channel/IRC.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use experimental qw(signatures);

use Future::AsyncAwait;

use JSON::MaybeXS;

use Synergy::Event;
use Synergy::Logger '$Logger';

Expand Down
1 change: 0 additions & 1 deletion lib/Synergy/Channel/Pushover.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package Synergy::Channel::Pushover;
use Moose;
use experimental qw(signatures);
use Future::AsyncAwait;
use JSON::MaybeXS qw(encode_json decode_json);

use Synergy::Logger '$Logger';

Expand Down
2 changes: 1 addition & 1 deletion lib/Synergy/Channel/Slack.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use Synergy::Logger '$Logger';

use namespace::autoclean;

my $JSON = JSON->new->canonical;
my $JSON = JSON::MaybeXS->new->canonical;

with 'Synergy::Role::Channel',
'Synergy::Role::ProvidesUserStatus';
Expand Down
4 changes: 2 additions & 2 deletions lib/Synergy/DiagnosticUplink.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use utf8;
use Future::AsyncAwait;
use Synergy::Logger '$Logger';

require JSON;
my $JSON = JSON->new;
require JSON::MaybeXS;
my $JSON = JSON::MaybeXS->new;

with 'Synergy::Role::HubComponent';

Expand Down
2 changes: 1 addition & 1 deletion lib/Synergy/External/Discord.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use Time::HiRes ();

use Synergy::Logger '$Logger';

my $JSON = JSON->new;
my $JSON = JSON::MaybeXS->new;

with 'Synergy::Role::HubComponent';

Expand Down
1 change: 0 additions & 1 deletion lib/Synergy/Reactor/InABox.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use Synergy::CommandPost;
use Synergy::Logger '$Logger';
use Synergy::Util qw(bool_from_text reformat_help);
use String::Switches qw(parse_switches);
use JSON::MaybeXS;
use Future::Utils qw(repeat);
use Text::Template;
use Time::Duration qw(ago);
Expand Down
2 changes: 1 addition & 1 deletion lib/Synergy/Reactor/Page.pm
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ END
my $opt_all = 0;
my @args;

my @words = length $event->text ? (split /\s+/, $event->text) : ();
my @words = length $event->text ? (split /\h+/, $event->text) : ();

shift @words; # get rid of the "page" command word

Expand Down
24 changes: 20 additions & 4 deletions lib/Synergy/TextThemer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ sub from_name ($class, $name) {
return $class->new($THEME{$name});
}

sub _format_raw ($self, $thing) {
return $thing;
}

sub _format_box ($self, $text, $title = undef) {
$self->_format_generic_box($text, 1, $title);
}
Expand Down Expand Up @@ -133,7 +137,11 @@ sub _format_notice ($self, $from, $text) {
return $message;
}

sub _format_message_compact ($self, $name, $address, $text) {
sub _format_message_compact ($self, $message) {
my $address = $message->{address};
my $name = $message->{name};
my $text = $message->{text};

return "❱❱ $name!$address ❱❱ $text\n" if $self->is_null;

my $c0 = $self->decoration_color;
Expand All @@ -148,7 +156,11 @@ sub _format_message_compact ($self, $name, $address, $text) {
. "\n";
}

sub _format_message_chonky ($self, $name, $address, $text) {
sub _format_message_chonky ($self, $message) {
my $address = $message->{address};
my $name = $message->{name};
my $text = $message->{text};

state $B_TL = q{╭};
state $B_BL = q{╰};
state $B_TR = q{╮};
Expand All @@ -165,10 +177,14 @@ sub _format_message_chonky ($self, $name, $address, $text) {
my $line_C = $themed ? $self->decoration_color_code : q{};
my $null_C = $themed ? Term::ANSIColor::color('reset') : q{};

my $dest_width = length "$name/$address";

my $dest = "$text_C$name$line_C!$text_C$address$line_C";

if (defined $message->{number}) {
$dest .= " ${line_C}#$text_C$message->{number}";
}

my $dest_width = plainlength($dest);

my $header = "$line_C$B_TL"
. ($B_hor x 5)
. "$B_boxleft $dest $B_boxright"
Expand Down