start.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * scientia
  3. *
  4. * A terminal client written in go.
  5. *
  6. * Copyright 2023 Johannes Keßler
  7. *
  8. * https://www.bananas-playground.net/projekt/scientia/
  9. *
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
  13. *
  14. * You should have received a copy of the
  15. * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
  16. * along with this program. If not, see http://www.sun.com/cddl/cddl.html
  17. *
  18. *
  19. * This is the start "screen" which displays the available actions which can be selected.
  20. * The values and identifiers are defined in the main file, since they are used there too
  21. */
  22. package main
  23. import (
  24. "fmt"
  25. "github.com/charmbracelet/bubbles/list"
  26. tea "github.com/charmbracelet/bubbletea"
  27. "github.com/charmbracelet/lipgloss"
  28. "io"
  29. )
  30. var (
  31. titleStyle = lipgloss.NewStyle().MarginLeft(0)
  32. itemStyle = lipgloss.NewStyle().PaddingLeft(4)
  33. selectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170"))
  34. paginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4)
  35. helpStyle = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1)
  36. )
  37. // item stuff
  38. type item struct {
  39. title, desc string
  40. }
  41. func (i item) Title() string { return i.title }
  42. func (i item) Description() string { return i.desc }
  43. func (i item) FilterValue() string { return i.title }
  44. type itemDelegate struct{}
  45. func (d itemDelegate) Height() int {
  46. return 1
  47. }
  48. func (d itemDelegate) Spacing() int {
  49. return 0
  50. }
  51. func (d itemDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd {
  52. return nil
  53. }
  54. func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
  55. i, ok := listItem.(item)
  56. if !ok {
  57. return
  58. }
  59. str := fmt.Sprintf("%d. %s", index+1, i.Title())
  60. fn := itemStyle.Render
  61. if index == m.Index() {
  62. fn = func(s string) string {
  63. return selectedItemStyle.Render("> " + s)
  64. }
  65. }
  66. fmt.Fprint(w, fn(str))
  67. }
  68. func initStart() list.Model {
  69. items := []list.Item{
  70. item{title: "Create", desc: ITEM_CREATE_VALUE},
  71. item{title: "List", desc: ITEM_LIST_VALUE},
  72. item{title: "Update", desc: ITEM_UPDATE_VALUE},
  73. }
  74. l := list.New(items, itemDelegate{}, 20, 14)
  75. l.Title = "Please select an option"
  76. l.SetShowStatusBar(false)
  77. l.SetFilteringEnabled(false)
  78. l.Styles.Title = titleStyle
  79. l.Styles.PaginationStyle = paginationStyle
  80. l.Styles.HelpStyle = helpStyle
  81. return l
  82. }
  83. func startView(m mainModel) string {
  84. return fmt.Sprintf("\n%s", m.start.View())
  85. }
  86. func startUpdate(msg tea.Msg, m mainModel) (tea.Model, tea.Cmd) {
  87. switch msg := msg.(type) {
  88. case tea.WindowSizeMsg:
  89. m.start.SetWidth(msg.Width)
  90. //m.viewport.Width = msg.Width
  91. //m.viewport.Height = msg.Height
  92. return m, nil
  93. case tea.KeyMsg:
  94. switch msg.Type {
  95. // esc does close?
  96. case tea.KeyEsc:
  97. return m, nil
  98. case tea.KeyCtrlC:
  99. m.quitting = true
  100. return m, tea.Quit
  101. case tea.KeyEnter:
  102. i, ok := m.start.SelectedItem().(item)
  103. if ok {
  104. m.choice = i.Description()
  105. }
  106. return m, nil
  107. case tea.KeyRunes:
  108. switch string(msg.Runes) {
  109. case "q":
  110. m.quitting = true
  111. return m, tea.Quit
  112. }
  113. }
  114. }
  115. var cmd tea.Cmd
  116. m.start, cmd = m.start.Update(msg)
  117. return m, cmd
  118. }