soimort / translate-shell

:speech_balloon: Command-line translator using Google Translate, Bing Translator, Yandex.Translate, etc.
https://www.soimort.org/translate-shell
The Unlicense
6.83k stars 387 forks source link

New feature advice: collect history to an output text file #487

Open deepcomp2023 opened 1 year ago

deepcomp2023 commented 1 year ago

First of all this tool is great for work under shell, thanks for your great job!

While for most of people, we often need to get the past query history for review. Could you add such as feature that collection the history input and output to a text file? And hopefully this is configurable but not inputting the filename every time.

Just for your reference and thanks.

guites commented 12 months ago

edit: I created a version that uses sqlite3: https://github.com/guites/transl

Hey, I'm doing just what you said by wrapping my calls to trans in another script, which in turn calls the original trans program, allowing me to save what I've used as inputs and the output to a separate file.

Here's an example using Python

# trans.py
import subprocess
import sys

# switch the first argument (the .py filename)
# with a call to the actual `trans` executable
args = ["trans"] + sys.argv[1:]

result = subprocess.run(
    args,
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
    text=True
)

# print the output as expected
print(result.stdout)

# save input and output to a file
with open("logs.txt", "a") as file:
    file.write(str(args) + "\n\n")
    file.write(result.stdout)
    file.write("\n\n=========================\n")

you can then call python3 trans.py instead of trans, ex.

python3 trans_wrap.py fr: "je suis français"
python3 trans_wrap.py pt-br:en "e é assim que fica o script" -speak

and the output would be saved to "logs.txt"

cat logs.txt

['trans', 'fr:', 'je suis français']

je suis français

I am French

Translations of je suis français
[ Français -> English ]

je suis français
    I am French, I'm French

=========================
['trans', 'pt-br:en', 'e é assim que fica o script', '-speak']

e é assim que fica o script

and this is how the script looks

Translations of e é assim que fica o script
[  -> English ]

e é assim que fica o script
    , and this is how the script looks like

=========================

:)