Merge pull request #43 from issadarkthing/refactor-color

Better color support
This commit is contained in:
Raziman Mahathir 2021-03-17 20:06:56 +08:00 committed by GitHub
commit 70167ba6c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 146 additions and 67 deletions

View File

@ -72,6 +72,7 @@ Each panel has it's own additional keybinding. To view the available keybinding
| ? | toggle help |
| m | open repl |
| T | switch lyrics |
| c | show colors |
| Key (Playlist) | Description |

115
colors.go
View File

@ -1,68 +1,117 @@
package main
import (
"fmt"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type Colors struct {
accent tcell.Color
foreground tcell.Color
background tcell.Color
foreground tcell.Color
// title refers to now_playing_title in config file
title tcell.Color
popup tcell.Color
playlist tcell.Color
title tcell.Color
popup tcell.Color
playlistHi tcell.Color
playlistDir tcell.Color
queueHi tcell.Color
subtitle string
}
func init() {
tcell.ColorNames["none"] = tcell.ColorDefault
}
func newColor() *Colors {
defaultColors := map[string]string{
"Color.accent": "#008B8B",
"Color.foreground": "#FFFFFF",
"Color.background": "none",
"Color.popup": "#0A0F14",
"Color.now_playing_title": "#017702",
"Color.playlist": "#008B8B",
"Color.accent": "darkcyan",
"Color.background": "none",
"Color.foreground": "white",
"Color.popup": "black",
"Color.playlist_directory": "darkcyan",
"Color.playlist_highlight": "darkcyan",
"Color.queue_highlight": "darkcyan",
"Color.now_playing_title": "darkgreen",
"Color.subtitle": "darkgoldenrod",
}
anko := gomu.anko
// Validate hex color
// checks for invalid color and set default fallback
for k, v := range defaultColors {
// color from the config file
cfgColor := anko.GetString(k)
if validHexColor(cfgColor) {
continue
if _, ok := tcell.ColorNames[cfgColor]; !ok {
// use default value if invalid hex color was given
anko.Set(k, v)
}
// use default value if invalid hex color was given
anko.Set(k, v)
}
// handle none background color
var bgColor tcell.Color
bg := anko.GetString("Color.background")
if bg == "none" {
bgColor = tcell.ColorDefault
} else {
bgColor = tcell.GetColor(bg)
}
accent := anko.GetString("Color.accent")
background := anko.GetString("Color.background")
foreground := anko.GetString("Color.foreground")
popup := anko.GetString("Color.popup")
playlistDir := anko.GetString("Color.playlist_directory")
playlistHi := anko.GetString("Color.playlist_highlight")
queueHi := anko.GetString("Color.queue_highlight")
title := anko.GetString("Color.now_playing_title")
playlist := anko.GetString("Color.playlist")
subtitle := anko.GetString("Color.subtitle")
color := &Colors{
accent: tcell.GetColor(accent),
foreground: tcell.GetColor(foreground),
background: bgColor,
popup: tcell.GetColor(popup),
title: tcell.GetColor(title),
playlist: tcell.GetColor(playlist),
accent: tcell.ColorNames[accent],
foreground: tcell.ColorNames[foreground],
background: tcell.ColorNames[background],
popup: tcell.ColorNames[popup],
playlistDir: tcell.ColorNames[playlistDir],
playlistHi: tcell.ColorNames[playlistHi],
queueHi: tcell.ColorNames[queueHi],
title: tcell.ColorNames[title],
subtitle: subtitle,
}
return color
}
func colorsPopup() tview.Primitive {
textView := tview.NewTextView().
SetWrap(true).
SetDynamicColors(true).
SetWrap(true).
SetWordWrap(true)
textView.
SetBorder(true).
SetTitle(" Colors ").
SetBorderPadding(1, 1, 2, 2)
i := 0
colorPad := strings.Repeat(" ", 5)
for name := range tcell.ColorNames {
fmt.Fprintf(textView, "%20s [:%s]%s[:-] ", name, name, colorPad)
if i == 2 {
fmt.Fprint(textView, "\n")
i = 0
continue
}
i++
}
textView.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyEsc:
gomu.pages.RemovePage("show-color-popup")
gomu.popups.pop()
}
return event
})
return textView
}

View File

