Added color theme support

This commit is contained in:
Christian Muehlhaeuser 2018-04-02 11:01:51 +02:00
parent ba3639228e
commit 0e848b9e6c
6 changed files with 73 additions and 8 deletions

View File

@ -40,6 +40,8 @@ func logsForm() (tview.Primitive, error) {
errLogView := tview.NewTextView() errLogView := tview.NewTextView()
errLogView.ScrollToEnd() errLogView.ScrollToEnd()
list.SetMainTextColor(tcell.GetColor(theme.Colors["Service"]))
list.SetSecondaryTextColor(tcell.GetColor(theme.Colors["Description"]))
list. list.
SetDoneFunc(func() { SetDoneFunc(func() {
app.SetFocus(logView) app.SetFocus(logView)

View File

@ -2,6 +2,7 @@ package main
import ( import (
"os" "os"
"strings"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -22,10 +23,26 @@ var (
apperr error apperr error
app = tview.NewApplication() app = tview.NewApplication()
menu = NewMenu(app) menu = NewMenu(app)
search string
search string
themeArg string
) )
func parseTheme() {
switch strings.ToLower(themeArg) {
case "ice":
theme = IceTheme
case "terminal":
theme = TerminalTheme
default:
panic("no such theme: " + themeArg)
}
}
func main() { func main() {
RootCmd.PersistentFlags().StringVar(&themeArg, "theme", "terminal", "color scheme (ice, terminal)")
cobra.OnInitialize(parseTheme)
if err := RootCmd.Execute(); err != nil { if err := RootCmd.Execute(); err != nil {
os.Exit(-1) os.Exit(-1)
} }

View File

@ -44,10 +44,10 @@ func (m *Menu) Draw(screen tcell.Screen) {
x++ x++
for _, i := range m.Items { for _, i := range m.Items {
tview.Print(screen, tcell.KeyNames[i.Shortcut], x, y, width-2, tview.AlignLeft, tcell.ColorYellow) tview.Print(screen, tcell.KeyNames[i.Shortcut], x, y, width-2, tview.AlignLeft, tcell.GetColor(theme.Colors["Shortcut"]))
x += 3 x += 3
tview.Print(screen, i.Text, x, y, width, tview.AlignLeft, tcell.ColorTeal) tview.Print(screen, i.Text, x, y, width, tview.AlignLeft, tcell.GetColor(theme.Colors["Menu"]))
x += runewidth.StringWidth(i.Text) + 2 x += runewidth.StringWidth(i.Text) + 2
} }
} }

View File

@ -98,7 +98,7 @@ func pipeReader(r *LogPipe, w io.Writer) {
} }
func logFormatter(entry *sdjournal.JournalEntry) (string, error) { func logFormatter(entry *sdjournal.JournalEntry) (string, error) {
color := "gray" color := theme.Colors["Message"]
switch entry.Fields["PRIORITY"] { switch entry.Fields["PRIORITY"] {
case "0": case "0":
fallthrough fallthrough
@ -107,14 +107,17 @@ func logFormatter(entry *sdjournal.JournalEntry) (string, error) {
case "2": case "2":
fallthrough fallthrough
case "3": case "3":
color = "red" color = theme.Colors["Error"]
case "4": case "4":
color = "darkred" color = theme.Colors["Warning"]
case "5": case "5":
color = "silver" color = theme.Colors["Notice"]
} }
return fmt.Sprintf("[green]%s [blue]%s [%s]%s\n",
return fmt.Sprintf("[%s]%s [%s]%s [%s]%s\n",
theme.Colors["Timestamp"],
time.Unix(0, int64(entry.RealtimeTimestamp)*int64(time.Microsecond)).Format("Jan 02 15:04:05"), time.Unix(0, int64(entry.RealtimeTimestamp)*int64(time.Microsecond)).Format("Jan 02 15:04:05"),
theme.Colors["Service"],
entry.Fields["SYSLOG_IDENTIFIER"], entry.Fields["SYSLOG_IDENTIFIER"],
color, color,
entry.Fields["MESSAGE"]), nil entry.Fields["MESSAGE"]), nil

View File

@ -37,6 +37,9 @@ func servicesForm() (tview.Primitive, error) {
pages := tview.NewPages() pages := tview.NewPages()
confirmDialog := tview.NewModal() confirmDialog := tview.NewModal()
list := NewServicesView() list := NewServicesView()
list.SetMainTextColor(tcell.GetColor(theme.Colors["Service"]))
list.SetSecondaryTextColor(tcell.GetColor(theme.Colors["Description"]))
err := list.loadModel(false, activeOnly) err := list.loadModel(false, activeOnly)
if err != nil { if err != nil {
return nil, err return nil, err

40
service-monitor/theme.go Normal file
View File

@ -0,0 +1,40 @@
package main
type Theme struct {
Name string
Colors map[string]string
}
var (
IceTheme = Theme{
Name: "Ice",
Colors: map[string]string{
"Error": "#aacdee",
"Warning": "#7997d6",
"Notice": "#5b73b4",
"Message": "gray",
"Timestamp": "#003d6b",
"Service": "#d5d8f0",
"Shortcut": "#00ffdf",
"Menu": "#85e7d2",
"Description": "#003d6b",
},
}
TerminalTheme = Theme{
Name: "Terminal",
Colors: map[string]string{
"Error": "red",
"Warning": "darkred",
"Notice": "silver",
"Message": "gray",
"Timestamp": "green",
"Service": "blue",
"Shortcut": "yellow",
"Menu": "teal",
"Description": "green",
},
}
theme = TerminalTheme
)