Debian Codegraph @

eg/histexamp

raw
1 #!/usr/bin/env perl
2 #
3 # histexamp -- history library example program.
4 # https://tiswww.case.edu/php/chet/readline/history.html#History-Programming-Example
5 #
6 # Copyright (C) 2024 Hiroo Hayashi
7 #
8 # Derived from: examples/histexamp.c in the GNU Readline Library
9 # Copyright (C) 1987-2009 Free Software Foundation, Inc.
10
11 use strict;
12 use warnings;
13 use Term::ReadLine;
14 use POSIX qw(strftime);
15
16 my $t = new Term::ReadLine 'histexamp';
17 my $a = $t->Attribs;
18
19 my $done = 0;
20 $t->using_history();
21 $| = 1; # autoflush
22 while (!$done) {
23 printf('history$ ');
24 my $line = <>;
25 $line = 'quit' unless $line;
26 chomp $line;
27
28 if ($line) {
29 my ($result, $expansion) = $t->history_expand($line);
30 print $expansion, "\n" if ($result);
31
32 continue if ($result < 0 || $result == 2);
33
34 $t->add_history($expansion);
35 $line = $expansion;
36 }
37
38 if ($line eq "quit") {
39 $done = 1;
40 } elsif ($line eq "save") {
41 $t->write_history("history_file");
42 } elsif ($line eq "read") {
43 $t->read_history("history_file");
44 } elsif ($line eq "list") {
45 my $i = 0;
46 for (my $i = 0; $i < $a->{history_length}; $i++) {
47 my $offset = $i + $a->{history_base};
48 my $tt = $t->history_get_time($offset);
49 my $timestr = strftime("%a %R", localtime($tt));
50 printf("%d: %s: %s\n", $offset, $timestr, $t->history_get($offset));
51 }
52 } elsif ($line =~ /^delete/) {
53 my $which;
54 if (($which) = $line =~ /^delete\s+(\d+)$/) {
55 my $entry = $t->remove_history($which - $a->{history_base});
56 if (!$entry) {
57 warn("No such entry $which\n");
58 }
59 } else {
60 warn("non-numeric arg given to `delete'\n");
61 }
62 }
63 }
64 exit 0;