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 | diff --git a/bin/unko.encode b/bin/unko.encode index d3e2e35..45f5796 100755 --- a/bin/unko.encode +++ b/bin/unko.encode @@ -1,7 +1,13 @@ #!/bin/bash -UNKO_OCT=(ใ† ใ‚“ ใ“ ใ‚ฆ ใƒณ ใ‚ณ ๐Ÿ’ฉ ๐Ÿ‘‘) +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]" echo "Unko encode or decode FILE, or standard input to standard output." @@ -11,38 +17,30 @@ 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" | + tr -d '\n' | + sed 's/.../\\&/g' + )" } -MODE="ENCODE" +EXEC_COMMAND="encode" FILE="-" for arg in "$@"; do @@ -51,15 +49,23 @@ 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" + 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 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 \ diff --git a/test/unko.encode-test.bats b/test/unko.encode-test.bats new file mode 100755 index 0000000..c683ab8 --- /dev/null +++ b/test/unko.encode-test.bats @@ -0,0 +1,80 @@ +#!/usr/bin/env bats +source functions.sh + +readonly TARGET_COMMAND="../bin/unko.encode" + +@test "-h ใงใƒ˜ใƒซใƒ—ใ‚’ๅ‡บๅŠ›ใ™ใ‚‹" { + run "$TARGET_COMMAND" -h + echo "$output" + [ "$status" -eq 0 ] + [[ "${lines[0]}" =~ ^Usage:.* ]] + coverage "$TARGET_COMMAND" -h +} + +@test "--help ใงใƒ˜ใƒซใƒ—ใ‚’ๅ‡บๅŠ›ใ™ใ‚‹" { + run "$TARGET_COMMAND" --help + echo "$output" + [ "$status" -eq 0 ] + [[ "${lines[0]}" =~ ^Usage:.* ]] + coverage "$TARGET_COMMAND" --help +} + +@test 'ๅผ•ๆ•ฐใชใ—ใฎใจใใฏๆจ™ๆบ–ๅ…ฅๅŠ›ใ‚’ใ‚จใƒณใ‚ณใƒผใƒ‰' { + run "$TARGET_COMMAND" <<< ใ†ใ‚“ใ“ + echo "$output" + [ "$status" -eq 0 ] + [ "${lines[0]}" = "ใ‚ฆใƒณใ‚ฆใ“ใ†ใ‚“ใ“ใ†๐Ÿ’ฉใ‚ฆใƒณใ‚ฆใ“ใ†ใ“ใ“ใ“ใ‚ฆใ‚ฆใƒณใ‚ฆใ“ใ†ใ‚“ใ“ใ“ใ‚ฆใ†ใ‚“ใ“" ] + coverage "$TARGET_COMMAND" <<< ใ†ใ‚“ใ“ +} + +@test '-d ใงๆจ™ๆบ–ๅ…ฅๅŠ›ใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ { + run "$TARGET_COMMAND" -d <<< ใ‚ฆใƒณใ‚ฆใ“ใ†ใ‚“ใ“ใ†๐Ÿ’ฉใ‚ฆใƒณใ‚ฆใ“ใ†ใ“ใ“ใ“ใ‚ฆใ‚ฆใƒณใ‚ฆใ“ใ†ใ‚“ใ“ใ“ใ‚ฆใ†ใ‚“ใ“ + echo "$output" + [ "$status" -eq 0 ] + [ "${lines[0]}" = "ใ†ใ‚“ใ“" ] + coverage "$TARGET_COMMAND" -d <<< ใ‚ฆใƒณใ‚ฆใ“ใ†ใ‚“ใ“ใ†๐Ÿ’ฉใ‚ฆใƒณใ‚ฆใ“ใ†ใ“ใ“ใ“ใ‚ฆใ‚ฆใƒณใ‚ฆใ“ใ†ใ‚“ใ“ใ“ใ‚ฆใ†ใ‚“ใ“ +} + +@test '--decode ใงๆจ™ๆบ–ๅ…ฅๅŠ›ใ‚’ใƒ‡ใ‚ณใƒผใƒ‰ { + run "$TARGET_COMMAND" --decode <<< ใ‚ฆใƒณใ‚ฆใ“ใ†ใ‚“ใ“ใ†๐Ÿ’ฉใ‚ฆใƒณใ‚ฆใ“ใ†ใ“ใ“ใ“ใ‚ฆใ‚ฆใƒณใ‚ฆใ“ใ†ใ‚“ใ“ใ“ใ‚ฆใ†ใ‚“ใ“ + echo "$output" + [ "$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" . + echo "$output" + [ "$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" . + echo "$output" + [ "$status" -eq 1 ] + [[ "${lines[0]}" =~ .*"Is a directory" ]] + coverage LANG=en_US.UTF-8 "$TARGET_COMMAND" . +}