| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use crate::{backend::Backend, buffer::Buffer}; |
| 5 |
use helix_view::editor::{Config as EditorConfig, KittyKeyboardProtocolConfig}; |
| 6 |
use helix_view::graphics::{CursorKind, Rect}; |
| 7 |
use std::io; |
| 8 |
|
| 9 |
#[derive(Debug, Clone, PartialEq)] |
| 10 |
|
| 11 |
enum ResizeBehavior { |
| 12 |
Fixed, |
| 13 |
Auto, |
| 14 |
} |
| 15 |
|
| 16 |
#[derive(Debug, Clone, PartialEq)] |
| 17 |
|
| 18 |
pub struct Viewport { |
| 19 |
area: Rect, |
| 20 |
resize_behavior: ResizeBehavior, |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
#[derive(Debug)] |
| 25 |
pub struct Config { |
| 26 |
pub enable_mouse_capture: bool, |
| 27 |
pub force_enable_extended_underlines: bool, |
| 28 |
pub kitty_keyboard_protocol: KittyKeyboardProtocolConfig, |
| 29 |
} |
| 30 |
|
| 31 |
impl From<&EditorConfig> for Config { |
| 32 |
fn from(config: &EditorConfig) -> Self { |
| 33 |
Self { |
| 34 |
enable_mouse_capture: config.mouse, |
| 35 |
force_enable_extended_underlines: config.undercurl, |
| 36 |
kitty_keyboard_protocol: config.kitty_keyboard_protocol, |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
impl Viewport { |
| 42 |
|
| 43 |
pub fn fixed(area: Rect) -> Viewport { |
| 44 |
Viewport { |
| 45 |
area, |
| 46 |
resize_behavior: ResizeBehavior::Fixed, |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
#[derive(Debug, Clone, PartialEq)] |
| 52 |
|
| 53 |
pub struct TerminalOptions { |
| 54 |
|
| 55 |
pub viewport: Viewport, |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
#[derive(Debug)] |
| 60 |
pub struct Terminal<B> |
| 61 |
where |
| 62 |
B: Backend, |
| 63 |
{ |
| 64 |
backend: B, |
| 65 |
|
| 66 |
|
| 67 |
buffers: [Buffer; 2], |
| 68 |
|
| 69 |
current: usize, |
| 70 |
|
| 71 |
cursor_kind: CursorKind, |
| 72 |
|
| 73 |
viewport: Viewport, |
| 74 |
|
| 75 |
|
| 76 |
force_clear: bool, |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
pub const DEFAULT_TERMINAL_SIZE: Rect = Rect { |
| 81 |
x: 0, |
| 82 |
y: 0, |
| 83 |
width: 80, |
| 84 |
height: 24, |
| 85 |
}; |
| 86 |
|
| 87 |
impl<B> Terminal<B> |
| 88 |
where |
| 89 |
B: Backend, |
| 90 |
{ |
| 91 |
|
| 92 |
|
| 93 |
pub fn new(backend: B) -> io::Result<Terminal<B>> { |
| 94 |
let size = backend.size().unwrap_or(DEFAULT_TERMINAL_SIZE); |
| 95 |
Terminal::with_options( |
| 96 |
backend, |
| 97 |
TerminalOptions { |
| 98 |
viewport: Viewport { |
| 99 |
area: size, |
| 100 |
resize_behavior: ResizeBehavior::Auto, |
| 101 |
}, |
| 102 |
}, |
| 103 |
) |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
pub fn with_options(backend: B, options: TerminalOptions) -> io::Result<Terminal<B>> { |
| 108 |
Ok(Terminal { |
| 109 |
backend, |
| 110 |
buffers: [ |
| 111 |
Buffer::empty(options.viewport.area), |
| 112 |
Buffer::empty(options.viewport.area), |
| 113 |
], |
| 114 |
current: 0, |
| 115 |
cursor_kind: CursorKind::Block, |
| 116 |
viewport: options.viewport, |
| 117 |
force_clear: false, |
| 118 |
}) |
| 119 |
} |
| 120 |
|
| 121 |
pub fn claim(&mut self) -> io::Result<()> { |
| 122 |
self.backend.claim() |
| 123 |
} |
| 124 |
|
| 125 |
pub fn reconfigure(&mut self, config: Config) -> io::Result<()> { |
| 126 |
self.backend.reconfigure(config) |
| 127 |
} |
| 128 |
|
| 129 |
pub fn restore(&mut self) -> io::Result<()> { |
| 130 |
self.backend.restore() |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
pub fn current_buffer_mut(&mut self) -> &mut Buffer { |
| 142 |
&mut self.buffers[self.current] |
| 143 |
} |
| 144 |
|
| 145 |
pub fn backend(&self) -> &B { |
| 146 |
&self.backend |
| 147 |
} |
| 148 |
|
| 149 |
pub fn backend_mut(&mut self) -> &mut B { |
| 150 |
&mut self.backend |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
pub fn flush(&mut self) -> io::Result<()> { |
| 156 |
if self.force_clear { |
| 157 |
self.backend.clear()?; |
| 158 |
self.force_clear = false; |
| 159 |
} |
| 160 |
let previous_buffer = &self.buffers[1 - self.current]; |
| 161 |
let current_buffer = &self.buffers[self.current]; |
| 162 |
let updates = previous_buffer.diff(current_buffer); |
| 163 |
self.backend.draw(updates.into_iter()) |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
pub fn resize(&mut self, area: Rect) -> io::Result<()> { |
| 169 |
self.buffers[self.current].resize(area); |
| 170 |
self.buffers[1 - self.current].resize(area); |
| 171 |
self.viewport.area = area; |
| 172 |
self.clear() |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
pub fn autoresize(&mut self) -> io::Result<Rect> { |
| 177 |
let size = self.size(); |
| 178 |
if size != self.viewport.area { |
| 179 |
self.resize(size)?; |
| 180 |
}; |
| 181 |
Ok(size) |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
pub fn draw( |
| 187 |
&mut self, |
| 188 |
cursor_position: Option<(u16, u16)>, |
| 189 |
cursor_kind: CursorKind, |
| 190 |
) -> io::Result<()> { |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
self.backend.start_sync()?; |
| 204 |
|
| 205 |
|
| 206 |
self.flush()?; |
| 207 |
|
| 208 |
if let Some((x, y)) = cursor_position { |
| 209 |
self.set_cursor(x, y)?; |
| 210 |
} |
| 211 |
|
| 212 |
match cursor_kind { |
| 213 |
CursorKind::Hidden => self.hide_cursor()?, |
| 214 |
kind => self.show_cursor(kind)?, |
| 215 |
} |
| 216 |
|
| 217 |
self.backend.end_sync()?; |
| 218 |
|
| 219 |
|
| 220 |
self.buffers[1 - self.current].reset(); |
| 221 |
self.current = 1 - self.current; |
| 222 |
|
| 223 |
|
| 224 |
self.backend.flush()?; |
| 225 |
Ok(()) |
| 226 |
} |
| 227 |
|
| 228 |
#[inline] |
| 229 |
pub fn cursor_kind(&self) -> CursorKind { |
| 230 |
self.cursor_kind |
| 231 |
} |
| 232 |
|
| 233 |
pub fn hide_cursor(&mut self) -> io::Result<()> { |
| 234 |
self.backend.hide_cursor()?; |
| 235 |
self.cursor_kind = CursorKind::Hidden; |
| 236 |
Ok(()) |
| 237 |
} |
| 238 |
|
| 239 |
pub fn show_cursor(&mut self, kind: CursorKind) -> io::Result<()> { |
| 240 |
self.backend.show_cursor(kind)?; |
| 241 |
self.cursor_kind = kind; |
| 242 |
Ok(()) |
| 243 |
} |
| 244 |
|
| 245 |
pub fn set_cursor(&mut self, x: u16, y: u16) -> io::Result<()> { |
| 246 |
self.backend.set_cursor(x, y) |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
pub fn clear(&mut self) -> io::Result<()> { |
| 254 |
self.force_clear = true; |
| 255 |
|
| 256 |
self.buffers[1 - self.current].reset(); |
| 257 |
Ok(()) |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
pub fn size(&self) -> Rect { |
| 262 |
self.backend.size().unwrap_or(DEFAULT_TERMINAL_SIZE) |
| 263 |
} |
| 264 |
} |