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

feat(console): lula validation view #727

Merged
merged 12 commits into from
Oct 18, 2024
10 changes: 10 additions & 0 deletions src/internal/tui/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/charmbracelet/bubbles/viewport"
"github.com/davecgh/go-spew/spew"
"github.com/mattn/go-runewidth"
"gopkg.in/yaml.v3"
)

const (
Expand Down Expand Up @@ -122,3 +123,12 @@ func DumpToLog(msg ...any) {
spew.Fdump(DumpFile, msg)
}
}

func ToYamlString(input interface{}) (string, error) {
yamlData, err := yaml.Marshal(input)
if err != nil {
return "", fmt.Errorf("failed to marshal to YAML: %w", err)
}

return string(yamlData), nil
}
92 changes: 92 additions & 0 deletions src/internal/tui/common/detailed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package common

import (
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

type ContentType string

const (
detailWidth = 80
detailHeight = 20
widthScale = 0.9
heightScale = 0.9
)

type DetailOpenMsg struct {
Content string
WindowHeight int
WindowWidth int
}

type DetailModel struct {
Open bool
help HelpModel
contentViewport viewport.Model
width int
height int
}

func NewDetailModel() DetailModel {
help := NewHelpModel(true)
help.ShortHelp = ShortHelpDetail

return DetailModel{
help: help,
contentViewport: viewport.New(detailWidth, detailHeight),
}
}

func (m DetailModel) Init() tea.Cmd {
return nil
}

func (m DetailModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
var cmd tea.Cmd

switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.updateSizing(int(float64(msg.Height)*heightScale), int(float64(msg.Width)*widthScale))

case tea.KeyMsg:
k := msg.String()
if m.Open {
switch k {
case ContainsKey(k, CommonKeys.Cancel.Keys()):
m.Open = false
}
}

case DetailOpenMsg:
m.Open = true
m.contentViewport.GotoTop()
m.contentViewport.SetContent(msg.Content)
m.updateSizing(int(float64(msg.WindowHeight)*heightScale), int(float64(msg.WindowWidth)*widthScale))
}

m.contentViewport, cmd = m.contentViewport.Update(msg)
cmds = append(cmds, cmd)

return m, tea.Batch(cmds...)
}

func (m DetailModel) View() string {
overlayDetailStyle := OverlayStyle.
Width(m.width).
Height(m.height)

detailContent := lipgloss.JoinVertical(lipgloss.Top, m.contentViewport.View(), "\n", m.help.View())

return overlayDetailStyle.Render(detailContent)
}

func (m *DetailModel) updateSizing(height, width int) {
m.height = height
m.width = width

m.contentViewport.Height = height - OverlayStyle.GetVerticalPadding() - 2
m.contentViewport.Width = width - OverlayStyle.GetHorizontalPadding()
}
38 changes: 38 additions & 0 deletions src/internal/tui/common/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Keys struct {
Edit key.Binding
Save key.Binding
Newline key.Binding
Detail key.Binding
}

var CommonKeys = Keys{
Expand Down Expand Up @@ -77,6 +78,10 @@ var CommonKeys = Keys{
key.WithKeys("ctrl+s"),
key.WithHelp("ctrl+s", "save"),
),
Detail: key.NewBinding(
key.WithKeys("d"),
key.WithHelp("d", "detail"),
),
}

func ContainsKey(v string, a []string) string {
Expand Down Expand Up @@ -217,3 +222,36 @@ var (
{EditKeys.Confirm}, {EditKeys.NewLine}, {EditKeys.DeleteWord}, {EditKeys.Cancel},
}
)

type detailKeys struct {
Up key.Binding
Down key.Binding
Cancel key.Binding
}

var DetailKeys = detailKeys{
Up: key.NewBinding(
key.WithKeys("up", "k"),
key.WithHelp("↑/k", "scroll up"),
),
Down: key.NewBinding(
key.WithKeys("down", "j"),
key.WithHelp("↓/j", "scroll down"),
),
Cancel: key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc", "cancel"),
),
}

var (
ShortHelpDetail = []key.Binding{
DetailKeys.Up, DetailKeys.Down, DetailKeys.Cancel,
}
FullHelpDetailOneLine = []key.Binding{
DetailKeys.Up, DetailKeys.Down, DetailKeys.Cancel,
}
FullHelpDetail = [][]key.Binding{
{DetailKeys.Up}, {DetailKeys.Down}, {DetailKeys.Cancel},
}
)
4 changes: 4 additions & 0 deletions src/internal/tui/common/picker.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,7 @@ func (m PickerModel) View() string {
pickerContent := lipgloss.JoinVertical(lipgloss.Top, s.String(), m.help.View())
return overlayPickerStyle.Render(pickerContent)
}

func (m *PickerModel) UpdateItems(items []string) {
m.items = items
}
3 changes: 0 additions & 3 deletions src/internal/tui/common/popup.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ func (m PopupModel) Init() tea.Cmd {
}

func (m PopupModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
PrintToLog("in popup update")
DumpToLog(msg)
switch msg := msg.(type) {
case PopupMsg:
PrintToLog("in popup msg")
m.UpdateText(msg.Title, msg.Content, msg.Warning)

case PopupClose:
Expand All @@ -68,7 +66,6 @@ func (m PopupModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m PopupModel) View() string {
PrintToLog("in popup view")
popupStyle := OverlayWarnStyle.
Width(popupWidth).
Height(popupHeight)
Expand Down
Loading