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

added gd-translate command #28

Merged
merged 12 commits into from
Jan 10, 2025

Conversation

NicolasNewman
Copy link
Contributor

@NicolasNewman NicolasNewman commented Jan 7, 2025

I've implemented this command for my own use but you are more then welcome to merge it upstream.

Feature:

  • Added command gd-translate
    • gd-translate --sentence %GDSEARCH%
    • gd-translate --spoiler yes --to fr --sentence %GDSEARCH%
  • Powered by Argos Translate (MIT license) which can be installed and ran locally with pip

Usage:

usage: gd-translate [OPTIONS]

Translate text from Japanese to target language

OPTIONS
        --to LANG             target language (default: en)
  --sentence SENTENCE         japanese sentence to translate
   --spoiler yes/no           black out the sentence with a spoiler box (default: no)

EXAMPLES
  gd-translate --spoiler yes --sentence %GDSEARCH%
  gd-translate --spoiler yes --to fr --sentence %GDSEARCH%

Example:
With --spoiler yes
spoiler
On hover (or without --spoiler yes):
no-spoiler

@tatsumoto-ren
Copy link
Member

Hello! Thanks for the PR!

I think that launching a process and redirecting its output to a file and then reading that file is too complicated. It would be much easier to launch a subprocess and capture its stdout directly. Additionally, passing the command as a string is error-prone because even if the arguments are escaped with quotes, I can break the command if my input contains quotes too.

@NicolasNewman
Copy link
Contributor Author

What would you recommend I use for making sub processes? I also saw there was popen but that's Unix specific and still takes a string as a command.

@tatsumoto-ren
Copy link
Member

tatsumoto-ren commented Jan 8, 2025

There are many libraries that implement subprocess. For example, this one https://github.com/arun11299/cpp-subprocess.

Usage example:

#include <string>
#include <print>
#include <subprocess.hpp>

namespace sp = subprocess;

int main() {
  try {
    auto const obuf = sp::check_output({ "ls", "-la" });
    auto const output = std::string(obuf.buf.data(), obuf.length);
    std::println("output: {}", output);
  } catch (std::runtime_error const &e) {
    std::println("error: {}", e.what());
  }
}

*/

#include "precompiled.h"
#include "subprocess.hpp"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to add this file to the project. Just include it as a dependency using xmake.

auto resp = cmd_tail.communicate().first;

std::println("<div{}>", params.spoiler == "yes" ? " class=\"spoiler\"" : "");
std::println("{}", resp.buf.data());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The string may not be null-terminated.

Comment on lines 73 to 86
auto cmd_argos = Popen(
{
"argos-translate",
"-f",
"ja",
"-t",
params.to,
params.gd_word,
},
output{ PIPE }
);

auto cmd_tail = Popen({ "tail", "-n1" }, input{ cmd_argos.output() }, output{ PIPE });

auto resp = cmd_tail.communicate().first;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're not catching the exceptions that may be thrown by cpp-subprocess.

@tatsumoto-ren
Copy link
Member

tatsumoto-ren commented Jan 9, 2025

I've removed tail -n1.

I'll merge if everything is working for you.

@NicolasNewman
Copy link
Contributor Author

Everything looks good locally! Thanks for the help in fixing those issues.

@tatsumoto-ren tatsumoto-ren merged commit 709e6ca into Ajatt-Tools:main Jan 10, 2025
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

Successfully merging this pull request may close these issues.

2 participants