mirror of
				https://github.com/taigrr/wtf
				synced 2025-01-18 04:03:14 -08:00 
			
		
		
		
	WTF-1031 Add scaffolding for main to support multiple WtfApp instances
Signed-off-by: Chris Cummer <chriscummer@me.com>
This commit is contained in:
		
							parent
							
								
									ee13fd83cc
								
							
						
					
					
						commit
						58318212f8
					
				@ -1,11 +1,14 @@
 | 
				
			|||||||
package app
 | 
					package app
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/gdamore/tcell"
 | 
						"github.com/gdamore/tcell"
 | 
				
			||||||
	_ "github.com/gdamore/tcell/terminfo/extended"
 | 
						_ "github.com/gdamore/tcell/terminfo/extended"
 | 
				
			||||||
 | 
						"github.com/logrusorgru/aurora"
 | 
				
			||||||
	"github.com/olebedev/config"
 | 
						"github.com/olebedev/config"
 | 
				
			||||||
	"github.com/radovskyb/watcher"
 | 
						"github.com/radovskyb/watcher"
 | 
				
			||||||
	"github.com/rivo/tview"
 | 
						"github.com/rivo/tview"
 | 
				
			||||||
@ -18,34 +21,36 @@ import (
 | 
				
			|||||||
// WtfApp is the container for a collection of widgets that are all constructed from a single
 | 
					// WtfApp is the container for a collection of widgets that are all constructed from a single
 | 
				
			||||||
// configuration file and displayed together
 | 
					// configuration file and displayed together
 | 
				
			||||||
type WtfApp struct {
 | 
					type WtfApp struct {
 | 
				
			||||||
 | 
						TViewApp *tview.Application
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	config         *config.Config
 | 
						config         *config.Config
 | 
				
			||||||
	configFilePath string
 | 
						configFilePath string
 | 
				
			||||||
	display        *Display
 | 
						display        *Display
 | 
				
			||||||
	focusTracker   FocusTracker
 | 
						focusTracker   FocusTracker
 | 
				
			||||||
	ghUser         *support.GitHubUser
 | 
						ghUser         *support.GitHubUser
 | 
				
			||||||
	pages          *tview.Pages
 | 
						pages          *tview.Pages
 | 
				
			||||||
	tviewApp       *tview.Application
 | 
					 | 
				
			||||||
	validator      *ModuleValidator
 | 
						validator      *ModuleValidator
 | 
				
			||||||
	widgets        []wtf.Wtfable
 | 
						widgets        []wtf.Wtfable
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewWtfApp creates and returns an instance of WtfApp
 | 
					// NewWtfApp creates and returns an instance of WtfApp
 | 
				
			||||||
func NewWtfApp(tviewApp *tview.Application, config *config.Config, configFilePath string) *WtfApp {
 | 
					func NewWtfApp(tviewApp *tview.Application, config *config.Config, configFilePath string) WtfApp {
 | 
				
			||||||
	wtfApp := WtfApp{
 | 
						wtfApp := WtfApp{
 | 
				
			||||||
		tviewApp:       tviewApp,
 | 
							TViewApp: tviewApp,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		config:         config,
 | 
							config:         config,
 | 
				
			||||||
		configFilePath: configFilePath,
 | 
							configFilePath: configFilePath,
 | 
				
			||||||
		pages:          tview.NewPages(),
 | 
							pages:          tview.NewPages(),
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	wtfApp.tviewApp.SetBeforeDrawFunc(func(s tcell.Screen) bool {
 | 
						wtfApp.TViewApp.SetBeforeDrawFunc(func(s tcell.Screen) bool {
 | 
				
			||||||
		s.Clear()
 | 
							s.Clear()
 | 
				
			||||||
		return false
 | 
							return false
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	wtfApp.widgets = MakeWidgets(wtfApp.tviewApp, wtfApp.pages, wtfApp.config)
 | 
						wtfApp.widgets = MakeWidgets(wtfApp.TViewApp, wtfApp.pages, wtfApp.config)
 | 
				
			||||||
	wtfApp.display = NewDisplay(wtfApp.widgets, wtfApp.config)
 | 
						wtfApp.display = NewDisplay(wtfApp.widgets, wtfApp.config)
 | 
				
			||||||
	wtfApp.focusTracker = NewFocusTracker(wtfApp.tviewApp, wtfApp.widgets, wtfApp.config)
 | 
						wtfApp.focusTracker = NewFocusTracker(wtfApp.TViewApp, wtfApp.widgets, wtfApp.config)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	githubAPIKey := readGitHubAPIKey(wtfApp.config)
 | 
						githubAPIKey := readGitHubAPIKey(wtfApp.config)
 | 
				
			||||||
	wtfApp.ghUser = support.NewGitHubUser(githubAPIKey)
 | 
						wtfApp.ghUser = support.NewGitHubUser(githubAPIKey)
 | 
				
			||||||
@ -63,14 +68,22 @@ func NewWtfApp(tviewApp *tview.Application, config *config.Config, configFilePat
 | 
				
			|||||||
		),
 | 
							),
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	wtfApp.tviewApp.SetInputCapture(wtfApp.keyboardIntercept)
 | 
						wtfApp.TViewApp.SetInputCapture(wtfApp.keyboardIntercept)
 | 
				
			||||||
	wtfApp.tviewApp.SetRoot(wtfApp.pages, true)
 | 
						wtfApp.TViewApp.SetRoot(wtfApp.pages, true)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return &wtfApp
 | 
						return wtfApp
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* -------------------- Exported Functions -------------------- */
 | 
					/* -------------------- Exported Functions -------------------- */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Run starts the underlying tview app
 | 
				
			||||||
 | 
					func (wtfApp *WtfApp) Run() {
 | 
				
			||||||
 | 
						if err := wtfApp.TViewApp.Run(); err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("\n%s %v\n", aurora.Red("ERROR"), err)
 | 
				
			||||||
 | 
							os.Exit(1)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Start initializes the app
 | 
					// Start initializes the app
 | 
				
			||||||
func (wtfApp *WtfApp) Start() {
 | 
					func (wtfApp *WtfApp) Start() {
 | 
				
			||||||
	go wtfApp.scheduleWidgets()
 | 
						go wtfApp.scheduleWidgets()
 | 
				
			||||||
@ -98,7 +111,7 @@ func (wtfApp *WtfApp) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
 | 
				
			|||||||
	switch event.Key() {
 | 
						switch event.Key() {
 | 
				
			||||||
	case tcell.KeyCtrlC:
 | 
						case tcell.KeyCtrlC:
 | 
				
			||||||
		wtfApp.Stop()
 | 
							wtfApp.Stop()
 | 
				
			||||||
		wtfApp.tviewApp.Stop()
 | 
							wtfApp.TViewApp.Stop()
 | 
				
			||||||
		wtfApp.DisplayExitMessage()
 | 
							wtfApp.DisplayExitMessage()
 | 
				
			||||||
	case tcell.KeyCtrlR:
 | 
						case tcell.KeyCtrlR:
 | 
				
			||||||
		wtfApp.refreshAllWidgets()
 | 
							wtfApp.refreshAllWidgets()
 | 
				
			||||||
@ -154,7 +167,7 @@ func (wtfApp *WtfApp) watchForConfigChanges() {
 | 
				
			|||||||
				wtfApp.Stop()
 | 
									wtfApp.Stop()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				config := cfg.LoadWtfConfigFile(wtfApp.configFilePath)
 | 
									config := cfg.LoadWtfConfigFile(wtfApp.configFilePath)
 | 
				
			||||||
				newApp := NewWtfApp(wtfApp.tviewApp, config, wtfApp.configFilePath)
 | 
									newApp := NewWtfApp(wtfApp.TViewApp, config, wtfApp.configFilePath)
 | 
				
			||||||
				openURLUtil := utils.ToStrs(config.UList("wtf.openUrlUtil", []interface{}{}))
 | 
									openURLUtil := utils.ToStrs(config.UList("wtf.openUrlUtil", []interface{}{}))
 | 
				
			||||||
				utils.Init(config.UString("wtf.openFileUtil", "open"), openURLUtil)
 | 
									utils.Init(config.UString("wtf.openFileUtil", "open"), openURLUtil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										34
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								main.go
									
									
									
									
									
								
							@ -1,10 +1,5 @@
 | 
				
			|||||||
package main
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Generators
 | 
					 | 
				
			||||||
// To generate the skeleton for a new TextWidget use 'WTF_WIDGET_NAME=MySuperAwesomeWidget go generate -run=text
 | 
					 | 
				
			||||||
//go:generate -command text go run generator/textwidget.go
 | 
					 | 
				
			||||||
//go:generate text
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"log"
 | 
						"log"
 | 
				
			||||||
@ -45,6 +40,14 @@ func setTerm(config *config.Config) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func makeWtfApp(config *config.Config, flagConfig string) app.WtfApp {
 | 
				
			||||||
 | 
						tviewApp = tview.NewApplication()
 | 
				
			||||||
 | 
						wtfApp := app.NewWtfApp(tviewApp, config, flagConfig)
 | 
				
			||||||
 | 
						wtfApp.Start()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return wtfApp
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* -------------------- Main -------------------- */
 | 
					/* -------------------- Main -------------------- */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func main() {
 | 
					func main() {
 | 
				
			||||||
@ -54,11 +57,11 @@ func main() {
 | 
				
			|||||||
	flags := flags.NewFlags()
 | 
						flags := flags.NewFlags()
 | 
				
			||||||
	flags.Parse()
 | 
						flags.Parse()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	hasCustom := flags.HasCustomConfig()
 | 
					 | 
				
			||||||
	cfg.Initialize(hasCustom)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	// Load the configuration file
 | 
						// Load the configuration file
 | 
				
			||||||
 | 
						cfg.Initialize(flags.HasCustomConfig())
 | 
				
			||||||
	config := cfg.LoadWtfConfigFile(flags.ConfigFilePath())
 | 
						config := cfg.LoadWtfConfigFile(flags.ConfigFilePath())
 | 
				
			||||||
 | 
						setTerm(config)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	flags.RenderIf(version, date, config)
 | 
						flags.RenderIf(version, date, config)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if flags.Profile {
 | 
						if flags.Profile {
 | 
				
			||||||
@ -69,15 +72,10 @@ func main() {
 | 
				
			|||||||
	openURLUtil := utils.ToStrs(config.UList("wtf.openUrlUtil", []interface{}{}))
 | 
						openURLUtil := utils.ToStrs(config.UList("wtf.openUrlUtil", []interface{}{}))
 | 
				
			||||||
	utils.Init(openFileUtil, openURLUtil)
 | 
						utils.Init(openFileUtil, openURLUtil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	setTerm(config)
 | 
						apps := []app.WtfApp{}
 | 
				
			||||||
 | 
						app := makeWtfApp(config, flags.Config)
 | 
				
			||||||
 | 
						apps = append(apps, app)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Build the application
 | 
						currentApp := apps[0]
 | 
				
			||||||
	tviewApp = tview.NewApplication()
 | 
						currentApp.Run()
 | 
				
			||||||
	wtfApp := app.NewWtfApp(tviewApp, config, flags.Config)
 | 
					 | 
				
			||||||
	wtfApp.Start()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if err := tviewApp.Run(); err != nil {
 | 
					 | 
				
			||||||
		fmt.Printf("\n%s %v\n", aurora.Red("ERROR"), err)
 | 
					 | 
				
			||||||
		os.Exit(1)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user