-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Conversation
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. |
What would you recommend I use for making sub processes? I also saw there was |
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());
}
} |
src/translate.cpp
Outdated
*/ | ||
|
||
#include "precompiled.h" | ||
#include "subprocess.hpp" |
There was a problem hiding this comment.
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.
src/translate.cpp
Outdated
auto resp = cmd_tail.communicate().first; | ||
|
||
std::println("<div{}>", params.spoiler == "yes" ? " class=\"spoiler\"" : ""); | ||
std::println("{}", resp.buf.data()); |
There was a problem hiding this comment.
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.
src/translate.cpp
Outdated
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; |
There was a problem hiding this comment.
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.
46f60ec
to
7cf37c1
Compare
I've removed I'll merge if everything is working for you. |
Everything looks good locally! Thanks for the help in fixing those issues. |
I've implemented this command for my own use but you are more then welcome to merge it upstream.
Feature:
gd-translate
gd-translate --sentence %GDSEARCH%
gd-translate --spoiler yes --to fr --sentence %GDSEARCH%
Usage:
Example:


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