kd icon indicating copy to clipboard operation
kd copied to clipboard

请求添加功能(交互模式和简明模式)

Open SilverofLight opened this issue 1 year ago • 5 comments

请问可不可以添加一个像无道那样的交互模式和简明模式啊,在需要查大量单词的时候希望可以不输入kd。

SilverofLight avatar Jun 28 '24 00:06 SilverofLight

@SilverofLight :ok_hand: 简明模式我最近找时间加一下。交互模式在计划中,会晚一些。

Karmenzind avatar Jun 28 '24 03:06 Karmenzind

@SilverofLight 我让 ChatGPT 帮忙写了个 Shell 脚本,满足简单的交互功能,你可以使用它暂时顶用。然而,不知道为什么,从文件读取的内容没有办法正常翻译,全都显示 Not Found =( 。我在本地排查了半天也不明白为什么,也许 @Karmenzind 知道。(系统为 Windows 10 22H2,shell 环境是 Msys2 zsh。我尝试了 kd -t $(cat ./one_word.txt) 这样简单的命令还是没有办法调用,输出 Not Found =(。犹豫是应该为此开一个新的 Issue,又或者造成该问题的原因是系统性的,没有考虑的可能性?)

#!/bin/bash 
#
# name    : ksd.sh
# author  : ChatGPT
# date    : 2024-10-28
# version : 1.0.0
#
# This script is a simple implementation of an interactive interface with `kd`,
# the crystal clear command-line dictionary

info() {
echo "
This script is used to interact with \`kd\`, the crystal clear command line 
dictionary. Every sequence you type down here will be passed to \`kd\`. Use 
'/help' for help message.
"
}

internal_commands_help() {
echo "
Internal Commands: 

/clear to clear outputs 
/quit to quit 
/help to show this message 
"
}

print_help() {
echo "
Usage: kds.sh [OPTIONS] 

Options:

-h, --help           Show this message 
-o, --options [OPTS] Specify options to be passed to the kd command
-f, --file    [FILE] Read queries from FILE 
"
info
}

process_input() {
    local input="$1"
    
    if [[ "$input" == "/quit" ]]; then
        echo "Quit kd."
        return 1  # Return non-zero to indicate quit
    elif [[ "$input" == "/help" ]]; then
        info
        echo
        internal_commands_help
    elif [[ "$input" == "/clear" ]]; then
        clear
    else
        # Combine kd_options with the user_input and call kd
        kd $kd_options "$input"
    fi

    return 0  # Return zero to indicate continue
}

# Initialize kd_options
kd_options=""

# Parse command line arguments
while [[ "$#" -gt 0 ]]; do
    case $1 in
        -h|--help)
            print_help
            exit 0
            ;;
        -o|--options)
            if [[ "$#" -gt 1 ]]; then
                kd_options="$kd_options$2"
                shift 2
            else
                echo "Error: --kd-options requires a value."
                exit 1
            fi
            ;;
        -f|--file)
            if [[ "$#" -gt 1 ]]; then
                file_input="$2"
                shift 2
            else
                echo "Error: --file requires a filename."
                exit 1
            fi
            ;;
        *) 
            echo "Unknown option: $1"
            print_help
            exit 1
            ;;
    esac
done

info

# If a file is specified, read from it
if [[ -n "$file_input" ]]; then
    if [[ -f "$file_input" ]]; then
        while IFS= read -r line; do
            if ! process_input "$line"; then
                break
            fi
        done < "$file_input"
    else
        echo "Error: File '$file_input' not found."
        exit 1
    fi
else
    while true; do
        echo "" # Always enter a space line
        read -p "KD $kd_options >" user_input
        
        if ! process_input "$user_input"; then
            break
        fi
    done
fi

GitHubonline1396529 avatar Oct 27 '24 19:10 GitHubonline1396529

@GitHubonline1396529 谢谢反馈!我有空研究一下

Karmenzind avatar Oct 29 '24 14:10 Karmenzind

@Karmenzind 我在考虑是否应该创建单独的 Issue 反馈文件读写的有关问题,考虑到问题的主题与当前的 Issue 无关。

GitHubonline1396529 avatar Oct 29 '24 18:10 GitHubonline1396529

@GitHubonline1396529 好的请创建~

Karmenzind avatar Oct 30 '24 01:10 Karmenzind