add json option for transl. + gofmt

This commit is contained in:
Arya 2023-08-29 14:50:56 +05:30
parent d92958aae0
commit 5ffd20046a
Signed by: arya
GPG Key ID: 842D12BDA50DF120
4 changed files with 58 additions and 30 deletions

View File

@ -2,9 +2,11 @@ package cmd
import (
"fmt"
"codeberg.org/aryak/mozhi/utils"
"github.com/spf13/cobra"
"github.com/ktr0731/go-fuzzyfinder"
"github.com/spf13/cobra"
)
var (

View File

@ -1,7 +1,9 @@
package cmd
import (
"encoding/json"
"fmt"
"codeberg.org/aryak/mozhi/utils"
"github.com/spf13/cobra"
@ -12,6 +14,7 @@ var (
query string
source string
dest string
rawjson bool
)
var translateCmd = &cobra.Command{
@ -20,27 +23,45 @@ var translateCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
if engine == "all" {
data := utils.TranslateAll(dest, source, query)
if rawjson {
j, err := json.Marshal(data)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(j))
}
} else {
for i := 0; i < len(data); i++ {
fmt.Println("-----------------------------------")
fmt.Println("Engine: "+data[i].Engine)
fmt.Println("Translated Text: "+data[i].OutputText)
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("Detected Language: " + data[i].AutoDetect)
}
fmt.Println("Source Language: " + data[i].SourceLang)
fmt.Println("Target Language: " + data[i].TargetLang)
}
fmt.Println("Source Language: "+data[i].SourceLang)
fmt.Println("Target Language: "+data[i].TargetLang)
}
} else {
data, err := utils.Translate(engine, dest, source, query)
if rawjson {
j, err := json.Marshal(data)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(j))
}
} else {
if err != nil {
fmt.Println(err)
}
fmt.Println("Translated Text: "+data.OutputText)
fmt.Println("Translated Text: " + data.OutputText)
if source == "auto" {
fmt.Println("Detected Language: "+data.AutoDetect)
fmt.Println("Detected Language: " + data.AutoDetect)
}
fmt.Println("Source Language: " + data.SourceLang)
fmt.Println("Target Language: " + data.TargetLang)
}
fmt.Println("Source Language: "+data.SourceLang)
fmt.Println("Target Language: "+data.TargetLang)
}
},
}
@ -53,6 +74,7 @@ func init() {
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")
translateCmd.Flags().BoolVarP(&rawjson, "raw", "r", false, "Return output as json")
translateCmd.MarkFlagRequired("engine")
translateCmd.MarkFlagRequired("source")

View File

@ -6,6 +6,7 @@ import (
)
// HandleSourceLanguages godoc
//
// @Summary Show list of available source languages for engine
// @Param engine query string true "Engine name"
// @Success 200 {object} utils.List
@ -23,6 +24,7 @@ func HandleSourceLanguages(c *fiber.Ctx) error {
}
// HandleTargetLanguages godoc
//
// @Summary Show list of available target languages for engine
// @Param engine query string true "Engine name"
// @Success 200 {object} utils.List
@ -40,6 +42,7 @@ func HandleTargetLanguages(c *fiber.Ctx) error {
}
// HandleTTS godoc
//
// @Summary Get Text-To-Speech for specified language using specified engine
// @Param engine query string true "Engine name"
// @Param lang query string true "Language being TTS'd"
@ -61,6 +64,7 @@ func HandleTTS(c *fiber.Ctx) error {
}
// HandleTranslate godoc
//
// @Summary Translate text
// @Description When engine is set to all, it will return an array of utils.LangOut.
// @Param engine query string true "Engine name"

View File

@ -6,9 +6,9 @@ import (
"os"
"runtime"
_ "codeberg.org/aryak/mozhi/docs"
"codeberg.org/aryak/mozhi/pages"
"codeberg.org/aryak/mozhi/utils"
_ "codeberg.org/aryak/mozhi/docs"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress"