mirror of
https://github.com/charmbracelet/gum.git
synced 2024-09-11 06:16:03 +03:00
feat: add stdin input handling and logging helpers
This commit is contained in:
parent
454040cf4d
commit
2320f10278
8
internal/log/log.go
Normal file
8
internal/log/log.go
Normal file
@ -0,0 +1,8 @@
|
||||
package log
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Error prints an error message to the user.
|
||||
func Error(message string) {
|
||||
fmt.Println("Error:", message)
|
||||
}
|
36
internal/stdin/stdin.go
Normal file
36
internal/stdin/stdin.go
Normal file
@ -0,0 +1,36 @@
|
||||
package stdin
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Read reads input from an stdin pipe.
|
||||
func Read() (string, error) {
|
||||
stat, err := os.Stdin.Stat()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if stat.Mode()&os.ModeNamedPipe == 0 && stat.Size() == 0 {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
var b strings.Builder
|
||||
|
||||
for {
|
||||
r, _, err := reader.ReadRune()
|
||||
if err != nil && err == io.EOF {
|
||||
break
|
||||
}
|
||||
_, err = b.WriteRune(r)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
return b.String(), nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user