<commit_before>use strict;
use warnings;

package AngelPS1::Shell::tcsh;


sub ps1_escape
{
    # csh special symbols
    (my $s = $_[1]) =~ s{%}{%%}gs;
    # csh special sequences for special chars, à la bindkey
    $s =~ s{\\}{\\\\}gs;
    $s =~ s{\^}{\\136}gs;
    # csh special: history number
    $s =~ s{!}{\\041}gs;
    # \n must be escaped as tcsh replaces it with space in backquote (`) output
    $s =~ s{\n}{\\n}gs;
    #print STDERR "Escape [$_[1]] => [$s]\n";
    $s
}

sub ps1_invisible
{
    shift; # $class
    (\'%{', @_, \'%}')
}

sub ps1_finalize
{
    my $prompt = $_[1];
    $prompt =~ s/'/'\\''/g;
    # tcsh replaces '\n' with a space in command substitution (backquotes: `)
    # so '\n' will be lost in our current implementation
    # TODO: try to replace with '\\n'
    warn "Prompt has '\\n' This is not supported by angel-PS1!" if $prompt =~ /\n/;
    qq{set prompt = '$prompt'};
}

sub shell_code_dynamic
{
    my ($class, %options) = @_;
    my ($DEBUG, $NAME, $IN, $OUT, $PID, $env) =
        @options{qw<debug name in out pid env>};

    # Reference for this csh shit:
    # - http://www.grymoire.com/Unix/CshTop10.txt
    # - http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

    # Note that because of bug http://bugs.gw.com/view.php?id=259
    # we can not use if/else/endif in aps1_precmd because that
    # pollutes the history, so we can't do check of existence of
    # $IN (see previous implementation in the history)

    my $shell_code = <<EOF;
if ( \${?aps1_name} ) then
    eval \$aps1_name leave
endif
set aps1_prompt = \$prompt:q
set aps1_precmd = '\\
echo -n "?=\$?:q\\0PWD=\$PWD:q" > $IN\\
eval "`cat $OUT`"'
alias precmd \$aps1_precmd:q
alias $NAME 'switch ( \\!* )\\
    case leave:\\
    case quit:\\
        set prompt = "\$aps1_prompt:q"\\
        kill \$aps1_pid\\
        rm -f -- $IN $OUT\\
        unset aps1_prompt aps1_pid aps1_name aps1_precmd\\
        unalias precmd $NAME\\
        breaksw\\<commit_msg>tcsh: restore check of $IN<commit_after>use strict;
use warnings;

package AngelPS1::Shell::tcsh;


sub ps1_escape
{
    # csh special symbols
    (my $s = $_[1]) =~ s{%}{%%}gs;
    # csh special sequences for special chars, à la bindkey
    $s =~ s{\\}{\\\\}gs;
    $s =~ s{\^}{\\136}gs;
    # csh special: history number
    $s =~ s{!}{\\041}gs;
    # \n must be escaped as tcsh replaces it with space in backquote (`) output
    $s =~ s{\n}{\\n}gs;
    #print STDERR "Escape [$_[1]] => [$s]\n";
    $s
}

sub ps1_invisible
{
    shift; # $class
    (\'%{', @_, \'%}')
}

sub ps1_finalize
{
    my $prompt = $_[1];
    $prompt =~ s/'/'\\''/g;
    # tcsh replaces '\n' with a space in command substitution (backquotes: `)
    # so '\n' will be lost in our current implementation
    # TODO: try to replace with '\\n'
    warn "Prompt has '\\n' This is not supported by angel-PS1!" if $prompt =~ /\n/;
    qq{set prompt = '$prompt'};
}

sub shell_code_dynamic
{
    my ($class, %options) = @_;
    my ($DEBUG, $NAME, $IN, $OUT, $PID, $env) =
        @options{qw<debug name in out pid env>};

    # Reference for this csh shit:
    # - http://www.grymoire.com/Unix/CshTop10.txt
    # - http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

    # Note that because of bug http://bugs.gw.com/view.php?id=259
    # we can not use if/else/endif in aps1_precmd because that
    # pollutes the history, so we can't do check of existence of
    # $IN (see previous implementation in the history)

    my $shell_code = <<EOF;
if ( \${?aps1_name} ) then
    eval \$aps1_name leave
endif
set aps1_prompt = \$prompt:q
set aps1_precmd = 'set aps1_err = \$?\\
if ( -p $IN ) then\\
    echo -n "?=\$aps1_err:q\\0PWD=\$PWD:q" > $IN\\
    eval "`cat $OUT`"\\
endif'
alias precmd 'eval \$aps1_precmd:q'
alias $NAME 'switch ( \\!* )\\
    case leave:\\
    case quit:\\
        set prompt = "\$aps1_prompt:q"\\
        kill \$aps1_pid\\
        rm -f -- $IN $OUT\\
        unset aps1_prompt aps1_pid aps1_name aps1_precmd\\
        unalias precmd $NAME\\
        breaksw\\
{'fraction_non_alphanumeric': 0.178269517652909, 'fraction_numerical': 0.016409746394828444, 'mean_word_length': 2.468103448275862, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1750582', 'n_tokens_mistral': 1618, 'n_tokens_neox': 1553, 'n_words': 441}