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.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/zkhvan/z
go 1.24.0

require (
github.com/atotto/clipboard v0.1.4
github.com/google/go-cmp v0.7.0
github.com/knadh/koanf/parsers/yaml v0.1.0
github.com/knadh/koanf/providers/file v1.1.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/MakeNowJust/heredoc/v2 v2.0.1 h1:rlCHh70XXXv7toz95ajQWOWQnN4WNLt0TdpZYIR/J6A=
github.com/MakeNowJust/heredoc/v2 v2.0.1/go.mod h1:6/2Abh5s+hc3g9nbWLe9ObDIOhaRrqsyY9MWy+4JdRM=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
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=
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/project/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (opts *Options) Run(ctx context.Context) error {
path = result.AbsolutePath
}

fmt.Fprintln(opts.io.Out, path)
fmt.Fprintf(opts.io.Out, "%s %s\n", result.Source, path)
}

return nil
Expand Down
70 changes: 65 additions & 5 deletions pkg/cmd/project/select/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ import (
"context"
"errors"
"fmt"
"os"

"github.com/MakeNowJust/heredoc/v2"
"github.com/atotto/clipboard"
"github.com/spf13/cobra"

"github.com/zkhvan/z/pkg/cmd/project/internal"
"github.com/zkhvan/z/pkg/cmdutil"
"github.com/zkhvan/z/pkg/fzf"
"github.com/zkhvan/z/pkg/gh"
"github.com/zkhvan/z/pkg/iolib"
"github.com/zkhvan/z/pkg/project"
"github.com/zkhvan/z/pkg/tmux"
)

