Changeset 5 in code for trunk


Ignore:
Timestamp:
Dec 13, 2022, 1:18:40 PM (2 years ago)
Author:
koizumi.aoi
Message:

Ready for 1.1

Signed-off-by: Aoi K <koizumi.aoi@…>

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Makefile

    r3 r5  
     1PREFIX=/usr/local
     2
    13build:
    24        go build
    35clean:
    46        rm -f stcli-go
     7install: build
     8        install -Dm0755 stcli-go ${PREFIX}/bin/stcli-go
     9uninstall:
     10        rm -f ${PREFIX}/bin/stcli-go
     11        rm -f ${PREFIX}/share/man/man1/stcli-go.1
  • trunk/main.go

    r4 r5  
     1// $KyokoNet: stcli-go,v 1.1 2022/12/13 10:07:00 akoizumi Exp
     2// Command line client for SimplyTranslate, a privacy friendly frontend to Google Translate
    13package main
    24
     
    1012        "os"
    1113)
    12 
    1314var (
    14         instanceURL string
    15         engine      string
    16         fromLang    string
    17         toLang      string
    18         text        string
     15        engine string
     16        from string
     17        instance string
     18        input string
     19        to string
    1920)
    20 
    21 type TranslateAPI struct {
    22         OutText string `json:"translated-text"`
     21type Translate struct {
     22        Output string `json:"translated-text"`
    2323}
    24 
    2524func init() {
    26         flag.StringVar(&instanceURL, "i", "https://translate.bus-hit.me/api/translate/", "Instance for SimplyTranslate (default: https://translate.bus-hit.me)")
    27         flag.StringVar(&engine, "e", "google", "Translation engine (default: google)")
    28         flag.StringVar(&fromLang, "f", "auto", "Source language (default: auto)")
    29         flag.StringVar(&toLang, "t", "", "Target language (default: unset)")
    30         flag.StringVar(&text, "T", "", "Text to translate (default: unset)")
     25        flag.StringVar(&engine, "e", "google", "Translation engine to use (default: google)")
     26        flag.StringVar(&from, "f", "auto", "Set the language to translate from. This can be skipped as it will autodetect the language you're translating from")
     27        flag.StringVar(&instance, "i", "https://simplytranslate.org/api/translate/", "Instance to use (default: https://simplytranslate.org/api/translate/)")
     28        flag.StringVar(&input, "I", "", "Enter the text to be translated")
     29        flag.StringVar(&to, "t", "en", "Set the language to translate to (default: en)")
    3130}
    3231func main() {
     32        // Begin flag parsing
    3333        flag.Parse()
    34         if len(text) == 0 || len(toLang) == 0 {
     34        // Check if any of those two variables is empty.
     35        // It actually needs the two to have content.
     36        if len(input) == 0 || len(to) == 0 {
    3537                log.Fatal("Missing either the text or the target language.")
    3638                os.Exit(1)
    3739        }
    38         var o TranslateAPI
    39         var queryURL = instanceURL + "?engine=" + engine + "&from=" + fromLang + "&to=" + toLang + "&text=" + text
     40        // Map a variable to the struct
     41        var translate Translate
     42        // Build the full URL to query
     43        var queryURL = instance + "?engine=" + engine + "&from=" + from + "&to=" + to + "&text=" + input
     44        // Begin the request and process the response
    4045        req, err := http.Get(queryURL)
    4146        sanityCheck(err)
    4247        defer req.Body.Close()
    4348        resp, err := io.ReadAll(req.Body)
    44         _ = json.Unmarshal([]byte(resp), &o)
     49        _ = json.Unmarshal([]byte(resp), &translate)
    4550        sanityCheck(err)
    46         fmt.Printf("Input: %s (%s)\n",text,fromLang)
    47         fmt.Printf("Output: %s (%s)\n",o.OutText,toLang)
     51        // Pretty-print both the input and the output given.
     52        fmt.Printf("Input: %v\n", input)
     53        fmt.Printf("Output: %v\n",translate.Output)
    4854}
    4955func sanityCheck(err error) {
  • trunk/stcli-go.1

    r3 r5  
    1 .Dd Aftermath 55, 3188
     1.Dd Aftermath 56, 3188
    22.Dt STCLI-GO 1
    33.Os
     
    1010.Fl f Ar from
    1111.Fl i Ar instance
     12.Fl I Ar input
    1213.Fl t Ar to
    13 .Fl T Ar text
    1414.Sh DESCRIPTION
    1515Self-explanatory, besides, this was made as
     
    1717and awk for dependencies. It fully serves
    1818as a drop-in replacement.
     19.Sh USAGE
     20.Bl -tag -width 11n -compact
     21.It -e
     22Translation engine to use
     23.It -f
     24Input language to translate from
     25.It -i
     26Instance to use
     27.It -I
     28Text to translate
     29.It -t
     30Target language to translate to
     31.El
    1932.Sh AUTHORS
    2033.An Aoi K. Aq Mt koizumi.aoi@kyoko-project.wer.ee
     
    2235.An Czar of KST Aq Mt czar@kalli.st
    2336.Pp
    24 .An mutefall
    25 .Pp
    2637.An Shokara Kou Aq Mt kou@clearnet.fqdn
    2738.Pp
    2839.An Baobab Aq Mt baobab@honeypot.im
     40.Sh BUGS
     41This utility cannot translate strings
     42separated by spaces.
Note: See TracChangeset for help on using the changeset viewer.