final.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package choose_fonts
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strings"
  6. "kitty/tools/config"
  7. "kitty/tools/tui/loop"
  8. "kitty/tools/utils"
  9. )
  10. var _ = fmt.Print
  11. type final_pane struct {
  12. handler *handler
  13. settings faces_settings
  14. family string
  15. lp *loop.Loop
  16. }
  17. func (self *final_pane) render_lines(start_y int, lines ...string) (y int) {
  18. sz, _ := self.handler.lp.ScreenSize()
  19. _, y, str := self.handler.render_lines.InRectangle(lines, 0, start_y, int(sz.WidthCells), int(sz.HeightCells)-y, &self.handler.mouse_state, self.on_click)
  20. self.handler.lp.QueueWriteString(str)
  21. return
  22. }
  23. func (self *final_pane) draw_screen() (err error) {
  24. s := self.lp.SprintStyled
  25. h := func(x string) string { return s(highlight_key_style, x) }
  26. self.render_lines(0,
  27. fmt.Sprintf("You have chosen the %s family", s(current_val_style, self.family)),
  28. "",
  29. "What would you like to do?",
  30. "",
  31. fmt.Sprintf("%s to modify %s and use the new fonts", h("Enter"), s("italic", `kitty.conf`)),
  32. "",
  33. fmt.Sprintf("%s to abort and return to font selection", h("Esc")),
  34. "",
  35. fmt.Sprintf("%s to write the new font settings to %s", h("s"), s("italic", `STDOUT`)),
  36. "",
  37. fmt.Sprintf("%s to quit", h("Ctrl+c")),
  38. )
  39. return
  40. }
  41. func (self *final_pane) initialize(h *handler) (err error) {
  42. self.handler = h
  43. self.lp = h.lp
  44. return
  45. }
  46. func (self *final_pane) on_wakeup() error {
  47. return self.handler.draw_screen()
  48. }
  49. func (self *final_pane) on_click(id string) (err error) {
  50. return
  51. }
  52. func (self faces_settings) serialized() string {
  53. return strings.Join([]string{
  54. "font_family " + self.font_family,
  55. "bold_font " + self.bold_font,
  56. "italic_font " + self.italic_font,
  57. "bold_italic_font " + self.bold_italic_font,
  58. }, "\n")
  59. }
  60. func (self *final_pane) on_key_event(event *loop.KeyEvent) (err error) {
  61. if event.MatchesPressOrRepeat("esc") {
  62. event.Handled = true
  63. self.handler.current_pane = &self.handler.faces
  64. return self.handler.draw_screen()
  65. }
  66. if event.MatchesPressOrRepeat("enter") {
  67. event.Handled = true
  68. patcher := config.Patcher{Write_backup: true}
  69. path := filepath.Join(utils.ConfigDir(), "kitty.conf")
  70. updated, err := patcher.Patch(path, "KITTY_FONTS", self.settings.serialized(), "font_family", "bold_font", "italic_font", "bold_italic_font")
  71. if err != nil {
  72. return err
  73. }
  74. if updated {
  75. switch self.handler.opts.Reload_in {
  76. case "parent":
  77. config.ReloadConfigInKitty(true)
  78. case "all":
  79. config.ReloadConfigInKitty(false)
  80. }
  81. }
  82. self.lp.Quit(0)
  83. return nil
  84. }
  85. return
  86. }
  87. func (self *final_pane) on_text(text string, from_key_event bool, in_bracketed_paste bool) (err error) {
  88. if from_key_event {
  89. switch text {
  90. case "s", "S":
  91. output_on_exit = self.settings.serialized() + "\n"
  92. self.lp.Quit(0)
  93. return
  94. }
  95. }
  96. return
  97. }
  98. func (self *final_pane) on_enter(family string, settings faces_settings) error {
  99. self.settings = settings
  100. self.family = family
  101. self.handler.current_pane = self
  102. return self.handler.draw_screen()
  103. }