Skip to content
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

Feature/tui poc #1

Merged
merged 18 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Integrated general template edit/add - missing case-when
  • Loading branch information
aurc committed Jun 5, 2022
commit fd52a7d4de84f98efc9328cd709aed166e5435b1
28 changes: 28 additions & 0 deletions internal/color/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package color

import "github.com/gdamore/tcell/v2"

const (
//ColorBackgroundField = tcell.Color236
ColorBackgroundField = tcell.ColorBlack
ColorForegroundField = tcell.ColorWhite
ColorSelectedBackground = tcell.Color69
ColorSelectedForeground = tcell.ColorWhite
ColorSecondaryBorder = tcell.Color240
)

var (
FieldStyle = tcell.StyleDefault.
Background(ColorBackgroundField).
Foreground(ColorForegroundField)
SelectStyle = tcell.StyleDefault.
Background(ColorSelectedBackground).
Foreground(ColorSelectedForeground)
)

const (
ClField = "[#ffaf00::b]"
ClWhite = "[#ffffff::-]"
ClNumeric = "[#00afff]"
ClString = "[#6A9F59]"
)
28 changes: 0 additions & 28 deletions internal/colour/colour.go

This file was deleted.

2 changes: 2 additions & 0 deletions internal/loggo/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type Loggo interface {
ShowModal(p tview.Primitive, width, height int)
DismissModal()
Config() *config.Config
StackView(p tview.Primitive)
PopView()
}

