Skip to content

Commit

Permalink
feat: improved config info command output, some bugs fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
metafates committed Dec 19, 2022
1 parent 538ced8 commit 8b24519
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 16 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
## 4.0.5

- Fixes runtime crash #135
- Option to disable colors in cli help `mangal config info -k cli.colored`
- Option to disable colors in cli help `mangal config info -k cli.colored` (why not? =P)
- Improved `config info` command output. It now shows default value and env variable name.
- Internal improvements

## 4.0.4
Expand Down
1 change: 0 additions & 1 deletion anilist/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ func GetByID(id int) (*Manga, error) {
}

// SearchByName returns a list of mangas that match the given name.
// TODO: keep failed names in cache for a minute
func SearchByName(name string) ([]*Manga, error) {
name = normalizedName(name)
_ = query.Remember(name, 1)
Expand Down
1 change: 1 addition & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ var configInfoCmd = &cobra.Command{

if i < len(fields)-1 {
fmt.Println()
fmt.Println()
}
}
},
Expand Down
75 changes: 65 additions & 10 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ import (
"encoding/json"
"fmt"
"github.com/metafates/mangal/color"
"github.com/metafates/mangal/constant"
"github.com/metafates/mangal/key"
"github.com/metafates/mangal/style"
"github.com/samber/lo"
"github.com/spf13/viper"
"reflect"
"strconv"
"strings"
"text/template"
)

// Field represents a single config field
Expand Down Expand Up @@ -55,19 +61,68 @@ func (f *Field) MarshalJSON() ([]byte, error) {
return json.Marshal(field)
}

// Pretty format field as string for further cli output
var prettyTemplate = lo.Must(template.New("pretty").Funcs(template.FuncMap{
"faint": style.Faint,
"bold": style.Bold,
"purple": style.Fg(color.Purple),
"blue": style.Fg(color.Blue),
"cyan": style.Fg(color.Cyan),
"value": func(k string) any { return viper.Get(k) },
"hl": func(v any) string {
switch value := v.(type) {
case bool:
b := strconv.FormatBool(value)
if value {
return style.Fg(color.Green)(b)
}

return style.Fg(color.Red)(b)
case string:
return style.Fg(color.Yellow)(value)
default:
return fmt.Sprint(value)
}
},
"typename": func(v any) string { return reflect.TypeOf(v).String() },
}).Parse(`{{ faint .Description }}
{{ blue "Key:" }} {{ purple .Key }}
{{ blue "Env:" }} {{ .Env }}
{{ blue "Value:" }} {{ hl (value .Key) }}
{{ blue "Default:" }} {{ hl (.Value) }}
{{ blue "Type:" }} {{ typename .Value }}`))

func (f *Field) Pretty() string {
return fmt.Sprintf(
`%s
%s: %s = %s
`,
style.Faint(f.Description),
style.Fg(color.Purple)(f.Key),
style.Fg(color.Yellow)(f.typeName()),
style.Fg(color.Cyan)(fmt.Sprintf("%v", viper.Get(f.Key))),
)
var b strings.Builder

lo.Must0(prettyTemplate.Execute(&b, f))

return b.String()
}

func (f *Field) Env() string {
env := strings.ToUpper(EnvKeyReplacer.Replace(f.Key))
appPrefix := strings.ToUpper(constant.Mangal + "_")

if strings.HasPrefix(env, appPrefix) {
return env
}

return appPrefix + env
}

// Pretty format field as string for further cli output
//func (f *Field) Pretty() string {
// return fmt.Sprintf(
// `%s
//%s: %s = %s
//`,
// style.Faint(f.Description),
// style.Fg(color.Purple)(f.Key),
// style.Fg(color.Yellow)(f.typeName()),
// style.Fg(color.Cyan)(fmt.Sprintf("%v", viper.Get(f.Key))),
// )
//}

// defaults contains all default values for the config.
// It must contain all fields defined in the constant package.
var defaults = [key.DefinedFieldsCount]Field{
Expand Down
1 change: 1 addition & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func Customs() []*Provider {
[]byte("require(\"headless\")"),
[]byte("require('headless')"),
[]byte("require(headless)"),
[]byte("require'headless'"),
})

name := util.FileStem(path)
Expand Down
10 changes: 8 additions & 2 deletions source/chapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (c *Chapter) DownloadPages(temp bool, progress func(string)) (err error) {

for _, page := range c.Pages {
if page == nil {
continue
return fmt.Errorf("page #%d is empty, aborting download", page.Index)
}

d := func(page *Page) {
Expand All @@ -84,7 +84,13 @@ func (c *Chapter) DownloadPages(temp bool, progress func(string)) (err error) {
}

wg.Wait()
c.isDownloaded = mo.Some(!temp && err == nil)

if err != nil {
c.isDownloaded = mo.Some(false)
return err
}

c.isDownloaded = mo.Some(!temp)
return
}

Expand Down
1 change: 0 additions & 1 deletion tui/keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ func newStatefulKeymap() *statefulKeymap {
}

// help returns short and full help for the state
// TODO: add more information for full help
func (k *statefulKeymap) help() ([]key.Binding, []key.Binding) {
h := func(bindings ...key.Binding) []key.Binding {
return bindings
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

var versionCacher = gache.New[string](&gache.Options{
Path: filepath.Join(where.Cache(), "version.json"),
Lifetime: time.Hour * 24,
Lifetime: time.Hour * 24 * 2,
FileSystem: &filesystem.GacheFs{},
})

Expand Down

0 comments on commit 8b24519

Please sign in to comment.