Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

allow piping into 'tell' command, closes #162

Dane Schneider5cead0b7f213
diff --git a/app/cli/cmd/tell.go b/app/cli/cmd/tell.go
index 6c1172da..49500154 100644
--- a/app/cli/cmd/tell.go
+++ b/app/cli/cmd/tell.go
@@ -58,6 +58,7 @@ func doTell(cmd *cobra.Command, args []string) {
}
var prompt string
+ var pipedData string
if len(args) > 0 {
prompt = args[0]
@@ -67,22 +68,30 @@ func doTell(cmd *cobra.Command, args []string) {
term.OutputErrorAndExit("Error reading prompt file: %v", err)
}
prompt = string(bytes)
- } else {
- // Check if there's piped input
- fileInfo, err := os.Stdin.Stat()
+ }
+
+ // Check if there's piped input
+ fileInfo, err := os.Stdin.Stat()
+ if err != nil {
+ term.OutputErrorAndExit("Failed to stat stdin: %v", err)
+ }
+
+ if fileInfo.Mode()&os.ModeNamedPipe != 0 {
+ reader := bufio.NewReader(os.Stdin)
+ pipedDataBytes, err := io.ReadAll(reader)
if err != nil {
- term.OutputErrorAndExit("Failed to stat stdin: %v", err)
+ term.OutputErrorAndExit("Failed to read piped data: %v", err)
}
+ pipedData = string(pipedDataBytes)
+ }
- if fileInfo.Mode()&os.ModeNamedPipe != 0 {
- reader := bufio.NewReader(os.Stdin)
- pipedData, err := io.ReadAll(reader)
- if err != nil {
- term.OutputErrorAndExit("Failed to read piped data: %v", err)
- }
- prompt = string(pipedData)
+ if prompt == "" && pipedData == "" {
+ prompt = getEditorPrompt()
+ } else if pipedData != "" {
+ if prompt != "" {
+ prompt = fmt.Sprintf("%s\n\n---\n\n%s", prompt, pipedData)
} else {
- prompt = getEditorPrompt()
+ prompt = pipedData
}
}
diff --git a/app/cli/stream_tui/update.go b/app/cli/stream_tui/update.go
index e73c6bf5..1376b10a 100644
--- a/app/cli/stream_tui/update.go
+++ b/app/cli/stream_tui/update.go
@@ -124,7 +124,7 @@ func (m *streamUIModel) updateReplyDisplay() {
s := ""
if m.prompt != "" {
- promptTxt, _ := term.GetPlain(m.prompt)
+ promptTxt := term.GetPlain(m.prompt)
s += color.New(color.BgGreen, color.Bold, color.FgHiWhite).Sprintf(" πŸ’¬ User prompt πŸ‘‡ ")
s += "\n\n" + strings.TrimSpace(promptTxt) + "\n"
diff --git a/app/cli/term/format.go b/app/cli/term/format.go
index 89bd17ea..88416e27 100644
--- a/app/cli/term/format.go
+++ b/app/cli/term/format.go
@@ -1,21 +1,16 @@
package term
import (
- "os"
"strings"
"github.com/charmbracelet/glamour"
"github.com/charmbracelet/glow/utils"
"github.com/muesli/reflow/wordwrap"
"github.com/muesli/termenv"
- "golang.org/x/term"
)
func GetMarkdown(input string) (string, error) {
- width, _, err := term.GetSize(int(os.Stdin.Fd()))
- if err != nil {
- return "", err
- }
+ width := getTerminalWidth()
inputBytes := utils.RemoveFrontmatter([]byte(input))
@@ -33,11 +28,8 @@ func GetMarkdown(input string) (string, error) {
return string(out), nil
}
-func GetPlain(input string) (string, error) {
- width, _, err := term.GetSize(int(os.Stdin.Fd()))
- if err != nil {
- return "", err
- }
+func GetPlain(input string) string {
+ width := getTerminalWidth()
s := wordwrap.String(input, min(width-2, 80))
@@ -53,5 +45,5 @@ func GetPlain(input string) (string, error) {
c = "251"
}
- return termenv.String(s).Foreground(termenv.ANSI256.Color(c)).String(), nil
+ return termenv.String(s).Foreground(termenv.ANSI256.Color(c)).String()
}
diff --git a/app/cli/term/utils.go b/app/cli/term/utils.go
index b8d91ca5..f9eb2395 100644
--- a/app/cli/term/utils.go
+++ b/app/cli/term/utils.go
@@ -2,9 +2,9 @@ package term
import (
"fmt"
- "log"
"os"
"os/exec"
+ "strconv"
"strings"
"golang.org/x/term"
@@ -64,16 +64,38 @@ func PageOutputReverse(output string) {
func GetDivisionLine() string {
// Get the terminal width
- terminalWidth, err := getTerminalWidth()
- if err != nil {
- log.Println("Error fetching terminal size:", err)
- terminalWidth = 50 // default width if unable to fetch width
- }
+ terminalWidth := getTerminalWidth()
return strings.Repeat("─", terminalWidth)
}
-func getTerminalWidth() (int, error) {
- width, _, err := term.GetSize(int(os.Stdout.Fd()))
+func getTerminalWidth() int {
+ // Try to get terminal size
+ if w, _, err := term.GetSize(int(os.Stdout.Fd())); err == nil {
+ return w
+ }
+
+ // Use tput to get terminal width
+ if w, err := getWidthFromTput(); err == nil {
+ return w
+ }
+
+ // Try to get width from environment variable
+ if w, err := strconv.Atoi(os.Getenv("COLUMNS")); err == nil {
+ return w
+ }
+
+ // Fallback to default width
+ return 50
+}
+
+func getWidthFromTput() (int, error) {
+ cmd := exec.Command("tput", "cols")
+ cmd.Stdin = os.Stdin
+ out, err := cmd.Output()
+ if err != nil {
+ return 0, err
+ }
+ width, err := strconv.Atoi(strings.TrimSpace(string(out)))
if err != nil {
return 0, err
}