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
pull/200/head
Simon Roberts 3 years ago committed by GitHub
parent ff24fb3b69
commit e1aded93e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -17,7 +17,7 @@ func HoldingsCmd() *cobra.Command {
var config string var config string
var sortBy string var sortBy string
var sortDesc bool var sortDesc bool
var format string = "table" var format = "table"
var humanReadable bool var humanReadable bool
var filter []string var filter []string
var cols []string var cols []string

@ -14,15 +14,15 @@ import (
// ServerCmd ... // ServerCmd ...
func ServerCmd() *cobra.Command { func ServerCmd() *cobra.Command {
var port uint = 22 port := uint(22)
var address string = "0.0.0.0" address := "0.0.0.0"
var idleTimeout uint = 0 idleTimeout := uint(0)
var maxTimeout uint = 0 maxTimeout := uint(0)
var maxSessions uint = 0 maxSessions := uint(0)
var executableBinary string = "cointop" executableBinary := "cointop"
var hostKeyFile string = cssh.DefaultHostKeyFile hostKeyFile := cssh.DefaultHostKeyFile
var userConfigType string = cssh.UserConfigTypePublicKey userConfigType := cssh.UserConfigTypePublicKey
var colorsDir string = os.Getenv("COINTOP_COLORS_DIR") colorsDir := os.Getenv("COINTOP_COLORS_DIR")
serverCmd := &cobra.Command{ serverCmd := &cobra.Command{
Use: "server", Use: "server",

@ -25,8 +25,7 @@ type ChartView = ui.View
// NewChartView returns a new chart view // NewChartView returns a new chart view
func NewChartView() *ChartView { func NewChartView() *ChartView {
var view *ChartView = ui.NewView("chart") return ui.NewView("chart")
return view
} }
var chartLock sync.Mutex var chartLock sync.Mutex
@ -50,17 +49,17 @@ func ChartRanges() []string {
// ChartRangesMap returns map of chart range time ranges // ChartRangesMap returns map of chart range time ranges
func ChartRangesMap() map[string]time.Duration { func ChartRangesMap() map[string]time.Duration {
return map[string]time.Duration{ return map[string]time.Duration{
"All Time": time.Duration(10 * 365 * 24 * time.Hour), "All Time": 10 * 365 * 24 * time.Hour,
"YTD": time.Duration(1 * time.Second), // this will be calculated "YTD": 1 * time.Second, // this will be calculated
"1Y": time.Duration(365 * 24 * time.Hour), "1Y": 365 * 24 * time.Hour,
"6M": time.Duration(365 / 2 * 24 * time.Hour), "6M": 365 / 2 * 24 * time.Hour,
"3M": time.Duration(365 / 4 * 24 * time.Hour), "3M": 365 / 4 * 24 * time.Hour,
"1M": time.Duration(365 / 12 * 24 * time.Hour), "1M": 365 / 12 * 24 * time.Hour,
"7D": time.Duration(24 * 7 * time.Hour), "7D": 24 * 7 * time.Hour,
"3D": time.Duration(24 * 3 * time.Hour), "3D": 24 * 3 * time.Hour,
"24H": time.Duration(24 * time.Hour), "24H": 24 * time.Hour,
"6H": time.Duration(6 * time.Hour), "6H": 6 * time.Hour,
"1H": time.Duration(1 * 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] rangeseconds := ct.chartRangesMap[ct.State.selectedChartRange]
if ct.State.selectedChartRange == "YTD" { 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 rangeseconds = time.Duration(ytd) * time.Second
} }
@ -216,7 +215,7 @@ func (ct *Cointop) PortfolioChart() error {
selectedChartRange := ct.State.selectedChartRange // cache here selectedChartRange := ct.State.selectedChartRange // cache here
rangeseconds := ct.chartRangesMap[selectedChartRange] rangeseconds := ct.chartRangesMap[selectedChartRange]
if selectedChartRange == "YTD" { if selectedChartRange == "YTD" {
ytd := time.Now().Unix() - int64(timeutil.BeginningOfYear().Unix()) ytd := time.Now().Unix() - timeutil.BeginningOfYear().Unix()
rangeseconds = time.Duration(ytd) * time.Second rangeseconds = time.Duration(ytd) * time.Second
} }

@ -182,19 +182,19 @@ var DefaultCurrency = "USD"
var DefaultChartRange = "1Y" var DefaultChartRange = "1Y"
// DefaultMaxChartWidth ... // DefaultMaxChartWidth ...
var DefaultMaxChartWidth int = 175 var DefaultMaxChartWidth = 175
// DefaultChartHeight ... // DefaultChartHeight ...
var DefaultChartHeight int = 10 var DefaultChartHeight = 10
// DefaultSortBy ... // DefaultSortBy ...
var DefaultSortBy = "rank" var DefaultSortBy = "rank"
// DefaultPerPage ... // DefaultPerPage ...
var DefaultPerPage uint = 100 var DefaultPerPage = uint(100)
// DefaultMaxPages ... // DefaultMaxPages ...
var DefaultMaxPages uint = 35 var DefaultMaxPages = uint(35)
// DefaultColorscheme ... // DefaultColorscheme ...
var DefaultColorscheme = "cointop" var DefaultColorscheme = "cointop"

@ -227,9 +227,9 @@ func (ct *Cointop) ConfigToToml() ([]byte, error) {
if !ok || entry.Coin == "" { if !ok || entry.Coin == "" {
continue continue
} }
var amount string = strconv.FormatFloat(entry.Holdings, 'f', -1, 64) amount := strconv.FormatFloat(entry.Holdings, 'f', -1, 64)
var coinName string = entry.Coin coinName := entry.Coin
var tuple []string = []string{coinName, amount} tuple := []string{coinName, amount}
holdingsIfc = append(holdingsIfc, tuple) holdingsIfc = append(holdingsIfc, tuple)
} }
sort.Slice(holdingsIfc, func(i, j int) bool { sort.Slice(holdingsIfc, func(i, j int) bool {

@ -377,7 +377,7 @@ func (ct *Cointop) SetKeybindings() error {
// TODO: use scrolling table // TODO: use scrolling table
keys := ct.SortedSupportedCurrencyConversions() keys := ct.SortedSupportedCurrencyConversions()
for i, k := range keys { 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 return nil

@ -19,8 +19,7 @@ type MarketbarView = ui.View
// NewMarketbarView returns a new marketbar view // NewMarketbarView returns a new marketbar view
func NewMarketbarView() *MarketbarView { func NewMarketbarView() *MarketbarView {
var view *MarketbarView = ui.NewView("marketbar") return ui.NewView("marketbar")
return view
} }
// UpdateMarketbar updates the market bar view // UpdateMarketbar updates the market bar view
@ -54,7 +53,7 @@ func (ct *Cointop) UpdateMarketbar() error {
var percentChange24H float64 var percentChange24H float64
for _, p := range ct.GetPortfolioSlice() { for _, p := range ct.GetPortfolioSlice() {
n := ((p.Balance / total) * p.PercentChange24H) n := (p.Balance / total) * p.PercentChange24H
if math.IsNaN(n) { if math.IsNaN(n) {
continue continue
} }

@ -10,8 +10,7 @@ type MenuView = ui.View
// NewMenuView returns a new menu view // NewMenuView returns a new menu view
func NewMenuView() *MenuView { func NewMenuView() *MenuView {
var view *MenuView = ui.NewView("menu") return ui.NewView("menu")
return view
} }
// HideMenu hides the menu view // HideMenu hides the menu view

@ -413,7 +413,7 @@ func (ct *Cointop) GoToGlobalIndex(idx int) error {
l := ct.TableRowsLen() l := ct.TableRowsLen()
atpage := idx / l atpage := idx / l
ct.SetPage(atpage) ct.SetPage(atpage)
rowIndex := (idx % l) rowIndex := idx % l
ct.HighlightRow(rowIndex) ct.HighlightRow(rowIndex)
ct.UpdateTable() ct.UpdateTable()
return nil return nil

@ -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) { if math.IsNaN(n) {
continue continue
} }

@ -14,8 +14,7 @@ type SearchFieldView = ui.View
// NewSearchFieldView returns a new search field view // NewSearchFieldView returns a new search field view
func NewSearchFieldView() *SearchFieldView { func NewSearchFieldView() *SearchFieldView {
var view *SearchFieldView = ui.NewView("searchfield") return ui.NewView("searchfield")
return view
} }
// InputView is structure for help view // InputView is structure for help view
@ -23,8 +22,7 @@ type InputView = ui.View
// NewInputView returns a new help view // NewInputView returns a new help view
func NewInputView() *InputView { func NewInputView() *InputView {
var view *InputView = ui.NewView("input") return ui.NewView("input")
return view
} }
// OpenSearch opens the search field // OpenSearch opens the search field

@ -15,8 +15,7 @@ type StatusbarView = ui.View
// NewStatusbarView returns a new statusbar view // NewStatusbarView returns a new statusbar view
func NewStatusbarView() *StatusbarView { func NewStatusbarView() *StatusbarView {
var view *StatusbarView = ui.NewView("statusbar") return ui.NewView("statusbar")
return view
} }
// UpdateStatusbar updates the statusbar view // UpdateStatusbar updates the statusbar view

@ -14,8 +14,7 @@ type TableView = ui.View
// NewTableView returns a new table view // NewTableView returns a new table view
func NewTableView() *TableView { func NewTableView() *TableView {
var view *TableView = ui.NewView("table") return ui.NewView("table")
return view
} }
const dots = "..." const dots = "..."

@ -128,8 +128,7 @@ type TableHeaderView = ui.View
// NewTableHeaderView returns a new table header view // NewTableHeaderView returns a new table header view
func NewTableHeaderView() *TableHeaderView { func NewTableHeaderView() *TableHeaderView {
var view *TableHeaderView = ui.NewView("table_header") return ui.NewView("table_header")
return view
} }
// GetActiveTableHeaders returns the list of active table headers // GetActiveTableHeaders returns the list of active table headers

@ -37,10 +37,10 @@ type Service struct {
// NewCoinGecko new service // NewCoinGecko new service
func NewCoinGecko(config *Config) *Service { func NewCoinGecko(config *Config) *Service {
var maxResultsPerPage uint = 250 // absolute max maxResultsPerPage := 250 // absolute max
var maxResults uint = 0 maxResults := uint(0)
var maxPages uint = 10 maxPages := uint(10)
var perPage uint = 100 perPage := uint(100)
if config.PerPage > 0 { if config.PerPage > 0 {
perPage = config.PerPage perPage = config.PerPage
} }

@ -45,7 +45,7 @@ func DamerauLevenshteinDistance(s1, s2 string) int {
// min returns the minimum number of passed int slices. // min returns the minimum number of passed int slices.
func min(is ...int) int { func min(is ...int) int {
min := int(math.MaxInt32) min := math.MaxInt32
for _, v := range is { for _, v := range is {
if min > v { if min > v {
min = v min = v

@ -8,8 +8,8 @@ import (
"github.com/acarl005/stripansi" "github.com/acarl005/stripansi"
) )
// AlignLeft align left // Left align left
func AlignLeft(t string, n int) string { func Left(t string, n int) string {
s := stripansi.Strip(t) s := stripansi.Strip(t)
slen := utf8.RuneCountInString(s) slen := utf8.RuneCountInString(s)
if slen > n { if slen > n {
@ -19,8 +19,8 @@ func AlignLeft(t string, n int) string {
return fmt.Sprintf("%s%s", t, strings.Repeat(" ", n-slen)) return fmt.Sprintf("%s%s", t, strings.Repeat(" ", n-slen))
} }
// AlignRight align right // Right align right
func AlignRight(t string, n int) string { func Right(t string, n int) string {
s := stripansi.Strip(t) s := stripansi.Strip(t)
slen := utf8.RuneCountInString(s) slen := utf8.RuneCountInString(s)
if slen > n { if slen > n {
@ -30,8 +30,8 @@ func AlignRight(t string, n int) string {
return fmt.Sprintf("%s%s", strings.Repeat(" ", n-slen), t) return fmt.Sprintf("%s%s", strings.Repeat(" ", n-slen), t)
} }
// AlignCenter align center // Center align center
func AlignCenter(t string, n int) string { func Center(t string, n int) string {
s := stripansi.Strip(t) s := stripansi.Strip(t)
slen := utf8.RuneCountInString(s) slen := utf8.RuneCountInString(s)
if slen > n { if slen > n {

@ -205,11 +205,11 @@ func (t *Table) Fprint(w io.Writer) {
var s string var s string
switch c.align { switch c.align {
case AlignLeft: case AlignLeft:
s = align.AlignLeft(c.name+" ", c.width) s = align.Left(c.name+" ", c.width)
case AlignRight: case AlignRight:
s = align.AlignRight(c.name+" ", c.width) s = align.Right(c.name+" ", c.width)
case AlignCenter: case AlignCenter:
s = align.AlignCenter(c.name+" ", c.width) s = align.Center(c.name+" ", c.width)
} }
fmt.Fprintf(w, "%s", s) fmt.Fprintf(w, "%s", s)
@ -237,11 +237,11 @@ func (t *Table) Fprint(w io.Writer) {
var s string var s string
switch c.align { switch c.align {
case AlignLeft: case AlignLeft:
s = align.AlignLeft(v, c.width) s = align.Left(v, c.width)
case AlignRight: case AlignRight:
s = align.AlignRight(v, c.width) s = align.Right(v, c.width)
case AlignCenter: case AlignCenter:
s = align.AlignCenter(v, c.width) s = align.Center(v, c.width)
} }
fmt.Fprintf(w, "%s", s) fmt.Fprintf(w, "%s", s)

@ -21,7 +21,7 @@ import (
g.PercentColor = termui.ColorBlue g.PercentColor = termui.ColorBlue
*/ */
const ColorUndef Attribute = Attribute(^uint16(0)) const ColorUndef = Attribute(^uint16(0))
type Gauge struct { type Gauge struct {
Block Block

Loading…
Cancel
Save