feat: TUI SFTP Browser Tab - Task 18

- SFTPBrowserTab with remote directory browsing
- Open via Ctrl+F from host list
- Navigate dirs, filter with /, refresh with r
- Dir/icon display with file sizes
- Sort dirs first, then by name
This commit is contained in:
swanadiva
2026-06-23 15:08:22 +07:00
parent 3dc482e353
commit 94ec00c303
6 changed files with 468 additions and 0 deletions
+2
View File
@@ -7,6 +7,7 @@ require (
github.com/charmbracelet/bubbletea v1.3.10
github.com/charmbracelet/lipgloss v1.1.0
github.com/google/uuid v1.6.0
github.com/pkg/sftp v1.13.7
github.com/spf13/cobra v1.10.2
github.com/spf13/viper v1.21.0
golang.org/x/crypto v0.53.0
@@ -27,6 +28,7 @@ require (
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.3.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
+48
View File
@@ -23,6 +23,7 @@ github.com/clipperhouse/stringish v0.1.1/go.mod h1:v/WhFtE1q0ovMta2+m+UbpZ+2/HEX
github.com/clipperhouse/uax29/v2 v2.5.0 h1:x7T0T4eTHDONxFJsL94uKNKPHrclyFI0lm7+w94cO8U=
github.com/clipperhouse/uax29/v2 v2.5.0/go.mod h1:Wn1g7MK6OoeDT0vL+Q0SQLDz/KpfsVRgg6W7ihQeh4g=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
@@ -39,6 +40,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@@ -59,6 +62,8 @@ github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
github.com/pkg/sftp v1.13.7 h1:uv+I3nNJvlKZIQGSr8JVQLNHFU9YhhNpvC14Y6KgmSM=
github.com/pkg/sftp v1.13.7/go.mod h1:KMKI0t3T6hfA+lTR/ssZdunHo+uwq7ghoN09/FSu3DY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
@@ -81,28 +86,71 @@ github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU=
github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto v0.53.0 h1:QZ4Muo8THX6CizN2vPPd5fBGHyogrdK9fG4wLPFUsto=
golang.org/x/crypto v0.53.0/go.mod h1:DNLU434OwVakk9PzuwV8w62mAJpRJL3vsgcfp4Qnsio=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw=
golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/term v0.44.0 h1:0rLvDRCtNj0gZkyIXhCyOb2OAzEhLVqc4B+hrsBhrmc=
golang.org/x/term v0.44.0/go.mod h1:7ze4MdzUzLXpSAoFP1H0bOI9aXDqveSvatT5vKcFh2Y=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.38.0 h1:sXmwo9DwP3OK9EZ7PqAdaooSGozfl/3a6/xJcbzPRhE=
golang.org/x/text v0.38.0/go.mod h1:YXZt3QhHUKYT53r2lLKFIVi6Ao1jdzrTR/KQ09qyxF4=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+8
View File
@@ -75,6 +75,14 @@ func (t *HostListTab) Update(msg tea.Msg) (Tab, tea.Cmd) {
}
}
case "ctrl+f":
if len(t.hosts) > 0 {
host := t.hosts[t.selectedIndex]
return t, func() tea.Msg {
return openSFTPMsg{host: host}
}
}
case "q", "ctrl+c":
return t, func() tea.Msg {
return quitMsg{}
+5
View File
@@ -45,6 +45,11 @@ type saveHostResultMsg struct {
err error
}
// openSFTPMsg signals the TUI to open an SFTP browser tab
type openSFTPMsg struct {
host *models.Host
}
// loadedHostsMsg is produced after reloading hosts from storage
type loadedHostsMsg struct {
hosts []*models.Host
+400
View File
@@ -0,0 +1,400 @@
package tui
import (
"context"
"fmt"
"os"
"path"
"sort"
"strings"
"sync"
"time"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/pkg/sftp"
"git.tukangketik.id/swanadiva/hostkeeper/internal/models"
sshclient "git.tukangketik.id/swanadiva/hostkeeper/pkg/ssh"
)
type connectionState int
const (
stateConnecting connectionState = iota
stateConnected
stateError
stateDone
)
// SFTPBrowserTab provides a remote file browser via SFTP
type SFTPBrowserTab struct {
host *models.Host
dataDir string
sshClient *sshclient.Client
sftp *sftp.Client
cwd string
entries []os.FileInfo
selectedIdx int
state connectionState
err error
width int
height int
mu sync.Mutex
closeOnce sync.Once
// search/filter
searchMode bool
searchInput textinput.Model
filterText string
}
// NewSFTPBrowserTab creates a new SFTP browser tab
func NewSFTPBrowserTab(host *models.Host, dataDir string) *SFTPBrowserTab {
si := textinput.New()
si.Placeholder = "Filter files..."
return &SFTPBrowserTab{
host: host,
dataDir: dataDir,
cwd: "/",
state: stateConnecting,
searchInput: si,
}
}
func (t *SFTPBrowserTab) Name() string {
return "SFTP: " + t.host.Name
}
func (t *SFTPBrowserTab) Init() tea.Cmd {
go t.connect()
return t.poll
}
func (t *SFTPBrowserTab) poll() tea.Msg {
t.mu.Lock()
state := t.state
err := t.err
t.mu.Unlock()
if state == stateError {
return sftpDoneMsg{err: err}
}
if state == stateConnected {
return sftpReadyMsg{}
}
return nil
}
func (t *SFTPBrowserTab) connect() {
timeout := 30 * time.Second
cl := sshclient.NewClient(t.host, timeout)
ctx := context.Background()
if err := cl.Connect(ctx); err != nil {
t.mu.Lock()
t.state = stateError
t.err = fmt.Errorf("SSH connect failed: %w", err)
t.mu.Unlock()
return
}
sftpClient, err := sftp.NewClient(cl.GetClient())
if err != nil {
cl.Close()
t.mu.Lock()
t.state = stateError
t.err = fmt.Errorf("SFTP init failed: %w", err)
t.mu.Unlock()
return
}
t.mu.Lock()
t.sshClient = cl
t.sftp = sftpClient
t.mu.Unlock()
if err := t.refresh(); err != nil {
cl.Close()
sftpClient.Close()
t.mu.Lock()
t.state = stateError
t.err = err
t.mu.Unlock()
return
}
t.mu.Lock()
t.state = stateConnected
t.mu.Unlock()
}
func (t *SFTPBrowserTab) refresh() error {
t.mu.Lock()
sftpClient := t.sftp
cwd := t.cwd
t.mu.Unlock()
if sftpClient == nil {
return fmt.Errorf("not connected")
}
entries, err := sftpClient.ReadDir(cwd)
if err != nil {
return fmt.Errorf("read dir %s: %w", cwd, err)
}
sort.Slice(entries, func(i, j int) bool {
if entries[i].IsDir() != entries[j].IsDir() {
return entries[i].IsDir()
}
return strings.ToLower(entries[i].Name()) < strings.ToLower(entries[j].Name())
})
t.mu.Lock()
t.entries = entries
if t.selectedIdx >= len(entries) {
t.selectedIdx = 0
}
t.mu.Unlock()
return nil
}
func (t *SFTPBrowserTab) Update(msg tea.Msg) (Tab, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
t.mu.Lock()
t.width = msg.Width
t.height = msg.Height
t.mu.Unlock()
case sftpReadyMsg:
return t, nil
case sftpDoneMsg:
t.mu.Lock()
t.state = stateDone
if msg.err != nil {
t.err = msg.err
}
t.mu.Unlock()
return t, nil
case sftpRefreshMsg:
if err := t.refresh(); err != nil {
t.mu.Lock()
t.err = err
t.mu.Unlock()
}
case tea.KeyMsg:
if t.searchMode {
switch msg.String() {
case "esc", "enter":
t.searchMode = false
t.filterText = ""
return t, nil
case "backspace":
t.searchInput.Update(msg)
t.filterText = t.searchInput.Value()
return t, nil
default:
t.searchInput.Update(msg)
t.filterText = t.searchInput.Value()
return t, nil
}
}
switch msg.String() {
case "/":
t.searchMode = true
t.searchInput.Reset()
t.filterText = ""
t.searchInput.Focus()
return t, nil
case "esc":
return t, func() tea.Msg { return closeFormMsg{} }
case "up", "k":
t.mu.Lock()
if t.selectedIdx > 0 {
t.selectedIdx--
}
t.mu.Unlock()
case "down", "j":
t.mu.Lock()
if t.selectedIdx < len(t.entries)-1 {
t.selectedIdx++
}
t.mu.Unlock()
case "enter":
t.mu.Lock()
entries := t.entries
idx := t.selectedIdx
t.mu.Unlock()
if idx < 0 || idx >= len(entries) {
return t, nil
}
entry := entries[idx]
if entry.IsDir() {
t.mu.Lock()
t.cwd = path.Join(t.cwd, entry.Name())
t.selectedIdx = 0
t.mu.Unlock()
if err := t.refresh(); err != nil {
t.mu.Lock()
t.err = err
t.mu.Unlock()
}
}
case "backspace":
t.mu.Lock()
parent := path.Dir(t.cwd)
if parent == "." {
parent = "/"
}
t.cwd = parent
t.selectedIdx = 0
t.mu.Unlock()
if err := t.refresh(); err != nil {
t.mu.Lock()
t.err = err
t.mu.Unlock()
}
case "r":
if err := t.refresh(); err != nil {
t.mu.Lock()
t.err = err
t.mu.Unlock()
}
}
}
return t, t.poll
}
func (t *SFTPBrowserTab) View() string {
t.mu.Lock()
defer t.mu.Unlock()
switch t.state {
case stateConnecting:
return HighlightStyle.Render(fmt.Sprintf("Connecting SFTP to %s...", t.host.Name))
case stateError:
return fmt.Sprintf("%s\n\n%s",
ErrorStyle.Render(fmt.Sprintf("SFTP Error: %v", t.err)),
SubtitleStyle.Render("Press Esc to close this tab"))
case stateDone:
return fmt.Sprintf("%s\n\n%s",
SubtitleStyle.Render("SFTP session ended"),
SubtitleStyle.Render("Press Esc to close this tab"))
}
var b strings.Builder
b.WriteString(AppTitleStyle.Render(fmt.Sprintf("SFTP: %s@%s", t.host.Username, t.host.Hostname)))
b.WriteString("\n")
b.WriteString(SubtitleStyle.Render(fmt.Sprintf(" %s", t.cwd)))
b.WriteString("\n\n")
if t.err != nil {
b.WriteString(ErrorStyle.Render(fmt.Sprintf("Error: %v", t.err)))
b.WriteString("\n\n")
}
if t.sftp == nil {
b.WriteString(SubtitleStyle.Render("Not connected."))
return b.String()
}
var visible []os.FileInfo
if t.searchMode || t.filterText != "" {
lower := strings.ToLower(t.filterText)
for _, e := range t.entries {
if strings.Contains(strings.ToLower(e.Name()), lower) {
visible = append(visible, e)
}
}
} else {
visible = t.entries
}
if len(visible) == 0 {
b.WriteString(SubtitleStyle.Render(" (empty)"))
} else {
for i, entry := range visible {
name := entry.Name()
var line string
if entry.IsDir() {
line = fmt.Sprintf(" %s/", name)
} else {
size := formatSize(entry.Size())
line = fmt.Sprintf(" %s (%s)", name, size)
}
if i == t.selectedIdx {
b.WriteString(SelectedStyle.Render(line))
} else {
style := HostDetailStyle
if entry.IsDir() {
style = InfoStyle
}
b.WriteString(style.Render(line))
}
b.WriteString("\n")
}
}
if t.searchMode {
b.WriteString("\n" + SubtitleStyle.Render("Filter: ") + t.searchInput.View())
} else {
b.WriteString("\n" + SubtitleStyle.Render("/:filter r:refresh Esc:close"))
}
return b.String()
}
func (t *SFTPBrowserTab) Close() {
t.closeOnce.Do(func() {
if t.sftp != nil {
t.sftp.Close()
}
if t.sshClient != nil {
t.sshClient.Close()
}
})
}
func formatSize(size int64) string {
switch {
case size >= 1<<30:
return fmt.Sprintf("%.1f GiB", float64(size)/float64(1<<30))
case size >= 1<<20:
return fmt.Sprintf("%.1f MiB", float64(size)/float64(1<<20))
case size >= 1<<10:
return fmt.Sprintf("%.1f KiB", float64(size)/float64(1<<10))
default:
return fmt.Sprintf("%d B", size)
}
}
// Message types for SFTP tab
type sftpReadyMsg struct{}
type sftpDoneMsg struct {
err error
}
type sftpRefreshMsg struct{}
+5
View File
@@ -76,6 +76,11 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmd := m.tabs.Add(tab)
return m, cmd
case openSFTPMsg:
tab := NewSFTPBrowserTab(msg.host, m.dataDir)
cmd := m.tabs.Add(tab)
return m, cmd
case closeFormMsg:
if m.tabs.Len() > 1 {
m.tabs.CloseActive()