diff options
| -rw-r--r-- | flags/flags.go | 1 | ||||
| -rw-r--r-- | main.go | 8 | ||||
| -rw-r--r-- | menu/source.go | 6 | ||||
| -rw-r--r-- | menu/target.go | 10 | ||||
| -rw-r--r-- | model/target.go | 4 | ||||
| -rw-r--r-- | run/edit.go | 23 | ||||
| -rw-r--r-- | runner/base.go | 51 | ||||
| -rw-r--r-- | runner/gnumake/make.go | 9 | ||||
| -rw-r--r-- | runner/gnumake/target.go | 12 | ||||
| -rw-r--r-- | runner/just/just.go | 9 | ||||
| -rw-r--r-- | runner/just/target.go | 8 | ||||
| -rw-r--r-- | runner/python/file.go | 22 | ||||
| -rw-r--r-- | runner/python/indexer.go | 4 | ||||
| -rw-r--r-- | runner/python/proj.go | 12 | ||||
| -rw-r--r-- | runner/python/runner.go | 3 | ||||
| -rw-r--r-- | runner/shell/hydrated.go | 4 | ||||
| -rw-r--r-- | runner/shell/target.go | 4 |
17 files changed, 114 insertions, 76 deletions
diff --git a/flags/flags.go b/flags/flags.go index e74017d..756d95b 100644 --- a/flags/flags.go +++ b/flags/flags.go @@ -25,4 +25,5 @@ var ( List = pflag.BoolP("list", "l", false, "list available targets and exit") // Inline means pik does not go to the terminal alt screen Inline = pflag.BoolP("inline", "i", false, "if true, will force alt screen; if forced false, will disable alt screen") + Edit = pflag.Bool("edit", false, "edit the target in $EDITOR") ) @@ -161,7 +161,7 @@ func main() { _, _ = spool.Warn("no target selected.\n") os.Exit(0) } - err = run.Run(source.Source, target, args...) + err = run.Run(source.Source, target.Target(), args...) if err != nil { _, _ = spool.Warn("%v\n", err) os.Exit(1) @@ -198,7 +198,11 @@ func main() { if result.Overridden { _, _ = fmt.Fprintln(os.Stderr, menu.OverrideWarning(result.Target)) } - err = run.Run(result.Source, result.Target, result.Args...) + if *flags.Edit { + err = run.Edit(result.Target, result.Source) + } else { + err = run.Run(result.Source, result.Target, result.Args...) + } if err != nil { _, _ = spool.Warn("%v\n", err) os.Exit(1) diff --git a/menu/source.go b/menu/source.go index 073c044..e08c8bd 100644 --- a/menu/source.go +++ b/menu/source.go @@ -38,16 +38,16 @@ func (m *Model) Source(src *model.HydratedSource) string { targets := make([]string, 0, len(src.Targets)) var sub []string for _, t := range src.HydratedTargets { - ts := t.Sub() + ts := t.Target().Sub() header := !slices.Equal(sub, ts) if header { sub = ts } - if header && strings.Join(ts, " ") != t.ShortestId() { + if header && strings.Join(ts, " ") != t.Target().ShortestId() { targets = append(targets, m.Category(strings.Join(ts, " "), "")) header = false } - targets = append(targets, m.Target(t, header)) + targets = append(targets, m.Target(src, t, header)) } targetContent := lipgloss.JoinVertical(lipgloss.Top, targets...) diff --git a/menu/target.go b/menu/target.go index 6d6fdec..e619cbc 100644 --- a/menu/target.go +++ b/menu/target.go @@ -48,7 +48,7 @@ var ( }) ) -func (m *Model) Target(t model.HydratedTarget, header bool) string { +func (m *Model) Target(src *model.HydratedSource, t model.HydratedTarget, header bool) string { _, selection := m.Result() selected := selection != nil && selection.Target() == t.Target() icon := "" @@ -67,12 +67,12 @@ func (m *Model) Target(t model.HydratedTarget, header bool) string { } var labelParts []string labelParts = append(labelParts, icon) - sub := t.Sub() - if sub != nil && sub[len(sub)-1] != t.ShortestId() { + sub := t.Target().Sub() + if sub != nil && sub[len(sub)-1] != t.Target().ShortestId() { labelParts = append(labelParts, TargetSubStyle.Render(sub...)) } - labelParts = append(labelParts, TargetLabelStyle.Render(t.Label())) - return lipgloss.JoinHorizontal(lipgloss.Left, selectionStyle.Render(labelParts...), selectionDescriptionStyle.Render(t.Description())) + labelParts = append(labelParts, TargetLabelStyle.Render(t.Target().Label())) + return lipgloss.JoinHorizontal(lipgloss.Left, selectionStyle.Render(labelParts...), selectionDescriptionStyle.Render(t.Description(src))) } func (m *Model) Category(input string, desc string) string { diff --git a/model/target.go b/model/target.go index 683ab8f..156ee47 100644 --- a/model/target.go +++ b/model/target.go @@ -20,15 +20,15 @@ type Target interface { Visible() bool // Invocation should return the "canonical invocation": simple to remember Invocation(src *Source) []string + File(src *Source) string } // HydratedTarget is something we want to show in the menu type HydratedTarget interface { - Target // Icon is some text which will be used as an icon Icon() string // Description is a one-line description of what this does - Description() string + Description(src *HydratedSource) string // Target returns our inner target Target() Target } diff --git a/run/edit.go b/run/edit.go new file mode 100644 index 0000000..ad7645c --- /dev/null +++ b/run/edit.go @@ -0,0 +1,23 @@ +package run + +import ( + "errors" + "os" + "os/exec" + "pik/model" +) + +var NoEditorError = errors.New("$EDITOR not set") + +func Edit(t model.Target, src *model.Source) error { + editor := os.Getenv("EDITOR") + if editor == "" { + return NoEditorError + } + cmd := exec.Command(editor, t.File(src)) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Dir = src.Path + return cmd.Run() +} diff --git a/runner/base.go b/runner/base.go index 3f405a5..c9825ae 100644 --- a/runner/base.go +++ b/runner/base.go @@ -1,7 +1,6 @@ package runner import ( - "os/exec" "pik/identity" "pik/model" ) @@ -35,58 +34,22 @@ func (b *BaseTarget) Invocation(src *model.Source) []string { func Hydrated[T model.Target](in T) BaseHydration[T] { return BaseHydration[T]{ - BaseTarget: in, + Self: in, } } type BaseHydration[T model.Target] struct { - BaseTarget T + Self T } -func (b BaseHydration[T]) Matches(input string) bool { - return b.BaseTarget.Matches(input) -} - -func (b BaseHydration[T]) Create(s *model.Source) *exec.Cmd { - return b.BaseTarget.Create(s) -} - -func (b BaseHydration[T]) Sub() []string { - return b.BaseTarget.Sub() -} - -func (b BaseHydration[T]) Label() string { - return b.BaseTarget.Label() -} - -func (b BaseHydration[T]) Hydrate(src *model.Source) (model.HydratedTarget, error) { - return b, nil -} - -func (b BaseHydration[T]) Invocation(src *model.Source) []string { - return b.BaseTarget.Invocation(src) -} - -func (b BaseHydration[T]) Visible() bool { - return b.BaseTarget.Visible() -} - -func (b BaseHydration[T]) Tags() model.Tags { - return b.BaseTarget.Tags() -} - -func (b BaseHydration[T]) ShortestId() string { - return b.BaseTarget.ShortestId() -} - -func (b BaseHydration[T]) Icon() string { - return " " +func (b *BaseHydration[T]) Icon() string { + return "" } -func (b BaseHydration[T]) Description() string { +func (b *BaseHydration[T]) Description() string { return "" } -func (b BaseHydration[T]) Target() model.Target { - return b.BaseTarget +func (b *BaseHydration[T]) Target() model.Target { + return b.Self } diff --git a/runner/gnumake/make.go b/runner/gnumake/make.go index 96e7efb..e04f927 100644 --- a/runner/gnumake/make.go +++ b/runner/gnumake/make.go @@ -4,6 +4,7 @@ import ( "errors" "io/fs" "os/exec" + "path/filepath" "pik/identity" "pik/model" "pik/runner" @@ -13,10 +14,13 @@ import ( ) type gnumake struct { - path string + path string + files map[string]string } -var Indexer = &gnumake{} +var Indexer = &gnumake{ + files: make(map[string]string), +} var Makefiles = []string{ "Makefile", @@ -32,6 +36,7 @@ func (m *gnumake) Index(path string, f fs.FS, _ []model.Runner) ([]model.Target, makefile := "" for _, e := range entries { if !e.IsDir() && slices.Contains(Makefiles, strings.ToLower(e.Name())) { + m.files[path] = filepath.Join(path, e.Name()) content, err := fs.ReadFile(f, e.Name()) if err != nil { return nil, err diff --git a/runner/gnumake/target.go b/runner/gnumake/target.go index 2f909bb..bde09f8 100644 --- a/runner/gnumake/target.go +++ b/runner/gnumake/target.go @@ -12,6 +12,10 @@ type Target struct { Description string } +func (j *Target) File(src *model.Source) string { + return Indexer.files[src.Path] +} + func (j *Target) Create(s *model.Source) *exec.Cmd { return exec.Command(Indexer.path, j.Identity.Full) } @@ -38,10 +42,10 @@ type Hydrated struct { runner.BaseHydration[*Target] } -func (h *Hydrated) Icon() string { - return "\uE673" +func (h *Hydrated) Description(src *model.HydratedSource) string { + return h.Self.Description } -func (h *Hydrated) Description() string { - return h.BaseTarget.Description +func (h *Hydrated) Icon() string { + return "\uE673" } diff --git a/runner/just/just.go b/runner/just/just.go index 7d6bb92..377553e 100644 --- a/runner/just/just.go +++ b/runner/just/just.go @@ -4,6 +4,7 @@ import ( "errors" "io/fs" "os/exec" + "path/filepath" "pik/identity" "pik/model" "pik/runner" @@ -11,10 +12,13 @@ import ( ) type just struct { - path string + path string + files map[string]string } -var Indexer = &just{} +var Indexer = &just{ + files: make(map[string]string), +} func (j *just) Index(path string, f fs.FS, runners []model.Runner) ([]model.Target, error) { @@ -25,6 +29,7 @@ func (j *just) Index(path string, f fs.FS, runners []model.Runner) ([]model.Targ hasJustfile := false for _, e := range entries { if !e.IsDir() && strings.ToLower(e.Name()) == "justfile" { + j.files[path] = filepath.Join(path, e.Name()) hasJustfile = true break } diff --git a/runner/just/target.go b/runner/just/target.go index ca33b6b..9319e73 100644 --- a/runner/just/target.go +++ b/runner/just/target.go @@ -11,6 +11,10 @@ type Target struct { Category string } +func (j Target) File(src *model.Source) string { + return Indexer.files[src.Path] +} + func (j Target) Create(s *model.Source) *exec.Cmd { return exec.Command(Indexer.path, j.Identity.Full) } @@ -36,6 +40,10 @@ type Hydrated struct { runner.BaseHydration[*Target] } +func (h *Hydrated) Description(src *model.HydratedSource) string { + return "" +} + func (h *Hydrated) Icon() string { return "\uF039" } diff --git a/runner/python/file.go b/runner/python/file.go index f8b6c2e..4734b0a 100644 --- a/runner/python/file.go +++ b/runner/python/file.go @@ -3,19 +3,33 @@ package python import ( "os/exec" "path/filepath" + "pik/describe" "pik/model" "pik/runner" + "pik/spool" ) type File struct { runner.BaseTarget - File string + Path string +} + +func (p *File) File(src *model.Source) string { + return p.Path } type HydratedFileTarget struct { runner.BaseHydration[*File] } +func (h *HydratedFileTarget) Description(src *model.HydratedSource) string { + desc, err := describe.Describe(h.Target(), h.Self.Path) + if err != nil { + spool.Warn("%v\n", err) + } + return desc +} + func (h *HydratedFileTarget) Icon() string { return "\uE606" } @@ -23,15 +37,15 @@ func (h *HydratedFileTarget) Icon() string { func (p *File) Create(s *model.Source) *exec.Cmd { var cmd []string if Python.Uv != "" { - cmd = []string{Python.Uv, "run", "--", p.File} + cmd = []string{Python.Uv, "run", "--", p.Path} } else if venv := Python.VenvFor(s); venv != "" { - cmd = []string{filepath.Join(s.Path, venv, "bin", "python3"), p.File} + cmd = []string{filepath.Join(s.Path, venv, "bin", "python3"), p.Path} } else { sysPath, err := exec.LookPath("python3") if err != nil { return nil } - cmd = []string{sysPath, p.File} + cmd = []string{sysPath, p.Path} } return exec.Command(cmd[0], cmd[1:]...) } diff --git a/runner/python/indexer.go b/runner/python/indexer.go index 3a61f7e..98502ca 100644 --- a/runner/python/indexer.go +++ b/runner/python/indexer.go @@ -14,7 +14,7 @@ type pyproj struct { } } -func (p python) Index(path string, f fs.FS, runners []model.Runner) ([]model.Target, error) { +func (p *python) Index(path string, f fs.FS, runners []model.Runner) ([]model.Target, error) { for _, pt := range VenvPaths { if stat, err := fs.Stat(f, filepath.Join(pt)); err == nil { if stat.IsDir() { @@ -29,6 +29,8 @@ func (p python) Index(path string, f fs.FS, runners []model.Runner) ([]model.Tar return nil, err } + p.files[path] = filepath.Join(path, "pyproject.toml") + pp := &pyproj{} err = toml.Unmarshal(content, pp) diff --git a/runner/python/proj.go b/runner/python/proj.go index 9a711bd..1230b1c 100644 --- a/runner/python/proj.go +++ b/runner/python/proj.go @@ -12,16 +12,20 @@ type Project struct { Cmd string } +func (p *Project) File(src *model.Source) string { + return Python.files[src.Path] +} + type Hydrated struct { runner.BaseHydration[*Project] } -func (h *Hydrated) Icon() string { - return "\uE606" +func (h *Hydrated) Description(src *model.HydratedSource) string { + return h.Self.Cmd } -func (h *Hydrated) Description() string { - return h.BaseTarget.Cmd +func (h *Hydrated) Icon() string { + return "\uE606" } func (p *Project) Create(s *model.Source) *exec.Cmd { diff --git a/runner/python/runner.go b/runner/python/runner.go index 2293336..73192e6 100644 --- a/runner/python/runner.go +++ b/runner/python/runner.go @@ -14,6 +14,7 @@ type python struct { Venvs map[string]string Uv string System string + files map[string]string } func (p python) Init() error { @@ -72,7 +73,7 @@ func (p python) CreateTarget(fs fs.FS, source string, file string, entry fs.DirE Identity: identity.New(filename), MyTags: model.TagsFromFilename(filename), }, - File: file, + Path: file, }, nil } diff --git a/runner/shell/hydrated.go b/runner/shell/hydrated.go index 1ecce51..dcd28be 100644 --- a/runner/shell/hydrated.go +++ b/runner/shell/hydrated.go @@ -16,8 +16,8 @@ func (h *Hydrated) Icon() string { return "\uF489" } -func (h *Hydrated) Description() string { - desc, err := describe.Describe(h.BaseTarget, h.BaseTarget.Script) +func (h *Hydrated) Description(src *model.HydratedSource) string { + desc, err := describe.Describe(h.Target(), h.Target().File(src.Source)) if err != nil { spool.Warn("%v\n", err) return "" diff --git a/runner/shell/target.go b/runner/shell/target.go index 8422cf0..189bcb7 100644 --- a/runner/shell/target.go +++ b/runner/shell/target.go @@ -12,6 +12,10 @@ type Target struct { Script string } +func (s *Target) File(src *model.Source) string { + return s.Script +} + func (s *Target) String() string { return s.Label() } |
