Adjust to highlight headings and totals
[pstop.git] / screen / screen.go
1 // This file configures the screen, basically remembering the size
2 // and foreground and background colours.
3 package screen
4
5 import (
6         "fmt"
7         "log"
8         "os"
9
10         "github.com/nsf/termbox-go"
11
12         "github.com/sjmudd/pstop/lib"
13         "github.com/sjmudd/pstop/version"
14 )
15
16 // this just allows me to use stuff with it
17 type TermboxScreen struct {
18         width, height int
19         fg, bg        termbox.Attribute
20 }
21
22 type TermboxAttribute termbox.Attribute
23
24 // reset the termbox to a clear screen
25 func (s *TermboxScreen) Initialise() {
26         err := termbox.Init()
27         if err != nil {
28                 fmt.Println("Could not start termbox for " + lib.MyName() + ". View ~/." + lib.MyName() + ".log for error messages.")
29                 log.Printf("Cannot start "+lib.MyName()+", termbox.Init() gave an error:\n%s\n", err)
30                 os.Exit(1)
31         }
32
33         s.Clear()
34         s.fg = termbox.ColorDefault
35         s.bg = termbox.ColorDefault
36
37         x, y := termbox.Size()
38         s.SetSize(x, y)
39 }
40
41 // clear the screen
42 func (s *TermboxScreen) Clear() {
43         termbox.Clear(termbox.ColorWhite, termbox.ColorBlack)
44 }
45
46 func (s *TermboxScreen) Flush() {
47         termbox.Flush()
48 }
49
50 func (s *TermboxScreen) SetSize(width, height int) {
51         // if we get bigger then clear out the bottom line
52         for x := 0; x < s.width; x++ {
53                 termbox.SetCell(x, s.height-1, ' ', s.fg, s.bg)
54         }
55         s.Flush()
56
57         s.width = width
58         s.height = height
59 }
60
61 func (s *TermboxScreen) Size() (int, int) {
62         return s.width, s.height
63 }
64
65 func (s *TermboxScreen) Height() int {
66         return s.height
67 }
68
69 // print the characters but don't print them outside the screen
70 func (s *TermboxScreen) PrintAt(x int, y int, text string) {
71         offset := 0
72         for c := range text {
73                 if (x + offset) < s.width {
74                         termbox.SetCell(x+offset, y, rune(text[c]), s.fg, s.bg)
75                         offset++
76                 }
77         }
78         s.Flush()
79 }
80
81 // print the characters in bold (for headings) but don't print them outside the screen
82 func (s *TermboxScreen) BoldPrintAt(x int, y int, text string) {
83         offset := 0
84         for c := range text {
85                 if (x + offset) < s.width {
86                         termbox.SetCell(x+offset, y, rune(text[c]), s.fg | termbox.AttrBold, s.bg)
87                         offset++
88                 }
89         }
90         s.Flush()
91 }
92
93 func (s *TermboxScreen) DisplayHelp() {
94         s.PrintAt(0, 0, lib.MyName()+" version "+version.Version()+" (C) 2014 Simon J Mudd <sjmudd@pobox.com>")
95
96         s.PrintAt(0, 2, "Program to show the top I/O information by accessing information from the")
97         s.PrintAt(0, 3, "performance_schema schema. Ideas based on mysql-sys.")
98
99         s.PrintAt(0, 5, "Keys:")
100         s.PrintAt(0, 6, "- - reduce the poll interval by 1 second (minimum 1 second)")
101         s.PrintAt(0, 7, "+ - increase the poll interval by 1 second")
102         s.PrintAt(0, 8, "h/? - this help screen")
103         s.PrintAt(0, 9, "q - quit")
104         s.PrintAt(0, 10, "t - toggle between showing time since resetting statistics or since P_S data was collected")
105         s.PrintAt(0, 11, "z - reset statistics")
106         s.PrintAt(0, 12, "<tab> or <right arrow> - change display modes between: latency, ops, file I/O, lock and user modes")
107         s.PrintAt(0, 13, "<left arrow> - change display modes to the previous screen (see above)")
108         s.PrintAt(0, 15, "Press h to return to main screen")
109 }
110
111 func (s *TermboxScreen) Close() {
112         termbox.Close()
113 }