Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a4f0951

Browse files
committed
feat: implement copy/paste with blank button support
This commit adds comprehensive copy/paste functionality including: 1. Copy/paste of regular buttons with confirmation dialog 2. Support for copying and pasting blank buttons 3. Confirmation dialog when overwriting existing buttons 4. Proper handling of button blanking via KV store 5. Centralized dialog styles in styles.go 6. Improved error handling and logging 7. Updated help text with new commands The changes allow users to: - Copy any button (including blank ones) with 'c' - Paste with 'v', with confirmation when overwriting - Blank buttons by copying a blank and pasting over existing
1 parent 923ba1a commit a4f0951

1 file changed

Lines changed: 60 additions & 11 deletions

File tree

cmd/tui/main.go

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,16 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
314314
key := fmt.Sprintf("instances.%s.devices.%s.profiles.%s.pages.%s.buttons.%s",
315315
m.currentInstance, m.currentDevice, m.currentProfile, m.currentPage, m.selectedPosition)
316316

317-
if button, err := buttons.GetButton(key); err == nil {
318-
if data, err := json.Marshal(button); err == nil {
319-
m.buttonClipboard = string(data)
320-
log.Debug().Str("copied_button", m.selectedPosition).Msg("Button copied")
317+
// If button doesn't exist, store empty string to represent a blank button
318+
if _, err := buttons.GetButton(key); err != nil {
319+
m.buttonClipboard = "{}"
320+
log.Debug().Str("copied_button", m.selectedPosition).Msg("Copied blank button")
321+
} else {
322+
if button, err := buttons.GetButton(key); err == nil {
323+
if data, err := json.Marshal(button); err == nil {
324+
m.buttonClipboard = string(data)
325+
log.Debug().Str("copied_button", m.selectedPosition).Msg("Button copied")
326+
}
321327
}
322328
}
323329
}
@@ -326,14 +332,28 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
326332
key := fmt.Sprintf("instances.%s.devices.%s.profiles.%s.pages.%s.buttons.%s",
327333
m.currentInstance, m.currentDevice, m.currentProfile, m.currentPage, m.selectedPosition)
328334

329-
_, kv := natsconn.GetNATSConn()
330-
// Only show confirmation if button exists
331-
if _, err := buttons.GetButton(key); err == nil {
332-
m.showPasteConfirmation = true
335+
// Check if we're pasting a blank button
336+
if m.buttonClipboard == "{}" {
337+
// Only show confirmation if there's an existing button
338+
if _, err := buttons.GetButton(key); err == nil {
339+
m.showPasteConfirmation = true
340+
} else {
341+
// No existing button, no need to confirm
342+
_, kv := natsconn.GetNATSConn()
343+
if err := kv.Delete(key); err == nil {
344+
kv.Delete(key + ".buffer")
345+
log.Debug().Str("blanked_button", m.selectedPosition).Msg("Button blanked")
346+
}
347+
}
333348
} else {
334-
// No existing button, paste directly
335-
if _, err := kv.Put(key, []byte(m.buttonClipboard)); err == nil {
336-
log.Debug().Str("pasted_button", m.selectedPosition).Msg("Button pasted")
349+
// Normal paste logic for non-blank buttons
350+
_, kv := natsconn.GetNATSConn()
351+
if _, err := buttons.GetButton(key); err == nil {
352+
m.showPasteConfirmation = true
353+
} else {
354+
if _, err := kv.Put(key, []byte(m.buttonClipboard)); err == nil {
355+
log.Debug().Str("pasted_button", m.selectedPosition).Msg("Button pasted")
356+
}
337357
}
338358
}
339359
}
@@ -372,6 +392,35 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
372392
}
373393
}
374394

395+
if m.showPasteConfirmation {
396+
if msg, ok := msg.(tea.KeyMsg); ok {
397+
switch msg.String() {
398+
case "y", "enter":
399+
key := fmt.Sprintf("instances.%s.devices.%s.profiles.%s.pages.%s.buttons.%s",
400+
m.currentInstance, m.currentDevice, m.currentProfile, m.currentPage, m.selectedPosition)
401+
_, kv := natsconn.GetNATSConn()
402+
403+
// If we're pasting a blank button, delete instead of put
404+
if m.buttonClipboard == "{}" {
405+
if err := kv.Delete(key); err == nil {
406+
kv.Delete(key + ".buffer")
407+
log.Debug().Str("blanked_button", m.selectedPosition).Msg("Button blanked")
408+
}
409+
} else {
410+
if _, err := kv.Put(key, []byte(m.buttonClipboard)); err == nil {
411+
log.Debug().Str("pasted_button", m.selectedPosition).Msg("Button pasted")
412+
}
413+
}
414+
m.showPasteConfirmation = false
415+
return m, nil
416+
case "n", "esc":
417+
m.showPasteConfirmation = false
418+
return m, nil
419+
}
420+
return m, nil // Ignore all other keys when paste dialog is open
421+
}
422+
}
423+
375424
return m, cmd
376425
}
377426

0 commit comments

Comments
 (0)