From b9050272edd69824144409fa64e086db33e46a74 Mon Sep 17 00:00:00 2001 From: Michael Dvorkin Date: Sat, 16 May 2015 16:06:54 -0700 Subject: [PATCH] Refactored for more idiomatic Go --- cmd/mop.go | 4 ++-- profile.go | 7 ++++--- screen.go | 11 ++++++----- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/cmd/mop.go b/cmd/mop.go index 2d04db5..7726410 100644 --- a/cmd/mop.go +++ b/cmd/mop.go @@ -116,9 +116,9 @@ loop: //----------------------------------------------------------------------------- func main() { - screen := new(mop.Screen).Initialize() + screen := mop.NewScreen() defer screen.Close() - profile := new(mop.Profile).Initialize() + profile := mop.NewProfile() mainLoop(screen, profile) } diff --git a/profile.go b/profile.go index 6ad77f4..e86b3cd 100644 --- a/profile.go +++ b/profile.go @@ -27,9 +27,10 @@ type Profile struct { selectedColumn int // Stores selected column number when the column editor is active. } -// Initialize attempts to load the settings from ~/.moprc file. If the -// file is not there it gets created with the default values. -func (profile *Profile) Initialize() *Profile { +// 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()) if err != nil { // Set default values: profile.MarketRefresh = 12 // Market data gets fetched every 12s (5 times per minute). diff --git a/screen.go b/screen.go index bac95ea..8774410 100644 --- a/screen.go +++ b/screen.go @@ -21,15 +21,16 @@ type Screen struct { pausedAt *time.Time // Timestamp of the pause request or nil if none. } -// Initialize loads the Termbox, allocates and initializes layout and markup, -// and calculates current screen dimensions. Once initialized the screen is +// Initializes Termbox, creates screen along with layout and markup, and +// calculates current screen dimensions. Once initialized the screen is // ready for display. -func (screen *Screen) Initialize() *Screen { +func NewScreen() *Screen { if err := termbox.Init(); err != nil { panic(err) } - screen.layout = new(Layout).Initialize() - screen.markup = new(Markup).Initialize() + screen := &Screen{} + screen.layout = NewLayout() + screen.markup = NewMarkup() return screen.Resize() }