Compare commits

...

2 Commits

Author SHA1 Message Date
Adolfo Perez Alvarez 3e32f1b04e pick - Select one entry from stdin and/or argv
Pick will take stdin and all command line arguments and display a
ksh-like selection menu will all input lines/arguments.
2023-09-13 14:33:21 +02:00
Adolfo Perez Alvarez 34cfc3cf12 scmfmt - Use par instead of fmt and increase default width
The `-p` option is only available in OpenBSD. As there's no standard
way of ignoring lines by prefix, use `par`, which at least will be
installable almost everywhere.
2023-09-12 17:02:57 +02:00
3 changed files with 62 additions and 2 deletions

35
pick Executable file
View File

@ -0,0 +1,35 @@
#!/usr/bin/env perl
use strict;
use warnings;
use Scalar::Util qw{looks_like_number};
my @choices;
for (@ARGV) {
if (/^--$/) {
push(@choices, <STDIN>);
} elsif (/^@(.+)$/) {
open(my $fh, '<', $1);
push(@choices, <$fh>);
close($fh);
} else {
push(@choices, "$_\n");
}
}
open(my $tty, '<', '/dev/tty');
my $sel;
until (looks_like_number($sel) && $sel > 0 && $sel <= @choices) {
while (my ($i, $c) = each @choices) {
print $i+1 . ') ' . $choices[$i];
}
print $ENV{'PS3'} || '#? ';
exit(1) unless defined($sel = <$tty>);
}
close($tty);
print $choices[$sel - 1];

25
pick.1 Normal file
View File

@ -0,0 +1,25 @@
.Dd Sep 13, 2023
.Dt TS 1
.Os
.Sh NAME
.Nm pick
.Nd Choose interactively from the set of arguments and input lines
.Sh SYNOPSIS
.Nm
.Ar arg ...
.Sh DESCRIPTION
.Nm
will display a Korn shell style selection menu with stdin contents and
arguments as options.
.Pp
When a selection is made, the command will print its value to stdout.
If the selection is aborted, nothing will be printed.
.Sh ENVIRONMENT
.Bl -tag -width PS3
.It Ev PS3
Prompt string displayed to the user (default: #?).
.El
.Sh EXIT STATUS
.Ex -std
.Sh AUTHORS
.An Adolfo Perez Alvarez Aq Mt adolfopa@sdf.org

4
scmfmt
View File

@ -21,9 +21,9 @@ i\
bl' |
sed -e '1s/^/./' -e 's/^ /./' |
fmt -p |
par -P=. |
awk '
BEGIN { LIMIT = 65 }
BEGIN { LIMIT = 72 }
NR == 1 || /^[^\.]/ || length($0) < LIMIT {
sub(/^\./, NR == 1 ? "" : " ");
print;