From 03e9a0d5ac5329a6e74f3e3bf34ef590c863a6d3 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 23 Mar 2018 14:44:36 +0200 Subject: Documentation and refactoring --- gomuks.go | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) (limited to 'gomuks.go') diff --git a/gomuks.go b/gomuks.go index 2dbbdff..0840bfe 100644 --- a/gomuks.go +++ b/gomuks.go @@ -22,7 +22,6 @@ import ( "path/filepath" "time" - "maunium.net/go/gomatrix" "maunium.net/go/gomuks/config" "maunium.net/go/gomuks/interface" "maunium.net/go/gomuks/matrix" @@ -31,6 +30,7 @@ import ( "maunium.net/go/tview" ) +// Gomuks is the wrapper for everything. type Gomuks struct { app *tview.Application ui *ui.GomuksUI @@ -41,6 +41,8 @@ type Gomuks struct { stop chan bool } +// NewGomuks creates a new Gomuks instance with everything initialized, +// but does not start it. func NewGomuks(enableDebug bool) *Gomuks { configDir := filepath.Join(os.Getenv("HOME"), ".config/gomuks") gmx := &Gomuks{ @@ -76,16 +78,7 @@ func NewGomuks(enableDebug bool) *Gomuks { return gmx } -func (gmx *Gomuks) Stop() { - gmx.debug.Print("Disconnecting from Matrix...") - gmx.matrix.Stop() - gmx.debug.Print("Cleaning up UI...") - gmx.app.Stop() - gmx.stop <- true - gmx.Save() - os.Exit(0) -} - +// Save saves the active session and message history. func (gmx *Gomuks) Save() { if gmx.config.Session != nil { gmx.debug.Print("Saving session...") @@ -95,6 +88,8 @@ func (gmx *Gomuks) Save() { gmx.ui.MainView().SaveAllHistory() } +// StartAutosave calls Save() every minute until it receives a stop signal +// on the Gomuks.stop channel. func (gmx *Gomuks) StartAutosave() { defer gmx.Recover() ticker := time.NewTicker(time.Minute) @@ -110,6 +105,21 @@ func (gmx *Gomuks) StartAutosave() { } } +// Stop stops the Matrix syncer, the tview app and the autosave goroutine, +// then saves everything and calls os.Exit(0). +func (gmx *Gomuks) Stop() { + gmx.debug.Print("Disconnecting from Matrix...") + gmx.matrix.Stop() + gmx.debug.Print("Cleaning up UI...") + gmx.app.Stop() + gmx.stop <- true + gmx.Save() + os.Exit(0) +} + +// Recover recovers a panic, closes the tcell screen and either re-panics or +// shows an user-friendly message about the panic depending on whether or not +// the debug mode is enabled. func (gmx *Gomuks) Recover() { if p := recover(); p != nil { if gmx.App().GetScreen() != nil { @@ -123,6 +133,10 @@ func (gmx *Gomuks) Recover() { } } +// Start opens a goroutine for the autosave loop and starts the tview app. +// +// If the tview app returns an error, it will be passed into panic(), which +// will be recovered as specified in Recover(). func (gmx *Gomuks) Start() { defer gmx.Recover() go gmx.StartAutosave() @@ -131,22 +145,22 @@ func (gmx *Gomuks) Start() { } } -func (gmx *Gomuks) Matrix() *gomatrix.Client { - return gmx.matrix.Client() -} - -func (gmx *Gomuks) MatrixContainer() ifc.MatrixContainer { +// Matrix returns the MatrixContainer instance. +func (gmx *Gomuks) Matrix() ifc.MatrixContainer { return gmx.matrix } +// App returns the tview Application instance. func (gmx *Gomuks) App() *tview.Application { return gmx.app } +// Config returns the Gomuks config instance. func (gmx *Gomuks) Config() *config.Config { return gmx.config } +// UI returns the Gomuks UI instance. func (gmx *Gomuks) UI() ifc.GomuksUI { return gmx.ui } -- cgit v1.2.3