From f9d6f706bcd6e5773b8889298c268b5788f37253 Mon Sep 17 00:00:00 2001 From: aQaTL Date: Mon, 11 Feb 2019 11:53:32 +0100 Subject: [PATCH] Added list-width config command --- anilist_app.go | 23 ++++++++++++++++++----- config.go | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/anilist_app.go b/anilist_app.go index 59c7912..add8c1b 100644 --- a/anilist_app.go +++ b/anilist_app.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "math" "net/url" "sort" "strconv" @@ -229,6 +230,13 @@ func AniListApp(app *cli.App) *cli.App { UsageText: "mal cfg max [number]", Action: configChangeMax, }, + cli.Command{ + Name: "list-width", + Usage: "Change the width of displayed list", + UsageText: "mal cfg list-width [width]", + SkipFlagParsing: true, + Action: configChangeListWidth, + }, cli.Command{ Name: "status", Usage: "Status value of displayed entries", @@ -307,18 +315,23 @@ func aniListDefaultAction(ctx *cli.Context) error { visibleEntries = len(list) } - fmt.Printf("No%64s%8s%6s\n", "Title", "Eps", "Score") - fmt.Println(strings.Repeat("=", 80)) - pattern := "%2d%64.64s%8s%6d\n" + numberFieldWidth := int(math.Max(math.Ceil(math.Log10(float64(visibleEntries+1))), 2)) + titleWidth := cfg.ListWidth - numberFieldWidth - 8 - 6 + fmt.Printf("%*s%*.*s%8s%6s\n", + numberFieldWidth, "No", titleWidth, titleWidth, "Title", "Eps", "Score") + fmt.Println(strings.Repeat("=", cfg.ListWidth)) + pattern := "%*d%*.*s%8s%6d\n" var entry *anilist.MediaListEntry for i := visibleEntries - 1; i >= 0; i-- { entry = &list[i] if entry.Id == cfg.ALSelectedID { - color.HiYellow(pattern, i+1, entry.Title.UserPreferred, + color.HiYellow(pattern, numberFieldWidth, i+1, titleWidth, titleWidth, + entry.Title.UserPreferred, fmt.Sprintf("%d/%d", entry.Progress, entry.Episodes), entry.Score) } else { - fmt.Printf(pattern, i+1, entry.Title.UserPreferred, + fmt.Printf(pattern, numberFieldWidth, i+1, titleWidth, titleWidth, + entry.Title.UserPreferred, fmt.Sprintf("%d/%d", entry.Progress, entry.Episodes), entry.Score) } diff --git a/config.go b/config.go index 0694900..6c23ccd 100644 --- a/config.go +++ b/config.go @@ -22,6 +22,7 @@ type Config struct { StatusAutoUpdateMode StatusAutoUpdateMode Sorting Sorting LastUpdate time.Time + ListWidth int BrowserPath string TorrentClientPath string @@ -42,6 +43,7 @@ func NewConfig() *Config { MaxVisibleEntries: 20, StatusAutoUpdateMode: Off, Sorting: ByLastUpdated, + ListWidth: 80, TorrentClientPath: "qbittorrent", @@ -131,6 +133,19 @@ func configChangeMax(ctx *cli.Context) error { return nil } +func configChangeListWidth(ctx *cli.Context) error { + cfg := LoadConfig() + + width, err := strconv.Atoi(ctx.Args().First()) + if err != nil || width < 20 { + return fmt.Errorf("invalid or too small width") + } + + cfg.ListWidth = width + cfg.Save() + return nil +} + func configChangeMalStatus(ctx *cli.Context) error { cfg := LoadConfig()