mirror of
https://codeberg.org/aryak/mozhi
synced 2024-11-23 09:32:58 +05:30
make translateall, cli better
This commit is contained in:
parent
b7e2755ab2
commit
d92958aae0
2
TODO.md
2
TODO.md
@ -3,6 +3,8 @@
|
||||
- Make CLI usable
|
||||
- CI/CD
|
||||
- Support for disabling engines
|
||||
- Ability for user to choose engines they want to use
|
||||
- Fix DDG
|
||||
- Support for changing LibreTranslate instance
|
||||
- Add actual explanations for CLI arguments
|
||||
- Proper Error handling for requests.go
|
||||
|
53
cmd/langlist.go
Normal file
53
cmd/langlist.go
Normal file
@ -0,0 +1,53 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"codeberg.org/aryak/mozhi/utils"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/ktr0731/go-fuzzyfinder"
|
||||
)
|
||||
|
||||
var (
|
||||
engineused string
|
||||
listtype string
|
||||
raw bool
|
||||
)
|
||||
|
||||
// langlistCmd represents the langlist command
|
||||
var langlistCmd = &cobra.Command{
|
||||
Use: "langlist",
|
||||
Short: "List/select languages supported by an engine.",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
list, err := utils.LangList(engineused, listtype)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
idxs, err := fuzzyfinder.FindMulti(
|
||||
list,
|
||||
func(i int) string {
|
||||
return list[i].Name
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
for _, idx := range idxs {
|
||||
if raw == true {
|
||||
fmt.Println(list[idx].Id)
|
||||
} else {
|
||||
fmt.Println("Selected Language:", list[idx].Id, "("+list[idx].Name+")")
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(langlistCmd)
|
||||
translateCmd.Flags().SortFlags = false
|
||||
|
||||
langlistCmd.Flags().StringVarP(&engineused, "engine", "e", "", "[google|libre|reverso|deepl|watson|yandex|mymemory|duckduckgo]")
|
||||
langlistCmd.Flags().StringVarP(&listtype, "type", "t", "", "[sl|tl] Choose language for source or target")
|
||||
langlistCmd.Flags().BoolVarP(&raw, "raw", "r", false, "Return only selected language code.")
|
||||
|
||||
langlistCmd.MarkFlagRequired("engine")
|
||||
langlistCmd.MarkFlagRequired("type")
|
||||
}
|
@ -2,7 +2,6 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/ktr0731/go-fuzzyfinder"
|
||||
"codeberg.org/aryak/mozhi/utils"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@ -13,50 +12,50 @@ var (
|
||||
query string
|
||||
source string
|
||||
dest string
|
||||
langlist string
|
||||
)
|
||||
|
||||
var translateCmd = &cobra.Command{
|
||||
Use: "translate",
|
||||
Short: "Translate.",
|
||||
Long: `Translate.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if langlist == "sl" || langlist == "tl" {
|
||||
list, err := utils.LangList(engine, langlist)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
if engine == "all" {
|
||||
data := utils.TranslateAll(dest, source, query)
|
||||
for i := 0; i < len(data); i++ {
|
||||
fmt.Println("-----------------------------------")
|
||||
fmt.Println("Engine: "+data[i].Engine)
|
||||
fmt.Println("Translated Text: "+data[i].OutputText)
|
||||
if source == "auto" {
|
||||
fmt.Println("Detected Language: "+data[i].AutoDetect)
|
||||
}
|
||||
fmt.Println("Source Language: "+data[i].SourceLang)
|
||||
fmt.Println("Target Language: "+data[i].TargetLang)
|
||||
}
|
||||
idxs, err := fuzzyfinder.FindMulti(
|
||||
list,
|
||||
func(i int) string {
|
||||
return list[i].Name
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
for _, idx := range idxs {
|
||||
fmt.Println("Selected Language:", list[idx].Id, "("+list[idx].Name+")")
|
||||
}
|
||||
} else if engine == "all" {
|
||||
fmt.Println(utils.TranslateAll(dest, source, query))
|
||||
} else {
|
||||
fmt.Println(utils.Translate(engine, dest, source, query))
|
||||
data, err := utils.Translate(engine, dest, source, query)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println("Translated Text: "+data.OutputText)
|
||||
if source == "auto" {
|
||||
fmt.Println("Detected Language: "+data.AutoDetect)
|
||||
}
|
||||
fmt.Println("Source Language: "+data.SourceLang)
|
||||
fmt.Println("Target Language: "+data.TargetLang)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(translateCmd)
|
||||
translateCmd.Flags().SortFlags = false
|
||||
|
||||
translateCmd.Flags().StringVarP(&engine, "engine", "e", "", "The source Mozhi will listen to. Defaults to 3000, and overrides the MOZHI_source environment variable.")
|
||||
translateCmd.Flags().StringVarP(&source, "source", "s", "", "The source Mozhi will listen to. Defaults to 3000, and overrides the MOZHI_source environment variable.")
|
||||
translateCmd.Flags().StringVarP(&dest, "dest", "t", "", "The dest Mozhi will listen to. Defaults to 3000, and overrides the MOZHI_dest environment variable.")
|
||||
translateCmd.Flags().StringVarP(&query, "query", "q", "", "The query Mozhi will listen to. Defaults to 3000, and overrides the MOZHI_query environment variable.")
|
||||
translateCmd.Flags().StringVarP(&langlist, "langlist", "l", "", "The query Mozhi will listen to. Defaults to 3000, and overrides the MOZHI_query environment variable.")
|
||||
translateCmd.Flags().StringVarP(&engine, "engine", "e", "", "[google|libre|reverso|deepl|watson|yandex|mymemory|duckduckgo]")
|
||||
translateCmd.Flags().StringVarP(&source, "source", "s", "", "Source language. Use langlist command to get code for your language")
|
||||
translateCmd.Flags().StringVarP(&dest, "dest", "t", "", "Target language. Use langlist command to get code for your language")
|
||||
translateCmd.Flags().StringVarP(&query, "query", "q", "", "Text to be translated")
|
||||
|
||||
engine = translateCmd.Flag("engine").Value.String()
|
||||
dest = translateCmd.Flag("dest").Value.String()
|
||||
source = translateCmd.Flag("source").Value.String()
|
||||
query = translateCmd.Flag("query").Value.String()
|
||||
langlist = translateCmd.Flag("query").Value.String()
|
||||
translateCmd.MarkFlagRequired("engine")
|
||||
translateCmd.MarkFlagRequired("source")
|
||||
translateCmd.MarkFlagRequired("dest")
|
||||
translateCmd.MarkFlagRequired("query")
|
||||
}
|
||||
|
@ -336,15 +336,13 @@ func TranslateDuckDuckGo(to string, from string, query string) (LangOut, error)
|
||||
}
|
||||
|
||||
func TranslateAll(to string, from string, query string) []LangOut {
|
||||
reverso, _ := TranslateReverso(to, from, query)
|
||||
google, _ := TranslateGoogle(to, from, query)
|
||||
libretranslate, _ := TranslateLibreTranslate(to, from, query)
|
||||
watson, _ := TranslateWatson(to, from, query)
|
||||
mymemory, _ := TranslateMyMemory(to, from, query)
|
||||
yandex, _ := TranslateYandex(to, from, query)
|
||||
deepl, _ := TranslateDeepl(to, from, query)
|
||||
duckduckgo, _ := TranslateDuckDuckGo(to, from, query)
|
||||
|
||||
langout := []LangOut{reverso, google, libretranslate, watson, mymemory, yandex, deepl, duckduckgo}
|
||||
engines := []string{"reverso", "google", "libre", "watson", "mymemory", "yandex", "deepl", "duckduckgo"}
|
||||
langout := []LangOut{}
|
||||
for i := 0; i < len(engines); i++ {
|
||||
data, err := Translate(engines[i], to, from, query)
|
||||
if err == nil {
|
||||
langout = append(langout, data)
|
||||
}
|
||||
}
|
||||
return langout
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user