# shellcheck shell=dash

___x_cmd_ascii_table(){
    [ "$#" -gt 0 ] ||   set -- --app

    local ___X_CMD_ASCII_LANG=en
    case "$LANG" in
        zh*)    ___X_CMD_ASCII_LANG=zh
    esac

    local op="$1"; shift

    case "$op" in
        -h|--help)          ___x_cmd help -m ascii table >&2;    return 1    ;;
        --app)              ___x_cmd_ascii_table_app        "$@" ;;

        --tsv)              ___x_cmd_ascii_table_tsv        "$@" ;;
        --csv)              ___x_cmd_ascii_table_csv        "$@" ;;

        --json)             ___x_cmd_ascii_table_json       "$@" ;;
        --yml)              ___x_cmd_ascii_table_yml        "$@" ;;
        --tbl)              ___x_cmd_ascii_table_tbl        "$@" ;;
        # Given hexdigit nubmer, provide the ascii item
        0x*|0??|1??)        ___x_cmd_ascii_table___hex  "$op" ;;
        # Given something like ^A (means ctrl-A), provide the ascii item
        ^?)                 ___x_cmd_ascii_table___ctrl "$op" ;;
        # fuzzy search
        ?)                  ___x_cmd_ascii_table_csv | command grep "$op" ;;

        letter|upper|lower|num|ctrl|dev)
                            ___x_cmd_ascii_table_app    "$op"  ;;

        *)                  ___x_cmd_ascii_table_app ;;
    esac
}


# awk -f lib/handle.awk <lib/code.txt
___x_cmd_ascii_table_tbl(){
    ___x_cmd_ascii_table_csv --lang "$___X_CMD_ASCII_LANG" "$@" | ___x_cmd csv static_tab
}

# topic: all, letter, upper, lower, num, ctrl, dev, all
___x_cmd_ascii_table_csv(){
    local lang=zh; [ "$1" != --lang ] || { lang="$2"; shift 2; }

    local topic="${1:-all}"
    case "$topic" in
        all|letter|upper|lower|num|ctrl|dev) ;;
        *)
            ascii:error  --available all,letter,upper,lower,num,ctrl,dev "Invalid topic ==> $topic"
            return 1
    esac

    <"$___X_CMD_ROOT_MOD/ascii/lib/table/code.txt" \
        ___x_cmd_cmds_awk -v topic="${topic}" -v lang="${lang}"   \
            -f "$___X_CMD_ROOT_MOD/awk/lib/core.awk"        \
            -f "$___X_CMD_ROOT_MOD/awk/lib/re.awk"          \
            -f "$___X_CMD_ROOT_MOD/ascii/lib/table/tocsv.awk"
}

___x_cmd_ascii_table_tsv(){
    DELIMETER="$___X_CMD_UNSEENCHAR_011" \
        ___x_cmd_ascii_table_csv "$@"
}

___x_cmd_ascii_table_json(){
    ___x_cmd_ascii_table_csv "$1" | ___x_cmd csv tojson
}

___x_cmd_ascii_table_yml(){
    ___x_cmd_ascii_table_json "$1" | ___x_cmd j2y
}

___x_cmd_ascii_table_app(){
    if ___x_cmd_is_stdout2tty; then
        ___x_cmd_ascii_table_app_ "$@"
    else
        ___x_cmd_ascii_table_tsv --lang "$___X_CMD_ASCII_LANG" "$@"
    fi
}

___x_cmd_ascii_table_app_(){
    ___X_CMD_SHBIN_CODE='
        xrc ascii ;
        "$@"
    ' ___x_cmd antizshbin ___x_cmd_ascii_table_app___ "$@"
}

___x_cmd_ascii_table_app___(){
    ___x_cmd_ascii_table_csv --lang "$___X_CMD_ASCII_LANG" "$@" | ___x_cmd csv app --hide_index --clear
}

___x_cmd_ascii_table___hex(){
    ___x_cmd_ascii_table_csv | ___x_cmd_cmds_awk -F',' -v item="$1" '$2 == item || $3 == item { print $0 }'
}

___x_cmd_ascii_table___ctrl(){
    local item="$1"
    ___x_cmd_ascii_table_csv ctrl | command grep "ctrl-${item#^}"
}




