Skip to content

Conversation

@Hanaasagi
Copy link
Owner

No description provided.

@Hanaasagi Hanaasagi marked this pull request as draft August 3, 2025 07:02
@Hanaasagi
Copy link
Owner Author

  1. bug:
time=2025-08-03T14:28:28.110+08:00 level=INFO msg="Adding shell prompt exclusion" prompt="(eval):1: can't change option: zle\n(eval):1: can't change option: zle\n(py38
) \x1b[01;32m➜  \x1b[36mmagonote\x1b[00m" workingDir=/root/.tmux/plugins/magonote
  1. Using a PTY to solve this is slow and fails to retrieve additional information, such as Git status.
func (m *Magonote) getShellPrompt(workingDir string) (string, error) {
	shellPath := os.Getenv("SHELL")
	if shellPath == "" {
		shellPath = "bash"
	} else {
		shellPath = filepath.Base(shellPath)
	}

	var cmd *exec.Cmd
	var promptCmd string

	switch shellPath {
	case "zsh":
		promptCmd = fmt.Sprintf(`cd "%s" && print -P "$PS1" && exit`, workingDir)
		cmd = exec.Command("zsh", "-i", "-c", promptCmd)
	default:
		promptCmd = fmt.Sprintf(`cd "%s" && echo "${PS1@P}" && exit`, workingDir)
		cmd = exec.Command("bash", "-i", "-c", promptCmd)
	}

	slog.Info("Running shell prompt command with PTY", "shell", shellPath, "workingDir", workingDir, "command", promptCmd)

	// Create PTY
	ptmx, err := pty.Start(cmd)
	if err != nil {
		slog.Warn("Failed to start shell with PTY", "error", err)
		return "", nil
	}
	defer ptmx.Close()

	// Read with timeout (in case the shell hangs)
	var buf bytes.Buffer
	done := make(chan struct{})
	go func() {
		_, _ = buf.ReadFrom(ptmx)
		close(done)
	}()

	select {
	case <-done:
	case <-time.After(500 * time.Millisecond):
		slog.Warn("Timeout while reading from shell PTY")
		return "", nil
	}

	// Parse and clean the prompt string
	output := strings.TrimSpace(buf.String())
	lines := strings.Split(output, "\n")
	prompt := ""
	for _, line := range lines {
		// heuristic: skip lines like `cd ...` or shell noise
		if !strings.HasPrefix(line, "cd ") && strings.TrimSpace(line) != "" {
			prompt = line
			break
		}
	}

	prompt = StripANSI(prompt)
	slog.Debug("Retrieved shell prompt (PTY)", "prompt", prompt, "shell", shellPath, "workingDir", workingDir)
	return prompt, nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant