Skip to content

Commit

Permalink
Console v3 (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
jippi committed Mar 2, 2024
1 parent 409dbc9 commit 5480d74
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dottie.exe
.task/
.idea/
.direnv
.dottie.hist

manpages/
completions/
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/jippi/dottie/cmd/json"
print_cmd "github.com/jippi/dottie/cmd/print"
"github.com/jippi/dottie/cmd/set"
"github.com/jippi/dottie/cmd/shell"
"github.com/jippi/dottie/cmd/update"
"github.com/jippi/dottie/cmd/validate"
"github.com/jippi/dottie/cmd/value"
Expand Down Expand Up @@ -45,6 +46,7 @@ func NewRootCommand() *cobra.Command {
root.AddCommand(enable.NewCommand())
root.AddCommand(disable.NewCommand())
root.AddCommand(update.NewCommand())
root.AddCommand(shell.NewCommand())

root.AddCommand(print_cmd.NewCommand())
root.AddCommand(value.NewCommand())
Expand Down
7 changes: 2 additions & 5 deletions cmd/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,8 @@ func runE(cmd *cobra.Command, args []string) error {
//

var (
allErrors error
stdout, stderr = tui.WritersFromContext(cmd.Context())
)

var (
allErrors error
stdout, stderr = tui.WritersFromContext(cmd.Context())
argumentCounter int
skipNextArgument bool
)
Expand Down
109 changes: 109 additions & 0 deletions cmd/shell/shell.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package shell

import (
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"

"github.com/jippi/dottie/pkg/tui"
"github.com/reeflective/console"
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "shell",
Short: "Dottie shell",
GroupID: "manipulate",
Args: cobra.ExactArgs(0),
RunE: runE,
}

return cmd
}

func runE(cmd *cobra.Command, args []string) error {
app := console.New("dottie")
app.Shell().AcceptMultiline = func(line []rune) (accept bool) { return true }

menu := app.ActiveMenu()
menu.SetErr(cmd.ErrOrStderr())
menu.SetOut(cmd.OutOrStdout())
menu.AddHistorySourceFile("default", ".dottie.hist")
menu.AddInterrupt(io.EOF, exitCtrlD)
menu.SetCommands(mainMenuCommands(cmd.Root(), app))
menu.ErrorHandler = func(err error) error {
tui.StderrFromContext(cmd.Context()).Danger().Println(err)

return nil
}

setupPrompt(menu)

return app.Start(cmd.Context())
}

func mainMenuCommands(rootCmd *cobra.Command, _ *console.Console) console.Commands {
// Commander
processCommand := func(cmd *cobra.Command) {
carapaceCompleter := carapace.Gen(cmd)
flagMap := make(carapace.ActionMap)

cmd.Flags().VisitAll(func(f *pflag.Flag) {
if f.Name == "file" || strings.Contains(f.Usage, "file") {
flagMap[f.Name] = carapace.ActionFiles()
}
})

carapaceCompleter.FlagCompletion(flagMap)
}

return func() *cobra.Command {
processCommand(rootCmd)

for _, cmd := range rootCmd.Commands() {
processCommand(cmd)
}

rootCmd.InitDefaultHelpCmd()
rootCmd.CompletionOptions.DisableDefaultCmd = true
// rootCmd.DisableFlagsInUseLine = true

return rootCmd
}
}

// exitCtrlD is a custom interrupt handler to use when the shell
// readline receives an io.EOF error, which is returned with CtrlD.
func exitCtrlD(c *console.Console) {
os.Exit(0)
}

// setupPrompt is a function which sets up the prompts for the main menu.
func setupPrompt(m *console.Menu) {
prompt := m.Prompt()

prompt.Primary = func() string {
prompt := "\x1b[33mdottie\x1b[0m in \x1b[34m%s\x1b[0m\n> "
wd, _ := os.Getwd()

dir, err := filepath.Rel(os.Getenv("HOME"), wd)
if err != nil {
dir = filepath.Base(wd)
}

return fmt.Sprintf(prompt, dir)
}

prompt.Secondary = func() string { return ">" }
prompt.Right = func() string {
return "\x1b[1;30m" + time.Now().Format("03:04:05.000") + "\x1b[0m"
}

prompt.Transient = func() string { return "\x1b[1;30m" + ">> " + "\x1b[0m" }
}
4 changes: 0 additions & 4 deletions cmd/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"github.com/jippi/dottie/pkg"
"github.com/jippi/dottie/pkg/ast"
"github.com/jippi/dottie/pkg/cli/shared"
"github.com/jippi/dottie/pkg/tui"
"github.com/spf13/cobra"
slogctx "github.com/veqryn/slog-context"
)

func NewCommand() *cobra.Command {
Expand Down Expand Up @@ -60,8 +58,6 @@ func runE(cmd *cobra.Command, args []string) error {
return err
}

slogctx.Info(cmd.Context(), "value.assignment.Interpolated", tui.StringDump("assignment.Interpolated", assignment.Interpolated))

fmt.Fprint(cmd.OutOrStdout(), assignment.Interpolated)

return nil
Expand Down
38 changes: 23 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ replace github.com/go-playground/validator/v10 => github.com/jippi/go-validator/

replace github.com/reugn/pkgslog => github.com/jippi/pkgslog v0.0.0-20240224183226-3fdc9d9d89a3

replace github.com/reeflective/console => github.com/jippi/go-console v0.0.0-20240302001452-e5453feb8929

// replace github.com/reeflective/console => ./3rd-party/go-console

require (
github.com/caarlos0/go-version v0.1.1
github.com/charmbracelet/huh v0.3.0
Expand All @@ -21,7 +25,9 @@ require (
github.com/lmittmann/tint v1.0.4
github.com/muesli/termenv v0.15.2
github.com/neilotoole/slogt v1.1.0
github.com/reeflective/console v0.1.15
github.com/reugn/pkgslog v0.0.0-20231009090135-bbaf4951c7eb
github.com/rsteube/carapace v0.46.3-0.20231214181515-27e49f3c3b69
github.com/samber/slog-multi v1.0.2
github.com/sebdah/goldie/v2 v2.5.3
github.com/spf13/cobra v1.8.0
Expand All @@ -35,10 +41,10 @@ require (
)

require (
cloud.google.com/go v0.110.4 // indirect
cloud.google.com/go/compute v1.21.0 // indirect
cloud.google.com/go v0.110.9 // indirect
cloud.google.com/go/compute v1.23.2 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.1 // indirect
cloud.google.com/go/iam v1.1.4 // indirect
cloud.google.com/go/storage v1.30.1 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aws/aws-sdk-go v1.44.122 // indirect
Expand All @@ -54,17 +60,17 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.11.0 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.4 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
Expand All @@ -77,26 +83,28 @@ require (
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/reeflective/readline v1.0.13 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rsteube/carapace-shlex v0.1.1 // indirect
github.com/samber/lo v1.38.1 // indirect
github.com/sergi/go-diff v1.3.1 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/oauth2 v0.11.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.126.0 // indirect
google.golang.org/api v0.128.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.3 // indirect
google.golang.org/genproto v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231016165738-49dd2c1f3d0b // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
modernc.org/b/v2 v2.1.0 // indirect
Expand Down
Loading

0 comments on commit 5480d74

Please sign in to comment.