create.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/textarea"
  25. tea "github.com/charmbracelet/bubbletea"
  26. "github.com/charmbracelet/lipgloss"
  27. )
  28. var (
  29. headline = lipgloss.NewStyle().Margin(1, 0, 1, 2)
  30. infoText = lipgloss.NewStyle().Margin(1, 0, 1, 2).Foreground(lipgloss.AdaptiveColor{Light: "#969B86", Dark: "#696969"})
  31. )
  32. func initCreate() textarea.Model {
  33. ta := textarea.New()
  34. ta.Placeholder = "Once upon a time..."
  35. ta.SetHeight(10)
  36. ta.SetWidth(80)
  37. ta.Focus()
  38. return ta
  39. }
  40. func createView(m mainModel) string {
  41. return lipgloss.JoinVertical(lipgloss.Left,
  42. headline.Render("Create a new entry"),
  43. m.create.View(),
  44. infoText.Render("esc*2 to get back and discard. ctrl+s to save."))
  45. }
  46. func createUpdate(msg tea.Msg, m mainModel) (tea.Model, tea.Cmd) {
  47. var cmds []tea.Cmd
  48. var cmd tea.Cmd
  49. switch msg := msg.(type) {
  50. case tea.WindowSizeMsg:
  51. m.create.SetWidth(msg.Width)
  52. return m, nil
  53. case tea.KeyMsg:
  54. switch msg.Type {
  55. case tea.KeyCtrlC:
  56. m.quitting = true
  57. return m, tea.Quit
  58. case tea.KeyCtrlS:
  59. m.choice = ""
  60. return m, nil
  61. case tea.KeyEsc:
  62. if m.create.Focused() {
  63. m.create.Blur()
  64. } else if !m.create.Focused() {
  65. m.choice = ""
  66. return m, nil
  67. }
  68. default:
  69. if !m.create.Focused() {
  70. cmd = m.create.Focus()
  71. cmds = append(cmds, cmd)
  72. }
  73. }
  74. }
  75. m.create, cmd = m.create.Update(msg)
  76. cmds = append(cmds, cmd)
  77. return m, tea.Batch(cmds...)
  78. }