74 lines
1.2 KiB
Go
74 lines
1.2 KiB
Go
package util
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"fmt"
|
|
"html/template"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
|
|
"git.andreafazzi.eu/andrea/probo/embed"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/glamour"
|
|
)
|
|
|
|
func RenderMarkdownTemplates(paths ...string) string {
|
|
tmpl, err := template.ParseFS(embed.CLI, paths...)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err = tmpl.Execute(&buf, nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
result, err := glamour.Render(buf.String(), "dark")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
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())
|
|
}
|