Skip to content

Commit

Permalink
Update alt coin link to use braces for template tags
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Mar 19, 2022
2 parents 4e5aa66 + c735c4a commit 7c063df
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 6 deletions.
5 changes: 5 additions & 0 deletions cointop/cointop.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type State struct {
favoritesCompactNotation bool
portfolioCompactNotation bool
enableMouse bool
altCoinLink string
}

// Cointop cointop
Expand Down Expand Up @@ -199,6 +200,9 @@ var DefaultCompactNotation = false
// DefaultEnableMouse ...
var DefaultEnableMouse = true

// DefaultAltCoinLink ...
var DefaultAltCoinLink = ""

// DefaultMaxChartWidth ...
var DefaultMaxChartWidth = 175

Expand Down Expand Up @@ -305,6 +309,7 @@ func NewCointop(config *Config) (*Cointop, error) {
},
compactNotation: DefaultCompactNotation,
enableMouse: DefaultEnableMouse,
altCoinLink: DefaultAltCoinLink,
tableCompactNotation: DefaultCompactNotation,
favoritesCompactNotation: DefaultCompactNotation,
portfolioCompactNotation: DefaultCompactNotation,
Expand Down
23 changes: 17 additions & 6 deletions cointop/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ type ConfigFileConfig struct {
RefreshRate interface{} `toml:"refresh_rate"`
CoinStructHash interface{} `toml:"coin_struct_version"`
CacheDir interface{} `toml:"cache_dir"`

CompactNotation interface{} `toml:"compact_notation"`
EnableMouse interface{} `toml:"enable_mouse"`

Table map[string]interface{} `toml:"table"`
Chart map[string]interface{} `toml:"chart"`
CompactNotation interface{} `toml:"compact_notation"`
EnableMouse interface{} `toml:"enable_mouse"`
AltCoinLink interface{} `toml:"alt_coin_link"` // TODO: should really be in API-specific section
Table map[string]interface{} `toml:"table"`
Chart map[string]interface{} `toml:"chart"`
}

// SetupConfig loads config file
Expand All @@ -77,6 +76,7 @@ func (ct *Cointop) SetupConfig() error {
ct.loadCacheDirFromConfig,
ct.loadCompactNotationFromConfig,
ct.loadEnableMouseFromConfig,
ct.loadAltCoinLinkFromConfig,
ct.loadPriceAlertsFromConfig,
ct.loadPortfolioFromConfig,
}
Expand Down Expand Up @@ -301,6 +301,7 @@ func (ct *Cointop) ConfigToToml() ([]byte, error) {
CoinStructHash: currentCoinHash,
CompactNotation: ct.State.compactNotation,
EnableMouse: ct.State.enableMouse,
AltCoinLink: ct.State.altCoinLink,
}

var b bytes.Buffer
Expand Down Expand Up @@ -528,6 +529,16 @@ func (ct *Cointop) loadEnableMouseFromConfig() error {
return nil
}

// loadAltCoinLinkFromConfig loads AltCoinLink setting from config file to struct
func (ct *Cointop) loadAltCoinLinkFromConfig() error {
log.Debug("loadAltCoinLinkFromConfig()")
if altCoinLink, ok := ct.config.AltCoinLink.(string); ok {
ct.State.altCoinLink = altCoinLink
}

return nil
}

// LoadAPIChoiceFromConfig loads API choices from config file to struct
func (ct *Cointop) loadAPIChoiceFromConfig() error {
log.Debug("loadAPIKeysFromConfig()")
Expand Down
1 change: 1 addition & 0 deletions cointop/default_shortcuts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func DefaultShortcuts() map[string]string {
"ctrl+d": "page_down",
"ctrl+f": "open_search",
"ctrl+n": "next_page",
"ctrl+o": "open_alt_link",
"ctrl+p": "previous_page",
"ctrl+r": "refresh",
"ctrl+R": "refresh",
Expand Down
2 changes: 2 additions & 0 deletions cointop/keybindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ func (ct *Cointop) SetKeybindingAction(shortcutKey string, action string) error
fn = ct.Keyfn(ct.NavigateLastLine)
case "open_link":
fn = ct.Keyfn(ct.OpenLink)
case "open_alt_link":
fn = ct.Keyfn(ct.OpenAltLink)
case "refresh":
fn = ct.Keyfn(ct.Refresh)
case "sort_column_asc":
Expand Down
26 changes: 26 additions & 0 deletions cointop/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cointop
import (
"fmt"
"net/url"
"strconv"
"strings"

"github.com/cointop-sh/cointop/pkg/ui"
Expand Down Expand Up @@ -203,6 +204,17 @@ func (ct *Cointop) RowLink() string {
return ct.api.CoinLink(coin.Slug)
}

// RowLink returns the row url link
func (ct *Cointop) RowAltLink() string {
log.Debug("RowAltLink()")
coin := ct.HighlightedRowCoin()
if coin == nil {
return ""
}

return ct.GetAltCoinLink(coin)
}

// RowLinkShort returns a shortened version of the row url link
func (ct *Cointop) RowLinkShort() string {
log.Debug("RowLinkShort()")
Expand All @@ -227,6 +239,20 @@ func (ct *Cointop) RowLinkShort() string {
return ""
}

func (ct *Cointop) GetAltCoinLink(coin *Coin) string {
if ct.State.altCoinLink == "" {
return ct.api.CoinLink(coin.Slug)
}

url := ct.State.altCoinLink
url = strings.Replace(url, "{{ID}}", coin.ID, -1)
url = strings.Replace(url, "{{NAME}}", coin.Name, -1)
url = strings.Replace(url, "{{RANK}}", strconv.Itoa(coin.Rank), -1)
url = strings.Replace(url, "{{SLUG}}", coin.Slug, -1)
url = strings.Replace(url, "{{SYMBOL}}", coin.Symbol, -1)
return url
}

// ToggleTableFullscreen toggles the table fullscreen mode
func (ct *Cointop) ToggleTableFullscreen() error {
log.Debug("ToggleTableFullscreen()")
Expand Down
7 changes: 7 additions & 0 deletions cointop/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ func (ct *Cointop) OpenLink() error {
return nil
}

// OpenLink opens the alternate url in a browser
func (ct *Cointop) OpenAltLink() error {
log.Debug("OpenAltLink()")
open.URL(ct.RowAltLink())
return nil
}

// GetBytes returns the interface in bytes form
func GetBytes(key interface{}) ([]byte, error) {
var buf bytes.Buffer
Expand Down
20 changes: 20 additions & 0 deletions pkg/eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,23 @@ func EvaluateExpressionToFloat64(input string, env interface{}) (float64, error)
}
return f64, nil
}

func EvaluateExpressionToString(input string, env interface{}) (string, error) {
input = strings.TrimSpace(input)
if input == "" {
return "", nil
}
program, err := expr.Compile(input, expr.Env(env))
if err != nil {
return "", err
}
result, err := expr.Run(program, env)
if err != nil {
return "", err
}
s, ok := result.(string)
if !ok {
return "", errors.New("expression did not return string type")
}
return s, nil
}

0 comments on commit 7c063df

Please sign in to comment.