You are not logged in.

#1 2016-11-04 16:24:01

usersandbox
Member
Registered: 2016-11-04
Posts: 3

Contrib: 3 urxvt-extensions

Hello everyone,

Decided to swing by and contribute some code.

It would be really great and kind if anyone adds them to the wiki page as well.

pinky-urxvt - Status line based on tabbed and tabbedex. The sole purpose of this extension is to make it easy for you to keep track of things that you are interested to monitor while hacking something in the terminal.

Notice the green line in the following picture:

2.png

Installation and usage:

Simply place it in /usr/lib/urxvt/perl/ for
system-wide availability or in ~/.urxvt/ext/ for user-only availability.
You can also put it in a folder of your choice, but then you have to add this
line to your .Xdefaults/.Xresources:


# Don't type ~ or $HOME
URxvt.perl-lib: /home/user/your/folder/

# Make sure to remove tabbed and/or tabbedex
# from the used extensions
URxvt.perl-ext-common: pinky

# Those colours are defined by you

# 0, 2 refer to:
*.color0: #222222
*.color2: #8ae234

URxvt.pinky.pinky-fg: 2
URxvt.pinky.pinky-bg: 0

# Usage

Use your favourite 'echo, print, printf' command to update the status line.

while true; do
  printf '\033]777;pinky;%s\007' 'Hello World'
  sleep 5
done &  # <-- "daemon mode"

The extension itself:

#!/usr/bin/env perl

# 11/02/2016

# Based on tabbed and tabbedex.
# Copyright (c) 2006-2012 tabbed authors
# Copyright (c) 2009-2014 tabbedex authors
# Copyright (c) 2016 Aaron Caffrey

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

use strict;
use warnings;


sub refresh {
  my ($self) = @_;

  my $ncol = $self->ncol;
  my $text = " " x $ncol;
  my $rend = [($self->{rs_pinky}) x $ncol];

  my $statusLine = $self->{pinky_line}[0];
  my $txt = $statusLine->{name} || "";
  my $len = length $txt;

  substr $text, 0, $len + 1, " $txt ";

  $self->ROW_t (0, $text, 0, 0, $ncol);
  $self->ROW_r (0, $rend, 0, 0, $ncol);

  $self->want_refresh;
  return;
}


sub new_pinky_status_line {
  my ($self) = @_;

  push @urxvt::TERM_INIT, sub {
    my ($term) = @_;
    $term->{parent} = $self;

    for (0 .. urxvt::NUM_RESOURCES - 1) {
      my $value = $self->{resource}[$_];
      if (defined $value) {
        $term->resource ("+$_" => $value);
      }
    }

    foreach my $opt (keys %urxvt::OPTION) {
      my $value = $self->{option}{$opt};
      $term->option($urxvt::OPTION{$opt}, $value);
    }

    $term->resource (perl_ext_2 =>
      $term->resource ('perl_ext_2') .
      $self->{perl_ext_blacklist}
    );
  };

  push @urxvt::TERM_EXT, urxvt::ext::pinky::pinky_status::;

  new urxvt::term $self->env, $urxvt::RXVTNAME,
    -embed => $self->parent, ();

  return;
}


sub rs_text {
  my ($self, $name, $default) = @_;

  return $self->x_resource ($self->{rs_prefix} . '.' . $name) // $default;
}


sub rs_color($$$$) {
  my ($self, $prefix, $def_fg, $def_bg) = @_;
  my $fg = $self->rs_text ($prefix . '-fg') // $def_fg;
  my $bg = $self->rs_text ($prefix . '-bg') // $def_bg;

  return urxvt::SET_COLOR (urxvt::DEFAULT_RSTYLE, $fg + 2, $bg + 2);
}


sub make_pinky_window {
  my ($self, $statusLine) = @_;
  $self->{cur} = $statusLine;

  $statusLine->XMoveResizeWindow (
    $statusLine->parent,
    0, $self->{lineheight},
    $self->width,
    $self->height - $self->{lineheight}
  );

  $statusLine->XMapWindow ($statusLine->parent);
  $self->refresh;
  return;
}


sub on_focus_in {
  my ($self, $event) = @_;

  $self->{cur}->focus_in;
  return;
}


sub on_tt_write {
  my ($self, $octets) = @_;

  $self->{cur}->tt_write ($octets);
  return 1;
}


sub on_init {
  my ($self) = @_;

  $self->{resource} = [map $self->resource ("+$_"), 0 .. urxvt::NUM_RESOURCES - 1];
  $self->resource (int_bwidth => 0);
  $self->resource (pty_fd => -1);

  $self->{rs_prefix} = 'pinky';
  $self->{rs_prefix} = $self->rs_text ('pinky-rs-prefix', 'pinky');

  $self->{option} = {};
  for my $key (keys %urxvt::OPTION) {
    $self->{option}{$key} = $self->option($urxvt::OPTION{$key});
  }

  # order is important
  $self->option ($urxvt::OPTION{scrollBar}, 0);
  $self->{rs_pinky} = $self->rs_color ('pinky', 3, 0);
  $self->{perl_ext_blacklist} = join (',-', ',-pinky');

  return;
}


