Skip to content

Commit

Permalink
Support full screen for processes
Browse files Browse the repository at this point in the history
  • Loading branch information
F1bonacc1 committed Aug 19, 2022
1 parent 10ca474 commit 8454d8a
Showing 1 changed file with 97 additions and 42 deletions.
139 changes: 97 additions & 42 deletions src/tui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,44 @@ import (
"github.com/rivo/tview"
)

type FullScrState int

const (
LogFull FullScrState = 0
ProcFull = 1
LogProcHalf = 2
)

type pcView struct {
procTable *tview.Table
statTable *tview.Table
appView *tview.Application
logsText *LogView
statusText *tview.TextView
helpText *tview.TextView
procNames []string
version string
logFollow bool
logFullScrn bool
loggedProc string
procTable *tview.Table
statTable *tview.Table
appView *tview.Application
logsText *LogView
statusText *tview.TextView
helpText *tview.TextView
procNames []string
version string
logFollow bool
fullScrState FullScrState
loggedProc string
}

func newPcView(version string, logLength int) *pcView {
pv := &pcView{
appView: tview.NewApplication(),
logsText: NewLogView(logLength),
statusText: tview.NewTextView().SetDynamicColors(true),
procNames: app.PROJ.GetLexicographicProcessNames(),
version: version,
logFollow: true,
logFullScrn: false,
helpText: tview.NewTextView().SetDynamicColors(true),
loggedProc: "",
appView: tview.NewApplication(),
logsText: NewLogView(logLength),
statusText: tview.NewTextView().SetDynamicColors(true),
procNames: app.PROJ.GetLexicographicProcessNames(),
version: version,
logFollow: true,
fullScrState: LogProcHalf,
helpText: tview.NewTextView().SetDynamicColors(true),
loggedProc: "",
}
pv.procTable = pv.createProcTable()
pv.statTable = pv.createStatTable()
pv.updateHelpTextView()
pv.appView.SetRoot(pv.createGrid(pv.logFullScrn), true).EnableMouse(true).SetInputCapture(pv.onAppKey)
pv.appView.SetRoot(pv.createGrid(pv.fullScrState), true).EnableMouse(true).SetInputCapture(pv.onAppKey)
if len(pv.procNames) > 0 {
name := pv.procNames[0]
pv.logsText.SetTitle(name)
Expand All @@ -55,21 +63,27 @@ func (pv *pcView) onAppKey(event *tcell.EventKey) *tcell.EventKey {
case tcell.KeyF10:
pv.terminateAppView()
case tcell.KeyF4:
pv.logFullScrn = !pv.logFullScrn
pv.appView.SetRoot(pv.createGrid(pv.logFullScrn), true)
case tcell.KeyF5:
pv.logFollow = !pv.logFollow
name := pv.getSelectedProcName()
if pv.logFollow {
pv.followLog(name)
go pv.updateLogs()
if pv.fullScrState == LogFull {
pv.fullScrState = LogProcHalf
} else {
pv.unFollowLog()
pv.fullScrState = LogFull
}
pv.appView.SetRoot(pv.createGrid(pv.fullScrState), true)
pv.updateHelpTextView()
case tcell.KeyF5:
pv.toggleLogFollow()
case tcell.KeyF6:
pv.logsText.ToggleWrap()
pv.updateHelpTextView()
case tcell.KeyF8:
if pv.fullScrState == ProcFull {
pv.fullScrState = LogProcHalf
} else {
pv.fullScrState = ProcFull
}
pv.appView.SetRoot(pv.createGrid(pv.fullScrState), true)
pv.onProcRowSpanChange()
pv.updateHelpTextView()
case tcell.KeyCtrlC:
pv.terminateAppView()
default:
Expand All @@ -87,7 +101,7 @@ func (pv *pcView) terminateAppView() {
if buttonLabel == "Quit" {
go pv.handleShutDown()
}
pv.appView.SetRoot(pv.createGrid(pv.logFullScrn), true)
pv.appView.SetRoot(pv.createGrid(pv.fullScrState), true)

})
// Display and focus the dialog
Expand Down Expand Up @@ -126,7 +140,7 @@ func (pv *pcView) fillTableData() {
}
}

func (pv pcView) getSelectedProcName() string {
func (pv *pcView) getSelectedProcName() string {
if pv.procTable == nil {
return ""
}
Expand All @@ -149,6 +163,34 @@ func (pv *pcView) onTableSelectionChange(row, column int) {
}
}

func (pv *pcView) onProcRowSpanChange() {
if pv.fullScrState == ProcFull && pv.logFollow {
pv.stopFollowLog()
}
}

func (pv *pcView) toggleLogFollow() {
if pv.logFollow {
pv.stopFollowLog()
} else {
name := pv.getSelectedProcName()
pv.startFollowLog(name)
}
}

func (pv *pcView) startFollowLog(name string) {
pv.logFollow = true
pv.followLog(name)
go pv.updateLogs()
pv.updateHelpTextView()
}

func (pv *pcView) stopFollowLog() {
pv.logFollow = false
pv.unFollowLog()
pv.updateHelpTextView()
}

func (pv *pcView) followLog(name string) {
pv.loggedProc = name
pv.logsText.Clear()
Expand Down Expand Up @@ -229,37 +271,49 @@ func (pv *pcView) updateHelpTextView() {
if pv.logsText.IsWrapOn() {
wrap = "Wrap Off"
}

follow := "Follow On"
if pv.logFollow {
follow = "Follow Off"
}

logScr := "Full"
procScr := "Full"
switch pv.fullScrState {
case LogFull:
logScr = "Half"
case ProcFull:
procScr = "Half"
}
pv.helpText.Clear()
fmt.Fprintf(pv.helpText, "%s ", "[lightskyblue:]LOGS:[-:-:-]")
fmt.Fprintf(pv.helpText, "%s ", "F4[black:green]Full Screen[-:-:-]")
fmt.Fprintf(pv.helpText, "%s%s%s ", "F4[black:green]", logScr, " Screen[-:-:-]")
fmt.Fprintf(pv.helpText, "%s%s%s ", "F5[black:green]", follow, "[-:-:-]")
fmt.Fprintf(pv.helpText, "%s%s%s ", "F6[black:green]", wrap, "[-:-:-]")
fmt.Fprintf(pv.helpText, "%s ", "[lightskyblue::b]PROCESS:[-:-:-]")
fmt.Fprintf(pv.helpText, "%s ", "F7[black:green]Start[-:-:-]")
fmt.Fprintf(pv.helpText, "%s%s%s ", "F8[black:green]", procScr, " Screen[-:-:-]")
fmt.Fprintf(pv.helpText, "%s ", "F9[black:green]Kill[-:-:-]")
fmt.Fprintf(pv.helpText, "%s ", "F10[black:green]Quit[-:-:-]")
}

func (pv pcView) createGrid(hideProcs bool) *tview.Grid {
logRowSpan := 1
logRow := 2
if hideProcs {
logRowSpan = 2
logRow = 1
}
func (pv *pcView) createGrid(fullScrState FullScrState) *tview.Grid {

grid := tview.NewGrid().
SetRows(3, 0, 0, 1).
//SetColumns(30, 0, 30).
SetBorders(true).
AddItem(pv.statTable, 0, 0, 1, 1, 0, 0, false).
AddItem(pv.logsText, logRow, 0, logRowSpan, 1, 0, 0, hideProcs).
AddItem(pv.helpText, 3, 0, 1, 1, 0, 0, false)
if !hideProcs {

switch fullScrState {
case LogFull:
grid.AddItem(pv.logsText, 1, 0, 2, 1, 0, 0, true)
case ProcFull:
grid.AddItem(pv.procTable, 1, 0, 2, 1, 0, 0, true)
case LogProcHalf:
grid.AddItem(pv.procTable, 1, 0, 1, 1, 0, 0, true)
grid.AddItem(pv.logsText, 2, 0, 1, 1, 0, 0, false)
}

grid.SetTitle("Process Compose")
Expand All @@ -274,6 +328,7 @@ func (pv *pcView) updateTable() {
})
}
}

func (pv *pcView) updateLogs() {
for {
pv.appView.QueueUpdateDraw(func() {
Expand Down

0 comments on commit 8454d8a

Please sign in to comment.