-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to run commands before trying to connect
- Loading branch information
1 parent
105fddb
commit 8228c5a
Showing
9 changed files
with
307 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,118 @@ | ||
package app | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"syscall" | ||
|
||
"github.com/gdamore/tcell/v2" | ||
"github.com/rivo/tview" | ||
) | ||
|
||
var App = tview.NewApplication() | ||
var ( | ||
App *Application | ||
Styles *Theme | ||
) | ||
|
||
type Application struct { | ||
*tview.Application | ||
|
||
context context.Context | ||
cancelfn context.CancelFunc | ||
wg sync.WaitGroup | ||
} | ||
|
||
type Theme struct { | ||
SidebarTitleBorderColor string | ||
tview.Theme | ||
} | ||
|
||
var Styles = Theme{ | ||
SidebarTitleBorderColor: "#666A7E", | ||
SidebarTitleBorderColor string | ||
} | ||
|
||
func init() { | ||
theme := tview.Theme{ | ||
PrimitiveBackgroundColor: tcell.ColorDefault, | ||
ContrastBackgroundColor: tcell.ColorBlue, | ||
MoreContrastBackgroundColor: tcell.ColorGreen, | ||
BorderColor: tcell.ColorWhite, | ||
TitleColor: tcell.ColorWhite, | ||
GraphicsColor: tcell.ColorGray, | ||
PrimaryTextColor: tcell.ColorDefault.TrueColor(), | ||
SecondaryTextColor: tcell.ColorYellow, | ||
TertiaryTextColor: tcell.ColorGreen, | ||
InverseTextColor: tcell.ColorWhite, | ||
ContrastSecondaryTextColor: tcell.ColorBlack, | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
App = &Application{ | ||
Application: tview.NewApplication(), | ||
context: ctx, | ||
cancelfn: cancel, | ||
} | ||
|
||
App.register() | ||
App.EnableMouse(true) | ||
|
||
Styles = &Theme{ | ||
Theme: tview.Theme{ | ||
PrimitiveBackgroundColor: tcell.ColorDefault, | ||
ContrastBackgroundColor: tcell.ColorBlue, | ||
MoreContrastBackgroundColor: tcell.ColorGreen, | ||
BorderColor: tcell.ColorWhite, | ||
TitleColor: tcell.ColorWhite, | ||
GraphicsColor: tcell.ColorGray, | ||
PrimaryTextColor: tcell.ColorDefault.TrueColor(), | ||
SecondaryTextColor: tcell.ColorYellow, | ||
TertiaryTextColor: tcell.ColorGreen, | ||
InverseTextColor: tcell.ColorWhite, | ||
ContrastSecondaryTextColor: tcell.ColorBlack, | ||
}, | ||
SidebarTitleBorderColor: "#666A7E", | ||
} | ||
|
||
Styles.Theme = theme | ||
tview.Styles = theme | ||
tview.Styles = Styles.Theme | ||
} | ||
|
||
// Context returns the application context. | ||
func (a *Application) Context() context.Context { | ||
return a.context | ||
} | ||
|
||
// Register adds a task to the wait group and returns a | ||
// function that decrements the task count when called. | ||
// | ||
// The application will not stop until all registered tasks | ||
// have finished by calling the returned function! | ||
func (a *Application) Register() func() { | ||
a.wg.Add(1) | ||
return a.wg.Done | ||
} | ||
|
||
// Run starts and blocks until the application is stopped. | ||
func (a *Application) Run(root *tview.Pages) error { | ||
a.SetRoot(root, true) | ||
return a.Application.Run() | ||
} | ||
|
||
// Stop cancels the application context, waits for all | ||
// tasks to finish, and then stops the application. | ||
func (a *Application) Stop() { | ||
a.cancelfn() | ||
a.wg.Wait() | ||
a.Application.Stop() | ||
} | ||
|
||
// register listens for interrupt and termination signals to | ||
// gracefully handle shutdowns by calling the Stop method. | ||
func (a *Application) register() { | ||
c := make(chan os.Signal, 2) | ||
signal.Notify(c, os.Interrupt, syscall.SIGTERM) | ||
|
||
go func() { | ||
<-c | ||
a.Stop() | ||
<-c | ||
os.Exit(1) | ||
}() | ||
|
||
// Override the default input capture to listen for Ctrl+C | ||
// and make it send an interrupt signal to the channel to | ||
// trigger a graceful shutdown instead of closing the app | ||
// immediately without waiting for tasks to finish. | ||
a.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { | ||
if event.Key() == tcell.KeyCtrlC { | ||
c <- os.Interrupt | ||
return nil | ||
} | ||
return event | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.