list.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 create "screen". It displays a textarea to input text into.
  20. * Does the save and creation
  21. */
  22. package main
  23. import (
  24. "github.com/charmbracelet/bubbles/table"
  25. tea "github.com/charmbracelet/bubbletea"
  26. "github.com/charmbracelet/lipgloss"
  27. )
  28. var (
  29. baseStyle = lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()).BorderForeground(lipgloss.Color("240"))
  30. headerStyle = lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()).BorderForeground(lipgloss.Color("240")).BorderBottom(true).Bold(false)
  31. selectedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("229")).Background(lipgloss.Color("57")).Bold(false)
  32. )
  33. func initList() table.Model {
  34. columns := []table.Column{
  35. {Title: "ID", Width: 4},
  36. {Title: "Date", Width: 10},
  37. {Title: "Body", Width: 10},
  38. }
  39. rows := []table.Row{
  40. {"B0ug", "2023-02-17", "Determine this makefile"},
  41. {"k291", "2023-02-16", "This is a terminal cli client written in g"},
  42. {"A93a", "2023-02-15", "ava fallstricke https://www.fs-fmc.kit.edu/sites/default/fil"},
  43. }
  44. t := table.New(
  45. table.WithColumns(columns),
  46. table.WithRows(rows),
  47. table.WithFocused(true),
  48. table.WithHeight(10),
  49. table.WithWidth(700),
  50. )
  51. s := table.DefaultStyles()
  52. s.Header = headerStyle
  53. s.Selected = selectedStyle
  54. t.SetStyles(s)
  55. return t
  56. }
  57. func listUpdate(msg tea.Msg, m mainModel) (tea.Model, tea.Cmd) {
  58. var cmds []tea.Cmd
  59. var cmd tea.Cmd
  60. switch msg := msg.(type) {
  61. case tea.WindowSizeMsg:
  62. m.list.SetWidth(msg.Width)
  63. return m, nil
  64. case tea.KeyMsg:
  65. switch msg.Type {
  66. case tea.KeyCtrlC:
  67. m.quitting = true
  68. return m, tea.Quit
  69. case tea.KeyEsc:
  70. if m.list.Focused() {
  71. m.list.Blur()
  72. }
  73. }
  74. }
  75. m.list, cmd = m.list.Update(msg)
  76. cmds = append(cmds, cmd)
  77. return m, tea.Batch(cmds...)
  78. }
  79. func listView(m mainModel) string {
  80. return baseStyle.Render(m.list.View()) + "\n"
  81. }