diff --git a/cmd/displaycache.go b/cmd/displaycache.go new file mode 100644 index 0000000..693497b --- /dev/null +++ b/cmd/displaycache.go @@ -0,0 +1,43 @@ +package cmd + +import ( + "fmt" + "os" + "os/user" + "path/filepath" +) + +// displayCachePath returns the UID-based path for the display cache temp file. +// On Windows, os.Getuid() returns -1, so we fall back to the username. +func displayCachePath() string { + uid := os.Getuid() + var id string + if uid == -1 { + // Windows: fall back to username. + if u, err := user.Current(); err == nil { + id = u.Username + } else { + id = "windows" + } + } else { + id = fmt.Sprintf("%d", uid) + } + return filepath.Join(os.TempDir(), fmt.Sprintf("uncompact-display-%s.txt", id)) +} + +// writeDisplayCache atomically writes content to the display cache file so that +// the UserPromptSubmit hook (show-cache) can read and display it on the next prompt. +func writeDisplayCache(content string) error { + cachePath := displayCachePath() + tmp, err := os.CreateTemp(filepath.Dir(cachePath), "uncompact-display-*.tmp") + if err != nil { + return err + } + if _, err := tmp.WriteString(content); err != nil { + tmp.Close() + os.Remove(tmp.Name()) + return err + } + tmp.Close() + return os.Rename(tmp.Name(), cachePath) +} diff --git a/cmd/run.go b/cmd/run.go index 5077a72..51b9b82 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -180,6 +180,11 @@ func runHandler(cmd *cobra.Command, args []string) error { // Emit context bomb to stdout fmt.Print(output) + // Write to display cache so the UserPromptSubmit hook (show-cache) can display it. + if err := writeDisplayCache(output); err != nil { + logFn("[warn] display cache write error: %v", err) + } + // Log the injection var staleLogTime *time.Time if stale { @@ -229,6 +234,10 @@ func runWithoutCache(cfg *config.Config, proj *project.Info, wm *project.Working } fmt.Print(output) + + // Write to display cache so the UserPromptSubmit hook (show-cache) can display it. + _ = writeDisplayCache(output) + return nil } diff --git a/cmd/showcache.go b/cmd/showcache.go index b809ed4..ec3bf84 100644 --- a/cmd/showcache.go +++ b/cmd/showcache.go @@ -3,8 +3,6 @@ package cmd import ( "fmt" "os" - "os/user" - "path/filepath" "github.com/spf13/cobra" ) @@ -22,19 +20,7 @@ func init() { } func showCacheHandler(cmd *cobra.Command, args []string) error { - uid := os.Getuid() - var cacheID string - if uid == -1 { - // Windows: os.Getuid() always returns -1; fall back to username. - if u, err := user.Current(); err == nil { - cacheID = u.Username - } else { - cacheID = "windows" - } - } else { - cacheID = fmt.Sprintf("%d", uid) - } - cachePath := filepath.Join(os.TempDir(), fmt.Sprintf("uncompact-display-%s.txt", cacheID)) + cachePath := displayCachePath() data, err := os.ReadFile(cachePath) if os.IsNotExist(err) {