49 lines
785 B
Go
49 lines
785 B
Go
package util
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
func LogToFile() *os.File {
|
|
if len(os.Getenv("DEBUG")) > 0 {
|
|
f, err := tea.LogToFile("probo-debug.log", "[DEBUG]")
|
|
if err != nil {
|
|
fmt.Println("fatal:", err)
|
|
os.Exit(1)
|
|
}
|
|
return f
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ReadStdin() string {
|
|
var b strings.Builder
|
|
|
|
stat, err := os.Stdin.Stat()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if stat.Mode()&os.ModeNamedPipe != 0 || stat.Size() != 0 {
|
|
reader := bufio.NewReader(os.Stdin)
|
|
for {
|
|
r, _, err := reader.ReadRune()
|
|
if err != nil && err == io.EOF {
|
|
break
|
|
}
|
|
_, err = b.WriteRune(r)
|
|
if err != nil {
|
|
fmt.Println("Error getting input:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
}
|
|
|
|
return strings.TrimSpace(b.String())
|
|
}
|