func NewLoggoApp(input <-chan string, configFile string) *LoggoApp {
Expand Down
23 changes: 18 additions & 5 deletions internal/loggo/app_scaffold.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package loggo

import (
"fmt"

"github.com/aurc/loggo/internal/config"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)

type appScaffold struct {
app *tview.Application
config *config.Config
pages *tview.Pages
modal *tview.Flex
app *tview.Application
config *config.Config
pages *tview.Pages
modal *tview.Flex
stackPages []tview.Primitive
}

type App interface {
Expand All @@ -28,7 +31,7 @@ func NewApp(configFile string) *appScaffold {

scaffold.app = app
scaffold.config = cfg

scaffold.stackPages = []tview.Primitive{}
scaffold.pages = tview.NewPages()

return scaffold
Expand All @@ -54,6 +57,16 @@ func (a *appScaffold) SetFocus(primitive tview.Primitive) {
a.app.SetFocus(primitive)
}

func (a *appScaffold) StackView(p tview.Primitive) {
a.stackPages = append(a.stackPages, p)
a.pages.AddPage(fmt.Sprintf(`_%d`, len(a.stackPages)), p, true, true)
}

func (a *appScaffold) PopView() {
a.pages.RemovePage(fmt.Sprintf(`_%d`, len(a.stackPages)))
a.stackPages = a.stackPages[:len(a.stackPages)-1]
}

func (a *appScaffold) ShowPrefabModal(text string, width, height int, buttons ...*tview.Button) {
modal := tview.NewFlex().SetDirection(tview.FlexRow)
modal.SetBackgroundColor(tcell.ColorDarkBlue)
Expand Down
126 changes: 126 additions & 0 deletions internal/loggo/color_picker_button.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package loggo

import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)

type ColorPickerButton struct {
tview.Flex
app Loggo
input *tview.InputField
button *tview.Button
label string
fieldWidth int
labelWidth int
labelColor tcell.Color
bgColor tcell.Color
fieldTextColor tcell.Color
fieldBgColor tcell.Color
labelText *tview.TextView
colorLabel *tview.TextView
changedFunc func(text string)
}

func NewColorPickerButton(app Loggo, label, value string, fieldWidth int, changedFunc func(string)) *ColorPickerButton {
c := &ColorPickerButton{
Flex: *tview.NewFlex().SetDirection(tview.FlexColumn),
app: app,
input: tview.NewInputField().SetText(value),
button: tview.NewButton("Choose"),
label: label,
labelText: tview.NewTextView().SetText(label),
colorLabel: tview.NewTextView().SetText(" ■ "),
changedFunc: changedFunc,
fieldWidth: fieldWidth,
}
c.makeLayout()
c.button.
SetSelectedFunc(func() {
cp := NewColorPickerView(app, "Choose a Color",
func(s string) {
c.input.SetText(s)
if c.changedFunc != nil {
c.changedFunc(s)
}
app.PopView()
}, nil, func() {
app.PopView()
})
app.StackView(cp)
go cp.SelectColor(c.input.GetText())
})
return c
}

func (c *ColorPickerButton) makeLayout() {
c.Flex.Clear().
AddItem(c.labelText, c.labelWidth, 1, false).
AddItem(c.input, c.fieldWidth-13, 1, false).
AddItem(c.colorLabel, 3, 1, false).
AddItem(c.button, 10, 1, false)
c.Flex.SetBackgroundColor(c.bgColor)
c.labelText.SetBackgroundColor(c.bgColor)
c.labelText.SetTextColor(c.labelColor)
c.input.SetBackgroundColor(c.bgColor)
c.input.SetFieldBackgroundColor(c.fieldBgColor)
c.input.SetFieldTextColor(c.fieldTextColor)
c.input.SetChangedFunc(c.changedFunc)
c.colorLabel.SetTextColor(tcell.GetColor(c.input.GetText()))
}

func (c *ColorPickerButton) Focus(delegate func(p tview.Primitive)) {
c.input.Focus(delegate)
}

func (c *ColorPickerButton) Blur() {
c.input.Blur()
}

func (c *ColorPickerButton) HasFocus() bool {
return c.input.HasFocus()
}

func (c *ColorPickerButton) SetFocusFunc(callback func()) *tview.Box {
return c.input.SetFocusFunc(callback)
}

func (c *ColorPickerButton) SetBlurFunc(callback func()) *tview.Box {
return c.input.SetBlurFunc(callback)
}

func (c *ColorPickerButton) SetLabel(label string) *ColorPickerButton {
c.label = label
c.labelText.SetText(label)
return c
}

func (c *ColorPickerButton) SetFieldWidth(width int) *ColorPickerButton {
c.fieldWidth = width
c.makeLayout()
return c
}

func (c *ColorPickerButton) GetLabel() string {
return c.label
}

func (c *ColorPickerButton) SetFormAttributes(
labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) tview.FormItem {
c.labelWidth = labelWidth
c.labelColor = labelColor
c.bgColor = bgColor
c.fieldTextColor = fieldTextColor
c.fieldBgColor = fieldBgColor
c.makeLayout()
return c
}

func (c *ColorPickerButton) GetFieldWidth() int {
return c.fieldWidth
}

func (c *ColorPickerButton) SetFinishedFunc(handler func(key tcell.Key)) tview.FormItem {
c.input.SetFinishedFunc(handler)
return c
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"fmt"
"sort"

"github.com/aurc/loggo/internal/colour"
"github.com/aurc/loggo/internal/color"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)

type ColourPickerView struct {
type ColorPickerView struct {
tview.Flex
app Loggo
contextMenu *tview.List
Expand All @@ -20,11 +20,12 @@ type ColourPickerView struct {
data *ColorPickerData
colors [][]string
title string
colorToCell map[string][]int
}

func NewColourPickerView(app Loggo, title string, onSelect func(string),
toggleFullScreenCallback, closeCallback func()) *ColourPickerView {
tv := &ColourPickerView{
func NewColorPickerView(app Loggo, title string, onSelect func(string),
toggleFullScreenCallback, closeCallback func()) *ColorPickerView {
tv := &ColorPickerView{
Flex: *tview.NewFlex(),
app: app,
onSelect: onSelect,
Expand All @@ -38,8 +39,15 @@ func NewColourPickerView(app Loggo, title string, onSelect func(string),
return tv
}

func (t *ColourPickerView) makeColorTable() {
func (t *ColorPickerView) SelectColor(color string) {
if rc, ok := t.colorToCell[color]; ok {
t.table.Select(rc[0], rc[1])
}
}

func (t *ColorPickerView) makeColorTable() {
const columns = 5
t.colorToCell = make(map[string][]int)
col := 0
row := 0
var currRow []string
Expand All @@ -52,6 +60,7 @@ func (t *ColourPickerView) makeColorTable() {
for _, c := range sortedCols {
if col < columns {
currRow = append(currRow, c)
t.colorToCell[c] = []int{row, col}
col++
if col == columns {
t.colors = append(t.colors, currRow)
Expand All @@ -66,15 +75,15 @@ func (t *ColourPickerView) makeColorTable() {
}
}

func (t *ColourPickerView) makeUIComponents() {
func (t *ColorPickerView) makeUIComponents() {
t.data = &ColorPickerData{
colourPickerView: t,
}
t.contextMenu = tview.NewList()
t.contextMenu.
SetBorder(true).
SetTitle("Context Menu").
SetBackgroundColor(colour.ColourBackgroundField)
SetBackgroundColor(color.ColorBackgroundField)

t.table = tview.NewTable().
SetSelectable(true, true).
Expand Down Expand Up @@ -108,17 +117,17 @@ func (t *ColourPickerView) makeUIComponents() {
})
}

func (t *ColourPickerView) makeLayouts() {
func (t *ColorPickerView) makeLayouts() {
t.makeContextMenu()
t.Flex.Clear().SetDirection(tview.FlexColumn).
AddItem(t.contextMenu, 30, 1, false).
AddItem(t.table, 0, 2, true).
SetBackgroundColor(colour.ColourBackgroundField).
SetBackgroundColor(color.ColorBackgroundField).
SetBorder(true).
SetTitle(t.title)
}

func (t *ColourPickerView) makeContextMenu() {
func (t *ColorPickerView) makeContextMenu() {
t.contextMenu.Clear().ShowSecondaryText(false).SetBorderPadding(0, 0, 1, 1)
t.contextMenu.
ShowSecondaryText(false)
Expand All @@ -143,7 +152,7 @@ func (t *ColourPickerView) makeContextMenu() {

type ColorPickerData struct {
tview.TableContentReadOnly
colourPickerView *ColourPickerView
colourPickerView *ColorPickerView
}

func (d *ColorPickerData) GetCell(row, column int) *tview.TableCell {
Expand Down
Loading