probo/cmd/rank.go
2024-06-05 12:16:08 +02:00

59 lines
1.2 KiB
Go

/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"os"
"git.andreafazzi.eu/andrea/probo/cmd/rank"
"git.andreafazzi.eu/andrea/probo/cmd/util"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
"github.com/spf13/cobra"
)
// rankCmd represents the rank command
var rankCmd = &cobra.Command{
Use: "rank",
Short: "Show a ranking from the given responses.",
Long: util.RenderMarkdownTemplates("cli/*.tmpl", "cli/rank/*.tmpl"),
Run: runRank,
}
func runRank(cmd *cobra.Command, args []string) {
path, err := cmd.Flags().GetString("input")
if err != nil {
panic(err)
}
f := util.LogToFile()
if f != nil {
defer f.Close()
}
lipgloss.SetColorProfile(termenv.TrueColor)
model, err := tea.NewProgram(
rank.New(path, util.ReadStdin()),
tea.WithOutput(os.Stderr),
).Run()
if err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
result := model.(*rank.RankModel)
if result.Result != "" {
fmt.Fprintf(os.Stdout, result.Result)
}
}
func init() {
rootCmd.AddCommand(rankCmd)
rootCmd.PersistentFlags().StringP("input", "i", "", "Specify an input file")
}