Add filter flags to holdings command

pull/61/head
Miguel Mota 4 years ago
parent fd87bb6dda
commit d8f88d8cf3

@ -17,6 +17,7 @@ func HoldingsCmd() *cobra.Command {
var sortDesc bool
var format string = "table"
var humanReadable bool
var filter []string
holdingsCmd := &cobra.Command{
Use: "holdings",
@ -39,6 +40,7 @@ func HoldingsCmd() *cobra.Command {
return ct.PrintTotalHoldings(&cointop.TablePrintOptions{
HumanReadable: humanReadable,
Format: format,
Filter: filter,
})
}
@ -47,6 +49,7 @@ func HoldingsCmd() *cobra.Command {
SortDesc: sortDesc,
HumanReadable: humanReadable,
Format: format,
Filter: filter,
})
},
}
@ -58,7 +61,8 @@ func HoldingsCmd() *cobra.Command {
holdingsCmd.Flags().StringVarP(&config, "config", "c", "", fmt.Sprintf("Config filepath. (default %s)", cointop.DefaultConfigFilepath))
holdingsCmd.Flags().StringVarP(&sortBy, "sort-by", "s", sortBy, `Sort by column. Options are "name", "symbol", "price", "holdings", "balance", "24h"`)
holdingsCmd.Flags().BoolVarP(&sortDesc, "sort-desc", "d", sortDesc, "Sort in descending order")
holdingsCmd.Flags().StringVarP(&format, "format", "f", format, `Ouput format. Options are "table", "csv", "json"`)
holdingsCmd.Flags().StringVarP(&format, "format", "", format, `Ouput format. Options are "table", "csv", "json"`)
holdingsCmd.Flags().StringSliceVarP(&filter, "filter", "f", filter, `Filter portfolio entries by coin name or symbol, comma separated. Example: "btc,eth,doge"`)
return holdingsCmd
}

@ -323,6 +323,7 @@ type TablePrintOptions struct {
SortDesc bool
HumanReadable bool
Format string
Filter []string
}
// outputFormats is list of valid output formats
@ -355,6 +356,7 @@ func (ct *Cointop) PrintHoldingsTable(options *TablePrintOptions) error {
sortDesc := options.SortDesc
format := options.Format
humanReadable := options.HumanReadable
filter := options.Filter
holdings := ct.GetPortfolioSlice()
if format == "" {
@ -378,6 +380,20 @@ func (ct *Cointop) PrintHoldingsTable(options *TablePrintOptions) error {
symbol := ct.CurrencySymbol()
for i, entry := range holdings {
if filter != nil && len(filter) > 0 {
found := false
for _, item := range filter {
item = strings.ToLower(strings.TrimSpace(item))
if strings.ToLower(entry.Symbol) == item || strings.ToLower(entry.Name) == item {
found = true
break
}
}
if !found {
continue
}
}
percentHoldings := (entry.Balance / total) * 1e2
if math.IsNaN(percentHoldings) {
percentHoldings = 0
@ -467,9 +483,28 @@ func (ct *Cointop) PrintTotalHoldings(options *TablePrintOptions) error {
ct.RefreshPortfolioCoins()
humanReadable := options.HumanReadable
total := ct.GetPortfolioTotal()
symbol := ct.CurrencySymbol()
format := options.Format
filter := options.Filter
portfolio := ct.GetPortfolioSlice()
var total float64
for _, entry := range portfolio {
if filter != nil && len(filter) > 0 {
found := false
for _, item := range filter {
item = strings.ToLower(strings.TrimSpace(item))
if strings.ToLower(entry.Symbol) == item || strings.ToLower(entry.Name) == item {
found = true
break
}
}
if !found {
continue
}
}
total += entry.Balance
}
value := strconv.FormatFloat(total, 'f', -1, 64)

Loading…
Cancel
Save