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

支持了生词本:通过bash函数为kd增加了自动将查询历史记录保存到生词本的功能 #37

Open
exaithrg opened this issue Nov 20, 2024 · 6 comments

Comments

@exaithrg
Copy link

通过bash函数添加了生词本。可以将下述代码加入到.bashrc或.zshrc中,从而让kd命令能够自动将所有查询历史记录都追加保存到一个词汇表(vocabulary book)中。同时,增加了直接同步和github上的远程单词表的函数。

# 定义自己的一个github仓库用来实时更新单词表,我的是https://github.com/exaithrg/vocab_book
ExaithrgVocabBookDir=~/github/exaithrg/vocab_book
# 提供一个函数,让我在任意目录下均能直接向我的单词表github仓库提交更新
git_push_vocab_book_func() {
    local current_dir=$(pwd)
    cd "$ExaithrgVocabBookDir" || {
        echo "Error: Failed to change directory to $ExaithrgVocabBookDir"
        return 1
    }
    git add . || {
        echo "Error: Failed to add changes"
        return 1
    }
    git commit -m "auto update"
    git push
    cd "$current_dir"
    echo "Auto update completed successfully"
}
# 下面的版本虽然实现简单,但是kd自身的颜色输出会因为重定向而被删掉
# Uncolored Version
# ff_func() {
#     mkdir -p "$ExaithrgVocabBookDir"
#     kd "$@" | tee -ai "$ExaithrgVocabBookDir/vocab_book.txt"
# }
# fff_func() {
#     mkdir -p "$ExaithrgVocabBookDir"
#     kd -t "$@" | tee -ai "$ExaithrgVocabBookDir/vocab_book.txt"
# }
# 为了保证kd仍然维持带颜色、下划线和斜体等格式的输出,而只将纯文本追加保存到一个.txt文件,使用如下函数:
# Colored Version
ff_func() {
    mkdir -p "$ExaithrgVocabBookDir"
    echo "========================================================================" >> "$ExaithrgVocabBookDir/vocab_book.txt"
    unbuffer kd "$@" | tee -ai >(sed 's/\x1b\[[0-9;]*m//g' >> "$ExaithrgVocabBookDir/vocab_book.txt")
    echo "" >> "$ExaithrgVocabBookDir/vocab_book.txt"
}
fff_func() {
    mkdir -p "$ExaithrgVocabBookDir"
    echo "========================================================================" >> "$ExaithrgVocabBookDir/vocab_book.txt"
    unbuffer kd -t "$@" | tee -ai >(sed 's/\x1b\[[0-9;]*m//g' >> "$ExaithrgVocabBookDir/vocab_book.txt")
    echo "" >> "$ExaithrgVocabBookDir/vocab_book.txt"
}
alias kdl='kd -t'
# 以ff替代td命令,因为一般翻译的时候左手在键盘上,右手摸着鼠标准备按右键向终端粘贴,所以最好翻译命令能非常容易的使用左手打出来,ff非常合适
alias ff='ff_func'
alias fff='fff_func'
# 有了ffpush命令后,可以在使用一段时间的ff hello/fff hello world之后,直接使用ffpush在任意路径下更新github仓库从而将词汇表更新到远程
alias ffpush='git_push_vocab_book_func'
@Karmenzind
Copy link
Owner

感谢反馈,我加到README里面了 https://github.com/Karmenzind/kd?tab=readme-ov-file#%E7%94%9F%E8%AF%8D%E6%9C%AC

其实可以直接改成调用两次kd,第二次加上NO_COLOR,因为直接走缓存所以开销为零。这样也方便对结果做处理,比如:

ff() {
    kd $1
    txt=$(NO_COLOR=1 kd $1)
    if [[ "$txt" == Not\ found* ]]; then
        return
    fi
    echo "$txt" >> $vocabfile
    echo "============================================" >> $vocabfile
}

另外,通过kd --status可以看到cache目录,下面有查词的频率记录,你可以用来优化生词本记录。
image

@exaithrg
Copy link
Author

感谢反馈,我加到README里面了 https://github.com/Karmenzind/kd?tab=readme-ov-file#%E7%94%9F%E8%AF%8D%E6%9C%AC

其实可以直接改成调用两次kd,第二次加上NO_COLOR,因为直接走缓存所以开销为零。这样也方便对结果做处理,比如:

ff() {
    kd $1
    txt=$(NO_COLOR=1 kd $1)
    if [[ "$txt" == Not\ found* ]]; then
        return
    fi
    echo "$txt" >> $vocabfile
    echo "============================================" >> $vocabfile
}

另外,通过kd --status可以看到cache目录,下面有查词的频率记录,你可以用来优化生词本记录。 image

