kitty-visual.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env ruby
  2. # Copyright 2024 Henrique Paone
  3. #
  4. # This file is part of Kitty-Visual.
  5. #
  6. # Kitty-Visual is free software: you can redistribute it and/or modify it under the
  7. # terms of the GNU General Public License as published by the Free Software
  8. # Foundation, either version 3 of the License, or (at your option) any later
  9. # version.
  10. #
  11. # Kitty-Visual is distributed in the hope that it will be useful, but WITHOUT ANY
  12. # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  13. # PARTICULAR PURPOSE. See the GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along with
  16. # Kitty-Visual. If not, see <https://www.gnu.org/licenses/>.
  17. require 'pty'
  18. require 'tempfile'
  19. require 'fileutils'
  20. require 'io/console'
  21. require 'msgpack/rpc'
  22. require 'msgpack/rpc/transport/unix'
  23. require_relative 'lib/nvim.rb'
  24. require_relative 'lib/screen.rb'
  25. include Tui
  26. include Kitty
  27. CTRL_V = "\u0016"
  28. Backspace = "\u007f"
  29. Visual_modes = ['v', 'V', CTRL_V]
  30. def isvisual?(mode)
  31. Visual_modes.include? mode
  32. end
  33. def clean_scrollback(scrollback)
  34. scrollback.map do |line|
  35. line.gsub(/(#{ Ansi_regex })|(#{ Kitty_builtin_regex })/, '')
  36. end
  37. end
  38. # Get scrollback
  39. scrollback = ARGF.read
  40. scrollback = scrollback.split "\n"
  41. # Kitty pipes the scrollback, so we need to reopen STDIN to read input
  42. STDIN.reopen '/dev/stdout'
  43. data = get_kitty_data
  44. scrolled = data[:scrolled]
  45. lines = data[:lines]
  46. cols = data[:cols]
  47. cline = data[:cline]
  48. ccol = data[:ccol]
  49. nvim = Nvim.new
  50. screen = Screen.new(
  51. nvim.get_bg_color('Visual'), nvim.get_bg_color('Search'),
  52. lines, cols, scrolled, cline, ccol, scrollback
  53. )
  54. nvim.scrollback = clean_scrollback(screen.contents_wrapped)
  55. nvim.cursor(screen.cursor)
  56. trap("WINCH") do
  57. height, width = IO.console.winsize
  58. screen.resized = true
  59. screen.height = height
  60. screen.width = width
  61. nvim.scrollback = clean_scrollback(screen.contents_wrapped)
  62. nvim.cursor(screen.cursor)
  63. screen.update
  64. end
  65. old_mode = 'n'
  66. cmdtype = ''
  67. # Main event loop
  68. loop do
  69. key = parse_escaped(get_escaped)
  70. next if key.nil?
  71. exit if key == '<Esc>'
  72. nvim.input key
  73. state = nvim.get_mode
  74. next if state['blocking']
  75. _, l, c, _ = nvim.getpos('.')
  76. # Command mode
  77. if state['mode'] == 'c'
  78. cmdline = nvim.getcmdline
  79. if old_mode != 'c' # First time entering
  80. old_mode = 'c'
  81. screen.move_cursor lines, 1
  82. cmdtype = nvim.getcmdtype
  83. print cmdtype
  84. end
  85. screen.move_cursor lines, 2
  86. screen.clear_current_line
  87. print "#{ cmdline }"
  88. screen.move_cursor lines, nvim.getcmdpos + 1
  89. next
  90. elsif old_mode == 'c'
  91. old_mode = state['mode']
  92. if ['/', '?'].include?(cmdtype)
  93. pattern = nvim.getreg('/')
  94. screen.match(nvim.getreg('/'), nvim.scrollback)
  95. end
  96. # Visual mode
  97. elsif isvisual? state['mode']
  98. old_mode = state ['mode'] unless isvisual? old_mode
  99. screen.selection = nvim.get_selection
  100. elsif isvisual? old_mode
  101. screen.selection = []
  102. old_mode = state['mode']
  103. end
  104. screen.cursor = l, c
  105. screen.update
  106. end