Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .go-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.25.5
98 changes: 98 additions & 0 deletions cmd/open.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package cmd

import (
"context"
"errors"
"fmt"

tea "github.com/charmbracelet/bubbletea"
"github.com/meigma/blob"
blobcore "github.com/meigma/blob/core"
"github.com/spf13/cobra"

internalcfg "github.com/meigma/blob-cli/internal/config"
"github.com/meigma/blob-cli/internal/tui/open"
)

var openCmd = &cobra.Command{
Use: "open <ref>",
Short: "Open an interactive file browser for a blob archive",
Long: `Open an interactive TUI to explore blob archive contents.

Features a split-view layout with file tree on the left and content
preview on the right. Files load on-demand via HTTP range requests
for fast navigation.

Navigation:
Arrow keys Navigate file list / scroll preview
Tab Switch focus between tree and preview
Enter/Right Enter directory or preview file
Left Go to parent directory
c Copy selected file (prompts for path)
q/Esc Quit`,
Example: ` blob open ghcr.io/acme/configs:v1.0.0
blob open myalias`,
Args: cobra.ExactArgs(1),
RunE: runOpen,
}

func init() {
rootCmd.AddCommand(openCmd)
}

func runOpen(cmd *cobra.Command, args []string) error {
// 1. Get config from context
cfg := internalcfg.FromContext(cmd.Context())
if cfg == nil {
return errors.New("configuration not loaded")
}

// 2. Parse arguments
inputRef := args[0]

// 3. Resolve alias
resolvedRef := cfg.ResolveAlias(inputRef)

// 4. Create client
client, err := newClient(cfg)
if err != nil {
return fmt.Errorf("creating client: %w", err)
}

// 5. Create loader function for async archive loading
ctx := cmd.Context()
loader := makeArchiveLoader(ctx, client, resolvedRef)

// 6. Create and run the TUI (starts with loading screen)
model := open.New(resolvedRef, loader)
p := tea.NewProgram(
model,
tea.WithAltScreen(),
tea.WithMouseCellMotion(),
)

if _, err := p.Run(); err != nil {
return fmt.Errorf("running TUI: %w", err)
}

return nil
}