感谢提示,我直接把这个统计查词频率的json文件改成了软链接,这样可以同步更新到github仓库
实际上有了所有输出的.txt文本文件后处理方式就比较多了,现在先把数据都记下来,什么时候有时间了再写个py脚本做个latex book
image

@exaithrg
Copy link
Author

另外经过一段时间的实际使用,建议

alias f='kd'
alias ff='kd -t'\

按两次f才能翻译一个单词还是太麻烦了,"f 单词"翻译单词,"ff 句子"翻译句子,尽可能保证阅读时的连贯性,不把时间浪费在打字上

@exaithrg
Copy link
Author

exaithrg commented Dec 7, 2024

最终版本

ExaithrgVocabBookDir=~/github/exaithrg/vocab_book
git_pull_vocab_book_func() {
    local current_dir=$(pwd)
    cd "$ExaithrgVocabBookDir" || {
        echo "Error: Failed to change directory to $ExaithrgVocabBookDir"
        return 1
    }
    git pull
    cd "$current_dir"
    # echo "Auto update completed successfully"
}
git_push_vocab_book_func() {
    local current_dir=$(pwd)
    cd "$ExaithrgVocabBookDir" || {
        echo "Error: Failed to change directory to $ExaithrgVocabBookDir"
        return 1
    }
    git add . || {
        echo "Error: Failed to add changes"
        return 1
    }
    git commit -m "auto update"
    git push
    cd "$current_dir"
    echo "Auto update completed successfully"
}
# Version A
# "sudo apt install expect" to support command "unbuffer"
# kd_func() {
#     mkdir -p "$ExaithrgVocabBookDir"
#     echo "========================================================================" >> "$ExaithrgVocabBookDir/vocab_book.txt"
#     unbuffer kd "$@" | tee -ai >(sed 's/\x1b\[[0-9;]*m//g' >> "$ExaithrgVocabBookDir/vocab_book.txt")
#     echo "" >> "$ExaithrgVocabBookDir/vocab_book.txt"
# }
# kdl_func() {
#     mkdir -p "$ExaithrgVocabBookDir"
#     echo "========================================================================" >> "$ExaithrgVocabBookDir/vocab_book.txt"
#     unbuffer kd -t "$@" | tee -ai >(sed 's/\x1b\[[0-9;]*m//g' >> "$ExaithrgVocabBookDir/vocab_book.txt")
#     echo "" >> "$ExaithrgVocabBookDir/vocab_book.txt"
# }
# Version B
kd_func() {
    local txt
    kd "$@"
    mkdir -p "$ExaithrgVocabBookDir"
    txt=$(NO_COLOR=1 kd "$@")
    # if [[ "$txt" == Not\ found* ]]; then
    #     return
    # fi
    echo "========================================================================" >> "$ExaithrgVocabBookDir/vocab_book.txt"
    echo ">>> $@" >> "$ExaithrgVocabBookDir/vocab_book.txt"
    echo "$txt" >> "$ExaithrgVocabBookDir/vocab_book.txt"
    echo "" >> "$ExaithrgVocabBookDir/vocab_book.txt"
}
kdl_func() {
    local txt
    kd -t "$@"
    mkdir -p "$ExaithrgVocabBookDir"
    txt=$(NO_COLOR=1 kd -t "$@")
    # if [[ "$txt" == Not\ found* ]]; then
    #     return
    # fi
    echo "========================================================================" >> "$ExaithrgVocabBookDir/vocab_book.txt"
    echo ">>> $@" >> "$ExaithrgVocabBookDir/vocab_book.txt"
    echo "$txt" >> "$ExaithrgVocabBookDir/vocab_book.txt"
    echo "" >> "$ExaithrgVocabBookDir/vocab_book.txt"
}
alias kdl='kd -t'
alias f='kd_func'
alias 发='kd_func'
alias F='kd_func'
alias ff='kdl_func'
alias FF='kdl_func'
alias fff='kdl_func'
alias FFF='kdl_func'
alias ffpush='git_push_vocab_book_func'
alias ffpull='git_pull_vocab_book_func'
alias ffp='git_push_vocab_book_func'
alias ggp='git_push_vocab_book_func'
alias ffl='git_pull_vocab_book_func'

@Karmenzind
Copy link
Owner

🤣 这么多alias

@exaithrg
Copy link
Author

🤣 这么多alias

实际上用到现在只有这几个用的比较多,其他的忘删了

alias f='kd_func'
alias 发='kd_func'
alias ff='kdl_func'
alias ffp='git_push_vocab_book_func'
alias ffl='git_pull_vocab_book_func'

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