refactor: cleanup and simplify code

This commit is contained in:
Bnyro 2024-02-20 10:11:45 +01:00
parent c985f14cfe
commit 65a9fe29b2
5 changed files with 43 additions and 45 deletions

View File

@ -33,7 +33,7 @@ var langlistCmd = &cobra.Command{
fmt.Println(err) fmt.Println(err)
} }
for _, idx := range idxs { for _, idx := range idxs {
if raw == true { if raw {
fmt.Println(list[idx].Id) fmt.Println(list[idx].Id)
} else { } else {
fmt.Println("Selected Language:", list[idx].Id, "("+list[idx].Name+")") fmt.Println("Selected Language:", list[idx].Id, "("+list[idx].Name+")")

View File

@ -17,6 +17,33 @@ var (
rawjson bool rawjson bool
) )
func printEngineResult(result libmozhi.LangOut, printPlaceHolderAndEngineName bool) {
if printPlaceHolderAndEngineName {
fmt.Println("-----------------------------------")
fmt.Println("Engine: " + result.Engine)
}
fmt.Println("Translated Text: " + result.OutputText)
if source == "auto" {
fmt.Println("Detected Language: " + result.AutoDetect)
}
fmt.Println("Source Language: " + result.SourceLang)
fmt.Println("Target Language: " + result.TargetLang)
if printPlaceHolderAndEngineName {
fmt.Println("-----------------------------------")
}
}
func printRaw(data interface{}) {
j, err := json.Marshal(data)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(j))
}
}
var translateCmd = &cobra.Command{ var translateCmd = &cobra.Command{
Use: "translate", Use: "translate",
Short: "Translate.", Short: "Translate.",
@ -24,44 +51,21 @@ var translateCmd = &cobra.Command{
if engine == "all" { if engine == "all" {
data := libmozhi.TranslateAll(dest, source, query) data := libmozhi.TranslateAll(dest, source, query)
if rawjson { if rawjson {
j, err := json.Marshal(data) printRaw(data)
if err != nil {
fmt.Println(err)
} else { } else {
fmt.Println(string(j)) for _, result := range data {
printEngineResult(result, true)
} }
} else {
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)
}
fmt.Println("-----------------------------------")
} }
} else { } else {
data, err := libmozhi.Translate(engine, dest, source, query) data, err := libmozhi.Translate(engine, dest, source, query)
if rawjson { if rawjson {
j, err := json.Marshal(data) printRaw(data)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(j))
}
} else { } else {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} else { } else {
fmt.Println("Translated Text: " + data.OutputText) printEngineResult(data, false)
if source == "auto" {
fmt.Println("Detected Language: " + data.AutoDetect)
}
fmt.Println("Source Language: " + data.SourceLang)
fmt.Println("Target Language: " + data.TargetLang)
} }
} }
} }

View File

@ -82,20 +82,14 @@ func HandleTranslate(c *fiber.Ctx) error {
if engine == "" || from == "" || to == "" || text == "" { if engine == "" || from == "" || to == "" || text == "" {
return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, text are required query strings.") return fiber.NewError(fiber.StatusBadRequest, "from, to, engine, text are required query strings.")
} }
var dataarr []libmozhi.LangOut
var data libmozhi.LangOut
var err error
if engine == "all" { if engine == "all" {
dataarr = libmozhi.TranslateAll(to, from, text) dataarr := libmozhi.TranslateAll(to, from, text)
return c.JSON(dataarr)
} else { } else {
data, err = libmozhi.Translate(engine, to, from, text) data, err := libmozhi.Translate(engine, to, from, text)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} }
}
if engine == "all" {
return c.JSON(dataarr)
} else {
return c.JSON(data) return c.JSON(data)
} }
} }

View File

@ -12,7 +12,7 @@ import (
func langListMerge(engines map[string]string) ([]libmozhi.List, []libmozhi.List) { func langListMerge(engines map[string]string) ([]libmozhi.List, []libmozhi.List) {
sl := []libmozhi.List{} sl := []libmozhi.List{}
tl := []libmozhi.List{} tl := []libmozhi.List{}
for key, _ := range engines { for key := range engines {
temp, _ := libmozhi.LangList(key, "sl") temp, _ := libmozhi.LangList(key, "sl")
temp2, _ := libmozhi.LangList(key, "tl") temp2, _ := libmozhi.LangList(key, "tl")
sl = append(sl, temp...) sl = append(sl, temp...)

View File

@ -7,6 +7,9 @@ import (
"regexp" "regexp"
) )
var nonAlphanumericRegex = regexp.MustCompile(`[^a-zA-Z]+`)
var nonAlphaRegex = regexp.MustCompile(`[^a-zA-Z0-9]+`)
func GetQueryOrFormValue(c *fiber.Ctx, key string) string { func GetQueryOrFormValue(c *fiber.Ctx, key string) string {
if c.Method() == "POST" { if c.Method() == "POST" {
return c.FormValue(key) return c.FormValue(key)
@ -16,15 +19,12 @@ func GetQueryOrFormValue(c *fiber.Ctx, key string) string {
} }
func EnvTrueNoExist(env string) bool { func EnvTrueNoExist(env string) bool {
if _, ok := os.LookupEnv(env); ok == false || os.Getenv(env) == "true" { _, envFound := os.LookupEnv(env)
return true
} return !envFound || os.Getenv(env) == "true"
return false
} }
func Sanitize(str string, strip string) string { func Sanitize(str string, strip string) string {
nonAlphanumericRegex := regexp.MustCompile(`[^a-zA-Z]+`)
nonAlphaRegex := regexp.MustCompile(`[^a-zA-Z0-9]+`)
if strip == "alpha" { if strip == "alpha" {
return nonAlphaRegex.ReplaceAllString(str, "") return nonAlphaRegex.ReplaceAllString(str, "")
} else if strip == "alphanumeric" { } else if strip == "alphanumeric" {