mirror of
https://codeberg.org/aryak/mozhi
synced 2024-12-22 12:19:53 +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
|
- Make CLI usable
|
||||||
- CI/CD
|
- CI/CD
|
||||||
- Support for disabling engines
|
- Support for disabling engines
|
||||||
|
- Ability for user to choose engines they want to use
|
||||||
|
- Fix DDG
|
||||||
- Support for changing LibreTranslate instance
|
- Support for changing LibreTranslate instance
|
||||||
- Add actual explanations for CLI arguments
|
- Add actual explanations for CLI arguments
|
||||||
- Proper Error handling for requests.go
|
- 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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/ktr0731/go-fuzzyfinder"
|
|
||||||
"codeberg.org/aryak/mozhi/utils"
|
"codeberg.org/aryak/mozhi/utils"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -13,50 +12,50 @@ var (
|
|||||||
query string
|
query string
|
||||||
source string
|
source string
|
||||||
dest string
|
dest string
|
||||||
langlist string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var translateCmd = &cobra.Command{
|
var translateCmd = &cobra.Command{
|
||||||
Use: "translate",
|
Use: "translate",
|
||||||
Short: "Translate.",
|
Short: "Translate.",
|
||||||
Long: `Translate.`,
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
if langlist == "sl" || langlist == "tl" {
|
if engine == "all" {
|
||||||
list, err := utils.LangList(engine, langlist)
|
data := utils.TranslateAll(dest, source, query)
|
||||||
if err != nil {
|
for i := 0; i < len(data); i++ {
|
||||||
fmt.Println(err)
|
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 {
|
} 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() {
|
func init() {
|
||||||
rootCmd.AddCommand(translateCmd)
|
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(&engine, "engine", "e", "", "[google|libre|reverso|deepl|watson|yandex|mymemory|duckduckgo]")
|
||||||
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(&source, "source", "s", "", "Source language. Use langlist command to get code for your language")
|
||||||
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(&dest, "dest", "t", "", "Target language. Use langlist command to get code for your language")
|
||||||
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(&query, "query", "q", "", "Text to be translated")
|
||||||
translateCmd.Flags().StringVarP(&langlist, "langlist", "l", "", "The query Mozhi will listen to. Defaults to 3000, and overrides the MOZHI_query environment variable.")
|
|
||||||
|
|
||||||
engine = translateCmd.Flag("engine").Value.String()
|
translateCmd.MarkFlagRequired("engine")
|
||||||
dest = translateCmd.Flag("dest").Value.String()
|
translateCmd.MarkFlagRequired("source")
|
||||||
source = translateCmd.Flag("source").Value.String()
|
translateCmd.MarkFlagRequired("dest")
|
||||||
query = translateCmd.Flag("query").Value.String()
|
translateCmd.MarkFlagRequired("query")
|
||||||
langlist = translateCmd.Flag("query").Value.String()
|
|
||||||
}
|
}
|
||||||
|
@ -336,15 +336,13 @@ func TranslateDuckDuckGo(to string, from string, query string) (LangOut, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TranslateAll(to string, from string, query string) []LangOut {
|
func TranslateAll(to string, from string, query string) []LangOut {
|
||||||
reverso, _ := TranslateReverso(to, from, query)
|
engines := []string{"reverso", "google", "libre", "watson", "mymemory", "yandex", "deepl", "duckduckgo"}
|
||||||
google, _ := TranslateGoogle(to, from, query)
|
langout := []LangOut{}
|
||||||
libretranslate, _ := TranslateLibreTranslate(to, from, query)
|
for i := 0; i < len(engines); i++ {
|
||||||
watson, _ := TranslateWatson(to, from, query)
|
data, err := Translate(engines[i], to, from, query)
|
||||||
mymemory, _ := TranslateMyMemory(to, from, query)
|
if err == nil {
|
||||||
yandex, _ := TranslateYandex(to, from, query)
|
langout = append(langout, data)
|
||||||
deepl, _ := TranslateDeepl(to, from, query)
|
}
|
||||||
duckduckgo, _ := TranslateDuckDuckGo(to, from, query)
|
}
|
||||||
|
|
||||||
langout := []LangOut{reverso, google, libretranslate, watson, mymemory, yandex, deepl, duckduckgo}
|
|
||||||
return langout
|
return langout
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user