<commit_before>#!/usr/bin/perl

# There are several ANSI escape codes that can be used between the server and client for
# query/reply.  This utility shows how your current terminal responds to a particular query.

    use strict;
    use warnings;

    use Time::HiRes qw[alarm];
    #use Term::ReadKey;

    use Data::Dumper;

    use constant TIMEOUT => 1.0;        # total seconds to wait for a reply


@ARGV or die "$0 <query> [<response_end_character>]\n";

my ($query, $response_end_character) = @ARGV;

$query = perl_string_decode($query);


## cooked mode, echo off
#Term::ReadKey::ReadMode(2);
system "stty", '-icanon', '-echo', 'eol', "\001";


print $query;
$|++;


my $reply = '';
eval {
    local $SIG{ALRM} = sub { die "alarm\n" };
    alarm(TIMEOUT);
    while (1) {
        if (defined(my $c = getc())) {
            #print ".";
            $reply .= $c;
            last if (defined($response_end_character)
                  && $c eq $response_end_character);
        }
    }
};
die $@      if ($@ && $@ ne "alarm\n");


print "  query:  ", perl_string_encode($query), "\n";
print "  reply:  ", perl_string_encode($reply), "\n";


## reset tty mode before exiting
#Term::ReadKey::ReadMode(0);         
system 'stty', 'icanon', 'echo', 'eol', chr(0);
<commit_msg>make ansi_reply callable from a script<commit_after>#!/usr/bin/perl

# There are several ANSI escape codes that can be used between the server and client for
# query/reply.  This utility shows how your current terminal responds to a particular query.

    use strict;
    use warnings;

    use Time::HiRes qw[alarm];
    #use Term::ReadKey;

    use Data::Dumper;

    use constant TIMEOUT => 1.0;        # total seconds to wait for a reply


@ARGV or die "$0 [-r] <query> [<response_end_character>]\n";

my $cmdline_raw = 0;      # print the reply, and nothing else   (useful within a shell script)
$cmdline_raw = shift @ARGV    if ($ARGV[0] eq '-r');

my ($query, $response_end_character) = @ARGV;

$query = perl_string_decode($query);


## cooked mode, echo off
#Term::ReadKey::ReadMode(2);
system "stty", '-icanon', '-echo', 'eol', "\001";


# print to STDERR instead of STDOUT, so this can work with
# Bash's $(ansi_reply.pl ...) mechanism
print STDERR $query;
$|++;


my $reply = '';
eval {
    local $SIG{ALRM} = sub { die "alarm\n" };
    alarm(TIMEOUT);
    while (1) {
        if (defined(my $c = getc())) {
            #print ".";
            $reply .= $c;
            last if (defined($response_end_character)
                  && $c eq $response_end_character);
        }
    }
};
die $@      if ($@ && $@ ne "alarm\n");


if (!$cmdline_raw) {
    print "  query:  ", perl_string_encode($query), "\n";
    print "  reply:  ", perl_string_encode($reply), "\n";
} else {
    #print perl_string_encode($reply), "\n";

    # Split into words, to make it easier to injest by Bash's array-splitting behavior, or by cut(1).
    # For example:
    #       arr=($(./ansi_reply.pl -r '\e[6n' R))
    #       echo current row: ${arr[1]} column: ${arr[3]}
    #       
    # Or:
    #       row=$(./ansi_reply.pl -r '\e[6n' R | cut -f 2)
    #       col=$(./ansi_reply.pl -r '\e[6n' R | cut -f 4)
    my @a = split /(?<=\d)(?=\D)|(?<=\D)(?=\d)|(?<=[a-z])(?=[^a-z])|(?<=[^a-z])(?=[a-z])/i, $reply;
    print join("\t", map {perl_string_encode($_)} @a), "\n";
}


## reset tty mode before exiting
#Term::ReadKey::ReadMode(0);         
system 'stty', 'icanon', 'echo', 'eol', chr(0);

{'fraction_non_alphanumeric': 0.18071593533487298, 'fraction_numerical': 0.008660508083140877, 'mean_word_length': 2.7868852459016393, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7429373', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1215, 'n_words': 352}