Repository: rsteube/carapace
Pull request #813 (2023-07-14 17:57:06)

ActionMessage: add nospace as late as possible


Patch:
diff --git a/defaultActions.go b/defaultActions.go
index 33470ca..1df7bcf 100644
--- a/defaultActions.go
+++ b/defaultActions.go
@@ -205,7 +205,7 @@ func ActionMessage(msg string, args ...interface{}) Action {
 		if len(args) > 0 {
 			msg = fmt.Sprintf(msg, args...)
 		}
-		a := ActionValues().NoSpace()
+		a := ActionValues()
 		a.meta.Messages.Add(stripansi.Strip(msg))
 		return a
 	})
diff --git a/internal/shell/shell.go b/internal/shell/shell.go
index fff6ea3..f578122 100644
--- a/internal/shell/shell.go
+++ b/internal/shell/shell.go
@@ -86,6 +86,11 @@ func Value(shell string, value string, meta common.Meta, values common.RawValues
 		default:
 			filtered = meta.Messages.Integrate(filtered, value)
 		}
+
+		if !meta.Messages.IsEmpty() && shell != "export" {
+			meta.Nospace.Add('*')
+		}
+
 		sort.Sort(common.ByDisplay(filtered))
 		return f(value, meta, filtered)
 	}
diff --git a/pkg/sandbox/sandbox.go b/pkg/sandbox/sandbox.go
index bb6b1df..74a33aa 100644
--- a/pkg/sandbox/sandbox.go
+++ b/pkg/sandbox/sandbox.go
@@ -16,6 +16,10 @@ import (
 	"github.com/spf13/cobra"
 )
 
+func init() {
+	os.Unsetenv("LS_COLORS")
+}
+
 type Sandbox struct {
 	t    *testing.T
 	cmdF func() *cobra.Command


Test patch:
diff --git a/action_test.go b/action_test.go
index d7edca5..a9c32f9 100644
--- a/action_test.go
+++ b/action_test.go
@@ -184,7 +184,7 @@ func TestActionFilesChdir(t *testing.T) {
 	oldWd, _ := os.Getwd()
 
 	assertEqual(t,
-		ActionMessage(fmt.Sprintf("stat %v: no such file or directory", wd("nonexistent"))).NoSpace('/').Invoke(Context{}),
+		ActionMessage(fmt.Sprintf("stat %v: no such file or directory", wd("nonexistent"))).Invoke(Context{}),
 		ActionFiles(".md").Chdir("nonexistent").Invoke(Context{}),
 	)
 
@@ -207,7 +207,7 @@ func TestActionFilesChdir(t *testing.T) {
 }
 
 func TestActionMessage(t *testing.T) {
-	expected := ActionValues().NoSpace()
+	expected := ActionValues()
 	expected.meta.Messages.Add("example message")
 
 	assertEqual(t,
@@ -222,13 +222,13 @@ func TestActionMessageSuppress(t *testing.T) {
 			ActionMessage("example message").Suppress("example"),
 			ActionValues("test"),
 		).ToA().Invoke(Context{}),
-		ActionValues("test").NoSpace('*').Invoke(Context{}), // TODO suppress does not reset nospace (is that even possible?)
+		ActionValues("test").Invoke(Context{}), // TODO suppress does not reset nospace (is that even possible?)
 	)
 }
 
 func TestActionExecCommand(t *testing.T) {
 	assertEqual(t,
-		ActionMessage("go unknown: unknown command").NoSpace('/').Invoke(Context{}).Prefix("docs/"),
+		ActionMessage("go unknown: unknown command").Invoke(Context{}).Prefix("docs/"),
 		ActionExecCommand("go", "unknown")(func(output []byte) Action { return ActionValues() }).Invoke(Context{Value: "docs/"}),
 	)
 
diff --git a/example/cmd/action_test.go b/example/cmd/action_test.go
index c192333..0c2b69c 100644
--- a/example/cmd/action_test.go
+++ b/example/cmd/action_test.go
@@ -22,12 +22,10 @@ func TestAction(t *testing.T) {
 
 		s.Run("action", "--callback", "").
 			Expect(carapace.ActionMessage("values flag is not set").
-				NoSpace().
 				Usage("ActionCallback()"))
 
 		s.Run("action", "--values", "first", "--callback", "").
 			Expect(carapace.ActionMessage("values flag is set to: 'first'").
-				NoSpace().
 				Usage("ActionCallback()"))
 
 		s.Run("action", "--directories", "").
@@ -81,7 +79,6 @@ func TestAction(t *testing.T) {
 
 		s.Run("action", "--message", "").
 			Expect(carapace.ActionMessage("example message").
-				NoSpace().
 				Usage("ActionMessage()"))
 
 		s.Run("action", "--message-multiple", "t").
@@ -91,7 +88,6 @@ func TestAction(t *testing.T) {
 				carapace.ActionMessage("third message"),
 				carapace.ActionValues("one", "two", "three")).
 				ToA().
-				NoSpace().
 				Usage("ActionMessage()"))
 
 		s.Run("action", "--multiparts", "").
@@ -209,7 +205,7 @@ func TestDash(t *testing.T) {
 func TestUnknownFlag(t *testing.T) {
 	sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) {
 		s.Run("action", "--unknown", "").
-			Expect(carapace.ActionMessage("unknown flag: --unknown").NoSpace())
+			Expect(carapace.ActionMessage("unknown flag: --unknown"))
 
 		s.Env("CARAPACE_LENIENT", "1")
 		s.Run("action", "--unknown", "").
diff --git a/example/cmd/modifier_test.go b/example/cmd/modifier_test.go
index 86369ee..5d37c6b 100644
--- a/example/cmd/modifier_test.go
+++ b/example/cmd/modifier_test.go
@@ -61,6 +61,7 @@ func TestTimeout(t *testing.T) {
 
 		s.Run("modifier", "--timeout", "3s:").
 			Expect(carapace.ActionMessage("timeout exceeded").
+				NoSpace(':').
 				Usage("Timeout()"))
 	})
 }