8000 Feature/help by anihm136 · Pull Request #74 · pesos/grofer · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Feature/help #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 78 additions & 9 deletions src/display/general/overallGraphs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ import (
"time"

ui "github.com/gizak/termui/v3"
h "github.com/pesos/grofer/src/display/misc"
info "github.com/pesos/grofer/src/general"
"github.com/pesos/grofer/src/utils"
)

var isCPUSet = false

var run = true
var helpVisible = false

// RenderCharts handles plotting graphs and charts for system stats in general.
func RenderCharts(ctx context.Context,
Expand All @@ -46,6 +48,8 @@ func RenderCharts(ctx context.Context,
var on sync.Once
var totalBytesRecv float64
var totalBytesSent float64
var help *h.HelpMenu = h.NewHelpMenu()
h.SelectHelpMenu("main")

// Get number of cores in machine
numCores := runtime.NumCPU()
Expand Down Expand Up @@ -80,14 +84,22 @@ func RenderCharts(ctx context.Context,
if isCPUSet {
for i := 0; i < numCores; i++ {
myPage.CPUCharts[i].SetRect(0, i*height, w/2, (i+1)*height)
ui.Render(myPage.CPUCharts[i])
}
}

// Adjust Grid dimensions
myPage.Grid.SetRect(w/2, 0, w, h-heightOffset)

ui.Render(myPage.Grid)
help.Resize(w, h)

if helpVisible {
ui.Render(help)
} else {
ui.Render(myPage.Grid)
for i := 0; i < numCores; i++ {
ui.Render(myPage.CPUCharts[i])
}
}
}

updateUI() // Initialize empty UI
Expand All @@ -107,8 +119,30 @@ func RenderCharts(ctx context.Context,
case "<Resize>":
updateUI()

case "s": // s to stop
pause()
case "?": // s to stop
helpVisible = !helpVisible
}
if helpVisible {
switch e.ID {
case "?":
updateUI()
case "<Escape>":
helpVisible = false
updateUI()
case "j", "<Down>":
help.List.ScrollDown()
ui.Render(help)
case "k", "<Up>":
help.List.ScrollUp()
ui.Render(help)
}
} else {
switch e.ID {
case "?":
updateUI()
case "s": //s to pause
pause()
}
}

case data := <-dataChannel:
Expand Down Expand Up @@ -206,7 +240,12 @@ func RenderCharts(ctx context.Context,
}

case <-tick: // Update page with new values
updateUI()
if !helpVisible {
ui.Render(myPage.Grid)
for i := 0; i < numCores; i++ {
ui.Render(myPage.CPUCharts[i])
}
}
}
}
}
Expand All @@ -216,6 +255,7 @@ func RenderCPUinfo(ctx context.Context,
refreshRate uint64) error {

var on sync.Once
var help *h.HelpMenu = h.NewHelpMenu()

if err := ui.Init(); err != nil {
return fmt.Errorf("failed to initialize termui: %v", err)
Expand All @@ -234,7 +274,12 @@ func RenderCPUinfo(ctx context.Context,
w, h := ui.TerminalDimensions()
ui.Clear()
myPage.Grid.SetRect(0, 0, w, h)
ui.Render(myPage.Grid)
help.Resize(w, h)
if helpVisible {
ui.Render(help)
} else {
ui.Render(myPage.Grid)
}
}

updateUI()
Expand All @@ -253,8 +298,30 @@ func RenderCPUinfo(ctx context.Context,
case "<Resize>":
updateUI()

case "s": // s to stop
pause()
case "?": // s to stop
helpVisible = !helpVisible
}
if helpVisible {
switch e.ID {
case "?":
updateUI()
case "<Escape>":
helpVisible = false
updateUI()
case "j", "<Down>":
help.List.ScrollDown()
ui.Render(help)
case "k", "<Up>":
help.List.ScrollUp()
ui.Render(help)
}
} else {
switch e.ID {
case "?":
updateUI()
case "s": //s to pause
pause()
}
}

case data := <-dataChannel: // Update chart values
Expand All @@ -279,7 +346,9 @@ func RenderCPUinfo(ctx context.Context,
}

case <-tick:
updateUI()
if !helpVisible {
ui.Render(myPage.Grid)
}
}
}
}
83 changes: 83 additions & 0 deletions src/display/misc/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright © 2020 The PES Open Source Team pesos@pes.edu

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package help

import (
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)

var KEYBINDS []string

type HelpMenu struct {
*widgets.List
}

func NewHelpMenu() *HelpMenu {
return &HelpMenu{
List: widgets.NewList(),
}
}

func (help *HelpMenu) Resize(termWidth, termHeight int) {
textWidth := 50
for _, line := range KEYBINDS {
if textWidth < len(line) {
textWidth = len(line) + 2
}
}
textHeight := len(KEYBINDS) + 3
x := (termWidth - textWidth) / 2
y := (termHeight - textHeight) / 2
if x < 0 {
x = 0
textWidth = termWidth
}
if y < 0 {
y = 0
textHeight = termHeight
}

help.List.SetRect(x, y, textWidth+x, textHeight+y)
}

func (help *HelpMenu) Draw(buf *ui.Buffer) {
help.List.Title = " Keybindings "

help.List.Rows = KEYBINDS
help.List.TextStyle = ui.NewStyle(ui.ColorYellow)
help.List.WrapText = false
help.List.Draw(buf)
}

func SelectHelpMenu(page string) {
switch page {
case "proc":
KEYBINDS = PROC_KEYBINDS
case "proc_pid":
KEYBINDS = PROC_PID_KEYBINDS
case "main":
KEYBINDS = MAIN_KEYBINDS
}
}

func maxInt(a int, b int) int {
if a > b {
return a
AE88 }
return b
}
42 changes: 42 additions & 0 deletions src/display/misc/keybinds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright © 2020 The PES Open Source Team pesos@pes.edu

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package help

var PROC_KEYBINDS = []string{
"Quit: q or <C-c>",
"",
"Process navigation:",
" - k and <Up>: up",
" - j and <Down>: down",
" - <C-u>: half page up",
" - <C-d>: half page down",
" - <C-b>: full page up",
" - <C-f>: full page down",
" - gg and <Home>: jump to top",
" - G and <End>: jump to bottom",
"",
"Process actions:",
" - K and <F9>: Kill process",
}

var MAIN_KEYBINDS = []string{
"Quit: q or <C-c>",
}

var PROC_PID_KEYBINDS = []string{
"Quit: q or <C-c>",
}
Loading
0