type Options struct {
Expand All @@ -23,6 +27,7 @@ type Options struct {
RefreshCache bool
Remote bool
Local bool
Tmux bool
}

func NewCmdSelect(f *cmdutil.Factory, projectOpts *internal.ProjectOptions) *cobra.Command {
Expand All @@ -36,8 +41,9 @@ func NewCmdSelect(f *cmdutil.Factory, projectOpts *internal.ProjectOptions) *cob
Use: "select",
Short: "Select a project interactively",
Long: heredoc.Doc(`
Interactively select a project from the list of known projects using a fuzzy finder.
Outputs the selected project's absolute path to stdout.
Interactively select a project from the list of known projects
using a fuzzy finder. Outputs the selected project's absolute path
to stdout.
`),
RunE: func(cmd *cobra.Command, args []string) error {
if err := opts.Complete(cmd, args); err != nil {
Expand All @@ -50,6 +56,7 @@ func NewCmdSelect(f *cmdutil.Factory, projectOpts *internal.ProjectOptions) *cob
cmd.Flags().BoolVar(&opts.RefreshCache, "refresh-cache", false, "Refresh the cache")
cmd.Flags().BoolVar(&opts.Remote, "remote", true, "List remote projects")
cmd.Flags().BoolVar(&opts.Local, "local", true, "List local projects")
cmd.Flags().BoolVar(&opts.Tmux, "tmux", false, "Open in tmux")

return cmd
}
Expand Down Expand Up @@ -90,10 +97,46 @@ func (opts *Options) Run(ctx context.Context) error {
return err
}

shouldCD := true

fzfOpts := []fzf.Option[project.Project]{
fzf.WithIterator(projectByPath),
fzf.WithBinding("ctrl-y", func(p project.Project) error {
shouldCD = false

return clipboard.WriteAll(p.AbsolutePath)
}),
fzf.WithBinding("alt-enter", func(p project.Project) error {
shouldCD = false

opts := &gh.RepoViewOptions{Web: true}

switch p.Source {
case project.SourceTypeRemote, project.SourceTypeSynced:
opts.RepositoryID = p.RemoteID
case project.SourceTypeLocal:
opts.WorkingDirectory = p.AbsolutePath
default:
return fmt.Errorf("unsupported project source: %s", p.Source)
}

_, err = gh.NewClient().RepoView(ctx, opts)
return err
}),
}

header := "CTRL-Y: Yank | ALT-ENTER: View in browser"
if opts.Tmux {
header = fmt.Sprintf("ENTER: Change session | %s", header)
} else {
header = fmt.Sprintf("ENTER: Change directory | %s", header)
}
fzfOpts = append(fzfOpts, fzf.WithHeader[project.Project](header))

proj, err := fzf.One(
ctx,
results,
projectByPath,
fzfOpts...,
)
if errors.Is(err, fzf.ErrCanceled) {
return nil
Expand All @@ -102,10 +145,27 @@ func (opts *Options) Run(ctx context.Context) error {
return err
}

fmt.Fprint(opts.io.Out, proj.AbsolutePath)
if _, err := os.Lstat(proj.AbsolutePath); os.IsNotExist(err) {
output, err := service.CloneProject(ctx, proj)
if err != nil {
return err
}
fmt.Fprintln(opts.io.Out, output)
}

if shouldCD {
if opts.Tmux {
return tmux.NewSession(ctx, &tmux.NewOptions{
Name: proj.LocalID,
Dir: proj.AbsolutePath,
})
}

fmt.Fprintf(opts.io.Out, "cd %s\n", proj.AbsolutePath)
}
return nil
}

func projectByPath(p project.Project, _ int) string {
return p.LocalID
return fmt.Sprintf("%s %s", p.Source, p.LocalID)
}
11 changes: 4 additions & 7 deletions pkg/cmd/shell/zsh/z.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ z() {

if [[ $exit_code -ne 0 ]]; then
echo "Error: 'z project select' failed with exit code $exit_code" >&2
echo "$output" >&2
print -r "$output" >&2
return $exit_code
fi

if [[ -d "${output}" ]]; then
cd "${output}"
print -r "${output}"
if [[ "${output##*$'\n'}" == "cd "* ]]; then
builtin cd -- "${output##*$'\n'#cd }"
return 0
else
echo "${output}"
return 1
fi

else
command z "$@"
return $?
Expand Down
14 changes: 4 additions & 10 deletions pkg/cmd/shell/zsh/zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package zsh

import (
"context"
_ "embed"

"github.com/spf13/cobra"

"github.com/zkhvan/z/pkg/cmdutil"
"github.com/zkhvan/z/pkg/iolib"

_ "embed"
)

//go:embed z.zsh
Expand All @@ -25,22 +26,15 @@ func NewCmdZsh(f *cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Use: "zsh",
Short: "A zsh shell integration wrapper for z",
RunE: func(cmd *cobra.Command, args []string) error {
if err := opts.Complete(cmd, args); err != nil {
return err
}
RunE: func(cmd *cobra.Command, _ []string) error {
return opts.Run(cmd.Context())
},
}

return cmd
}

func (opts *Options) Complete(cmd *cobra.Command, args []string) error {
return nil
}

func (opts *Options) Run(ctx context.Context) error {
func (opts *Options) Run(_ context.Context) error {
_, err := opts.io.Out.Write(zshScript)
if err != nil {
return err
Expand Down
19 changes: 0 additions & 19 deletions pkg/cmd/tmux/psession/psession.go

This file was deleted.

74 changes: 0 additions & 74 deletions pkg/cmd/tmux/psession/use/use.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/cmd/tmux/session/use/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (opts *Options) Run(ctx context.Context) error {
return err
}

session, err := fzf.One(ctx, sessions, sessionByName)
session, err := fzf.One(ctx, sessions, fzf.WithIterator(sessionByName))
if errors.Is(err, fzf.ErrCanceled) {
return nil
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/cmd/tmux/tmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tmux
import (
"github.com/spf13/cobra"

psessionCmd "github.com/zkhvan/z/pkg/cmd/tmux/psession"
sessionCmd "github.com/zkhvan/z/pkg/cmd/tmux/session"
"github.com/zkhvan/z/pkg/cmdutil"
)
Expand All @@ -15,7 +14,6 @@ func NewCmdTmux(f *cmdutil.Factory) *cobra.Command {
}

cmd.AddCommand(sessionCmd.NewCmdSession(f))
cmd.AddCommand(psessionCmd.NewCmdPSession(f))

return cmd
}
5 changes: 5 additions & 0 deletions pkg/fd/fd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Options struct {
MaxDepth *int
NoIgnoreVCS *bool
Path *string
Follow *bool
}

func Run(ctx context.Context, pattern string, opts *Options) ([]string, error) {
Expand Down Expand Up @@ -42,6 +43,10 @@ func Run(ctx context.Context, pattern string, opts *Options) ([]string, error) {
cmd.Args = append(cmd.Args, "--no-ignore-vcs")
}

if opts.Follow != nil && *opts.Follow {
cmd.Args = append(cmd.Args, "--follow")
}

// If a path is specified, it is required to be after the pattern since
// it's a positional argument.
if opts.Path != nil && len(*opts.Path) > 0 {
Expand Down
Loading
Loading