Skip to content

Commit

Permalink
add paramter text input (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviergodart committed Nov 7, 2024
1 parent a544ecf commit 3584699
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
10 changes: 10 additions & 0 deletions filesystem/keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type KeyMap struct {
EditDown string `json:"edit_down"`
EditLeft string `json:"edit_left"`

EditInput string `json:"edit_input"`

Bank string `json:"bank"`

AddBang string `json:"add_bang"`
Expand Down Expand Up @@ -78,6 +80,8 @@ func NewDefaultAzertyKeyMap() KeyMap {
EditDown: "ctrl+down",
EditLeft: "ctrl+left",

EditInput: "<",

Bank: "tab",

AddBang: "&",
Expand Down Expand Up @@ -139,6 +143,8 @@ func NewDefaultAzertyMacKeyMap() KeyMap {
EditDown: "ctrl+down",
EditLeft: "ctrl+left",

EditInput: "<",

Bank: "tab",

AddBang: "&",
Expand Down Expand Up @@ -199,6 +205,8 @@ func NewDefaultQwertyKeyMap() KeyMap {
EditDown: "ctrl+down",
EditLeft: "ctrl+left",

EditInput: "e",

Bank: "tab",

AddBang: "1",
Expand Down Expand Up @@ -260,6 +268,8 @@ func NewDefaultQwertyMacKeyMap() KeyMap {
EditDown: "ctrl+down",
EditLeft: "ctrl+left",

EditInput: "e",

Bank: "tab",

AddBang: "1",
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
)

require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/x/ansi v0.2.3 // indirect
github.com/charmbracelet/x/term v0.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/bubbles v0.20.0 h1:jSZu6qD8cRQ6k9OMfR1WlM+ruM8fkPWkHvQWD9LIutE=
Expand Down
9 changes: 8 additions & 1 deletion ui/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ func (m mainModel) renderControl() string {
}

var pane string
if m.edit {
if m.edit && m.input.Focused() {
pane = fmt.Sprintf(
"%s (%s) %s",
m.params[m.param].Name(),
m.params[m.param].Display(),
m.input.View(),
)
} else if m.edit {
pane = m.nodeEdit()
} else {
pane = m.gridInfo()
Expand Down
8 changes: 7 additions & 1 deletion ui/keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type keyMap struct {
EditDown key.Binding
EditLeft key.Binding

EditInput key.Binding

Bank key.Binding

AddBang key.Binding
Expand Down Expand Up @@ -75,7 +77,7 @@ func (k keyMap) ShortHelp() []key.Binding {
func (k keyMap) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Bank, k.AddBang, k.AddEuclid, k.AddPass, k.AddSpread, k.AddCycle, k.AddDice, k.AddToll, k.AddZone, k.AddHole, k.RootNoteUp, k.RootNoteDown, k.ScaleUp, k.ScaleDown, k.Cancel, k.SelectMidiDevice, k.FitGridToWindow, k.Help, k.Quit},
{k.Play, k.EditNode, k.RemoveNode, k.TriggerNode, k.MuteNode, k.MuteAllNode, k.Copy, k.Cut, k.Paste, k.Up, k.Right, k.Down, k.Left, k.SelectionUp, k.SelectionRight, k.SelectionDown, k.SelectionLeft, k.EditUp, k.EditDown, k.EditRight, k.EditLeft},
{k.Play, k.EditNode, k.RemoveNode, k.TriggerNode, k.MuteNode, k.MuteAllNode, k.Copy, k.Cut, k.Paste, k.Up, k.Right, k.Down, k.Left, k.SelectionUp, k.SelectionRight, k.SelectionDown, k.SelectionLeft, k.EditUp, k.EditDown, k.EditRight, k.EditLeft, k.EditInput},
}
}

Expand Down Expand Up @@ -176,6 +178,10 @@ func newKeyMap(keys filesystem.KeyMap) keyMap {
key.WithKeys(keys.EditLeft),
key.WithHelp(keys.EditLeft, "decrease parameter mode value"),
),
EditInput: key.NewBinding(
key.WithKeys(keys.EditInput),
key.WithHelp(keys.EditInput, "input parameter"),
),
Bank: key.NewBinding(
key.WithKeys(keys.Bank),
key.WithHelp(keys.Bank, "show bank"),
Expand Down
35 changes: 35 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ui

import (
"fmt"
"strconv"
"time"

"signls/core/common"
Expand All @@ -13,6 +14,7 @@ import (

"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
Expand Down Expand Up @@ -44,6 +46,7 @@ type mainModel struct {
viewport viewport
keymap keyMap
help help.Model
input textinput.Model
params []param.Param
gridParams []param.Param
bankClipboard filesystem.Grid
Expand All @@ -63,11 +66,15 @@ type mainModel struct {
// New creates a new mainModel that hols the ui state. It takes a new grid.
// Check the core package.
func New(config filesystem.Configuration, grid *field.Grid, bank *filesystem.Bank) tea.Model {
ti := textinput.New()
ti.CharLimit = 10
ti.Width = 12
model := mainModel{
bank: bank,
grid: grid,
keymap: newKeyMap(config.KeyMap),
help: help.New(),
input: ti,
gridParams: param.NewParamsForGrid(grid),
cursorX: 1,
cursorY: 1,
Expand Down Expand Up @@ -113,10 +120,38 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

case blinkMsg:
m.blink = !m.blink
m.input.Cursor.Blink = !m.input.Cursor.Blink
return m, blink()

case tea.KeyMsg:
if m.input.Focused() {
var cmd tea.Cmd
switch {
case key.Matches(msg, m.keymap.EditNode):
m.input.Blur()
val, err := strconv.Atoi(m.input.Value())
if err != nil {
val = m.params[m.param].Value()
}
m.params[m.param].Set(val)
return m, nil
case key.Matches(msg, m.keymap.Cancel):
m.input.Blur()
return m, nil
default:
m.input, cmd = m.input.Update(msg)
return m, cmd
}
}

switch {
case key.Matches(msg, m.keymap.EditInput):
if !m.edit {
return m, nil
}
m.input.Focus()
m.input.Reset()
return m, nil
case key.Matches(msg, m.keymap.Play):
m.grid.TogglePlay()
return m, nil
Expand Down

0 comments on commit 3584699

Please sign in to comment.