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.ScrollToEnd()
list.SetMainTextColor(tcell.GetColor(theme.Colors["Service"]))
list.SetSecondaryTextColor(tcell.GetColor(theme.Colors["Description"]))
list.
SetDoneFunc(func() {
app.SetFocus(logView)

View File

@ -2,6 +2,7 @@ package main
import (
"os"
"strings"
"github.com/rivo/tview"
"github.com/spf13/cobra"
@ -22,10 +23,26 @@ var (
apperr error
app = tview.NewApplication()
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() {
RootCmd.PersistentFlags().StringVar(&themeArg, "theme", "terminal", "color scheme (ice, terminal)")
cobra.OnInitialize(parseTheme)
if err := RootCmd.Execute(); err != nil {
os.Exit(-1)
}

View File

@ -44,10 +44,10 @@ func (m *Menu) Draw(screen tcell.Screen) {
x++
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
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
}
}

View File

@ -98,7 +98,7 @@ func pipeReader(r *LogPipe, w io.Writer) {
}
func logFormatter(entry *sdjournal.JournalEntry) (string, error) {
color := "gray"
color := theme.Colors["Message"]
switch entry.Fields["PRIORITY"] {
case "0":
fallthrough
@ -107,14 +107,17 @@ func logFormatter(entry *sdjournal.JournalEntry) (string, error) {
case "2":
fallthrough
case "3":
color = "red"
color = theme.Colors["Error"]
case "4":
color = "darkred"
color = theme.Colors["Warning"]
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"),
theme.Colors["Service"],
entry.Fields["SYSLOG_IDENTIFIER"],
color,
entry.Fields["MESSAGE"]), nil

View File

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