Skip to content

Commit

Permalink
feat: Replaced compact with separate option
Browse files Browse the repository at this point in the history
  • Loading branch information
achannarasappa committed Jan 31, 2021
1 parent bf0e934 commit a67e94e
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ ticker -w NET,AAPL,TSLA
|-w|--watchlist||comma separated list of symbols to watch|
|-e|--extra-info-exchange||display currency, exchange name, and quote delay for each quote |
|-q|--extra-info-fundamentals||display open price, high, low, and volume for each quote |
| |--compact||compact layout without separators between each quote|
|-s|--separate||layout with separators between each quote|

## Configuration

Expand Down
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var (
config cli.Config
watchlist string
refreshInterval int
compact bool
separate bool
extraInfoExchange bool
extraInfoFundamentals bool
rootCmd = &cobra.Command{
Expand All @@ -32,7 +32,7 @@ var (
ConfigPath: &configPath,
RefreshInterval: &refreshInterval,
Watchlist: &watchlist,
Compact: &compact,
Separate: &separate,
ExtraInfoExchange: &extraInfoExchange,
ExtraInfoFundamentals: &extraInfoFundamentals,
},
Expand All @@ -53,7 +53,7 @@ func init() {
rootCmd.Flags().StringVar(&configPath, "config", "", "config file (default is $HOME/.ticker.yaml)")
rootCmd.Flags().StringVarP(&watchlist, "watchlist", "w", "", "comma separated list of symbols to watch")
rootCmd.Flags().IntVarP(&refreshInterval, "interval", "i", 0, "refresh interval in seconds")
rootCmd.Flags().BoolVar(&compact, "compact", false, "compact layout without separators between each quote")
rootCmd.Flags().BoolVar(&separate, "separate", false, "layout with separators between each quote")
rootCmd.Flags().BoolVar(&extraInfoExchange, "extra-info-exchange", false, "display currency, exchange name, and quote delay for each quote")
rootCmd.Flags().BoolVar(&extraInfoFundamentals, "extra-info-fundamentals", false, "display open price, high, low, and volume for each quote")
}
Expand Down
6 changes: 3 additions & 3 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Config struct {
RefreshInterval int `yaml:"interval"`
Watchlist []string `yaml:"watchlist"`
Lots []position.Lot `yaml:"lots"`
Compact bool `yaml:"compact"`
Separate bool `yaml:"separate"`
ExtraInfoExchange bool `yaml:"extra-info-exchange"`
ExtraInfoFundamentals bool `yaml:"extra-info-fundamentals"`
}
Expand All @@ -24,7 +24,7 @@ type Options struct {
ConfigPath *string
RefreshInterval *int
Watchlist *string
Compact *bool
Separate *bool
ExtraInfoExchange *bool
ExtraInfoFundamentals *bool
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func read(fs afero.Fs, options Options, configFile *Config) (Config, error) {
}

config.RefreshInterval = getRefreshInterval(*options.RefreshInterval, configFile.RefreshInterval)
config.Compact = getBoolOption(*options.Compact, configFile.Compact)
config.Separate = getBoolOption(*options.Separate, configFile.Separate)
config.ExtraInfoExchange = getBoolOption(*options.ExtraInfoExchange, configFile.ExtraInfoExchange)
config.ExtraInfoFundamentals = getBoolOption(*options.ExtraInfoFundamentals, configFile.ExtraInfoFundamentals)

Expand Down
34 changes: 17 additions & 17 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var _ = Describe("Cli", func() {
watchlist string
refreshInterval int
configPath string
compact bool
separate bool
)

BeforeEach(func() {
Expand All @@ -76,11 +76,11 @@ var _ = Describe("Cli", func() {
ConfigPath: &configPath,
Watchlist: &watchlist,
RefreshInterval: &refreshInterval,
Compact: &compact,
Separate: &separate,
}
watchlist = "GME,BB"
refreshInterval = 0
compact = false
separate = false
configPath = ""
fs = afero.NewMemMapFs()
fs.MkdirAll("./", 0755)
Expand Down Expand Up @@ -193,36 +193,36 @@ var _ = Describe("Cli", func() {
})
})

Describe("compact option", func() {
When("compact flag is set as a cli argument", func() {
Describe("separate option", func() {
When("separate flag is set as a cli argument", func() {
It("should set the config to the cli argument value", func() {
compact = true
separate = true
Validate(&config, fs, options)(&cobra.Command{}, []string{})
Expect(config.Compact).To(Equal(true))
Expect(config.Separate).To(Equal(true))
})

When("the config file also has a compact flag defined", func() {
It("should set the compact flag from the cli argument", func() {
compact = true
config.Compact = false
When("the config file also has a separate flag defined", func() {
It("should set the separate flag from the cli argument", func() {
separate = true
config.Separate = false
Validate(&config, fs, options)(&cobra.Command{}, []string{})
Expect(config.Compact).To(Equal(true))
Expect(config.Separate).To(Equal(true))
})
})
})

When("compact flag is set in the config file", func() {
When("separate flag is set in the config file", func() {
It("should set the config to the cli argument value", func() {
config.Compact = true
config.Separate = true
Validate(&config, fs, options)(&cobra.Command{}, []string{})
Expect(config.Compact).To(Equal(true))
Expect(config.Separate).To(Equal(true))
})
})

When("compact flag is not set", func() {
When("separate flag is not set", func() {
It("should set a default watch interval", func() {
Validate(&config, fs, options)(&cobra.Command{}, []string{})
Expect(config.Compact).To(Equal(false))
Expect(config.Separate).To(Equal(false))
})
})
})
Expand Down
12 changes: 6 additions & 6 deletions internal/ui/component/watchlist/watchlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ type Model struct {
Width int
Quotes []quote.Quote
Positions map[string]position.Position
Compact bool
Separate bool
ExtraInfoExchange bool
ExtraInfoFundamentals bool
}

// NewModel returns a model with default values.
func NewModel(compact bool, extraInfoExchange bool, extraInfoFundamentals bool) Model {
func NewModel(separate bool, extraInfoExchange bool, extraInfoFundamentals bool) Model {
return Model{
Width: 80,
Compact: compact,
Separate: separate,
ExtraInfoExchange: extraInfoExchange,
ExtraInfoFundamentals: extraInfoFundamentals,
}
Expand Down Expand Up @@ -71,11 +71,11 @@ func (m Model) View() string {
)
}

return strings.Join(items, separator(m.Compact, m.Width))
return strings.Join(items, separator(m.Separate, m.Width))
}

func separator(isCompact bool, width int) string {
if !isCompact {
func separator(isSeparated bool, width int) string {
if isSeparated {
return "\n" + Line(
width,
Cell{
Expand Down
9 changes: 4 additions & 5 deletions internal/ui/component/watchlist/watchlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,21 +204,18 @@ var _ = Describe("Watchlist", func() {
expected := strings.Join([]string{
"BTC-USD ⦿ 50000.00",
"Bitcoin ↑ 10000.00 (20.00%)",
"⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯",
"TW ⦾ 109.04",
"ThoughtWorks ↑ 3.53 (5.65%)",
"⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯",
"GOOG ⦾ 2523.53",
"Google Inc. ↓ -32.02 (-1.35%)",
"⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯",
"AAPL 1.05",
"Apple Inc. 0.00 (0.00%)",
}, "\n")
Expect(removeFormatting(m.View())).To(Equal(expected))
})

When("the compact layout flag is set", func() {
It("should render a watchlist without separators", func() {
When("the separate layout flag is set", func() {
It("should render a watchlist with separators", func() {

m := NewModel(true, false, false)
m.Quotes = []Quote{
Expand Down Expand Up @@ -259,8 +256,10 @@ var _ = Describe("Watchlist", func() {
expected := strings.Join([]string{
"BTC-USD ⦿ 50000.00",
"Bitcoin ↑ 10000.00 (20.00%)",
"⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯",
"TW ⦾ 109.04",
"ThoughtWorks ↑ 3.53 (5.65%)",
"⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯",
"GOOG ⦾ 2523.53",
"Google Inc. ↓ -32.02 (-1.35%)",
}, "\n")
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func NewModel(config cli.Config, client *resty.Client) Model {
requestInterval: 3,
getQuotes: quote.GetQuotes(*client, symbols),
getPositions: position.GetPositions(aggregatedLots),
watchlist: watchlist.NewModel(config.Compact, config.ExtraInfoExchange, config.ExtraInfoFundamentals),
watchlist: watchlist.NewModel(config.Separate, config.ExtraInfoExchange, config.ExtraInfoFundamentals),
}
}

Expand Down

0 comments on commit a67e94e

Please sign in to comment.