2024-04-22 14:24:29 +02:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2024-05-22 10:00:10 +02:00
|
|
|
"bytes"
|
2024-04-22 14:24:29 +02:00
|
|
|
"fmt"
|
2024-05-22 10:00:10 +02:00
|
|
|
"html/template"
|
2024-04-22 14:24:29 +02:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2024-05-22 10:00:10 +02:00
|
|
|
"git.andreafazzi.eu/andrea/probo/embed"
|
2024-04-22 14:24:29 +02:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2024-05-22 10:00:10 +02:00
|
|
|
"github.com/charmbracelet/glamour"
|
2024-04-22 14:24:29 +02:00
|
|
|
)
|
|
|
|
|
2024-05-22 10:00:10 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-04-22 14:24:29 +02:00
|
|
|
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())
|
|
|
|
}
|