sub on_start {
  my ($self) = @_;

  $self->{lineheight} = $self->int_bwidth + $self->fheight + $self->lineSpace;
  $self->cmd_parse ("\033[?25l");

  $self->new_pinky_status_line;
  return;
}


sub on_wm_delete_window {
  my ($self) = @_;

  $_->destroy for @{$self->{pinky_line}};
  return 1;
}


sub pinky_status_start {
  my ($self, $statusLine) = @_;

  push @{$self->{pinky_line}}, $statusLine;
  $self->make_pinky_window ($statusLine);
  return;
}


sub pinky_status_destroy {
  my ($self) = @_;

  $self->{destroy} = urxvt::iw->new->start->cb (sub { $self->destroy });
  return;
}


package urxvt::ext::pinky::pinky_status;

{
  for my $hook ( qw(start destroy) ) {
    eval qq{
      sub on_$hook {
        my \$parent = \$_[0]{term}{parent}
        or return;
        \$parent->pinky_status_$hook (\@_)
      }
    };
    die if $@;
  }
}


sub on_osc_seq_perl {
  my ($self, $osc) = @_;

  return unless $osc =~ s/^pinky;//;

  my $statusLine = $self->{term}->{parent};
  my ($msg) = split /;/, $osc, 0;

  $statusLine->{cur}->{name} = $msg;
  $statusLine->refresh;
  return 1;
}

The system information in the above picture is produced by pinky-bar - https://notabug.org/void0/pinky-bar

---


urxvt-man2pdf - Convert man page while reading it to pdf. No need to quit reading or open another terminal instance/tab.

6e02ec1c-41d5-11e6-9ca6-1009de85e11a.png

You can convert man pages with your mouse, by default this functionallity is disabled so not to confuse any other extensions, such as url-select.

To enable this functionallity add the following to your **.Xdefaults/.Xresources**:

URxvt.perl-ext-common           : selection-to-clipboard,man

# 3 is the "right" mouse click button
URxvt.man2pdf.button: 3

# Mark the man page name with
# your left mouse button and right click
# to start the conversion process

Same installation as the previous extension with the following difference:

URxvt.perl-ext-common           : man

# keyboard shortcut to trigger the extension
URxvt.keysym.Control-Shift-X    : perl:man:topdf

The extension itself:

#!/usr/bin/env perl
# Author:   Aaron Caffrey
# Website:  https://notabug.org/void0/urxvt-man2pdf
# License:  GPLv3

# Usage: put the following lines in your .Xdefaults/.Xresources:
# URxvt.perl-ext-common           : man
# URxvt.keysym.Control-Shift-X    : perl:man:topdf

use strict;
use warnings;


sub on_user_command {
  my ($self, $cmd) = @_;

  if ($cmd eq "man:topdf") {
    my $cur_row = $self->nrow - 1;
    my $top = $self->top_row;

    return unless (-w $ENV{HOME});

    while ($cur_row >= $top) {
      my $line_obj = $self->line ($cur_row);
      my $it_found_smth = $self->scan_line (\$line_obj->t);

      last if ($it_found_smth);
      $cur_row = $line_obj->beg - 1;
    }
  }
  return;
}


sub on_start {
  my ($self) = @_;

  $self->{manBtn} = $self->x_resource("man2pdf.button") || 1337;
  return 0;
}


sub on_button_press {
  my ($self, $evt) = @_;

  return 0 unless ($evt->{button} == $self->{manBtn});

  my $page = $self->selection;
  return 0 unless defined $page;

  if ($page ne "") {
    if (20 > length $page) {
      my $clean_sel = ${$self->trim_em(\$page)};
      $self->check_n_convert (\$clean_sel);
    }
  }
  return 0;
}


sub trim_em {
  my ($self, $refs) = @_;
  my $deref = $$refs;

  $deref =~ s/\([^)]*\)//g;             # strip (1) from printf(1)
  $deref =~ s/(?!\-|\.)[[:punct:]]//g;  # strip [\$#@~!&*()\[\];,:?^`\\\/]+;

  return \$deref;
}


sub send_notif {
  my ($self, $refs) = @_;

  my $msg = "Trying to convert $$refs";
  my $overlayz = $self->overlay_simple (0, 0, $msg);

  $self->{taimer} = urxvt::timer->new->after(3)->cb (
   sub {
     delete $self->{taimer};
     undef $overlayz;
  });

  return;
}