@ -445,6 +445,12 @@ func (c Command) defineCommands() {
}
})
c.define("show_colors", func() {
cp := colorsPopup()
gomu.pages.AddPage("show-color-popup", center(cp, 95, 40), true, true)
gomu.popups.push(cp)
})
for name, cmd := range c.commands {
err := gomu.anko.Define(name, cmd)
if err != nil {

View File

@ -130,8 +130,8 @@ func (g *Gomu) setFocusPanel(panel Panel) {
// Removes the color of the given panel
func (g *Gomu) setUnfocusPanel(panel Panel) {
g.prevPanel.SetBorderColor(g.colors.background)
g.prevPanel.SetTitleColor(g.colors.background)
g.prevPanel.SetBorderColor(g.colors.foreground)
g.prevPanel.SetTitleColor(g.colors.foreground)
}
// Quit the application and do the neccessary clean up

View File

@ -37,6 +37,7 @@ func newPlayingBar() *PlayingBar {
textView := tview.NewTextView().SetTextAlign(tview.AlignCenter)
textView.SetBackgroundColor(gomu.colors.background)
textView.SetDynamicColors(true)
frame := tview.NewFrame(textView).SetBorders(1, 1, 1, 1, 1, 1)
frame.SetBorder(true).SetTitle(" Now Playing ")
@ -107,10 +108,11 @@ func (p *PlayingBar) run() error {
}
gomu.app.QueueUpdateDraw(func() {
p.text.SetText(fmt.Sprintf("%s ┃%s┫ %s\n\n%v",
p.text.SetText(fmt.Sprintf("%s ┃%s┫ %s\n\n[%s]%v[-]",
fmtDuration(start),
progressBar,
fmtDuration(end),
gomu.colors.subtitle,
lyricText,
))
})

View File

@ -114,7 +114,7 @@ func newPlaylist(args Args) *Playlist {
}
root := tview.NewTreeNode(rootTextView).
SetColor(gomu.colors.accent)
SetColor(gomu.colors.playlistDir)
tree := tview.NewTreeView().SetRoot(root)
tree.SetBackgroundColor(gomu.colors.background)
@ -132,6 +132,7 @@ func newPlaylist(args Args) *Playlist {
}
root.SetReference(rootAudioFile)
root.SetColor(gomu.colors.playlistDir)
playlist.
SetTitle(playlist.defaultTitle).
@ -419,15 +420,17 @@ func (p *Playlist) createPlaylist(name string) error {
func (p *Playlist) setHighlight(currNode *tview.TreeNode) {
if p.prevNode != nil {
p.prevNode.SetColor(gomu.colors.background)
if p.prevNode.GetReference().(*AudioFile).isAudioFile {
p.prevNode.SetColor(gomu.colors.foreground)
} else {
p.prevNode.SetColor(gomu.colors.playlistDir)
}
}
currNode.SetColor(gomu.colors.accent)
currNode.SetColor(gomu.colors.playlistHi)
p.SetCurrentNode(currNode)
if currNode.GetReference().(*AudioFile).isAudioFile {
p.prevNode = currNode
}
p.prevNode = currNode
}
// Traverses the playlist and finds the AudioFile struct
@ -733,7 +736,7 @@ func populate(root *tview.TreeNode, rootPath string, sortMtime bool) error {
displayText := setDisplayText(audioFile)
child.SetReference(audioFile)
child.SetColor(gomu.colors.accent)
child.SetColor(gomu.colors.playlistDir)
child.SetText(displayText)
root.AddChild(child)
populate(child, path, sortMtime)

View File

@ -271,6 +271,7 @@ func helpPopup(panel Panel) {
"? toggle help",
"m open repl",
"T switch lyrics",
"c show colors",
}
list := tview.NewList().ShowSecondaryText(false)

View File

@ -358,10 +358,7 @@ func (q *Queue) shuffle() {
// Initiliaze new queue with default values
func newQueue() *Queue {
list := tview.NewList().
ShowSecondaryText(false)
list.SetBackgroundColor(gomu.colors.background)
list := tview.NewList()
queue := &Queue{
List: list,
@ -409,12 +406,19 @@ func newQueue() *Queue {
})
queue.updateTitle()
queue.SetBorder(true).SetTitleAlign(tview.AlignLeft)
queue.
SetSelectedBackgroundColor(tcell.ColorDarkCyan).
SetSelectedTextColor(tcell.ColorWhite).
SetHighlightFullLine(true).
SetBorderPadding(0, 0, 1, 1)
ShowSecondaryText(false).
SetSelectedBackgroundColor(gomu.colors.queueHi).
SetSelectedTextColor(gomu.colors.foreground).
SetHighlightFullLine(true)
queue.
SetBorder(true).
SetTitleAlign(tview.AlignLeft).
SetBorderPadding(0, 0, 1, 1).
SetBorderColor(gomu.colors.foreground).
SetBackgroundColor(gomu.colors.background)
return queue

View File

@ -224,14 +224,19 @@ module Emoji {
}
module Color {
# not all colors can be reproducible in terminal
# changing hex colors may or may not produce expected result
accent = "#008B8B"
# you may choose colors by pressing 'c'
accent = "darkcyan"
background = "none"
foreground = "#FFFFFF"
now_playing_title = "#017702"
playlist = "#008B8B"
popup = "#0A0F14"
foreground = "white"
popup = "black"
playlist_directory = "darkcyan"
playlist_highlight = "darkcyan"
queue_highlight = "darkcyan"
now_playing_title = "darkgreen"
subtitle = "darkgoldenrod"
}
# you can get the syntax highlighting for this language here:
@ -465,6 +470,7 @@ func start(application *tview.Application, args Args) {
'B': "rewind_fast",
'm': "repl",
'T': "switch_lyric",
'c': "show_colors",
}
for key, cmd := range cmds {

View File

@ -32,14 +32,19 @@ module Emoji {
}
module Color {
# not all colors can be reproducible in terminal
# changing hex colors may or may not produce expected result
accent = "#008B8B"
# you may choose colors by pressing 'c'
accent = "darkcyan"
background = "none"
foreground = "#FFFFFF"
now_playing_title = "#017702"
playlist = "#008B8B"
popup = "#0A0F14"
foreground = "white"
popup = "black"
playlist_directory = "darkcyan"
playlist_highlight = "darkcyan"
queue_highlight = "darkcyan"
now_playing_title = "darkgreen"
subtitle = "darkgoldenrod"
}
func fib(x) {
@ -115,6 +120,8 @@ Keybinds.def_q("i", func() {
})
})
Keybinds.def_g("c", show_colors)
# you can get the syntax highlighting for this language here:
# https://github.com/mattn/anko/tree/master/misc/vim
# vim: ft=anko