Antsy

 view release on metacpan or  search on metacpan

lib/Antsy.pm  view on Meta::CPAN

		$value = bg_rgb( $rgb->@* );
		_export( $name, 'bg' );
		}
	}

=head2 Character shortcuts

=over 4

=item * BELL - \007

=item * CSI - ESC [

=item * ESC - \x1b

=item * OSC - ESC ]

=item * ST - BELL or ESC \

=item * SP - literal space

=back

=cut

sub BELL () { "\007" }
sub CSI  () { ESC() . '[' }
sub ESC  () { "\x1b" }
sub OSC  () { ESC() . ']' }
sub SP   () { ' ' }
sub ST   () { BELL() }

=head2 Editor-specific codes

=head3 iTerm2

iTerm2 supports proprietary

=over 4

=item * iterm_bg_color()

=item * iterm_fg_color() OSC 4 ; -1; ? ST

Returns an array reference of the decimal values for the Red, Green
and Blue components of the background or foreground. These triplets
may be 2 or 4 digits in each component.

=cut

sub _iterm_id { 'iTerm.app' }

sub _is_term_type ( $id ) {
	$ENV{TERM_PROGRAM} =~ m/\A\Q$id\E\z/;
	}

sub _is_iterm { _is_term_type( _iterm_id() ) }

sub _iterm_seq ( $command, @args ) {
	unless( _is_iterm() ) {
		my $sub = ( caller(1) )[3];
		carp( "$sub only works in iTerm2" );
		return;
		}

	OSC() . join( ';', @args, '' ) . $command . ST();
	}

sub _iterm_query ( $command, @args ) {
	my $terminal = do {
		state $rc = require Term::ReadKey;
		chomp( my $tty = `/usr/bin/tty` );
		# say "Term: ", $tty;
		open my $terminal, '+<', $tty;
		my $old = select( $terminal );
		$|++;
		select( $old );
		$terminal;
		};

	print { $terminal } _iterm_seq( $command, @args );;
	Term::ReadKey::ReadMode('raw');
	my $response;
	my $key;
	while( defined ($key = Term::ReadKey::ReadKey(0)) ) {
		$response .= $key;
		last if ord( $key ) == 3; # Control-C
		last if ord( $key ) == 7;
	}
	Term::ReadKey::ReadMode('normal');

	$response;
	}

sub _iterm_rgb_query ( $type ) {
	state $OSC = qr/ ( \007 | \x1b \\ ) /xn;
	my $response = _iterm_query( '?', 4, $type );
	my( $r, $g, $b ) = $response =~ m|rgb:(.+?)/(.+?)/(.+?)$OSC|;
	[ $r, $g, $b ]
	}

sub iterm_bg_color () { _iterm_rgb_query( -2 ) } # OSC 4 ; -2; ? ST
sub iterm_fg_color () { _iterm_rgb_query( -1 ) } # OSC 4 ; -1; ? ST

=item * iterm_start_link( URL [, ID] )

=item * iterm_end_link()

Mark some text as a clickable URL.
OSC 8 ; [params] ; [url] ST id is only param

=item * iterm_linked_text( TEXT, URL, [, ID] )

=cut

sub iterm_start_link ( $url, $id = undef ) {
	$id = defined $id ? 'id=$id' : '';
	OSC() . 8 . ';' . $id . ';' . $url . ST();
	}

sub iterm_end_link () { OSC() . 8 . ';;' . ST() }

sub iterm_linked_text ( $text, $url, $id ) {
	iterm_start_link( $url, $id ) .
	$text .
	iterm_end_link();
	}

=item * set_cursor_shape( N )

=over 4

=item * 0 Block

=item * 1 Vertical bar

=item * 2 Underline

=back

=item * iterm_set_block_cursor

=item * iterm_set_bar_cursor

=item * iterm_set_underline_cursor

=cut

sub _osc_1337 ( $content ) {
	unless( _is_iterm() ) {
		my $sub = ( caller(1) )[3];
		carp( "$sub only works in iTerm2" );
		return;
		}

	OSC() . 1337 . ';' . $content . ST()
	}

# OSC 1337 ; CursorShape=[N] ST
sub _iterm_set_cursor ( $n ) {
	unless( $n == 0 or $n == 1 or $n == 2 ) {
		carp "The cursor type can be 0, 1, or 2, but you specified <$n>";
		return;
		}

	OSC() . 1337 . ';' . "CursorShape=$n" . 'ST'
	}

sub iterm_set_block_cursor ()     { state $s = _iterm_set_cursor(0); $s }
sub iterm_set_bar_cursor ()       { state $s = _iterm_set_cursor(1); $s }
sub iterm_set_underline_cursor () { state $s = _iterm_set_cursor(2); $s }

=item * set_mark

Same as Command-Shift-M. Mark the current location and jump back to it
with Command-Shift-J.

=cut

# OSC 1337 ; SetMark ST
sub set_mark () { state $s = _osc_1337( 'SetMark' ); $s }

=item * steal_focus

Bring the window to the foreground.

=cut

# OSC 1337 ; StealFocus ST
sub steal_focus () { state $s = _osc_1337( 'StealFocus' ); $s }

=item * clear_scrollback_history

Erase the scrollback history.

=cut

# OSC 1337 ; ClearScrollback ST
sub clear_scrollback_history () { state $s = _osc_1337( 'ClearScrollback' ); $s }

=item * post_notification

=cut

# OSC 9 ; [Message content goes here] ST

=item * set_current_directory

=cut

# OSC 1337 ; CurrentDir=[current directory] ST



( run in 0.648 second using v1.01-cache-2.11-cpan-5c0b1e786e0 )