Stuck on Multiple completions #WH6

I’m stuck on Stage Multiple completions #WH6

I’ve tried spliting the s to get the first $ xyz_ only, but nothing worked.
I don’t understand how to handle this problem, and you give me some tips.

GitHub repo: https://github.com/logan1o1/codecrafters-shell-go

Here are my logs:

[tester::#WH6] Running ./your_program.sh
[tester::#WH6] ✓ Received prompt ($ )
[tester::#WH6] Typed "xyz_"
[your-program] $ xyz_
[tester::#WH6] ✓ Prompt line matches "$ xyz_"
[tester::#WH6] Pressed "<TAB>" (expecting bell to ring)
[tester::#WH6] Pressed "<TAB>" (expecting autocomplete to "xyz_bar  xyz_baz  xyz_quz")
[tester::#WH6] Output does not match expected value.
[tester::#WH6] Expected: "$ xyz_"
[tester::#WH6] Received: "$ xyz_$ xyz_"
[tester::#WH6] Assertion failed.
[tester::#WH6] Test failed

And here’s a snippet of my code:

rl, err := readline.NewEx(&readline.Config{
		Prompt: "$ ",
		// AutoComplete:    completer,
		InterruptPrompt: "^C",
		EOFPrompt:       "exit",
	})
	if err != nil {
		fmt.Println(err)
	}

	rl.CaptureExitSignal()
	defer rl.Close()

	cachedExe := CustomExeFromPath()

	var lastPrefix string
	var tabCount int

	completer := readline.NewPrefixCompleter(
		readline.PcItem("echo"),
		readline.PcItem("exit"),
		readline.PcItemDynamic(func(s string) []string {

			if s != lastPrefix {
				lastPrefix = s
				tabCount = 0
			}
			tabCount++

			var match []string
			for _, cxe := range cachedExe {
				if strings.HasPrefix(cxe, s) {
					// fmt.Print("\a")
					match = append(match, cxe)
				}
			}
			sort.Strings(match)

			if len(match) == 0 {
				rl.Write([]byte("\a"))
				return nil
			}

			if len(match) == 1 {
				return match
			}

			if tabCount == 1 {
				rl.Write([]byte("\a"))
				return nil
			}

			fmt.Printf("$ %s\n", s)
			tabCount = 0
			return nil
		}),
	)

	rl.Config.AutoComplete = completer

completed

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.