Skip to content

Commit

Permalink
feat: copy address to the clipboard (#275)
Browse files Browse the repository at this point in the history
closes #274
  • Loading branch information
caarlos0 authored Feb 22, 2024
1 parent 7bb7a3a commit 4eef722
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions wishlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ package wishlist

import (
"errors"
"fmt"
"net"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/muesli/termenv"
)

var (
copyIPAddr = key.NewBinding(
key.WithKeys("y"),
key.WithHelp("y", "copy address"),
)
enter = key.NewBinding(
key.WithKeys("enter", "o"),
key.WithHelp("enter/o", "connect"),
Expand All @@ -22,6 +29,9 @@ var (
func NewListing(endpoints []*Endpoint, client SSHClient, r *lipgloss.Renderer) *ListModel {
l := list.New(nil, list.NewDefaultDelegate(), 0, 0)
l.Title = "Directory Listing"
l.AdditionalFullHelpKeys = func() []key.Binding {
return []key.Binding{copyIPAddr}
}
l.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{enter}
}
Expand Down Expand Up @@ -126,17 +136,21 @@ func (m *ListModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if key.Matches(msg, list.DefaultKeyMap().Quit) && !m.list.SettingFilter() && m.list.FilterState() != list.FilterApplied {
m.quitting = true
}
if key.Matches(msg, copyIPAddr) && !m.list.SettingFilter() {
if w := m.selected(); w != nil {
host, _, _ := net.SplitHostPort(w.endpoint.Address)
termenv.Copy(host)
return m, m.list.NewStatusMessage(fmt.Sprintf("copied %q to the clipboard", host))
}

return m, nil
}
if key.Matches(msg, enter) {
if m.list.SettingFilter() {
break
}
selectedItem := m.list.SelectedItem()
if selectedItem == nil {
return m, nil
}
w, ok := selectedItem.(ItemWrapper)
if !ok {
// this should never happen
w := m.selected()
if w == nil {
return m, nil
}
cmd := m.client.For(w.endpoint)
Expand Down Expand Up @@ -168,6 +182,19 @@ func (m *ListModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, cmd
}

func (m *ListModel) selected() *ItemWrapper {
selectedItem := m.list.SelectedItem()
if selectedItem == nil {
return nil
}
w, ok := selectedItem.(ItemWrapper)
if !ok {
// this should never happen
return nil
}
return &w
}

// View comply with tea.Model interface.
func (m *ListModel) View() string {
if m.quitting {
Expand Down

0 comments on commit 4eef722

Please sign in to comment.