Skip to content

Commit

Permalink
allow piping into 'tell' command, closes #162
Browse files Browse the repository at this point in the history
  • Loading branch information
danenania committed Oct 2, 2024
1 parent 19dcf37 commit 5cead0b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 33 deletions.
33 changes: 21 additions & 12 deletions app/cli/cmd/tell.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func doTell(cmd *cobra.Command, args []string) {
}

var prompt string
var pipedData string

if len(args) > 0 {
prompt = args[0]
Expand All @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/cli/stream_tui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 4 additions & 12 deletions app/cli/term/format.go
Original file line number Diff line number Diff line change
@@ -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))

Expand All @@ -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))

Expand All @@ -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()
}
38 changes: 30 additions & 8 deletions app/cli/term/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package term

import (
"fmt"
"log"
"os"
"os/exec"
"strconv"
"strings"

"golang.org/x/term"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 5cead0b

Please sign in to comment.