Skip to content

Commit

Permalink
refactor: Refactor echo package print functions and improve localization
Browse files Browse the repository at this point in the history
- New print functions have been added to the echo package: Println, Printfln, and Warningf, which utilize localization to enhance output in different languages.

- The Warning function now also uses localization and has been simplified.

- Print function calls have been updated in several files, such as main.go and progress.go, to use the new echo functions.

- Localization files have been updated to support the new format for error and warning messages.

- Code readability and maintainability have been improved by centralizing print logic in the echo package.
  • Loading branch information
Tom5521 committed Nov 5, 2024
1 parent ac3d790 commit 0fc3531
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 33 deletions.
31 changes: 29 additions & 2 deletions echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"fmt"

"github.com/Tom5521/fsize/flags"
"github.com/Tom5521/fsize/locales"
"github.com/Tom5521/fsize/settings"
conf "github.com/Tom5521/goconf"
"github.com/gookit/color"
)

var po = locales.Po

func Settings(s *conf.Preferences) {
printSetting := func(key string) {
s := s.Get(key)
Expand All @@ -29,9 +32,33 @@ func Settings(s *conf.Preferences) {
}
}

func Warning(warn ...any) {
func Println(txt string, args ...any) {
fmt.Print(po.Get(txt))
if len(args) > 0 {
fmt.Print(args...)
}
fmt.Println()
}

func Printfln(format string, a ...any) {
fmt.Println(po.Get(format, a...))
}

func Warningf(format string, arg ...any) {
Warning(po.Get(format, arg...))
}

func Warning(warn string) {
if flags.NoWarns {
return
}
color.Warnln(warn...)
color.Warnln(po.Get(warn))
}

func Info(format string, a ...any) {
color.Info.Println(po.Get(format, a...))
}

func Error(format string, a ...any) {
color.Error.Println(po.Get(format, a...))
}
6 changes: 3 additions & 3 deletions filecount/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import (
"io/fs"
"path/filepath"

"github.com/Tom5521/fsize/echo"
"github.com/Tom5521/fsize/flags"
"github.com/Tom5521/fsize/locales"
"github.com/gookit/color"
)

var po = locales.Po

func Print(count, size *int64, path string) (err error) {
err = filepath.Walk(path, func(name string, info fs.FileInfo, err error) error {
if err != nil {
warning(err)
echo.Warning(err.Error())
return nil
}
if flags.PrintOnWalk {
color.Infof(po.Get("Reading \"%s\"...", name))
echo.Info("Reading \"%s\"...", name)
}

*size += info.Size()
Expand Down
10 changes: 3 additions & 7 deletions filecount/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ import (

_ "unsafe"

_ "github.com/Tom5521/fsize/echo"
"github.com/gookit/color"
"github.com/Tom5521/fsize/echo"
"github.com/schollz/progressbar/v3"
)

//go:linkname warning github.com/Tom5521/fsize/echo.Warning
func warning(...any) // FUCK THE IMPORT CYCLE.

func Progress(count, size *int64, path string) (err error) {
color.Infoln(po.Get("Counting the amount of files..."))
echo.Info("Counting the amount of files...")

var warnings []error

Expand Down Expand Up @@ -45,7 +41,7 @@ func Progress(count, size *int64, path string) (err error) {
}

for _, e := range warnings {
warning(e.Error())
echo.Warning(e.Error())
}
if err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion locales/po/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ msgstr ""
msgid "Upgrade completed successfully"
msgstr ""

msgid "not found."
msgid "%s not found."
msgstr ""

msgid "error getting the corresponding structure from fileinfo.Sys()"
Expand Down
4 changes: 2 additions & 2 deletions locales/po/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ msgstr "Actualizando las autocompletaciones..."
msgid "Upgrade completed successfully"
msgstr "Actualización completada exitosamente"

msgid "not found."
msgstr "no encontrado."
msgid "%s not found."
msgstr "%s no encontrado."

msgid "error getting the corresponding structure from fileinfo.Sys()"
msgstr "error al obtener la estructura correspondiente de fileinfo.Sys()"
Expand Down
4 changes: 2 additions & 2 deletions locales/po/pt.po
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ msgstr "Atualizando as conclusões..."
msgid "Upgrade completed successfully"
msgstr "Atualização concluída com sucesso"

msgid "not found."
msgstr "não encontrado."
msgid "%s not found."
msgstr "%s não encontrado."

msgid "error getting the corresponding structure from fileinfo.Sys()"
msgstr "erro ao obter a estrutura correspondente de fileinfo.Sys()"
Expand Down
4 changes: 2 additions & 2 deletions locales/po/ru.po
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ msgstr "Обновление автозаполнений..."
msgid "Upgrade completed successfully"
msgstr "Обновление успешно завершено"

msgid "not found."
msgstr "не найдено."
msgid "%s not found."
msgstr "%s не найдено."

msgid "error getting the corresponding structure from fileinfo.Sys()"
msgstr "ошибка получения соответствующей структуры от fileinfo.Sys()"
Expand Down
16 changes: 7 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func RunE(cmd *cobra.Command, args []string) (err error) {
case len(flags.SettingsFlag) != 0:
err = settings.Parse(flags.SettingsFlag)
if err != nil {
color.Infoln(po.Get("Available configuration keys:"))
echo.Info("Available configuration keys:")
echo.Settings(Settings)
}
case flags.Update:
Expand All @@ -65,7 +65,7 @@ func RunE(cmd *cobra.Command, args []string) (err error) {
return
}
if updated {
color.Info.Println(po.Get("Already in latest version"))
echo.Info("Already in latest version")
return
}
err = update.ApplyUpdate(tag)
Expand All @@ -79,18 +79,16 @@ func RunE(cmd *cobra.Command, args []string) (err error) {
return
}

fmt.Println(po.Get("Version:"), meta.Version)
fmt.Println(po.Get("Updated:"), updated)
echo.Println("Version:", meta.Version)
echo.Println("Updated:", updated)
if !updated {
fmt.Println(po.Get("Latest version:"), tag)
echo.Println("Latest version:", tag)
}

fmt.Println(po.Get("Source Code: %s", "https://github.com/Tom5521/fsize"))
echo.Printfln("Source Code: %s", "https://github.com/Tom5521/fsize")
default:
if len(args) == 0 {
echo.Warning(
po.Get("No file/directory was specified, the current directory will be used. (.)"),
)
echo.Warning("No file/directory was specified, the current directory will be used. (.)")
args = append(args, ".")
}
for _, f := range args {
Expand Down
9 changes: 5 additions & 4 deletions update/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/Tom5521/fsize/checkos"
"github.com/Tom5521/fsize/echo"
"github.com/Tom5521/fsize/locales"
"github.com/Tom5521/fsize/meta"
"github.com/gookit/color"
Expand All @@ -21,7 +22,7 @@ const UpdateURL string = "https://github.com/Tom5521/fsize/releases/latest"
var po = locales.Po

func CheckUpdate() (tag string, latest bool, err error) {
color.Infoln(po.Get("Checking the latest version available..."))
echo.Info("Checking the latest version available...")
resp, err := http.Get(UpdateURL)
if err != nil {
return
Expand Down Expand Up @@ -74,21 +75,21 @@ func ApplyUpdate(tag string) (err error) {
if err != nil {
return
}
color.Infoln(po.Get("Writing to binary..."))
echo.Info("Writing to binary...")
err = selfupdate.Apply(&buf, selfupdate.Options{})
if err != nil {
return
}

if checkos.Unix {
color.Infoln(po.Get("Updating completions..."))
echo.Info("Updating completions...")
err = updateCompletions()
if err != nil {
return
}
}

color.Infoln(po.Get("Upgrade completed successfully"))
echo.Info("Upgrade completed successfully")
fmt.Printf("%s -> %s\n", color.Red.Render(meta.Version), color.Green.Render(tag))

return
Expand Down
2 changes: 1 addition & 1 deletion update/update_completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ zsh|/usr/local/share/zsh/site-functions/_fsize`

_, exists := exec.LookPath(shell)
if exists != nil {
echo.Warning(shell, po.Get("not found."))
echo.Warningf("%s not found.", shell)
continue
}
cmd := exec.Command("fsize", fmt.Sprintf("--gen-%s-completion", shell), path)
Expand Down

0 comments on commit 0fc3531

Please sign in to comment.