mirror of
https://github.com/Slug-Boi/cocommit.git
synced 2026-05-13 12:45:47 +00:00
feat: add tui show users (WIP) and prefer bat if present
This commit is contained in:
@@ -0,0 +1,106 @@
|
|||||||
|
package tui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/charmbracelet/bubbles/viewport"
|
||||||
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
"github.com/charmbracelet/glamour"
|
||||||
|
"github.com/charmbracelet/lipgloss"
|
||||||
|
)
|
||||||
|
|
||||||
|
//TODO: MAybe change away from glamour if the weird email issue can't be solved
|
||||||
|
|
||||||
|
var content string
|
||||||
|
|
||||||
|
var helpStyle_us = lipgloss.NewStyle().Foreground(lipgloss.Color("241")).Render
|
||||||
|
|
||||||
|
type example struct {
|
||||||
|
viewport viewport.Model
|
||||||
|
}
|
||||||
|
|
||||||
|
func newExample() (*example, error) {
|
||||||
|
const width = 78
|
||||||
|
|
||||||
|
vp := viewport.New(width, 20)
|
||||||
|
vp.Style = lipgloss.NewStyle().
|
||||||
|
BorderStyle(lipgloss.RoundedBorder()).
|
||||||
|
BorderForeground(lipgloss.Color("62")).
|
||||||
|
PaddingRight(2)
|
||||||
|
|
||||||
|
renderer, err := glamour.NewTermRenderer(
|
||||||
|
glamour.WithPreservedNewLines(),
|
||||||
|
glamour.WithAutoStyle(),
|
||||||
|
glamour.WithWordWrap(width),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
str, err := renderer.Render(content)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
vp.SetContent(str)
|
||||||
|
|
||||||
|
return &example{
|
||||||
|
viewport: vp,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e example) Init() tea.Cmd {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e example) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
|
switch msg := msg.(type) {
|
||||||
|
case tea.KeyMsg:
|
||||||
|
switch msg.String() {
|
||||||
|
case "q", "ctrl+c", "esc":
|
||||||
|
return e, tea.Quit
|
||||||
|
default:
|
||||||
|
var cmd tea.Cmd
|
||||||
|
e.viewport, cmd = e.viewport.Update(msg)
|
||||||
|
return e, cmd
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e example) View() string {
|
||||||
|
return e.viewport.View() + e.helpView()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e example) helpView() string {
|
||||||
|
return helpStyle_us("\n ↑/↓: Navigate • q: Quit\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
func Entry_US(author_file string) {
|
||||||
|
file, err := os.Open(author_file)
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
var cnt strings.Builder
|
||||||
|
|
||||||
|
for scanner.Scan() {
|
||||||
|
cnt.WriteString(scanner.Text() + "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
content = cnt.String()
|
||||||
|
|
||||||
|
model, err := newExample()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Could not initialize Bubble Tea model:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := tea.NewProgram(model).Run(); err != nil {
|
||||||
|
fmt.Println("Bummer, there's been an error:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,10 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"main/src_code/go_src/cmd/tui"
|
||||||
"main/src_code/go_src/cmd/utils"
|
"main/src_code/go_src/cmd/utils"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"slices"
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -19,6 +21,9 @@ var usersCmd = &cobra.Command{
|
|||||||
Long: `Displays all users from the author file located at: ` + authorfile,
|
Long: `Displays all users from the author file located at: ` + authorfile,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
//TODO: make this print a bit prettier (sort it and maybe use a table)
|
//TODO: make this print a bit prettier (sort it and maybe use a table)
|
||||||
|
// check if the no pretty print flag is set
|
||||||
|
np, _ := cmd.Flags().GetBool("np")
|
||||||
|
if np {
|
||||||
println("List of users:\nFormat: <shortname>/<name> -> Username: <username> Email: <email>")
|
println("List of users:\nFormat: <shortname>/<name> -> Username: <username> Email: <email>")
|
||||||
seen_users := []utils.User{}
|
seen_users := []utils.User{}
|
||||||
user_sb := []string{}
|
user_sb := []string{}
|
||||||
@@ -31,9 +36,21 @@ var usersCmd = &cobra.Command{
|
|||||||
sort.Strings(user_sb)
|
sort.Strings(user_sb)
|
||||||
println(strings.Join(user_sb, ""))
|
println(strings.Join(user_sb, ""))
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
bat_check := exec.Command("which", "bat")
|
||||||
|
out, _ := bat_check.CombinedOutput()
|
||||||
|
if string(out) == "" {
|
||||||
|
tui.Entry_US(authorfile)
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
bat := exec.Command("bat", authorfile)
|
||||||
|
bat.Stdout = os.Stdout
|
||||||
|
bat.Stderr = os.Stderr
|
||||||
|
bat.Run()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(usersCmd)
|
rootCmd.AddCommand(usersCmd)
|
||||||
|
usersCmd.Flags().BoolP("np", "n", false, "No pretty print of the users")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user