Antsy
view release on metacpan or search on metacpan
lib/Antsy.pm view on Meta::CPAN
use v5.26;
package Antsy;
use strict;
use warnings;
use utf8;
use experimental qw(signatures);
use Carp qw(carp);
use Exporter qw(import);
our( @EXPORT, @EXPORT_OK, %EXPORT_TAGS );
our $VERSION = '0.907';
=encoding utf8
=head1 NAME
Antsy - Streaming ANSI escape sequences
=head1 SYNOPSIS
use Antsy qw(:all);
print bold, underline, text_red, "Hello", reset;
=head1 DESCRIPTION
Subroutines to deal with ANSI terminal sequences. You can emit these
without knowing what's coming up.
=head2 Yet another module?
There are several modules that come close to this, but so far
everything is incomplete or requires you to know all of the upcoming
text ahead of time so you can use of it as an argument to a function.
I want to emit the sequence in a stream without knowing what's coming
up.
=over 4
=item * L<Term::ANSIColor>
Wraps ANSI color stuff around text. This comes with Perl v5.10 and
later.
=item * L<Text::ANSI::Util>
Routines for dealing with text that contains ANSI code. For example,
ignore the ANSI sequences in computing length.
=item * L<Text::ANSI::Printf>
I don't really know what this does.
=item * L<Term::ANSIScreen>
=back
=head2 Methods
=over 4
=item * bg_256( N )
=item * bg_rgb
=item * bg_black
=item * bg_blue
=item * bg_cyan
=item * bg_green
=item * bg_magenta
=item * bg_orange
lib/Antsy.pm view on Meta::CPAN
=item * text_bright_black
=item * text_bright_blue
=item * text_bright_cyan
=item * text_bright_green
=item * text_bright_magenta
=item * text_bright_red
=item * text_bright_white
=item * text_bright_yellow
Make foreground text the named color and bright (however your terminal
does that).
=item * text_concealed
Conceal the text.
=item * underline
Turn on underlining.
=back
=cut
sub _256 ( $i, $n ) {
carp "Bad 256 $n" unless( int($n) == $n and $n >= 0 and $n <= 255 );
_seq( 'm', $i, 5, $n );
}
sub _bg () { 48 } # a magic number that applies the SGR to the background
sub _erase ( $n, $command ) {
carp "Bad value <$n>. Should be 0, 1, or 2"
unless grep { $_ == $n } qw(0 1 2);
carp "Bad erase command <$command>. Should be J or K"
unless grep { $_ == $n } qw(0 1 2);
_seq( $command, $n );
}
sub _export ( $name, $tag ) {
push @EXPORT_OK, $name;
push $EXPORT_TAGS{all }->@*, $name;
push $EXPORT_TAGS{$tag}->@*, $name;
}
sub _rgb ( $i, $r, $g, $b ) {
carp "Bad RGB $r;$b;$g" unless
3 == grep { int($_) == $_ and $_ >= 0 and $_ <= 255 }
( $r, $b, $g );
_seq( 'm', $i, 2, $r, $g, $b );
}
# _seq forms the ANSI escape sequence. There's the start, some arguments
# separated by `;`, then a string for the argument
sub _seq ( $command, @args ) { join '', "\x1b[", join( ';', @args ), $command }
sub _encode_seq ( $string ) {
local $_ = $string;
s/(.)/sprintf '%02X ', ord($1) /ge;
s/1b /ESC /ig;
s/07 /BEL /ig;
s/31 33 33 37 /1337 /;
s/([2-7A-F][0-9A-F])\x20/ chr( hex($1) ) . ' ' /ge;
$_;
}
sub _start () { "\x1b[" }
sub _text () { 38 } # a magic number that applies the SGR to the text
sub bg_256 ( $n ) { _256( _bg(), $n ) }
sub bg_rgb ( $r, $g, $b ) { _rgb( _bg(), $r, $g, $b ) }
sub cursor_row_column ( $n = 1, $m = 1 ) { _seq( 'H', $n, $m ) }
sub text_256 ( $n ) { _256( _text(), $n ) }
sub text_rgb ( $r, $g, $b ) { _rgb( _text(), $r, $g, $b ) }
# This section takes the subroutines that we've already defined to
# adds them to the export lists.
BEGIN {
my @subs = qw( bg_256 bg_rgb text_256 text_rgb
erase_in_display erase_in_line cursor_row_column
);
push @EXPORT_OK, @subs;
push $EXPORT_TAGS{all }->@*, @subs;
push $EXPORT_TAGS{bg }->@*, grep { /\Abg_/ } @subs;
push $EXPORT_TAGS{text }->@*, grep { /\Atext_/ } @subs;
push $EXPORT_TAGS{erase }->@*, grep { /\Aerase_/ } @subs;
push $EXPORT_TAGS{cursor}->@*, grep { /\Acursor_/ } @subs;
}
BEGIN {
my @groups = (
[ qw( J screen) ],
[ qw( K line ) ],
);
my @templates = ( 'clear_to_%s_end', 'clear_to_%s_start', 'clear_%s' );
foreach my $group ( @groups ) {
no strict 'refs';
foreach my $i ( 0 .. 2 ) {
my $name = sprintf $templates[$i], $group->[1];
my $value = _seq( $group->[0], $i );
*{$name} = sub () { $value };
_export( $name, 'clear' );
}
lib/Antsy.pm view on Meta::CPAN
=item * iterm_unbounce_dock_icon
Stop bouncing the Dock icon
=item * iterm_fireworks
Show animated fireworks.
=cut
# OSC 1337 ; RequestAttention=[value] ST
sub iterm_attention ( $value ) {
state $allowed = do {
my %hash = map { $_, 1 } qw( fireworks no once yes );
\%hash;
};
unless( exists $allowed->{$value} ) {
carp "iterm_attention argument can be one of <@{[ join ',', sort keys %$allowed ]}>, but you specified <$value>";
return;
}
my $r = _osc_1337( "RequestAttention=$value" );
say _encode_seq( $r );
$r;
}
sub iterm_bounce_dock_icon { iterm_attention( 'yes' ) }
sub iterm_bounce_dock_icon_once { iterm_attention( 'once' ) }
sub iterm_unbounce_dock_icon { iterm_attention( 'no' ) }
sub iterm_fireworks { iterm_attention( 'fireworks' ) }
=item * background_image_file
OSC 1337 ; SetBackgroundImageFile=[base64] ST
The value of [base64] is a base64-encoded filename to display as a background image. If it is an empty string then the background image will be removed. User confirmation is required as a security measure.
=item * report_cell_cell
OSC 1337 ; ReportCellSize ST
The terminal responds with either:
OSC 1337 ; ReportCellSize=[height];[width] ST
Or, in newer versions:
OSC 1337 ; ReportCellSize=[height];[width];[scale] ST
[scale] gives the number of pixels (physical units) to points (logical units). 1.0 means non-retina, 2.0 means retina. It could take other values in the future.
[height] and [width] are floating point values giving the size in points of a single character cell. For example:
OSC 1337 ; ReportCellSize=17.50;8.00;2.0 ST
=item * copy_to_pasteboard
You can place a string in the system's pasteboard with this sequence:
OSC 1337 ; Copy=:[base64] ST
Where [base64] is the base64-encoded string to copy to the pasteboard.
=item * report_variable
Each iTerm2 session has internal variables (as described in Scripting Fundamentals). This escape sequence reports a variable's value:
OSC 1337 ; ReportVariable=[base64] ST
Where [base64] is a base64-encoded variable name, like session.name. It responds with:
OSC 1337 ; ReportVariable=[base64] ST
Where [base64] is a base64-encoded value.
https://iterm2.com/documentation-scripting-fundamentals.html
=item * badge
The badge may be set with the following control sequence:
https://iterm2.com/documentation-badges.html
OSC 1337 ; SetBadgeFormat=Base-64 encoded badge format ST
Here's an example that works in bash:
# Set badge to show the current session name and git branch, if any is set.
printf "\e]1337;SetBadgeFormat=%s\a" \
$(echo -n "\(session.name) \(user.gitBranch)" | base64)
=item * downloads
https://iterm2.com/documentation-images.html
The width and height are given as a number followed by a unit, or the word "auto".
iTerm2 extends the xterm protocol with a set of proprietary escape sequences. In general, the pattern is:
ESC ] 1337 ; key = value ^G
Whitespace is shown here for ease of reading: in practice, no spaces should be used.
For file transfer and inline images, the code is:
ESC ] 1337 ; File = [arguments] : base-64 encoded file contents ^G
The arguments are formatted as key=value with a semicolon between each key-value pair. They are described below:
Key Description of value
name base-64 encoded filename. Defaults to "Unnamed file".
size File size in bytes. The file transfer will be canceled if this size is exceeded.
width Optional. Width to render. See notes below.
height Optional. Height to render. See notes below.
preserveAspectRatio Optional. If set to 0, then the image's inherent aspect ratio will not be respected; otherwise, it will fill the specified width and height as much as possible without stretching. Defaults to 1.
inline Optional. If set to 1, the file will be displayed inline. Otherwise, it will be downloaded with no visual representation in the terminal session. Defaults to 0.
N: N character cells.
Npx: N pixels.
N%: N percent of the session's width or height.
auto: The image's inherent size will be used to determine an appropriate dimension.
More on File Transfers
By omitting the inline argument (or setting its value to 0), files will be downloaded and saved in the Downloads folder instead of being displayed inline. Any kind of file may be downloaded, but only images will display inline. Any image format that ...
If the file's size exceeds the declared size, the transfer may be canceled. This is a security measure to prevent a download gone wrong from using unbounded memory.
=item * uploads
To request the user select one or more files to upload, send:
OSC 1337 ; RequestUpload=format=[type] ST
In the future the [type] may be configurable, but for now it must always be tgz, which is a tar and gzipped file.
When iTerm2 receives this it will respond with a status of ok or abort followed by a newline. If the status is ok then it will be followed by a base-64 encoded tar.gz file.
If the user selects multiple files they will be placed in a directory within the tar file.
=item * set_touchbar_key_levels
You can configure touch bar key labels for function keys and for the "status" button. The code used is:
OSC 1337 ; SetKeyLabel=[key]=[value] ST
Where [key] is one of F1, F2, ..., F24, to adjust a function key label; or it can be status to adjust the touch bar status button. You can also save and restore sets of key labels using a stack. To push the current key labels on the stack use:
OSC 1337 ; PushKeyLabels ST
To pop them:
OSC 1337 ; PopKeyLabels ST
You can optionally label the entry in the stack when you push so that pop will pop multiple sets of key labels if needed. This is useful if a program crashes or an ssh session exits unexpectedly. The corresponding codes with labels are:
OSC 1337 ; PushKeyLabels=[label] ST
OSC 1337 ; PopKeyLabels=[label] ST
Where [label] is an ASCII string that works best if it is unique in the stack.
=item * unicode_version
iTerm2 by default uses Unicode 9's width tables. The user can opt to use Unicode 8's tables with a preference (for backward compatibility with older locale databases). Since not all apps will be updated at the same time, you can tell iTerm2 to use a ...
OSC 1337 ; UnicodeVersion=[n] ST
Where [n] is 8 or 9
You can push the current value on a stack and pop it off to return to the previous value by setting n to push or pop. Optionally, you may affix a label after push by setting n to something like push mylabel. This attaches a label to that stack entry....
=back
=head1 SEE ALSO
=over 4
=item * Everything you never wanted to know about ANSI escape codes https://notes.burke.libbey.me/ansi-escape-codes/
=item * https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
=item * iTerm2 ANSI codes https://iterm2.com/documentation-escape-codes.html
=back
=head1 SOURCE AVAILABILITY
This source is in Github:
http://github.com/briandfoy/antsy
=head1 AUTHOR
brian d foy, C<< <briandfoy@pobox.com> >>
=head1 COPYRIGHT AND LICENSE
Copyright © 2021-2025, brian d foy, All Rights Reserved.
You may redistribute this under the terms of the Artistic License 2.0.
=cut
1;
( run in 1.678 second using v1.01-cache-2.11-cpan-56fb94df46f )