From e1aded93e84141df5f6181ac4b24e6ab945fe1b2 Mon Sep 17 00:00:00 2001 From: Simon Roberts Date: Tue, 5 Oct 2021 08:01:42 +1100 Subject: [PATCH] More minor cleanups (no functional change) (#198) * Redundant type conversions * Remove redudant type declarations. Inline one-line constructors. Remove unnecessary brackets. * Simplify name - it's used via package name anyway * Simplify variable initializations * Change `var x uint = Y` to `x := uint(Y)` * More shorthand initialization --- cmd/commands/holdings.go | 2 +- cmd/commands/server.go | 18 +++++++++--------- cointop/chart.go | 29 ++++++++++++++--------------- cointop/cointop.go | 8 ++++---- cointop/config.go | 6 +++--- cointop/keybindings.go | 2 +- cointop/marketbar.go | 5 ++--- cointop/menu.go | 3 +-- cointop/navigation.go | 2 +- cointop/portfolio.go | 2 +- cointop/search.go | 6 ++---- cointop/statusbar.go | 3 +-- cointop/table.go | 3 +-- cointop/table_header.go | 3 +-- pkg/api/impl/coingecko/coingecko.go | 8 ++++---- pkg/levenshtein/levenshtein.go | 2 +- pkg/table/align/align.go | 12 ++++++------ pkg/table/table.go | 12 ++++++------ pkg/termui/gauge.go | 2 +- 19 files changed, 60 insertions(+), 68 deletions(-) diff --git a/cmd/commands/holdings.go b/cmd/commands/holdings.go index 91d2967..8a01c8b 100644 --- a/cmd/commands/holdings.go +++ b/cmd/commands/holdings.go @@ -17,7 +17,7 @@ func HoldingsCmd() *cobra.Command { var config string var sortBy string var sortDesc bool - var format string = "table" + var format = "table" var humanReadable bool var filter []string var cols []string diff --git a/cmd/commands/server.go b/cmd/commands/server.go index 4fab961..91a06dc 100644 --- a/cmd/commands/server.go +++ b/cmd/commands/server.go @@ -14,15 +14,15 @@ import ( // ServerCmd ... func ServerCmd() *cobra.Command { - var port uint = 22 - var address string = "0.0.0.0" - var idleTimeout uint = 0 - var maxTimeout uint = 0 - var maxSessions uint = 0 - var executableBinary string = "cointop" - var hostKeyFile string = cssh.DefaultHostKeyFile - var userConfigType string = cssh.UserConfigTypePublicKey - var colorsDir string = os.Getenv("COINTOP_COLORS_DIR") + port := uint(22) + address := "0.0.0.0" + idleTimeout := uint(0) + maxTimeout := uint(0) + maxSessions := uint(0) + executableBinary := "cointop" + hostKeyFile := cssh.DefaultHostKeyFile + userConfigType := cssh.UserConfigTypePublicKey + colorsDir := os.Getenv("COINTOP_COLORS_DIR") serverCmd := &cobra.Command{ Use: "server", diff --git a/cointop/chart.go b/cointop/chart.go index 25875fd..dcca3f4 100644 --- a/cointop/chart.go +++ b/cointop/chart.go @@ -25,8 +25,7 @@ type ChartView = ui.View // NewChartView returns a new chart view func NewChartView() *ChartView { - var view *ChartView = ui.NewView("chart") - return view + return ui.NewView("chart") } var chartLock sync.Mutex @@ -50,17 +49,17 @@ func ChartRanges() []string { // ChartRangesMap returns map of chart range time ranges func ChartRangesMap() map[string]time.Duration { return map[string]time.Duration{ - "All Time": time.Duration(10 * 365 * 24 * time.Hour), - "YTD": time.Duration(1 * time.Second), // this will be calculated - "1Y": time.Duration(365 * 24 * time.Hour), - "6M": time.Duration(365 / 2 * 24 * time.Hour), - "3M": time.Duration(365 / 4 * 24 * time.Hour), - "1M": time.Duration(365 / 12 * 24 * time.Hour), - "7D": time.Duration(24 * 7 * time.Hour), - "3D": time.Duration(24 * 3 * time.Hour), - "24H": time.Duration(24 * time.Hour), - "6H": time.Duration(6 * time.Hour), - "1H": time.Duration(1 * time.Hour), + "All Time": 10 * 365 * 24 * time.Hour, + "YTD": 1 * time.Second, // this will be calculated + "1Y": 365 * 24 * time.Hour, + "6M": 365 / 2 * 24 * time.Hour, + "3M": 365 / 4 * 24 * time.Hour, + "1M": 365 / 12 * 24 * time.Hour, + "7D": 24 * 7 * time.Hour, + "3D": 24 * 3 * time.Hour, + "24H": 24 * time.Hour, + "6H": 6 * time.Hour, + "1H": 1 * time.Hour, } } @@ -119,7 +118,7 @@ func (ct *Cointop) ChartPoints(symbol string, name string) error { rangeseconds := ct.chartRangesMap[ct.State.selectedChartRange] if ct.State.selectedChartRange == "YTD" { - ytd := time.Now().Unix() - int64(timeutil.BeginningOfYear().Unix()) + ytd := time.Now().Unix() - timeutil.BeginningOfYear().Unix() rangeseconds = time.Duration(ytd) * time.Second } @@ -216,7 +215,7 @@ func (ct *Cointop) PortfolioChart() error { selectedChartRange := ct.State.selectedChartRange // cache here rangeseconds := ct.chartRangesMap[selectedChartRange] if selectedChartRange == "YTD" { - ytd := time.Now().Unix() - int64(timeutil.BeginningOfYear().Unix()) + ytd := time.Now().Unix() - timeutil.BeginningOfYear().Unix() rangeseconds = time.Duration(ytd) * time.Second } diff --git a/cointop/cointop.go b/cointop/cointop.go index 8f47d68..fa54c78 100644 --- a/cointop/cointop.go +++ b/cointop/cointop.go @@ -182,19 +182,19 @@ var DefaultCurrency = "USD" var DefaultChartRange = "1Y" // DefaultMaxChartWidth ... -var DefaultMaxChartWidth int = 175 +var DefaultMaxChartWidth = 175 // DefaultChartHeight ... -var DefaultChartHeight int = 10 +var DefaultChartHeight = 10 // DefaultSortBy ... var DefaultSortBy = "rank" // DefaultPerPage ... -var DefaultPerPage uint = 100 +var DefaultPerPage = uint(100) // DefaultMaxPages ... -var DefaultMaxPages uint = 35 +var DefaultMaxPages = uint(35) // DefaultColorscheme ... var DefaultColorscheme = "cointop" diff --git a/cointop/config.go b/cointop/config.go index 8b32c04..02cc058 100644 --- a/cointop/config.go +++ b/cointop/config.go @@ -227,9 +227,9 @@ func (ct *Cointop) ConfigToToml() ([]byte, error) { if !ok || entry.Coin == "" { continue } - var amount string = strconv.FormatFloat(entry.Holdings, 'f', -1, 64) - var coinName string = entry.Coin - var tuple []string = []string{coinName, amount} + amount := strconv.FormatFloat(entry.Holdings, 'f', -1, 64) + coinName := entry.Coin + tuple := []string{coinName, amount} holdingsIfc = append(holdingsIfc, tuple) } sort.Slice(holdingsIfc, func(i, j int) bool { diff --git a/cointop/keybindings.go b/cointop/keybindings.go index cb2993c..2f0b6b3 100644 --- a/cointop/keybindings.go +++ b/cointop/keybindings.go @@ -377,7 +377,7 @@ func (ct *Cointop) SetKeybindings() error { // TODO: use scrolling table keys := ct.SortedSupportedCurrencyConversions() for i, k := range keys { - ct.SetKeybindingMod(rune(alphanumericcharacters[i]), gocui.ModNone, ct.Keyfn(ct.SetCurrencyConverstionFn(k)), ct.Views.Menu.Name()) + ct.SetKeybindingMod(alphanumericcharacters[i], gocui.ModNone, ct.Keyfn(ct.SetCurrencyConverstionFn(k)), ct.Views.Menu.Name()) } return nil diff --git a/cointop/marketbar.go b/cointop/marketbar.go index 95a7d96..d43478a 100644 --- a/cointop/marketbar.go +++ b/cointop/marketbar.go @@ -19,8 +19,7 @@ type MarketbarView = ui.View // NewMarketbarView returns a new marketbar view func NewMarketbarView() *MarketbarView { - var view *MarketbarView = ui.NewView("marketbar") - return view + return ui.NewView("marketbar") } // UpdateMarketbar updates the market bar view @@ -54,7 +53,7 @@ func (ct *Cointop) UpdateMarketbar() error { var percentChange24H float64 for _, p := range ct.GetPortfolioSlice() { - n := ((p.Balance / total) * p.PercentChange24H) + n := (p.Balance / total) * p.PercentChange24H if math.IsNaN(n) { continue } diff --git a/cointop/menu.go b/cointop/menu.go index 4da3640..c138b26 100644 --- a/cointop/menu.go +++ b/cointop/menu.go @@ -10,8 +10,7 @@ type MenuView = ui.View // NewMenuView returns a new menu view func NewMenuView() *MenuView { - var view *MenuView = ui.NewView("menu") - return view + return ui.NewView("menu") } // HideMenu hides the menu view diff --git a/cointop/navigation.go b/cointop/navigation.go index 8d98aa6..abbd2be 100644 --- a/cointop/navigation.go +++ b/cointop/navigation.go @@ -413,7 +413,7 @@ func (ct *Cointop) GoToGlobalIndex(idx int) error { l := ct.TableRowsLen() atpage := idx / l ct.SetPage(atpage) - rowIndex := (idx % l) + rowIndex := idx % l ct.HighlightRow(rowIndex) ct.UpdateTable() return nil diff --git a/cointop/portfolio.go b/cointop/portfolio.go index 9ce4564..722c217 100644 --- a/cointop/portfolio.go +++ b/cointop/portfolio.go @@ -983,7 +983,7 @@ func (ct *Cointop) PrintHoldings24HChange(options *TablePrintOptions) error { } } - n := ((entry.Balance / total) * entry.PercentChange24H) + n := (entry.Balance / total) * entry.PercentChange24H if math.IsNaN(n) { continue } diff --git a/cointop/search.go b/cointop/search.go index 7e95ff3..9643cce 100644 --- a/cointop/search.go +++ b/cointop/search.go @@ -14,8 +14,7 @@ type SearchFieldView = ui.View // NewSearchFieldView returns a new search field view func NewSearchFieldView() *SearchFieldView { - var view *SearchFieldView = ui.NewView("searchfield") - return view + return ui.NewView("searchfield") } // InputView is structure for help view @@ -23,8 +22,7 @@ type InputView = ui.View // NewInputView returns a new help view func NewInputView() *InputView { - var view *InputView = ui.NewView("input") - return view + return ui.NewView("input") } // OpenSearch opens the search field diff --git a/cointop/statusbar.go b/cointop/statusbar.go index 65fcaa4..5bd1e29 100644 --- a/cointop/statusbar.go +++ b/cointop/statusbar.go @@ -15,8 +15,7 @@ type StatusbarView = ui.View // NewStatusbarView returns a new statusbar view func NewStatusbarView() *StatusbarView { - var view *StatusbarView = ui.NewView("statusbar") - return view + return ui.NewView("statusbar") } // UpdateStatusbar updates the statusbar view diff --git a/cointop/table.go b/cointop/table.go index 9cc0575..bfdf8ab 100644 --- a/cointop/table.go +++ b/cointop/table.go @@ -14,8 +14,7 @@ type TableView = ui.View // NewTableView returns a new table view func NewTableView() *TableView { - var view *TableView = ui.NewView("table") - return view + return ui.NewView("table") } const dots = "..." diff --git a/cointop/table_header.go b/cointop/table_header.go index c3d36ed..ded019e 100644 --- a/cointop/table_header.go +++ b/cointop/table_header.go @@ -128,8 +128,7 @@ type TableHeaderView = ui.View // NewTableHeaderView returns a new table header view func NewTableHeaderView() *TableHeaderView { - var view *TableHeaderView = ui.NewView("table_header") - return view + return ui.NewView("table_header") } // GetActiveTableHeaders returns the list of active table headers diff --git a/pkg/api/impl/coingecko/coingecko.go b/pkg/api/impl/coingecko/coingecko.go index 8990292..e196870 100644 --- a/pkg/api/impl/coingecko/coingecko.go +++ b/pkg/api/impl/coingecko/coingecko.go @@ -37,10 +37,10 @@ type Service struct { // NewCoinGecko new service func NewCoinGecko(config *Config) *Service { - var maxResultsPerPage uint = 250 // absolute max - var maxResults uint = 0 - var maxPages uint = 10 - var perPage uint = 100 + maxResultsPerPage := 250 // absolute max + maxResults := uint(0) + maxPages := uint(10) + perPage := uint(100) if config.PerPage > 0 { perPage = config.PerPage } diff --git a/pkg/levenshtein/levenshtein.go b/pkg/levenshtein/levenshtein.go index f855d0c..f76bfb2 100644 --- a/pkg/levenshtein/levenshtein.go +++ b/pkg/levenshtein/levenshtein.go @@ -45,7 +45,7 @@ func DamerauLevenshteinDistance(s1, s2 string) int { // min returns the minimum number of passed int slices. func min(is ...int) int { - min := int(math.MaxInt32) + min := math.MaxInt32 for _, v := range is { if min > v { min = v diff --git a/pkg/table/align/align.go b/pkg/table/align/align.go index 5e0c97d..78568c0 100644 --- a/pkg/table/align/align.go +++ b/pkg/table/align/align.go @@ -8,8 +8,8 @@ import ( "github.com/acarl005/stripansi" ) -// AlignLeft align left -func AlignLeft(t string, n int) string { +// Left align left +func Left(t string, n int) string { s := stripansi.Strip(t) slen := utf8.RuneCountInString(s) if slen > n { @@ -19,8 +19,8 @@ func AlignLeft(t string, n int) string { return fmt.Sprintf("%s%s", t, strings.Repeat(" ", n-slen)) } -// AlignRight align right -func AlignRight(t string, n int) string { +// Right align right +func Right(t string, n int) string { s := stripansi.Strip(t) slen := utf8.RuneCountInString(s) if slen > n { @@ -30,8 +30,8 @@ func AlignRight(t string, n int) string { return fmt.Sprintf("%s%s", strings.Repeat(" ", n-slen), t) } -// AlignCenter align center -func AlignCenter(t string, n int) string { +// Center align center +func Center(t string, n int) string { s := stripansi.Strip(t) slen := utf8.RuneCountInString(s) if slen > n { diff --git a/pkg/table/table.go b/pkg/table/table.go index fea28e0..341bfa0 100644 --- a/pkg/table/table.go +++ b/pkg/table/table.go @@ -205,11 +205,11 @@ func (t *Table) Fprint(w io.Writer) { var s string switch c.align { case AlignLeft: - s = align.AlignLeft(c.name+" ", c.width) + s = align.Left(c.name+" ", c.width) case AlignRight: - s = align.AlignRight(c.name+" ", c.width) + s = align.Right(c.name+" ", c.width) case AlignCenter: - s = align.AlignCenter(c.name+" ", c.width) + s = align.Center(c.name+" ", c.width) } fmt.Fprintf(w, "%s", s) @@ -237,11 +237,11 @@ func (t *Table) Fprint(w io.Writer) { var s string switch c.align { case AlignLeft: - s = align.AlignLeft(v, c.width) + s = align.Left(v, c.width) case AlignRight: - s = align.AlignRight(v, c.width) + s = align.Right(v, c.width) case AlignCenter: - s = align.AlignCenter(v, c.width) + s = align.Center(v, c.width) } fmt.Fprintf(w, "%s", s) diff --git a/pkg/termui/gauge.go b/pkg/termui/gauge.go index 9f6ce3a..ca57975 100644 --- a/pkg/termui/gauge.go +++ b/pkg/termui/gauge.go @@ -21,7 +21,7 @@ import ( g.PercentColor = termui.ColorBlue */ -const ColorUndef Attribute = Attribute(^uint16(0)) +const ColorUndef = Attribute(^uint16(0)) type Gauge struct { Block