sub check_n_convert {
  my ($self, $refs) = @_;

  my $page = lc $$refs;
  my $pdf_dir = $ENV{HOME} . "/man2pdf";

  my $has_ext = `man -Iw $page`;
  return 0 unless (0 == $?);

  if ($has_ext =~ /\.\w+$/) {
    mkdir ($pdf_dir, 0700) unless (-d $pdf_dir);
    my $pdf = "$pdf_dir/$page.pdf";

    $self->exec_async ("man -Tpdf $page > $pdf");
    $self->send_notif (\$pdf);
    return 1;
  }
  return 0;
}


sub scan_line {
  my ($self, $refs) = @_;
  my $deref = $$refs;

  return 0 unless $deref =~ /\([^)]*\)/;
  $deref = ${$self->trim_em($refs)};

  my @arr = split(/\s+/, $deref);
  my $page = $arr[$#arr] ? lc $arr[$#arr] : "";
  return 0 if $page eq "";

  # the LESS pager line makes it easy for us
  if ($page =~ /\d+$/) {
    my @new_arr = split(" ", join(" ", @arr)); # strip left-right space
    if (lc $new_arr[0] eq "manual" and lc $new_arr[3] eq "line") {
      $page = lc $new_arr[2];
    }
  }
  return 0 if $page eq ".";

  return $self->check_n_convert (\$page);
}

urxvt-pasta - It allows the user to bind different keyboard shortcut than the default ones to paste from the clipboard without using xclip and xsel, utilizing only the default subroutines provided by urxvt itself.

Same installation as the previous extension with the following difference:

do not omit selection-to-clipboard
URxvt.perl-ext-common           : selection-to-clipboard,pasta

# keyboard shortcut to trigger the lib
URxvt.keysym.Control-Shift-V    : perl:pasta:paste

The extension itself:

#! /usr/bin/env perl
# Author:   Aaron Caffrey
# Website:  https://github.com/wifiextender/urxvt-pasta
# License:  GPLv3

# Usage: put the following lines in your .Xdefaults/.Xresources:
# URxvt.perl-ext-common           : selection-to-clipboard,pasta
# URxvt.keysym.Control-Shift-V    : perl:pasta:paste

use strict;
use warnings;

sub on_user_command {
  my ($self, $cmd) = @_;
  if ($cmd eq "pasta:paste") {
    $self->selection_request (urxvt::CurrentTime, 3);
  }
  ()
}

---

Updated the urxvt-man2pdf code.

Last edited by usersandbox (2016-11-07 14:41:01)

Offline

#2 2016-11-04 16:28:54

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,774

Re: Contrib: 3 urxvt-extensions

Very nice.  And welcome to Arch Linux.

Although, I see you are using the penultimate Linux distribution tongue


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#3 2016-11-05 19:10:31

usersandbox
Member
Registered: 2016-11-04
Posts: 3

Re: Contrib: 3 urxvt-extensions

ewaller wrote:

Very nice.  And welcome to Arch Linux.

Although, I see you are using the penultimate Linux distribution tongue

Been using Arch for 2 years and in August 2015 switched to FreeBSD then to ended up with Gentoo. For past three months I had to use several BSD flavours while developing on of my programs. I did started programming when I used Arch, so the nostalgy to come back and contribute to the community that I was loyal for so long is inevitable part of me (wink wink Jason). I'm more than sure that Jason can recognize the man page colours, and bet that he uses the urxvt-man2pdf extension.

Offline

#4 2016-11-05 23:40:56

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 19,774

Re: Contrib: 3 urxvt-extensions

usersandbox wrote:

Been using Arch for 2 years and in August 2015 switched to FreeBSD then to ended up with Gentoo.

I love both Gentoo and Arch.  It had always been a toss up as to which one to use -- of course, lately, I am a bit invested in Arch.


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way

Offline

#5 2016-11-07 15:28:33

usersandbox
Member
Registered: 2016-11-04
Posts: 3

Re: Contrib: 3 urxvt-extensions

ewaller wrote:
usersandbox wrote:

Been using Arch for 2 years and in August 2015 switched to FreeBSD then to ended up with Gentoo.

I love both Gentoo and Arch.  It had always been a toss up as to which one to use -- of course, lately, I am a bit invested in Arch.

So true indeed. I reached a point where I wanted to have more control over the programs/libraries features.

---

Anyway, I have two ideas about writing another urxvt extension.

Idea 1: Simple terminal multiplexer based on tabbede/tabbedex.

Idea 2: Colour scheme switcher FgziwOX.png targeting newer users that are not sure what colours they want to use.

But everything comes down to the user base, what does the people want ?

Offline

#6 2016-11-07 16:20:31

chickenPie4tea
Member
Registered: 2012-08-21
Posts: 309

Re: Contrib: 3 urxvt-extensions

I think the tabbed idea is cool for people who don't want to use tmux


You can like linux without becoming a fanatic!

Offline

Board footer

Powered by FluxBB