Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions cmd/displaycache.go
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 9 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
16 changes: 1 addition & 15 deletions cmd/showcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package cmd
import (
"fmt"
"os"
"os/user"
"path/filepath"

"github.com/spf13/cobra"
)
Expand All @@ -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) {
Expand Down