// makeArchiveLoader creates a LoadFunc that fetches the archive from the registry.
func makeArchiveLoader(ctx context.Context, client *blob.Client, ref string) open.LoadFunc {
return func() (*blob.IndexView, *blob.Archive, error) {
// Pull archive (lazy - does NOT download data blob)
archive, err := client.Pull(ctx, ref)
if err != nil {
return nil, nil, fmt.Errorf("accessing archive %s: %w", ref, err)
}

// Create index view from the archive's index data
index, err := blobcore.NewIndexView(archive.IndexData())
if err != nil {
return nil, nil, fmt.Errorf("parsing index: %w", err)
}

return index, archive, nil
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/agnivade/levenshtein v1.2.1 // indirect
github.com/alecthomas/chroma/v2 v2.23.1 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
Expand All @@ -47,6 +48,7 @@ require (
github.com/digitorus/pkcs7 v0.0.0-20230818184609-3a137a874352 // indirect
github.com/digitorus/timestamp v0.0.0-20231217203849-220c5c2851b7 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/dlclark/regexp2 v1.11.5 // indirect
github.com/docker/docker v28.5.2+incompatible // indirect
github.com/docker/go-connections v0.6.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERo
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/agnivade/levenshtein v1.2.1 h1:EHBY3UOn1gwdy/VbFwgo4cxecRznFk7fKWN1KOX7eoM=
github.com/agnivade/levenshtein v1.2.1/go.mod h1:QVVI16kDrtSuwcpd0p1+xMC6Z/VfhtCyDIjcwga4/DU=
github.com/alecthomas/chroma/v2 v2.23.1 h1:nv2AVZdTyClGbVQkIzlDm/rnhk1E9bU9nXwmZ/Vk/iY=
github.com/alecthomas/chroma/v2 v2.23.1/go.mod h1:NqVhfBR0lte5Ouh3DcthuUCTUpDC9cxBOfyMbMQPs3o=
github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0=
github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
Expand Down Expand Up @@ -150,6 +152,8 @@ github.com/digitorus/timestamp v0.0.0-20231217203849-220c5c2851b7 h1:lxmTCgmHE1G
github.com/digitorus/timestamp v0.0.0-20231217203849-220c5c2851b7/go.mod h1:GvWntX9qiTlOud0WkQ6ewFm0LPy5JUR1Xo0Ngbd1w6Y=
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/dlclark/regexp2 v1.11.5 h1:Q/sSnsKerHeCkc/jSTNq1oCm7KiVgUMZRDUoRu0JQZQ=
github.com/dlclark/regexp2 v1.11.5/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/docker/docker v28.5.2+incompatible h1:DBX0Y0zAjZbSrm1uzOkdr1onVghKaftjlSWt4AFexzM=
github.com/docker/docker v28.5.2+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/go-connections v0.6.0 h1:LlMG9azAe1TqfR7sO+NJttz1gy6KO7VJBh+pMmjSD94=
Expand Down
137 changes: 137 additions & 0 deletions internal/tui/components/copydialog/copydialog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Package copydialog provides a modal dialog for copying files.
package copydialog

import (
"path/filepath"

"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

// Model represents the copy dialog component state.
type Model struct {
input textinput.Model
sourcePath string
visible bool
width int
height int
}

// New creates a new copy dialog component.
func New() Model {
ti := textinput.New()
ti.Placeholder = "destination path"
ti.CharLimit = 256
ti.Width = 40

return Model{
input: ti,
}
}

// Show displays the dialog for copying a file.
func (m *Model) Show(sourcePath string) {
m.sourcePath = sourcePath
m.visible = true

// Set default destination to current directory with source filename
baseName := filepath.Base(sourcePath)
m.input.SetValue(baseName)
m.input.Focus()
m.input.CursorEnd()
}

// Hide hides the dialog.
func (m *Model) Hide() {
m.visible = false
m.input.Blur()
}

// Visible returns whether the dialog is visible.
func (m *Model) Visible() bool {
return m.visible
}

// SourcePath returns the source file path.
func (m *Model) SourcePath() string {
return m.sourcePath
}

// Destination returns the entered destination path.
func (m *Model) Destination() string {
return m.input.Value()
}

// SetSize updates the dialog dimensions.
func (m *Model) SetSize(width, height int) {
m.width = width
m.height = height
// Adjust input width based on dialog size
inputWidth := min(width-10, 60)
inputWidth = max(inputWidth, 20)
m.input.Width = inputWidth
}

// Init initializes the component.
//
//nolint:gocritic // hugeParam: value receiver required by tea.Model interface
func (m Model) Init() tea.Cmd {
return textinput.Blink
}

// Update handles messages.
//
//nolint:gocritic // hugeParam: value receiver required by tea.Model interface
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
if !m.visible {
return m, nil
}

var cmd tea.Cmd
m.input, cmd = m.input.Update(msg)
return m, cmd
}

// View renders the component.
//
//nolint:gocritic // hugeParam: value receiver required by tea.Model interface
func (m Model) View() string {
if !m.visible {
return ""
}

dialogWidth := 50
if m.width > 0 && m.width < dialogWidth+4 {
dialogWidth = m.width - 4
}

borderStyle := lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62")).
Padding(1, 2).
Width(dialogWidth)

titleStyle := lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("229"))

labelStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("252"))

hintStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("240"))

content := lipgloss.JoinVertical(lipgloss.Left,
titleStyle.Render("Copy File"),
"",
labelStyle.Render("Source: "+m.sourcePath),
"",
labelStyle.Render("Destination:"),
m.input.View(),
"",
hintStyle.Render("Enter: confirm Esc: cancel"),
)

return borderStyle.Render(content)
}
Loading