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: copy address to the clipboard #275

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Changes from all commits
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
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
Loading