final.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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", self.handler.opts.Config_file_name)),
  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 := ""
  70. if filepath.IsAbs(self.handler.opts.Config_file_name) {
  71. path = self.handler.opts.Config_file_name
  72. } else {
  73. path = filepath.Join(utils.ConfigDir(), self.handler.opts.Config_file_name)
  74. }
  75. updated, err := patcher.Patch(path, "KITTY_FONTS", self.settings.serialized(), "font_family", "bold_font", "italic_font", "bold_italic_font")
  76. if err != nil {
  77. return err
  78. }
  79. if updated {
  80. switch self.handler.opts.Reload_in {
  81. case "parent":
  82. config.ReloadConfigInKitty(true)
  83. case "all":
  84. config.ReloadConfigInKitty(false)
  85. }
  86. }
  87. self.lp.Quit(0)
  88. return nil
  89. }
  90. return
  91. }
  92. func (self *final_pane) on_text(text string, from_key_event bool, in_bracketed_paste bool) (err error) {
  93. if from_key_event {
  94. switch text {
  95. case "s", "S":
  96. output_on_exit = self.settings.serialized() + "\n"
  97. self.lp.Quit(0)
  98. return
  99. }
  100. }
  101. return
  102. }
  103. func (self *final_pane) on_enter(family string, settings faces_settings) error {
  104. self.settings = settings
  105. self.family = family
  106. self.handler.current_pane = self
  107. return self.handler.draw_screen()
  108. }