aboutsummaryrefslogtreecommitdiff
path: root/gomuks.go
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2018-04-18 18:35:24 +0300
committerTulir Asokan <tulir@maunium.net>2018-04-18 18:35:54 +0300
commitb17ff318c26aecdcf2c6719ed5b0872be5254c44 (patch)
tree43f5f9aa3ce300c15376860727ff13d3716f3751 /gomuks.go
parent3b2d5fa0349bf01217f6954813c669680cdd16d4 (diff)
Disconnect UI from main Gomuks struct. Fixes #21
Diffstat (limited to 'gomuks.go')
-rw-r--r--gomuks.go69
1 files changed, 15 insertions, 54 deletions
diff --git a/gomuks.go b/gomuks.go
index 494f182..ffdfad3 100644
--- a/gomuks.go
+++ b/gomuks.go
@@ -17,7 +17,6 @@
package main
import (
- "fmt"
"os"
"path/filepath"
"time"
@@ -26,44 +25,36 @@ import (
"maunium.net/go/gomuks/debug"
"maunium.net/go/gomuks/interface"
"maunium.net/go/gomuks/matrix"
- "maunium.net/go/gomuks/ui"
- "maunium.net/go/tview"
)
// Gomuks is the wrapper for everything.
type Gomuks struct {
- app *tview.Application
- ui *ui.GomuksUI
- matrix *matrix.Container
- debugMode bool
- config *config.Config
- stop chan bool
+ ui ifc.GomuksUI
+ matrix *matrix.Container
+ config *config.Config
+ stop chan bool
}
// NewGomuks creates a new Gomuks instance with everything initialized,
// but does not start it.
-func NewGomuks(enableDebug bool) *Gomuks {
+func NewGomuks(uiProvider ifc.UIProvider) *Gomuks {
configDir := filepath.Join(os.Getenv("HOME"), ".config/gomuks")
gmx := &Gomuks{
- app: tview.NewApplication(),
- stop: make(chan bool, 1),
- debugMode: enableDebug,
+ stop: make(chan bool, 1),
}
gmx.config = config.NewConfig(configDir)
- gmx.ui = ui.NewGomuksUI(gmx)
+ gmx.ui = uiProvider(gmx)
gmx.matrix = matrix.NewContainer(gmx)
+ gmx.ui.Init()
+
+ debug.OnRecover = gmx.ui.Finish
gmx.config.Load()
if len(gmx.config.UserID) > 0 {
_ = gmx.config.LoadSession(gmx.config.UserID)
}
- _ = gmx.matrix.InitClient()
-
- main := gmx.ui.InitViews()
- gmx.app.SetRoot(main, true)
-
return gmx
}
@@ -80,7 +71,7 @@ func (gmx *Gomuks) Save() {
// StartAutosave calls Save() every minute until it receives a stop signal
// on the Gomuks.stop channel.
func (gmx *Gomuks) StartAutosave() {
- defer gmx.Recover()
+ defer debug.Recover()
ticker := time.NewTicker(time.Minute)
for {
select {
@@ -100,36 +91,21 @@ func (gmx *Gomuks) Stop() {
debug.Print("Disconnecting from Matrix...")
gmx.matrix.Stop()
debug.Print("Cleaning up UI...")
- gmx.app.Stop()
+ gmx.ui.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 {
- gmx.App().GetScreen().Fini()
- }
- if gmx.debugMode {
- panic(p)
- } else {
- debug.PrettyPanic(p)
- }
- }
-}
-
// 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()
+ _ = gmx.matrix.InitClient()
+
go gmx.StartAutosave()
- if err := gmx.app.Run(); err != nil {
+ if err := gmx.ui.Start(); err != nil {
panic(err)
}
}
@@ -139,11 +115,6 @@ 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
@@ -153,13 +124,3 @@ func (gmx *Gomuks) Config() *config.Config {
func (gmx *Gomuks) UI() ifc.GomuksUI {
return gmx.ui
}
-
-func main() {
- enableDebug := len(os.Getenv("DEBUG")) > 0
- NewGomuks(enableDebug).Start()
-
- // We use os.Exit() everywhere, so exiting by returning from Start() shouldn't happen.
- time.Sleep(5 * time.Second)
- fmt.Println("Unexpected exit by return from Gomuks#Start().")
- os.Exit(2)
-}