scientia-tui.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. package main
  19. import (
  20. "fmt"
  21. "github.com/charmbracelet/bubbles/table"
  22. "github.com/charmbracelet/bubbles/textarea"
  23. "github.com/charmbracelet/bubbles/viewport"
  24. "github.com/charmbracelet/lipgloss"
  25. "os"
  26. "github.com/charmbracelet/bubbles/list"
  27. tea "github.com/charmbracelet/bubbletea"
  28. )
  29. // the unique identifiers for each action of the initial list of actions
  30. const (
  31. ITEM_CREATE_VALUE = "create"
  32. ITEM_LIST_VALUE = "list"
  33. ITEM_UPDATE_VALUE = "update"
  34. )
  35. // some global vars
  36. var (
  37. quitTextStyle = lipgloss.NewStyle().Margin(1, 0, 1, 2)
  38. )
  39. // mainModel Holds all the important stuff
  40. // Needs to be extended if a new action is added
  41. type mainModel struct {
  42. start list.Model
  43. create textarea.Model
  44. list table.Model
  45. choice string
  46. quitting bool
  47. viewport viewport.Model
  48. }
  49. func (m mainModel) Init() tea.Cmd {
  50. return nil
  51. }
  52. // Update The main Update method. Decides the correct action update method
  53. func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  54. switch m.choice {
  55. case ITEM_UPDATE_VALUE:
  56. //return quitTextStyle.Render("Update it is")
  57. case ITEM_LIST_VALUE:
  58. return listUpdate(msg, m)
  59. case ITEM_CREATE_VALUE:
  60. return createUpdate(msg, m)
  61. }
  62. return startUpdate(msg, m)
  63. }
  64. // View The main View method. Decides which action view is called
  65. func (m mainModel) View() string {
  66. if m.quitting {
  67. return quitTextStyle.Render("Good day.")
  68. }
  69. switch m.choice {
  70. case ITEM_UPDATE_VALUE:
  71. return quitTextStyle.Render("Update it is")
  72. case ITEM_LIST_VALUE:
  73. return listView(m)
  74. case ITEM_CREATE_VALUE:
  75. return createView(m)
  76. }
  77. return startView(m)
  78. }
  79. func main() {
  80. m := mainModel{start: initStart(), create: initCreate(), list: initList()}
  81. p := tea.NewProgram(m, tea.WithAltScreen())
  82. if _, err := p.Run(); err != nil {
  83. fmt.Println("Error running program:", err)
  84. os.Exit(1)
  85. }
  86. }