Skip to content

Commit

Permalink
first working version
Browse files Browse the repository at this point in the history
arrufat committed Jan 10, 2019
1 parent 15a4c99 commit 0a71b7d
Showing 3 changed files with 123 additions and 0 deletions.
7 changes: 7 additions & 0 deletions clipboard_non_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build darwin windows

package main

var hasPrimary = false

func setPrimary(enabled bool) {}
12 changes: 12 additions & 0 deletions clipboard_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// +build freebsd linux netbsd openbsd solaris dragonfly

package main

import "github.com/atotto/clipboard"

var hasPrimary = true

func setPrimary(enabled bool) {
clipboard.Primary = enabled

}
104 changes: 104 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"flag"
"fmt"
"log"

"github.com/0xAX/notificator"
"github.com/arrufat/papago"
"github.com/atotto/clipboard"
)

func main() {
known := flag.String("k", "en", "the language you already know")
learn := flag.String("l", "ko", "the language you are learning")
list := flag.Bool("list", false, "list all possible language codes")
var primary bool
if hasPrimary {
flag.BoolVar(&primary, "p", false, "use primary selection")
}
flag.Parse()

if *list {
fmt.Println("Supported language codes:")
for _, lang := range papago.SupportedLanguages() {
fmt.Printf("%s \t=> %s\n", lang.Code(), lang)
}
return
}

// notification setup
notify := notificator.New(notificator.Options{
DefaultIcon: "icon/default.png",
AppName: "Papago",
})

knownLang, err := papago.ParseLanguageCode(*known)
if err != nil {
notify.Push("Error", err.Error(), "", notificator.UR_NORMAL)
log.Fatal(err)
}
learnLang, err := papago.ParseLanguageCode(*learn)
if err != nil {
notify.Push("Error", err.Error(), "", notificator.UR_NORMAL)
log.Fatal(err)
}

// set clipboard to use selection
if primary {
setPrimary(true)
}

// read from the clipboard
text, err := clipboard.ReadAll()
if err != nil {
notify.Push("Error", err.Error(), "", notificator.UR_NORMAL)
log.Fatal(err)
}
if text == "" {
notify.Push("Error", "No text selected", "", notificator.UR_NORMAL)
return
}
log.Println("selected text:", text)

// detect language from selection
lang, err := papago.Detect(text)
if err != nil {
notify.Push("Error", "Unable to detect the language", "", notificator.UR_NORMAL)
log.Fatal(err)
}
log.Println("detected language:", lang)

// translate
var trans string
switch lang {
case knownLang:
trans, err = papago.Translate(text,
knownLang,
learnLang)
default:
trans, err = papago.Translate(text,
lang,
knownLang)
}
// handle translation errors
if err != nil {
notify.Push("Error", err.Error(), "", notificator.UR_NORMAL)
log.Fatal(err)
}
log.Println("translation:", trans)

// copy the translation to the clipboard
if primary {
setPrimary(false)
}
if err := clipboard.WriteAll(trans); err != nil {
log.Fatal(err)
}

// notify
notify.Push(fmt.Sprintf("Translating from %s: %s", lang, text), trans, "", notificator.UR_CRITICAL)

return
}

0 comments on commit 0a71b7d

Please # to comment.