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
38 changes: 35 additions & 3 deletions cmd/current.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,57 @@ import (
"os"

"github.com/janpieper/jm/internal/marker"
"github.com/janpieper/jm/internal/table"
"github.com/spf13/cobra"
)

var detailed bool

var currentCmd = &cobra.Command{
Use: "current",
Short: "Show current jump marker",
Run: func(cmd *cobra.Command, args []string) {
file, err := marker.Current(alternativeMarkers)
match, err := marker.Current(alternativeMarkers)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if file != nil {
fmt.Printf("%s\n", *file)

if detailed {
showDetailed(match)
} else {
showSimple(match)
}

},
}

func showSimple(match *marker.MarkerMatch) {
if match != nil {
fmt.Printf("%s\n", match.Path)
}
}

func showDetailed(match *marker.MarkerMatch) {
t := table.New([]string{"Path", "Marker"})

if match != nil {
err := t.Append([]string{match.Path, match.Marker})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

err := t.Render()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {
currentCmd.Flags().StringSliceVarP(&alternativeMarkers, "alternate", "a", []string{}, "Alternative markers")
currentCmd.Flags().BoolVarP(&detailed, "details", "d", false, "Show details")
rootCmd.AddCommand(currentCmd)
}
37 changes: 34 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,56 @@ import (
"os"

"github.com/janpieper/jm/internal/marker"
"github.com/janpieper/jm/internal/table"
"github.com/spf13/cobra"
)

var detailedList bool

var listCmd = &cobra.Command{
Use: "list",
Short: "List jump markers",
Run: func(cmd *cobra.Command, args []string) {
files, err := marker.List(alternativeMarkers)
matches, err := marker.List(alternativeMarkers)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, file := range files {
fmt.Println(file)

if detailedList {
showDetailedList(matches)
} else {
showSimpleList(matches)
}
},
}

func showDetailedList(matches []marker.MarkerMatch) {
t := table.New([]string{"Path", "Marker"})

for _, match := range matches {
err := t.Append([]string{match.Path, match.Marker})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

err := t.Render()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func showSimpleList(matches []marker.MarkerMatch) {
for _, match := range matches {
fmt.Println(match.Path)
}
}

func init() {
listCmd.Flags().StringSliceVarP(&alternativeMarkers, "alternate", "a", []string{}, "Alternative markers")
listCmd.Flags().BoolVarP(&detailedList, "details", "d", false, "Show details")
rootCmd.AddCommand(listCmd)
}
13 changes: 12 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@ module github.com/janpieper/jm

go 1.24.5

require github.com/spf13/cobra v1.9.1
require (
github.com/olekukonko/tablewriter v1.0.9
github.com/spf13/cobra v1.9.1
)

require (
github.com/fatih/color v1.15.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/olekukonko/errors v1.1.0 // indirect
github.com/olekukonko/ll v0.0.9 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.6 // indirect
golang.org/x/sys v0.12.0 // indirect
)
21 changes: 21 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,31 @@
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/olekukonko/errors v1.1.0 h1:RNuGIh15QdDenh+hNvKrJkmxxjV4hcS50Db478Ou5sM=
github.com/olekukonko/errors v1.1.0/go.mod h1:ppzxA5jBKcO1vIpCXQ9ZqgDh8iwODz6OXIGKU8r5m4Y=
github.com/olekukonko/ll v0.0.9 h1:Y+1YqDfVkqMWuEQMclsF9HUR5+a82+dxJuL1HHSRpxI=
github.com/olekukonko/ll v0.0.9/go.mod h1:En+sEW0JNETl26+K8eZ6/W4UQ7CYSrrgg/EdIYT2H8g=
github.com/olekukonko/tablewriter v1.0.9 h1:XGwRsYLC2bY7bNd93Dk51bcPZksWZmLYuaTHR0FqfL8=
github.com/olekukonko/tablewriter v1.0.9/go.mod h1:5c+EBPeSqvXnLLgkm9isDdzR3wjfBkHR9Nhfp3NWrzo=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
23 changes: 14 additions & 9 deletions internal/marker/marker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (

const Marker = ".jm"

type MarkerMatch struct {
Path string
Marker string
}

func Exists() bool {
stat, _ := os.Stat(Marker)
return stat != nil
Expand All @@ -20,34 +25,34 @@ func Remove() error {
return os.Remove(Marker)
}

func Current(alternatives []string) (*string, error) {
files, err := List(alternatives)
func Current(alternatives []string) (*MarkerMatch, error) {
matches, err := List(alternatives)
if err != nil {
return nil, err
}

if len(files) > 0 {
file := files[0]
return &file, nil
if len(matches) > 0 {
match := matches[0]
return &match, nil
}

return nil, nil
}

func List(alternatives []string) ([]string, error) {
func List(alternatives []string) ([]MarkerMatch, error) {
dir, err := os.Getwd()
if err != nil {
return nil, err
}

var files []string
var matches []MarkerMatch
markers := append([]string{Marker}, alternatives...)

for {
for _, marker := range markers {
file := filepath.Join(dir, marker)
if _, err := os.Stat(file); err == nil {
files = append(files, filepath.Dir(file))
matches = append(matches, MarkerMatch{filepath.Dir(file), marker})
break
}
}
Expand All @@ -60,5 +65,5 @@ func List(alternatives []string) ([]string, error) {
dir = parent
}

return files, nil
return matches, nil
}
37 changes: 37 additions & 0 deletions internal/table/table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package table

import (
"os"

"github.com/olekukonko/tablewriter"
"github.com/olekukonko/tablewriter/tw"
)

func New(headers []string) *tablewriter.Table {
table := tablewriter.NewTable(os.Stdout)
table.Header(headers)

table.Configure(func(cfg *tablewriter.Config) {
// Left align the headers
cfg.Header.Alignment.Global = tw.AlignLeft

// Remove padding (except between columns)
cfg.Header.Padding.Global = tw.Padding{Left: "", Right: ""}
cfg.Row.Padding.Global = tw.Padding{Left: "", Right: " "}
})

table.Options(tablewriter.WithRendition(
tw.Rendition{
// Disable borders
Borders: tw.Border{Left: tw.Off, Right: tw.Off},

// Disable separators & lines
Settings: tw.Settings{
Separators: tw.Separators{BetweenColumns: tw.Off},
Lines: tw.Lines{ShowTop: tw.Off, ShowBottom: tw.Off, ShowHeaderLine: tw.Off},
},
},
))

return table
}