Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows 上无法处理文件中的 DOS 换行符 #35

Open
GitHubonline1396529 opened this issue Oct 30, 2024 · 2 comments
Open

Windows 上无法处理文件中的 DOS 换行符 #35

GitHubonline1396529 opened this issue Oct 30, 2024 · 2 comments

Comments

@GitHubonline1396529
Copy link

环境

  • 操作系统:Windows 10 22H2,
  • Shell 环境:MSYS 2 Zsh
  • 脚本执行环境:MSYS 2 Bash

描述

我在 Issue #30 中将问题描述为“从文件中读取单词通过标准输入输出流调用 kd 会导致单词查询失败,kd 似乎只接受用户从控制塔输入的字符,不接受从文件读取的字符”。但我在写 Issue 的时候,进行了新一轮的排查。经过排查之后发现导致这个问题的真正原因是 kd 在 Windows 平台上无法处理文件中的 DOS 换行符。

我不知道该 Bug 是否有修复的价值,但是以防万一我还是来反馈一下。

复现方法

可以首先使用 echo "abandon" > ./one_word.txt 命令将单词写入本地文件 one_word.txt 使用:

kd -t $(cat ./one_word.txt)

命令,会发现 kd 正确翻译了单纯 abandon;但是如果在 Windows 上使用任何文本编辑器编辑 one_word.txt。然后使用 kd -t $(cat ./one_word.txt),就会发现 kd 无法翻译单词。这个时候可以尝试命令 dos2unix ./one_word.txt,再尝试就没有问题了。

我是在测试如下脚本的时候遇到问题的,该脚本也可用于反映问题。

#!/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
@Karmenzind
Copy link
Owner

谢谢反馈,这两天很可能没空,后续答复你

@GitHubonline1396529
Copy link
Author

@Karmenzind 啊,感谢您关照我提交的 Issue。我想这不是一般用户会遇到的问题,如果很忙的话完全不必着急的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants