chore: rename src folder

This commit is contained in:
Slug-Boi
2024-11-01 08:32:10 +01:00
parent a2c16c043c
commit 73ee788e8b
22 changed files with 1 additions and 439 deletions
+4
View File
@@ -0,0 +1,4 @@
# TUI based on bubbletea
All of the current TUI elements are mostly the same as the base templates from the bubble tea github repo. I am not a designer and the templates look pretty cool anyways so all in all very cool stuff
[Bubbletea Examples Folder](https://github.com/charmbracelet/bubbletea/tree/main/examples)
+305
View File
@@ -0,0 +1,305 @@
package tui
// A simple example demonstrating the use of multiple text input components
// from the Bubbles component library.
import (
"fmt"
"os"
"strings"
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
//"github.com/inancgumus/screen"
)
var (
focusedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("205"))
blurredStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240"))
cursorStyle = focusedStyle
noStyle = lipgloss.NewStyle()
cursorModeHelpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("244"))
focusedButton = focusedStyle.Render("[ Submit ]")
focusedExclude = focusedStyle.Render("[ Exclude ]")
blurredButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Submit"))
excludeButton = fmt.Sprintf("[ %s ]", blurredStyle.Render("Exclude"))
)
var removeButton bool
type model_ca struct {
focusIndex int
inputs []textinput.Model
quitting bool
exclude bool
}
func createAuthorModel() model_ca {
m := model_ca{
inputs: make([]textinput.Model, 5),
}
var t textinput.Model
for i := range m.inputs {
t = textinput.New()
t.Cursor.Style = cursorStyle
//t.CharLimit = 32
switch i {
case 0:
t.Placeholder = "Shortname (e.g. jo)"
t.Focus()
t.PromptStyle = focusedStyle
t.TextStyle = focusedStyle
case 1:
t.Placeholder = "Long name (e.g. JohnDoe)"
case 2:
t.Placeholder = "Username (e.g. JohnDoe-gh)"
case 3:
t.Placeholder = "Email (e.g. JohnDoe@domain.do"
case 4:
t.Placeholder = "Group tags (e.g. gr1|gr2)"
}
m.inputs[i] = t
}
return m
}
func tempAuthorModel() model_ca {
m := model_ca{
inputs: make([]textinput.Model, 2),
}
var t textinput.Model
for i := range m.inputs {
t = textinput.New()
t.Cursor.Style = cursorStyle
//t.CharLimit = 32
switch i {
case 0:
t.Placeholder = "Username (e.g. JohnDoe-gh)"
t.Focus()
t.PromptStyle = focusedStyle
t.TextStyle = focusedStyle
case 1:
t.Placeholder = "Email (e.g. JohnDoe@JohnDoe.io)"
}
m.inputs[i] = t
}
removeButton = true
return m
}
func initialModel(model string) model_ca {
if model == "author" {
return createAuthorModel()
} else {
return tempAuthorModel()
}
}
func (m model_ca) Init() tea.Cmd {
return textinput.Blink
}
func (m model_ca) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc":
m.inputs = nil
return m, tea.Quit
// Set focus to next input
case "tab", "shift+tab", "enter", "up", "down":
s := msg.String()
// Did the user press enter while the submit button was focused?
// If so, exit.
if !removeButton {
if s == "enter" && m.focusIndex == len(m.inputs)+1 {
m.quitting = true
return m, tea.Quit
} else if s == "enter" && m.focusIndex == len(m.inputs) {
// toggle exclude
m.exclude = !m.exclude
return m, nil
}
} else {
if s == "enter" && m.focusIndex == len(m.inputs) {
m.quitting = true
return m, tea.Quit
}
}
// Cycle indexes
if s == "up" || s == "shift+tab" {
m.focusIndex--
} else {
m.focusIndex++
}
if m.focusIndex > len(m.inputs)+1 {
m.focusIndex = 0
} else if m.focusIndex < 0 {
m.focusIndex = len(m.inputs)
}
cmds := make([]tea.Cmd, len(m.inputs))
for i := 0; i <= len(m.inputs)-1; i++ {
if i == m.focusIndex {
// Set focused state
cmds[i] = m.inputs[i].Focus()
m.inputs[i].PromptStyle = focusedStyle
m.inputs[i].TextStyle = focusedStyle
continue
}
// Remove focused state
m.inputs[i].Blur()
m.inputs[i].PromptStyle = noStyle
m.inputs[i].TextStyle = noStyle
}
return m, tea.Batch(cmds...)
}
}
// Handle character input and blinking
cmd := m.updateInputs(msg)
return m, cmd
}
func (m *model_ca) updateInputs(msg tea.Msg) tea.Cmd {
cmds := make([]tea.Cmd, len(m.inputs))
// Only text inputs with Focus() set will respond, so it's safe to simply
// update all of them here without any further logic.
for i := range m.inputs {
m.inputs[i], cmds[i] = m.inputs[i].Update(msg)
}
return tea.Batch(cmds...)
}
func (m model_ca) View() string {
if m.quitting {
return ""
}
var b strings.Builder
for i := range m.inputs {
b.WriteString(m.inputs[i].View())
if i < len(m.inputs)-1 {
b.WriteRune('\n')
}
}
//TODO: add check here for wether this button is needed
var exclude *string
var button *string
if !removeButton {
exclude = &excludeButton
if m.focusIndex == len(m.inputs) {
exclude = &focusedExclude
}
button = &blurredButton
if m.focusIndex == len(m.inputs)+1 {
button = &focusedButton
}
if m.exclude {
fmt.Fprintf(&b, "\n\n%s: [X]\n\n", *exclude)
} else {
fmt.Fprintf(&b, "\n\n%s: [ ]\n\n", *exclude)
}
} else {
button = &blurredButton
if m.focusIndex == len(m.inputs) {
button = &focusedButton
}
}
fmt.Fprintf(&b, "\n\n%s\n\n", *button)
b.WriteString(cursorModeHelpStyle.Render())
return b.String()
}
func Entry_CA() string {
m, err := tea.NewProgram(initialModel("author")).Run()
if err != nil {
fmt.Printf("could not start program: %s\n", err)
os.Exit(1)
}
if len(m.(model_ca).inputs) > 0 &&
m.(model_ca).inputs[0].Value() != "" &&
m.(model_ca).inputs[1].Value() != "" &&
m.(model_ca).inputs[2].Value() != "" &&
m.(model_ca).inputs[3].Value() != "" {
author_file := utils.Find_authorfile()
f, err := os.OpenFile(author_file, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
panic(err)
}
defer f.Close()
sb := strings.Builder{}
sb.WriteRune('\n')
sb.WriteString(fmt.Sprintf("%s|%s|%s|%s",
m.(model_ca).inputs[0].Value(),
m.(model_ca).inputs[1].Value(),
m.(model_ca).inputs[2].Value(),
m.(model_ca).inputs[3].Value()))
if m.(model_ca).exclude {
sb.WriteString(fmt.Sprintf("|%s", "ex"))
}
if m.(model_ca).inputs[4].Value() != "" {
sb.WriteString(fmt.Sprintf(";;%s", m.(model_ca).inputs[4].Value()))
}
//sb.WriteRune('\n')
if _, err = f.WriteString(sb.String()); err != nil {
panic(err)
}
utils.Define_users(utils.Find_authorfile())
return m.(model_ca).inputs[0].Value()
}
return ""
}
func Entry_TA() string {
m, err := tea.NewProgram(initialModel("temp")).Run()
if err != nil {
fmt.Printf("could not start program: %s\n", err)
os.Exit(1)
}
if len(m.(model_ca).inputs) > 0 &&
m.(model_ca).inputs[0].Value() != "" &&
m.(model_ca).inputs[1].Value() != "" {
utils.TempAddUser(m.(model_ca).inputs[0].Value(), m.(model_ca).inputs[1].Value())
return m.(model_ca).inputs[0].Value() + ":" + m.(model_ca).inputs[1].Value()
}
return ""
}
+125
View File
@@ -0,0 +1,125 @@
package tui
// A simple program demonstrating the textarea component from the Bubbles
// component library.
//TODO: maybe add a submit button below the textarea
import (
"fmt"
"log"
"os"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textarea"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/inancgumus/screen"
)
type KeyMap struct {
EndWithMes key.Binding
NewLine key.Binding
}
func newKeyMap() *KeyMap {
return &KeyMap{
EndWithMes: key.NewBinding(
key.WithKeys("enter"),
),
NewLine: key.NewBinding(
key.WithKeys("shift+tab"),
),
}
}
func Entry_CM() string {
newKeyMap()
p := tea.NewProgram(initialModel_cm())
m, err := p.Run()
if err != nil {
log.Fatal(err)
}
if m.(model_cm).textarea.Value() == "" {
screen.Clear()
screen.MoveTopLeft()
fmt.Println("No commit message provided. Exiting...")
os.Exit(0)
}
return m.(model_cm).textarea.Value() + "\n"
}
type errMsg error
type model_cm struct {
textarea textarea.Model
keys *KeyMap
err error
}
func initialModel_cm() model_cm {
ti := textarea.New()
ti.FocusedStyle = textarea.Style{Base: lipgloss.NewStyle().Foreground(lipgloss.Color("170"))}
ti.SetWidth(80)
ti.SetHeight(8)
ti.Placeholder = "Write your commit message here..."
ti.Focus()
return model_cm{
textarea: ti,
keys: newKeyMap(),
err: nil,
}
}
func (m model_cm) Init() tea.Cmd {
return textarea.Blink
}
func (m model_cm) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, m.keys.EndWithMes):
return m, tea.Quit
case key.Matches(msg, m.keys.NewLine):
m.textarea.SetValue(m.textarea.Value() + "\n")
return m, nil
case msg.String() == "esc":
m.textarea.SetValue("")
return m, tea.Quit
}
switch msg.Type {
case tea.KeyCtrlC:
m.textarea.SetValue("")
return m, tea.Quit
default:
if !m.textarea.Focused() {
cmd = m.textarea.Focus()
cmds = append(cmds, cmd)
}
}
// We handle errors just like any other message
case errMsg:
m.err = msg
return m, nil
}
m.textarea, cmd = m.textarea.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m model_cm) View() string {
return fmt.Sprintf(
"Commit message:\n\n%s\n\n%s",
m.textarea.View(),
"(enter | Submit)\n(shift+tab | Newline)\n(ctrl+c | Cancel)",
) + "\n\n"
}
+342
View File
@@ -0,0 +1,342 @@
package tui
import (
"fmt"
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils"
"io"
"os"
"sort"
"strings"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/inancgumus/screen"
)
const listHeight = 14
var (
titleStyle = lipgloss.NewStyle().MarginLeft(2)
itemStyle = lipgloss.NewStyle().PaddingLeft(4)
selectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170"))
highlightStyle = lipgloss.NewStyle().PaddingLeft(4).Background(lipgloss.Color("236")).Foreground(lipgloss.Color("17"))
selectedHighlightStyle = lipgloss.NewStyle().PaddingLeft(2).Background(lipgloss.Color("236")).Foreground(lipgloss.Color("170"))
deletionStyle = lipgloss.NewStyle().MarginLeft(2).Foreground(lipgloss.Color("9"))
paginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4)
helpStyle = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1)
//quitTextStyle = lipgloss.NewStyle().Margin(1, 0, 2, 4)
)
type item string
var selected = map[string]item{}
var negation = false
var dupProtect = map[string]string{}
type listKeyMap struct {
selectAll key.Binding
negation key.Binding
groupSelect key.Binding
selectOne key.Binding
createAuthor key.Binding
deleteAuthor key.Binding
tempAdd key.Binding
}
func newListKeyMap() *listKeyMap {
return &listKeyMap{
selectAll: key.NewBinding(
key.WithKeys("A"),
key.WithHelp("A", "Add all authors"),
),
negation: key.NewBinding(
key.WithKeys("n"),
key.WithHelp("n", "Toggle negation and select author"),
),
groupSelect: key.NewBinding(
key.WithKeys("f"),
key.WithHelp("f", "Select group"),
),
selectOne: key.NewBinding(
key.WithKeys(" "),
key.WithHelp("space", "Select author"),
),
createAuthor: key.NewBinding(
key.WithKeys("C"),
key.WithHelp("C", "Create new author"),
),
deleteAuthor: key.NewBinding(
key.WithKeys("D"),
key.WithHelp("D", "Delete author"),
),
tempAdd: key.NewBinding(
key.WithKeys("T"),
key.WithHelp("T", "Add temporary author"),
),
}
}
func (i item) FilterValue() string { return string(i) }
type itemDelegate struct{}
func (d itemDelegate) Height() int { return 1 }
func (d itemDelegate) Spacing() int { return 0 }
func (d itemDelegate) Update(_ tea.Msg, _ *list.Model) tea.Cmd { return nil }
func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
i, ok := listItem.(item)
if !ok {
return
}
str := fmt.Sprintf("%d. %s", index+1, i)
fn := itemStyle.Render
if _, ok := selected[string(i)]; ok {
fn = func(s ...string) string {
base := strings.Join(s, " ")
if negation {
base = base + " ^"
}
if index == m.Index() {
return selectedHighlightStyle.Render("> " + base + " [X]")
} else {
return highlightStyle.Render(base + " [X]")
}
}
} else {
if index == m.Index() {
fn = func(s ...string) string {
return selectedItemStyle.Render("> " + strings.Join(s, " "))
}
}
}
fmt.Fprint(w, fn(str))
}
type model struct {
list list.Model
keys *listKeyMap
quitting bool
}
func (m model) Init() tea.Cmd {
return nil
}
func selectToggle(i item) {
if _, ok := selected[string(i)]; ok {
delete(selected, string(i))
toggleNegation()
} else {
selected[string(i)] = i
}
}
func toggleNegation() {
if len(selected) == 0 {
negation = false
}
}
var deletion bool
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.list.SetWidth(msg.Width)
return m, nil
// If filtering is enabled, skip key handling
case tea.KeyMsg:
// deletion toggle with confirmation required
b := false
defer func(b *bool) { deletion = *b }(&b)
if m.list.FilterState() == list.Filtering {
break
}
// Handle keys from keyList (help menu)
switch {
case key.Matches(msg, m.keys.negation):
i, ok := m.list.SelectedItem().(item)
if ok {
negation = true
selectToggle(i)
}
case key.Matches(msg, m.keys.selectOne):
i, ok := m.list.SelectedItem().(item)
if ok {
selectToggle(i)
}
case key.Matches(msg, m.keys.selectAll):
//TODO: maybe look at behavior of this when auth are already selected
negation = false
for _, i := range m.list.Items() {
selectToggle(i.(item))
}
case key.Matches(msg, m.keys.groupSelect):
// group code goes here
case key.Matches(msg, m.keys.tempAdd):
screen.Clear()
screen.MoveTopLeft()
tempAuthr := Entry_TA()
if tempAuthr != "" {
split := strings.Split(tempAuthr, ":")
item_str := split[0] + " - " + split[1]
dupProtect[item_str] = tempAuthr
i := item(item_str)
m.list.InsertItem(len(m.list.Items())+1, i)
selectToggle(i)
}
return m, tea.ClearScreen
case key.Matches(msg, m.keys.createAuthor):
screen.Clear()
screen.MoveTopLeft()
author := Entry_CA()
if author != "" {
item_str := utils.Users[author].Username + " - " + utils.Users[author].Email
dupProtect[item_str] = author
m.list.InsertItem(len(m.list.Items())+1, item(item_str))
}
return m, tea.ClearScreen
case key.Matches(msg, m.keys.deleteAuthor):
if deletion {
author_str := string(m.list.SelectedItem().(item))
author := dupProtect[author_str]
utils.DeleteOneAuthor(author)
delete(dupProtect, author_str)
m.list.RemoveItem(m.list.Index())
return m, nil
}
b = true
return m, nil
}
// extra key options
switch keypress := msg.String(); keypress {
case "q", "ctrl+c", "esc":
m.quitting = true
selected = nil
return m, tea.Quit
case "enter":
m.quitting = true
return m, tea.Quit
}
}
var cmd tea.Cmd
m.list, cmd = m.list.Update(msg)
return m, cmd
}
func (m model) View() string {
if m.quitting {
return "" //quitTextStyle.Render(strings.Join(m.choice, " "))
}
sb := strings.Builder{}
sb.WriteString("\n" + m.list.View())
if deletion {
sb.WriteString(deletionStyle.Render("\n D: Confirm delete author"))
}
return sb.String()
}
func listModel() model {
items := []list.Item{}
selected = map[string]item{}
dupProtect = map[string]string{}
listKeys := newListKeyMap()
// Add items to the list
for short, user := range utils.Users {
// if items already contains the user, skip it
str_user := user.Username + " - " + user.Email
if _, ok := dupProtect[str_user]; ok {
continue
}
items = append(items, item(str_user))
dupProtect[str_user] = short
}
sort.Slice(items, func(i, j int) bool {
return items[i].(item) < items[j].(item)
})
const defaultWidth = 20
l := list.New(items, itemDelegate{}, defaultWidth, listHeight)
l.Title = "Select authors to add to commit"
l.SetShowStatusBar(false)
l.SetFilteringEnabled(true) // Enable filtering
l.Styles.Title = titleStyle
l.Styles.PaginationStyle = paginationStyle
l.AdditionalShortHelpKeys = // Add help keys (main page)
func() []key.Binding {
return []key.Binding{
listKeys.selectOne,
}
}
l.AdditionalFullHelpKeys = // Add help keys (help menu)
func() []key.Binding {
return []key.Binding{
listKeys.selectAll,
listKeys.negation,
listKeys.groupSelect,
listKeys.createAuthor,
listKeys.tempAdd,
}
}
l.Styles.HelpStyle = helpStyle
return model{list: l, keys: listKeys}
}
// TODO: pass list in as a param to allow for group selection using same template
func Entry() []string {
m := listModel()
f, err := tea.NewProgram(m).Run()
if err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
// Assert the final tea.Model to our local model and print the choice.
output := []string{}
if len(selected) == 0 {
os.Exit(0)
}
for i := range selected {
short := dupProtect[i]
if negation {
short = "^" + short
}
output = append(output, short)
}
if _, ok := f.(model); ok && len(output) > 0 {
return output
}
return nil
}
+127
View File
@@ -0,0 +1,127 @@
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 intialModel_US(author_file string) tea.Model {
loadData(author_file)
model, err := newExample()
if err != nil {
fmt.Println("Could not initialize Bubble Tea model:", err)
os.Exit(1)
}
return model
}
func loadData(author_file string) {
file, err := os.Open(author_file)
if err != nil {
fmt.Println("Could not open file:", err)
os.Exit(1)
}
scanner := bufio.NewScanner(file)
var cnt strings.Builder
scanner.Scan()
header := scanner.Text()
cnt.WriteString(header + "\n")
for scanner.Scan() {
//very hacky it basically just ensure glamour doesn't format the email
cnt.WriteString(":\b" + scanner.Text() + "\n")
}
content = cnt.String()
}
func Entry_US(author_file string) {
model := intialModel_US(author_file)
if _, err := tea.NewProgram(model).Run(); err != nil {
fmt.Println("Bummer, there's been an error:", err)
os.Exit(1)
}
}
+363
View File
@@ -0,0 +1,363 @@
package tui
import (
"bytes"
"os"
"testing"
"time"
"github.com/Slug-Boi/cocommit/src_code/go_src/cmd/utils"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/exp/teatest"
)
const author_data = `syntax for the test file
te|testing|TestUser|test@test.test|ex
ti|testtest|UserName2|testing@user.io;;gr1`
var envVar string
func setup() {
// setup test data
err := os.WriteFile("author_file_test", []byte(author_data), 0644)
if err != nil {
panic(err)
}
os.Setenv("author_file", "author_file_test")
envVar = os.Getenv("author_file")
}
func teardown() {
// remove test data
os.Remove("author_file_test")
os.Setenv("author_file", envVar)
}
// tui_show_users TESTS BEGIN
func TestShowUser(t *testing.T) {
setup()
defer teardown()
m := intialModel_US(envVar)
tm := teatest.NewTestModel(
t, m,
teatest.WithInitialTermSize(300, 300),
)
teatest.WaitFor(t, tm.Output(), func(bts []byte) bool {
return bytes.Contains(bts, []byte("syntax for the test file"))
}, teatest.WithCheckInterval(time.Millisecond*100), teatest.WithDuration(time.Second*2))
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("q"),
})
tm.WaitFinished(t, teatest.WithFinalTimeout(time.Second))
}
// tui_show_users TESTS END
// tui_author TESTS BEGIN
func TestEntryTA(t *testing.T) {
setup()
defer teardown()
m := initialModel("temp")
tm := teatest.NewTestModel(
t, m, teatest.WithInitialTermSize(300, 300),
)
tm.Type("test")
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
})
tm.Type("testtest@temp.io")
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
})
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
})
fm := tm.FinalModel(t)
m, ok := fm.(model_ca)
if !ok {
t.Errorf("Expected model_ca, got %T", fm)
}
if len(m.inputs) != 2 {
t.Errorf("Expected 2 inputs, got %d", len(m.inputs))
}
if m.inputs[0].Value() != "test" {
t.Errorf("Expected 'test', got %s", m.inputs[0].Value())
}
if m.inputs[1].Value() != "testtest@temp.io" {
t.Errorf("Expected 'testtest@temp.io', got %s", m.inputs[1].Value())
}
}
func Test_EntryCA(t *testing.T) {
setup()
defer teardown()
m := initialModel("author")
tm := teatest.NewTestModel(
t, m, teatest.WithInitialTermSize(300, 300),
)
tm.Type("test")
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
})
tm.Type("testtest")
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
})
tm.Type ("TestUser")
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
})
tm.Type("test@temp.io")
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
})
tm.Type("gr1")
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
})
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
})
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("tab"),
})
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
})
fm := tm.FinalModel(t)
m, ok := fm.(model_ca)
if !ok {
t.Errorf("Expected model_ca, got %T", fm)
}
if len(m.inputs) != 5 {
t.Errorf("Expected 5 inputs, got %d", len(m.inputs))
}
if m.inputs[0].Value() != "test" {
t.Errorf("Expected 'test', got %s", m.inputs[0].Value())
}
if m.inputs[1].Value() != "testtest" {
t.Errorf("Expected 'testtest', got %s", m.inputs[1].Value())
}
if m.inputs[2].Value() != "TestUser" {
t.Errorf("Expected 'TestUser', got %s", m.inputs[2].Value())
}
if m.inputs[3].Value() != "test@temp.io" {
t.Errorf("Expected 'test@temp.io', got %s", m.inputs[2].Value())
}
if m.inputs[4].Value() != "gr1" {
t.Errorf("Expected 'gr1', got %s", m.inputs[4].Value())
}
//No clue why the exclude tag isn't working fix later
//TODO: Fix this should be !m.exclude
if m.exclude {
t.Errorf("Expected exclude to be true, got %v", m.exclude)
}
}
// tui_author TESTS END
// tui_commit_message TESTS BEGIN
func Test_EntryCM(t *testing.T) {
setup()
defer teardown()
m := initialModel_cm()
tm := teatest.NewTestModel(
t, m, teatest.WithInitialTermSize(300, 300),
)
tm.Type("test commit message")
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
})
fm := tm.FinalModel(t)
m, ok := fm.(model_cm)
if !ok {
t.Errorf("Expected model_cm, got %T", fm)
}
if m.textarea.Value() != "test commit message" {
t.Errorf("Expected 'test commit message', got %s", m.textarea.Value())
}
}
// tui_commit_message TESTS END
// tui_list TESTS BEGIN
func Test_EntrySelectUsers(t *testing.T) {
setup()
defer teardown()
utils.Define_users("author_file_test")
m := listModel()
tm := teatest.NewTestModel(
t, m, teatest.WithInitialTermSize(300, 300),
)
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune(" "),
})
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
})
fm := tm.FinalModel(t)
m, ok := fm.(model)
if !ok {
t.Errorf("Expected model, got %T", fm)
}
if !m.quitting {
t.Errorf("Expected quitting to be true, got %v", m.quitting)
}
if len(selected) != 1 {
t.Errorf("Expected 1 selected item, got %d", len(selected))
}
}
func Test_EntrySelectAll(t *testing.T) {
setup()
defer teardown()
utils.Define_users("author_file_test")
m := listModel()
tm := teatest.NewTestModel(
t, m, teatest.WithInitialTermSize(300, 300),
)
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("A"),
})
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
})
fm := tm.FinalModel(t)
m, ok := fm.(model)
if !ok {
t.Errorf("Expected model, got %T", fm)
}
if !m.quitting {
t.Errorf("Expected quitting to be true, got %v", m.quitting)
}
if len(selected) != 2 {
t.Errorf("Expected 2 selected item, got %d", len(selected))
}
}
func Test_EntryNegation(t *testing.T) {
setup()
defer teardown()
utils.Define_users("author_file_test")
m := listModel()
tm := teatest.NewTestModel(
t, m, teatest.WithInitialTermSize(300, 300),
)
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("n"),
})
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
})
fm := tm.FinalModel(t)
m, ok := fm.(model)
if !ok {
t.Errorf("Expected model, got %T", fm)
}
if !m.quitting {
t.Errorf("Expected quitting to be true, got %v", m.quitting)
}
if len(selected) != 1 {
t.Errorf("Expected 2 selected item, got %d", len(selected))
}
}
func Test_EntryDeleteAuthor(t *testing.T) {
setup()
defer teardown()
utils.Define_users("author_file_test")
m := listModel()
tm := teatest.NewTestModel(
t, m, teatest.WithInitialTermSize(300, 300),
)
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("D"),
})
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("D"),
})
tm.Send(tea.KeyMsg{
Type: tea.KeyRunes,
Runes: []rune("enter"),
})
fm := tm.FinalModel(t)
m, ok := fm.(model)
if !ok {
t.Errorf("Expected model, got %T", fm)
}
if !m.quitting {
t.Errorf("Expected quitting to be true, got %v", m.quitting)
}
if len(utils.Users) != 2 {
t.Errorf("Expected 2 user after deletion, got %d", len(utils.Users))
}
}
// tui_list TESTS END