Merge pull request #56 from mendelgusmao/master

Add support for specifying a configuration file
master
Brandon Lee Camilleri 5 years ago committed by GitHub
commit 92434d7b0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      cmd/mop/main.go
  2. 28
      profile.go

@ -5,11 +5,18 @@
package main
import (
`github.com/mop-tracker/mop`
`github.com/nsf/termbox-go`
`time`
"flag"
"os/user"
"path"
"time"
"github.com/mop-tracker/mop"
"github.com/nsf/termbox-go"
)
// File name in user's home directory where we store the settings.
const defaultProfile = `.moprc`
const help = `Mop v0.2.0 -- Copyright (c) 2013-2016 by Michael Dvorkin. All Rights Reserved.
NO WARRANTIES OF ANY KIND WHATSOEVER. SEE THE LICENSE FILE FOR DETAILS.
@ -119,6 +126,14 @@ func main() {
screen := mop.NewScreen()
defer screen.Close()
profile := mop.NewProfile()
usr, err := user.Current()
if err != nil {
panic(err)
}
profileName := flag.String("profile", path.Join(usr.HomeDir, defaultProfile), "path to profile")
flag.Parse()
profile := mop.NewProfile(*profileName)
mainLoop(screen, profile)
}

@ -5,15 +5,11 @@
package mop
import (
`encoding/json`
`io/ioutil`
`os/user`
`sort`
"encoding/json"
"io/ioutil"
"sort"
)
// File name in user's home directory where we store the settings.
const moprc = `/.moprc`
// Profile manages Mop program settings as defined by user (ex. list of
// stock tickers). The settings are serialized using JSON and saved in
// the ~/.moprc file.
@ -25,13 +21,14 @@ type Profile struct {
Ascending bool // True when sort order is ascending.
Grouped bool // True when stocks are grouped by advancing/declining.
selectedColumn int // Stores selected column number when the column editor is active.
filename string // Path to the file in which the configuration is stored
}
// Creates the profile and attempts to load the settings from ~/.moprc file.
// If the file is not there it gets created with default values.
func NewProfile() *Profile {
profile := &Profile{}
data, err := ioutil.ReadFile(profile.defaultFileName())
func NewProfile(filename string) *Profile {
profile := &Profile{filename: filename}
data, err := ioutil.ReadFile(filename)
if err != nil { // Set default values:
profile.MarketRefresh = 12 // Market data gets fetched every 12s (5 times per minute).
profile.QuotesRefresh = 5 // Stock quotes get updated every 5s (12 times per minute).
@ -55,7 +52,7 @@ func (profile *Profile) Save() error {
return err
}
return ioutil.WriteFile(profile.defaultFileName(), data, 0644)
return ioutil.WriteFile(profile.filename, data, 0644)
}
// AddTickers updates the list of existing tikers to add the new ones making
@ -123,12 +120,3 @@ func (profile *Profile) Regroup() error {
profile.Grouped = !profile.Grouped
return profile.Save()
}
//-----------------------------------------------------------------------------
func (profile *Profile) defaultFileName() string {
usr, err := user.Current()
if err != nil {
panic(err)
}
return usr.HomeDir + moprc
}

Loading…
Cancel
Save