Repository: rsteube/carapace
Pull request #592 (2022-11-13 12:22:24)

readme: moved example


Patch:
diff --git a/README.md b/README.md
index 0bf32b9..1f18191 100644
--- a/README.md
+++ b/README.md
@@ -33,68 +33,16 @@ import (
 carapace.Gen(rootCmd)
 ```
 
-## Standalone Mode
-
-Carapace can also be used to provide completion for arbitrary commands as well (similar to [aws_completer](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-completion.html)).
-See [rsteube/carapace-bin](https://github.com/rsteube/carapace-bin) for examples. There is also a binary to parse flags from gnu help pages at [caraparse](https://github.com/rsteube/carapace-bin/tree/master/cmd/caraparse).
-
 ## Example
 
 An example implementation can be found in the [example](./example/) folder.
 
-```sh
-cd example
-go build .
-
-# bash
-PATH=$PATH:$(pwd)
-source <(example _carapace bash)
-
-# elvish
-paths=[$@paths (pwd)]
-eval (example _carapace elvish | slurp)
-
-# fish
-set PATH $PATH (pwd) 
-example _carapace fish | source
-
-# nushell
-example _carapace nushell
-
-# oil
-PATH=$PATH:$(pwd)
-source <(example _carapace oil)
 
-# powershell
-Set-PSReadLineOption -Colors @{ "Selection" = "`e[7m" }
-Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
-$env:PATH += ":$pwd"
-example _carapace powershell | out-string | Invoke-Expression
-
-# tcsh
-set autolist
-eval `example _carapace tcsh`
-
-# xonsh
-$COMPLETION_QUERY_LIMIT = 500 # increase limit
-$PATH.append($(pwd))
-exec($(example _carapace xonsh))
-
-# zsh
-PATH=$PATH:$(pwd)
-source <(example _carapace zsh)
-
-example <TAB>
-```
-
-or use [docker-compose](https://docs.docker.com/compose/):
-```sh
-docker-compose pull
-docker-compose run --rm build
-docker-compose run --rm [bash|elvish|fish|ion|nushell|oil|powershell|tcsh|xonsh|zsh]
+## Standalone Mode
 
-example <TAB>
-```
+Carapace can also be used to provide completion for arbitrary commands as well.
+See [rsteube/carapace-bin](https://github.com/rsteube/carapace-bin) for examples.
+There is also a binary to parse flags from gnu help pages at [caraparse](https://github.com/rsteube/carapace-bin/tree/master/cmd/caraparse).
 
 ## Related Projects
 
diff --git a/carapace.go b/carapace.go
index 175a8ba..38c0451 100644
--- a/carapace.go
+++ b/carapace.go
@@ -2,9 +2,15 @@
 package carapace
 
 import (
+	"fmt"
+	"io"
+	"io/ioutil"
+	"log"
 	"os"
 
-	"github.com/rsteube/carapace/internal/shell"
+	_shell "github.com/rsteube/carapace/internal/shell"
+	"github.com/rsteube/carapace/internal/uid"
+	"github.com/rsteube/carapace/pkg/ps"
 	"github.com/spf13/cobra"
 	"github.com/spf13/pflag"
 )
@@ -99,8 +105,8 @@ func (c Carapace) Standalone() {
 }
 
 // Snippet creates completion script for given shell.
-func (c Carapace) Snippet(name string) (string, error) {
-	return shell.Snippet(c.cmd, name)
+func (c Carapace) Snippet(shell string) (string, error) {
+	return _shell.Snippet(c.cmd, shell)
 }
 
 // IsCallback returns true if current program invocation is a callback.
@@ -108,6 +114,29 @@ func IsCallback() bool {
 	return len(os.Args) > 1 && os.Args[1] == "_carapace"
 }
 
+var logger = log.New(ioutil.Discard, "", log.Flags())
+
+func init() {
+	if _, enabled := os.LookupEnv("CARAPACE_LOG"); enabled {
+		if err := initLogger(); err != nil {
+			log.Fatal(err.Error())
+		}
+	}
+}
+
+func initLogger() (err error) {
+	tmpdir := fmt.Sprintf("%v/carapace", os.TempDir())
+	if err = os.MkdirAll(tmpdir, os.ModePerm); err == nil {
+		var logfileWriter io.Writer
+		if logfileWriter, err = os.OpenFile(fmt.Sprintf("%v/%v.log", tmpdir, uid.Executable()), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666); err == nil {
+			Lmsgprefix := 1 << 6
+			logger = log.New(logfileWriter, ps.DetermineShell()+" ", log.Flags()|Lmsgprefix)
+			//logger = log.New(logfileWriter, determineShell()+" ", log.Flags()|log.Lmsgprefix)
+		}
+	}
+	return
+}
+
 // Test verifies the configuration (e.g. flag name exists)
 //
 //	func TestCarapace(t *testing.T) {
diff --git a/command.go b/command.go
index ed88d19..dee7e02 100644
--- a/command.go
+++ b/command.go
@@ -27,7 +27,7 @@ func addCompletionCommand(cmd *cobra.Command) {
 			}
 		},
 		Run: func(cmd *cobra.Command, args []string) {
-			logger.PrintArgs(os.Args)
+			logger.Println(os.Args) // TODO replace last with '' if empty
 			if s, err := complete(cmd, args); err != nil {
 				fmt.Fprintln(io.MultiWriter(cmd.OutOrStderr(), logger.Writer()), err.Error())
 			} else {
diff --git a/example/README.md b/example/README.md
new file mode 100644
index 0000000..c9ae44f
--- /dev/null
+++ b/example/README.md
@@ -0,0 +1,48 @@
+# Example
+
+```sh
+go install .
+
+# bash
+source <(example _carapace bash)
+
+# elvish
+eval (example _carapace elvish | slurp)
+
+# fish
+example _carapace fish | source
+
+# nushell
+example _carapace nushell # update config.nu according to output
+
+# oil
+source <(example _carapace oil)
+
+# powershell
+Set-PSReadLineOption -Colors @{ "Selection" = "`e[7m" }
+Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
+example _carapace powershell | out-string | Invoke-Expression
+
+# tcsh
+set autolist
+eval `example _carapace tcsh`
+
+# xonsh
+$COMPLETION_QUERY_LIMIT = 500 # increase limit
+exec($(example _carapace xonsh))
+
+# zsh
+source <(example _carapace zsh)
+
+example <TAB>
+```
+
+or use [docker-compose](https://docs.docker.com/compose/):
+```sh
+docker-compose pull
+docker-compose run --rm build
+docker-compose run --rm [bash|elvish|fish|ion|nushell|oil|powershell|tcsh|xonsh|zsh]
+
+example <TAB>
+```
+
diff --git a/log.go b/log.go
deleted file mode 100644
index cb14dcf..0000000
--- a/log.go
+++ /dev/null
@@ -1,42 +0,0 @@
-package carapace
-
-import (
-	"encoding/json"
-	"fmt"
-	"io/ioutil"
-	"log"
-	"os"
-
-	"github.com/rsteube/carapace/internal/uid"
-	"github.com/rsteube/carapace/pkg/ps"
-)
-
-type _logger struct {
-	*log.Logger
-}
-
-func (l _logger) PrintArgs(args []string) {
-	if m, err := json.Marshal(args); err == nil {
-		l.Println(string(m))
-	}
-}
-
-var logger = _logger{log.New(ioutil.Discard, "", log.Flags())}
-
-func init() {
-	if _, enabled := os.LookupEnv("CARAPACE_LOG"); !enabled {
-		return
-	}
-
-	tmpdir := fmt.Sprintf("%v/carapace", os.TempDir())
-	if err := os.MkdirAll(tmpdir, os.ModePerm); err != nil {
-		log.Fatal(err.Error())
-	}
-
-	file := fmt.Sprintf("%v/%v.log", tmpdir, uid.Executable())
-	if logfileWriter, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666); err != nil {
-		log.Fatal(err.Error())
-	} else {
-		logger = _logger{log.New(logfileWriter, ps.DetermineShell()+" ", log.Flags()|log.Lmsgprefix)}
-	}
-}


Test patch:
diff --git a/action_test.go b/action_test.go
index d13728e..f1983b2 100644
--- a/action_test.go
+++ b/action_test.go
@@ -166,6 +166,7 @@ func TestActionFiles(t *testing.T) {
 
 	assertEqual(t,
 		ActionStyledValues(
+			"README.md", style.Of("fg-default", "bg-default"),
 			"_test/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
 			"cmd/", style.Of("fg-default", "bg-default", style.Blue, style.Bold),
 			"main.go", style.Of("fg-default", "bg-default"),
diff --git a/carapace_test.go b/carapace_test.go
index 60b0915..6536f6d 100644
--- a/carapace_test.go
+++ b/carapace_test.go
@@ -2,6 +2,7 @@ package carapace
 
 import (
 	"encoding/json"
+	"fmt"
 	"os"
 	"strings"
 	"testing"
@@ -172,6 +173,15 @@ func TestStandalone(t *testing.T) {
 	}
 }
 
+func TestInitLogger(t *testing.T) {
+	initLogger()
+	tmpdir := fmt.Sprintf("%v/carapace", os.TempDir())
+	if _, err := os.Stat(fmt.Sprintf("%v/%v.log", tmpdir, uid.Executable())); os.IsNotExist(err) {
+
+		t.Fail()
+	}
+}
+
 func TestIsCallback(t *testing.T) {
 	os.Args = []string{uid.Executable(), "subcommand"}
 	if IsCallback() {