From cf9227128ac1921bb3ba76b312b800e4fe9f74e6 Mon Sep 17 00:00:00 2001 From: Khronos31 <39792509+Khronos31@users.noreply.github.com> Date: Thu, 3 Mar 2022 17:04:09 +0900 Subject: [PATCH 1/7] =?UTF-8?q?unko.encode=E3=82=92=E5=B0=91=E3=81=97?= =?UTF-8?q?=E3=81=A0=E3=81=91=E6=9C=80=E9=81=A9=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/unko.encode | 54 ++++++++++++++++++++----------------------------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/bin/unko.encode b/bin/unko.encode index d3e2e35..ccbcfc9 100755 --- a/bin/unko.encode +++ b/bin/unko.encode @@ -1,7 +1,6 @@ #!/bin/bash -UNKO_OCT=(う ん こ ウ ン コ 💩 👑) - +UNKO_OCT="うんこウンコ💩👑" usage() { echo "Usage: $0 [OPTION] [FILE]" echo "Unko encode or decode FILE, or standard input to standard output." @@ -11,38 +10,29 @@ usage() { } encode() { - od -b -v -An "$1" | - tr -d ' \n' | - sed " - s/0/${UNKO_OCT[0x0]}/g; - s/1/${UNKO_OCT[0x1]}/g; - s/2/${UNKO_OCT[0x2]}/g; - s/3/${UNKO_OCT[0x3]}/g; - s/4/${UNKO_OCT[0x4]}/g; - s/5/${UNKO_OCT[0x5]}/g; - s/6/${UNKO_OCT[0x6]}/g; - s/7/${UNKO_OCT[0x7]}/g" + od -b -v -An | + sed -e "y/01234567/$UNKO_OCT/" | + tr -d ' \n' echo } decode() { - sed "s/[^うんこウンコ💩👑]//g" "$1" | - tr -d '\n' | - sed " - s/${UNKO_OCT[0x0]}/0/g; - s/${UNKO_OCT[0x1]}/1/g; - s/${UNKO_OCT[0x2]}/2/g; - s/${UNKO_OCT[0x3]}/3/g; - s/${UNKO_OCT[0x4]}/4/g; - s/${UNKO_OCT[0x5]}/5/g; - s/${UNKO_OCT[0x6]}/6/g; - s/${UNKO_OCT[0x7]}/7/g" | - sed 's/.../0& /g' | - xargs printf %02x | - xxd -p -r + printf '%b' "$( + sed "s/[^$UNKO_OCT]//g" | + sed \ + -e "s/${UNKO_OCT:0:1}/0/g" \ + -e "s/${UNKO_OCT:1:1}/1/g" \ + -e "s/${UNKO_OCT:2:1}/2/g" \ + -e "s/${UNKO_OCT:3:1}/3/g" \ + -e "s/${UNKO_OCT:4:1}/4/g" \ + -e "s/${UNKO_OCT:5:1}/5/g" \ + -e "s/${UNKO_OCT:6:1}/6/g" \ + -e "s/${UNKO_OCT:7:1}/7/g" | + sed 's/.../\\&/g' + )" } -MODE="ENCODE" +EXEC_COMMAND="encode" FILE="-" for arg in "$@"; do @@ -51,15 +41,15 @@ for arg in "$@"; do usage exit 0 ;; - "-d" | "--decode") MODE="DECODE" ;; + "-d" | "--decode") EXEC_COMMAND="decode" ;; *) FILE="$arg" ;; esac done -if [ "$MODE" = "DECODE" ]; then - decode "$FILE" +if [ "$FILE" = - ]; then + "$EXEC_COMMAND" else - encode "$FILE" + "$EXEC_COMMAND" < "$FILE" fi exit 0 From 4b1852ff8dfa9478edf3b2e626eb24b56c28e264 Mon Sep 17 00:00:00 2001 From: Khronos31 <39792509+Khronos31@users.noreply.github.com> Date: Thu, 3 Mar 2022 19:12:22 +0900 Subject: [PATCH 2/7] =?UTF-8?q?unko.encode=E3=81=AE=E5=87=BA=E5=8A=9B?= =?UTF-8?q?=E3=82=92=E4=B8=80=E9=83=A8=E7=BF=BB=E8=A8=B3=E5=AF=BE=E8=B1=A1?= =?UTF-8?q?=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/unko.encode | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/bin/unko.encode b/bin/unko.encode index ccbcfc9..4f2c3e3 100755 --- a/bin/unko.encode +++ b/bin/unko.encode @@ -1,5 +1,12 @@ #!/bin/bash +if [[ "$LANG" = zh* ]] || [[ "$LANG" = en* ]]; then + export TEXTDOMAINDIR="/usr/share/locale-langpack/${LANG%%.*}/LC_MESSAGES" +else + export TEXTDOMAINDIR="/usr/share/locale-langpack/${LANG%%_*}/LC_MESSAGES" +fi +export TEXTDOMAIN=libc + UNKO_OCT="うんこウンコ💩👑" usage() { echo "Usage: $0 [OPTION] [FILE]" @@ -49,7 +56,15 @@ done if [ "$FILE" = - ]; then "$EXEC_COMMAND" else - "$EXEC_COMMAND" < "$FILE" + if [ -f "$FILE" ]; then + "$EXEC_COMMAND" < "$FILE" + elif [ -d "$FILE" ]; then + printf '%s: %s: %s\n' "$0" "$FILE" $"Is a directory" >&2 + exit 1 + elif ! [ -e "$FILE" ]; then + printf '%s: %s: %s\n' "$0" "$FILE" $"No such file or directory" >&2 + exit 1 + fi fi exit 0 From 302bdcd51ef05f182e08b2760ce584628a776f48 Mon Sep 17 00:00:00 2001 From: Khronos31 <39792509+Khronos31@users.noreply.github.com> Date: Thu, 3 Mar 2022 19:12:59 +0900 Subject: [PATCH 3/7] =?UTF-8?q?unko.encode=E3=81=AE=E3=83=86=E3=82=B9?= =?UTF-8?q?=E3=83=88=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/unko.encode-test.bats | 73 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100755 test/unko.encode-test.bats diff --git a/test/unko.encode-test.bats b/test/unko.encode-test.bats new file mode 100755 index 0000000..66ee988 --- /dev/null +++ b/test/unko.encode-test.bats @@ -0,0 +1,73 @@ +#!/usr/bin/env bats +source functions.sh + +readonly TARGET_COMMAND="../bin/unko.encode" + +@test "-h でヘルプを出力する" { + run "$TARGET_COMMAND" -h + [ "$status" -eq 0 ] + [[ "${lines[0]}" =~ ^Usage:.* ]] + coverage "$TARGET_COMMAND" -h +} + +@test "--help でヘルプを出力する" { + run "$TARGET_COMMAND" --help + [ "$status" -eq 0 ] + [[ "${lines[0]}" =~ ^Usage:.* ]] + coverage "$TARGET_COMMAND" --help +} + +@test '引数なしのときは標準入力をエンコード' { + run "$TARGET_COMMAND" <<< うんこ + [ "$status" -eq 0 ] + [ "${lines[0]}" = "ウンウこうんこう💩ウンウこうこここウウンウこうんここウうんこ" ] + coverage "$TARGET_COMMAND" <<< うんこ +} + +@test '-d で標準入力をデコード { + run "$TARGET_COMMAND" -d <<< ウンウこうんこう💩ウンウこうこここウウンウこうんここウうんこ + [ "$status" -eq 0 ] + [ "${lines[0]}" = "うんこ" ] + coverage "$TARGET_COMMAND" -d <<< ウンウこうんこう💩ウンウこうこここウウンウこうんここウうんこ +} + +@test '--decode で標準入力をデコード { + run "$TARGET_COMMAND" --decode <<< ウンウこうんこう💩ウンウこうこここウウンウこうんここウうんこ + [ "$status" -eq 0 ] + [ "${lines[0]}" = "うんこ" ] + coverage "$TARGET_COMMAND" --decode <<< ウンウこうんこう💩ウンウこうこここウウンウこうんここウうんこ +} + +@test 'ファイルが存在しない場合エラー' { + export LANG=ja_JP.UTF-8 + run "$TARGET_COMMAND" うんこ + echo "$output" + [ "$status" -eq 1 ] + [[ "${lines[0]}" =~ .*そのようなファイルやディレクトリはありません ]] + coverage LANG=ja_JP.UTF-8 "$TARGET_COMMAND" うんこ +} + +@test 'ディレクトリの場合エラー' { + export LANG=ja_JP.UTF-8 + run "$TARGET_COMMAND" . + [ "$status" -eq 1 ] + [[ "${lines[0]}" =~ .*ディレクトリです ]] + coverage LANG=ja_JP.UTF-8 "$TARGET_COMMAND" . +} + +@test 'ファイルが存在しない場合エラー(英語版)' { + export LANG=en_US.UTF-8 + run "$TARGET_COMMAND" うんこ + echo "$output" + [ "$status" -eq 1 ] + [[ "${lines[0]}" =~ .*"No such file or directory" ]] + coverage LANG=en_US.UTF-8 "$TARGET_COMMAND" うんこ +} + +@test 'ディレクトリの場合エラー(英語版)' { + export LANG=en_US.UTF-8 + run "$TARGET_COMMAND" . + [ "$status" -eq 1 ] + [[ "${lines[0]}" =~ .*"Is a directory" ]] + coverage LANG=en_US.UTF-8 "$TARGET_COMMAND" . +} From 938f70e52399bea3224d7bb52db67225feef03fd Mon Sep 17 00:00:00 2001 From: Khronos31 <39792509+Khronos31@users.noreply.github.com> Date: Thu, 3 Mar 2022 19:29:58 +0900 Subject: [PATCH 4/7] =?UTF-8?q?unko.encode=E3=81=AE=E8=AA=AC=E6=98=8E?= =?UTF-8?q?=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 5a84bd3..17c7760 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ Commands | unko.king | Build your king shift tower. | | unko.fizzbuzz | No need to implement FizzBuzz. | | unko.ping | Ping the 💩 domain. | +| unko.encode | Encode/Decode data and print to standard output. | | unko.date | TBD | | unko.awk | TBD | | unko.xargs | TBD | From b86941f206c713630fce28ea48409bfe630077af Mon Sep 17 00:00:00 2001 From: Khronos31 <39792509+Khronos31@users.noreply.github.com> Date: Thu, 3 Mar 2022 20:19:10 +0900 Subject: [PATCH 5/7] add language-pack-ja-base --- docker/Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/docker/Dockerfile b/docker/Dockerfile index d22865b..85c8109 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -15,6 +15,7 @@ RUN echo -e $'\n\ ENV LANG ja_JP.UTF-8 RUN apt update -yqq \ && apt install -y --no-install-recommends \ + language-pack-ja-base \ toilet \ figlet \ bc \ From fefb43f03d15c410fbe60ee005adbaeb33c817f3 Mon Sep 17 00:00:00 2001 From: Khronos31 <39792509+Khronos31@users.noreply.github.com> Date: Thu, 3 Mar 2022 20:43:38 +0900 Subject: [PATCH 6/7] =?UTF-8?q?=E3=82=A8=E3=83=B3=E3=82=B3=E3=83=BC?= =?UTF-8?q?=E3=83=89=E6=B8=88=E3=81=BF=E3=81=AE=E3=83=87=E3=83=BC=E3=82=BF?= =?UTF-8?q?=E3=81=AB=E6=94=B9=E8=A1=8C=E3=81=8C=E5=90=AB=E3=81=BE=E3=82=8C?= =?UTF-8?q?=E3=81=A6=E3=82=8B=E5=A0=B4=E5=90=88=E3=82=92=E5=BF=98=E3=82=8C?= =?UTF-8?q?=E3=81=A6=E3=81=9F=E3=81=AE=E3=81=A7=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/unko.encode | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/unko.encode b/bin/unko.encode index 4f2c3e3..45f5796 100755 --- a/bin/unko.encode +++ b/bin/unko.encode @@ -35,6 +35,7 @@ decode() { -e "s/${UNKO_OCT:5:1}/5/g" \ -e "s/${UNKO_OCT:6:1}/6/g" \ -e "s/${UNKO_OCT:7:1}/7/g" | + tr -d '\n' | sed 's/.../\\&/g' )" } From 7b577c6e998e3d724a5859ea4dab0c6108dff21e Mon Sep 17 00:00:00 2001 From: Khronos31 <39792509+Khronos31@users.noreply.github.com> Date: Thu, 3 Mar 2022 20:45:25 +0900 Subject: [PATCH 7/7] =?UTF-8?q?=E3=83=86=E3=82=B9=E3=83=88=E3=81=8C?= =?UTF-8?q?=E5=A4=B1=E6=95=97=E3=81=97=E3=81=9F=E3=81=A8=E3=81=8D=E3=83=87?= =?UTF-8?q?=E3=83=BC=E3=82=BF=E3=82=92=E5=87=BA=E5=8A=9B=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/unko.encode-test.bats | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/unko.encode-test.bats b/test/unko.encode-test.bats index 66ee988..c683ab8 100755 --- a/test/unko.encode-test.bats +++ b/test/unko.encode-test.bats @@ -5,6 +5,7 @@ readonly TARGET_COMMAND="../bin/unko.encode" @test "-h でヘルプを出力する" { run "$TARGET_COMMAND" -h + echo "$output" [ "$status" -eq 0 ] [[ "${lines[0]}" =~ ^Usage:.* ]] coverage "$TARGET_COMMAND" -h @@ -12,6 +13,7 @@ readonly TARGET_COMMAND="../bin/unko.encode" @test "--help でヘルプを出力する" { run "$TARGET_COMMAND" --help + echo "$output" [ "$status" -eq 0 ] [[ "${lines[0]}" =~ ^Usage:.* ]] coverage "$TARGET_COMMAND" --help @@ -19,6 +21,7 @@ readonly TARGET_COMMAND="../bin/unko.encode" @test '引数なしのときは標準入力をエンコード' { run "$TARGET_COMMAND" <<< うんこ + echo "$output" [ "$status" -eq 0 ] [ "${lines[0]}" = "ウンウこうんこう💩ウンウこうこここウウンウこうんここウうんこ" ] coverage "$TARGET_COMMAND" <<< うんこ @@ -26,6 +29,7 @@ readonly TARGET_COMMAND="../bin/unko.encode" @test '-d で標準入力をデコード { run "$TARGET_COMMAND" -d <<< ウンウこうんこう💩ウンウこうこここウウンウこうんここウうんこ + echo "$output" [ "$status" -eq 0 ] [ "${lines[0]}" = "うんこ" ] coverage "$TARGET_COMMAND" -d <<< ウンウこうんこう💩ウンウこうこここウウンウこうんここウうんこ @@ -33,6 +37,7 @@ readonly TARGET_COMMAND="../bin/unko.encode" @test '--decode で標準入力をデコード { run "$TARGET_COMMAND" --decode <<< ウンウこうんこう💩ウンウこうこここウウンウこうんここウうんこ + echo "$output" [ "$status" -eq 0 ] [ "${lines[0]}" = "うんこ" ] coverage "$TARGET_COMMAND" --decode <<< ウンウこうんこう💩ウンウこうこここウウンウこうんここウうんこ @@ -50,6 +55,7 @@ readonly TARGET_COMMAND="../bin/unko.encode" @test 'ディレクトリの場合エラー' { export LANG=ja_JP.UTF-8 run "$TARGET_COMMAND" . + echo "$output" [ "$status" -eq 1 ] [[ "${lines[0]}" =~ .*ディレクトリです ]] coverage LANG=ja_JP.UTF-8 "$TARGET_COMMAND" . @@ -67,6 +73,7 @@ readonly TARGET_COMMAND="../bin/unko.encode" @test 'ディレクトリの場合エラー(英語版)' { export LANG=en_US.UTF-8 run "$TARGET_COMMAND" . + echo "$output" [ "$status" -eq 1 ] [[ "${lines[0]}" =~ .*"Is a directory" ]] coverage LANG=en_US.UTF-8 "$TARGET_COMMAND" .