Skip to content

Commit

Permalink
echo-wrap: awk/mawk compat
Browse files Browse the repository at this point in the history
/close #249 #239
  • Loading branch information
balupton committed Sep 16, 2024
1 parent e68e2e0 commit 7deab4e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
4 changes: 4 additions & 0 deletions commands/echo-regexp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const sep = newlines ? '\n' : ''
const global = flags.has('g')
const globalFlags = new Set([...Array.from(flags), 'g'])
// rust regex to js regex
// > sd '[:blank:]' '_' <<< 'hello world'
// he__o wor_d
// > sd '[[:blank:]]' '_' <<< 'hello world'
// hello_world
const find = (Deno.args[1] || '')
.replaceAll('(?P<', '(?<')
.replaceAll('[[:alnum:]]', '[0-9A-Za-z]')
Expand Down
10 changes: 6 additions & 4 deletions commands/echo-wrap
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function echo_wrap_test() (
echo-style --h1="TEST: $0"

local input expected
input=$'123456123456 123456 \n1 2 3\t4 5 6 7\t8 9\n\n 1 2 3\t4 5 7\n\t8\t\n9\n'
input=$'123456123456 123456 \n1 2 3\t4 5 6 7\t8 9\n\n 1 2 3\t4 5 7\n\t8\t\n9\n\n'"$(echo-style --green='1' --red='2' --blue='3' --yellow='4' --magenta='5' --cyan='6' --white='7' --black='8' --reset='9')"
expected="$(
cat <<-EOF
123456
Expand All @@ -22,6 +22,7 @@ function echo_wrap_test() (
8
9
$(echo-style --green='1' --red='2' --blue='3' --yellow='4' --magenta='5' --cyan=$'6\n' --white='7' --black='8' --reset='9')
EOF
)"
eval_tester --name='echo-wrap is working as expected' --stdout="$expected" \
Expand Down Expand Up @@ -51,7 +52,8 @@ function echo_wrap() (
$(stdinargs_options_help --)
QUIRKS:
Tabs are converted into 4 spaces. Broken spacing (spacing at the end or start of a segment) is trimmed. Spacing is preserved at the start and end of input lines.
Tabs are converted into 4 spaces. Broken spacing (spacing at the end or start of a segment) is trimmed. Spacing is preserved at the start and end of input lines. Backticks are trimmed as they are used internally as a delimiter.
EOF
return 22 # EINVAL 22 Invalid argument
}
Expand Down Expand Up @@ -86,15 +88,15 @@ function echo_wrap() (
# Dependencies

local bin_gawk_or_awk awk_script
bin_gawk_or_awk="$(echo-gnu-command --install --no-fallback -- gawk)"
bin_gawk_or_awk="$(echo-gnu-command --install -- gawk)"
awk_script="$(type -P echo-wrap.awk)"

# =====================================
# Action

local buffer=''
function on_line {
buffer+="${1}"
buffer+="${1}\`"
}
function on_inline {
buffer+="$1"
Expand Down
22 changes: 15 additions & 7 deletions commands/echo-wrap.awk
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
# Signficantly modified to:
# - include all the ANSI escape codes that Dorothy is aware of
# - support tabs
# don't use l as that results in: awk: towc: multibyte conversion failure on
BEGIN {
line_regex=""
ansi_regex="[[:cntrl:]][0-9;[?]*[ABCDEFGHJKSTfhlmnsu]"
line_regex="[`]"
tab_parts_regex="[`]"
tab_replacement="````"
ansi_regex="[[:cntrl:]][[0-9;?]*[ABCDEFGHJKSTfhlmnsu]"
}
{
file=$0
output=""
split(file, lines, line_regex)
for (i=1; i<=length(lines); i++) {
line=lines[i]
gsub(/\t/, "����", line)
gsub(/\t/, tab_replacement, line)
# repeatedly strip off "option_wrap_width" characters until we have processed the entire line
do {
columns=option_wrap_width
Expand All @@ -23,7 +26,12 @@ BEGIN {
if (RSTART && RSTART <= columns) {
segment=segment substr(line, 1, RSTART + RLENGTH - 1)
line=substr(line, RSTART + RLENGTH)
columns=columns - (RSTART > 1 ? RSTART - 1 : 0)
# don't use conditionals, as that fails with awk
if (RSTART > 1) {
columns=columns - (RSTART - 1)
} else {
columns=columns - 0
}
}
else {
segment=segment substr(line, 1, columns)
Expand All @@ -32,8 +40,8 @@ BEGIN {
}
}
# remove breaks (tabs, spaces)
gsub(/[ ]+$/, "", segment)
gsub(/^[ ]+/, "", line)
gsub(/[` ]+$/, "", segment)
gsub(/^[` ]+/, "", line)
# save the processed line
output=output segment "\n"
# if ( line != "" ) {
Expand All @@ -45,7 +53,7 @@ BEGIN {
# remove the final newline from the concat above
gsub(/\n$/, "", output)
# remove intermediate tabs
gsub(//, " ", output)
gsub(tab_parts_regex, " ", output)
# done
printf("%s", output)
}
8 changes: 8 additions & 0 deletions commands/eval-tester
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ function eval_tester() (
--header3='<expected stdout>' $'\n' \
"$option_stdout" $'\n' \
--header3='</expected stdout>'
if [[ $option_stdout =~ [[:cntrl:]] ]] || [[ $stdout =~ [[:cntrl:]] ]]; then
echo-style --header3='<actual escaped stdout>' $'\n' \
"$(printf '%q' "$stdout")" $'\n' \
--header3='</actual escaped stdout>' $'\n' \
--header3='<expected escaped stdout>' $'\n' \
"$(printf '%q' "$option_stdout")" $'\n' \
--header3='</expected escaped stdout>'
fi
else
echo-style --green="Actual Stdout == Expected Stdout"
fi
Expand Down

0 comments on commit 7deab4e

Please sign in to comment.