| 1 |
use std::io::{self, Write as _}; |
| 2 |
|
| 3 |
use helix_view::{ |
| 4 |
editor::KittyKeyboardProtocolConfig, |
| 5 |
graphics::{CursorKind, Rect, UnderlineStyle}, |
| 6 |
theme::{self, Color, Modifier}, |
| 7 |
}; |
| 8 |
use termina::{ |
| 9 |
escape::{ |
| 10 |
csi::{self, Csi, SgrAttributes, SgrModifiers}, |
| 11 |
dcs::{self, Dcs}, |
| 12 |
osc::{self, Osc}, |
| 13 |
}, |
| 14 |
style::{CursorStyle, RgbColor}, |
| 15 |
Event, OneBased, PlatformTerminal, Terminal as _, WindowSize, |
| 16 |
}; |
| 17 |
|
| 18 |
use crate::{buffer::Cell, terminal::Config}; |
| 19 |
|
| 20 |
use super::Backend; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
macro_rules! decset { |
| 25 |
($mode:ident) => { |
| 26 |
Csi::Mode(csi::Mode::SetDecPrivateMode(csi::DecPrivateMode::Code( |
| 27 |
csi::DecPrivateModeCode::$mode, |
| 28 |
))) |
| 29 |
}; |
| 30 |
} |
| 31 |
macro_rules! decreset { |
| 32 |
($mode:ident) => { |
| 33 |
Csi::Mode(csi::Mode::ResetDecPrivateMode(csi::DecPrivateMode::Code( |
| 34 |
csi::DecPrivateModeCode::$mode, |
| 35 |
))) |
| 36 |
}; |
| 37 |
} |
| 38 |
|
| 39 |
fn term_program() -> Option<String> { |
| 40 |
|
| 41 |
match std::env::var("TERM_PROGRAM") { |
| 42 |
Err(_) => std::env::var("TERM").ok(), |
| 43 |
Ok(term_program) => Some(term_program), |
| 44 |
} |
| 45 |
} |
| 46 |
fn vte_version() -> Option<usize> { |
| 47 |
std::env::var("VTE_VERSION").ok()?.parse().ok() |
| 48 |
} |
| 49 |
|
| 50 |
#[derive(Debug, Default, Clone, Copy)] |
| 51 |
struct Capabilities { |
| 52 |
kitty_keyboard: KittyKeyboardSupport, |
| 53 |
synchronized_output: bool, |
| 54 |
true_color: bool, |
| 55 |
extended_underlines: bool, |
| 56 |
|
| 57 |
dynamic_background_color: bool, |
| 58 |
theme_mode: Option<theme::Mode>, |
| 59 |
} |
| 60 |
|
| 61 |
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] |
| 62 |
enum KittyKeyboardSupport { |
| 63 |
|
| 64 |
#[default] |
| 65 |
None, |
| 66 |
|
| 67 |
|
| 68 |
Some, |
| 69 |
|
| 70 |
Partial, |
| 71 |
|
| 72 |
Full, |
| 73 |
} |
| 74 |
|
| 75 |
#[derive(Debug)] |
| 76 |
pub struct TerminaBackend { |
| 77 |
terminal: PlatformTerminal, |
| 78 |
config: Config, |
| 79 |
capabilities: Capabilities, |
| 80 |
reset_cursor_command: String, |
| 81 |
is_synchronized_output_set: bool, |
| 82 |
|
| 83 |
background_color: Option<RgbColor>, |
| 84 |
|
| 85 |
|
| 86 |
original_background_color: Option<RgbColor>, |
| 87 |
} |
| 88 |
|
| 89 |
impl TerminaBackend { |
| 90 |
pub fn new(config: Config) -> io::Result<Self> { |
| 91 |
use std::time::{Duration, Instant}; |
| 92 |
|
| 93 |
let mut terminal = PlatformTerminal::new()?; |
| 94 |
|
| 95 |
|
| 96 |
const TEST_COLOR: RgbColor = RgbColor::new(59, 34, 76); |
| 97 |
|
| 98 |
terminal.enter_raw_mode()?; |
| 99 |
|
| 100 |
let mut capabilities = Capabilities::default(); |
| 101 |
let mut original_background_color = None; |
| 102 |
let start = Instant::now(); |
| 103 |
|
| 104 |
|
| 105 |
capabilities.dynamic_background_color = std::env::var_os("TMUX").is_none(); |
| 106 |
|
| 107 |
capabilities.kitty_keyboard = match config.kitty_keyboard_protocol { |
| 108 |
KittyKeyboardProtocolConfig::Disabled => KittyKeyboardSupport::None, |
| 109 |
KittyKeyboardProtocolConfig::Enabled => KittyKeyboardSupport::Full, |
| 110 |
KittyKeyboardProtocolConfig::Auto => { |
| 111 |
write!(terminal, "{}", Csi::Keyboard(csi::Keyboard::QueryFlags))?; |
| 112 |
KittyKeyboardSupport::None |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
write!( |
| 122 |
terminal, |
| 123 |
"{}{}{}{}{}{}{}{}", |
| 124 |
|
| 125 |
Csi::Mode(csi::Mode::QueryDecPrivateMode(csi::DecPrivateMode::Code( |
| 126 |
csi::DecPrivateModeCode::SynchronizedOutput |
| 127 |
))), |
| 128 |
|
| 129 |
Csi::Mode(csi::Mode::QueryTheme), |
| 130 |
|
| 131 |
|
| 132 |
Csi::Sgr(csi::Sgr::Background(TEST_COLOR.into())), |
| 133 |
Csi::Sgr(csi::Sgr::UnderlineColor(TEST_COLOR.into())), |
| 134 |
Dcs::Request(dcs::DcsRequest::GraphicRendition), |
| 135 |
Csi::Sgr(csi::Sgr::Reset), |
| 136 |
Osc::ChangeDynamicColors( |
| 137 |
osc::DynamicColorNumber::TextBackgroundColor, |
| 138 |
vec![osc::ColorOrQuery::Query] |
| 139 |
), |
| 140 |
|
| 141 |
Csi::Device(csi::Device::RequestPrimaryDeviceAttributes), |
| 142 |
)?; |
| 143 |
terminal.flush()?; |
| 144 |
|
| 145 |
let device_attributes = |event: &Event| { |
| 146 |
matches!( |
| 147 |
event, |
| 148 |
Event::Csi(Csi::Device(csi::Device::DeviceAttributes(_))) |
| 149 |
) |
| 150 |
}; |
| 151 |
|
| 152 |
let poll_duration = Duration::from_millis(100); |
| 153 |
if terminal.poll(device_attributes, Some(poll_duration))? { |
| 154 |
while terminal.poll(Event::is_escape, Some(Duration::ZERO))? { |
| 155 |
match terminal.read(Event::is_escape)? { |
| 156 |
Event::Csi(Csi::Keyboard(csi::Keyboard::ReportFlags(_))) => { |
| 157 |
capabilities.kitty_keyboard = KittyKeyboardSupport::Some; |
| 158 |
} |
| 159 |
Event::Csi(Csi::Mode(csi::Mode::ReportDecPrivateMode { |
| 160 |
mode: csi::DecPrivateMode::Code(csi::DecPrivateModeCode::SynchronizedOutput), |
| 161 |
setting: csi::DecModeSetting::Set | csi::DecModeSetting::Reset, |
| 162 |
})) => { |
| 163 |
capabilities.synchronized_output = true; |
| 164 |
} |
| 165 |
Event::Csi(Csi::Mode(csi::Mode::ReportTheme(mode))) => { |
| 166 |
capabilities.theme_mode = Some(mode.into()); |
| 167 |
} |
| 168 |
Event::Osc(Osc::ChangeDynamicColors( |
| 169 |
osc::DynamicColorNumber::TextBackgroundColor, |
| 170 |
colors, |
| 171 |
)) => { |
| 172 |
if let Some(osc::ColorOrQuery::Color(color)) = colors.first() { |
| 173 |
original_background_color = Some(*color); |
| 174 |
} |
| 175 |
} |
| 176 |
Event::Dcs(dcs::Dcs::Response { |
| 177 |
value: dcs::DcsResponse::GraphicRendition(sgrs), |
| 178 |
.. |
| 179 |
}) => { |
| 180 |
capabilities.true_color = |
| 181 |
sgrs.contains(&csi::Sgr::Background(TEST_COLOR.into())); |
| 182 |
capabilities.extended_underlines = |
| 183 |
sgrs.contains(&csi::Sgr::UnderlineColor(TEST_COLOR.into())); |
| 184 |
} |
| 185 |
_ => (), |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
let end = Instant::now(); |
| 190 |
log::debug!( |
| 191 |
"Detected terminal capabilities in {:?}: {capabilities:?}", |
| 192 |
end.duration_since(start) |
| 193 |
); |
| 194 |
} else { |
| 195 |
log::debug!("Failed to detect terminal capabilities within {poll_duration:?}. Using default capabilities only"); |
| 196 |
} |
| 197 |
|
| 198 |
capabilities.extended_underlines |= config.force_enable_extended_underlines; |
| 199 |
|
| 200 |
let mut reset_cursor_command = String::new(); |
| 201 |
if let Ok(t) = termini::TermInfo::from_env() { |
| 202 |
capabilities.extended_underlines |= t.extended_cap("Smulx").is_some() |
| 203 |
|| t.extended_cap("Su").is_some() |
| 204 |
|| vte_version() >= Some(5102) |
| 205 |
|
| 206 |
|
| 207 |
|| matches!(term_program().as_deref(), Some("WezTerm")); |
| 208 |
|
| 209 |
if let Some(termini::Value::Utf8String(se_str)) = t.extended_cap("Se") { |
| 210 |
reset_cursor_command.push_str(se_str); |
| 211 |
}; |
| 212 |
reset_cursor_command.push_str( |
| 213 |
t.utf8_string_cap(termini::StringCapability::CursorNormal) |
| 214 |
.unwrap_or(""), |
| 215 |
); |
| 216 |
log::debug!( |
| 217 |
"Cursor reset escape sequence detected from terminfo: {reset_cursor_command:?}" |
| 218 |
); |
| 219 |
} else { |
| 220 |
log::debug!("terminfo could not be read, using default cursor reset escape sequence: {reset_cursor_command:?}"); |
| 221 |
} |
| 222 |
reset_cursor_command |
| 223 |
.push_str(&Csi::Cursor(csi::Cursor::CursorStyle(CursorStyle::Default)).to_string()); |
| 224 |
|
| 225 |
terminal.enter_cooked_mode()?; |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
let hook_reset_cursor_command = reset_cursor_command.clone(); |
| 231 |
terminal.set_panic_hook(move |term| { |
| 232 |
let _ = write!( |
| 233 |
term, |
| 234 |
"{}{}{}{}{}{}{}{}{}{}{}{}", |
| 235 |
Csi::Keyboard(csi::Keyboard::PopFlags(1)), |
| 236 |
decreset!(MouseTracking), |
| 237 |
decreset!(ButtonEventMouse), |
| 238 |
decreset!(AnyEventMouse), |
| 239 |
decreset!(RXVTMouse), |
| 240 |
decreset!(SGRMouse), |
| 241 |
&hook_reset_cursor_command, |
| 242 |
decreset!(BracketedPaste), |
| 243 |
decreset!(FocusTracking), |
| 244 |
Osc::ResetDynamicColor(osc::DynamicColorNumber::TextBackgroundColor), |
| 245 |
Csi::Edit(csi::Edit::EraseInDisplay(csi::EraseInDisplay::EraseDisplay)), |
| 246 |
decreset!(ClearAndEnableAlternateScreen), |
| 247 |
); |
| 248 |
}); |
| 249 |
|
| 250 |
Ok(Self { |
| 251 |
terminal, |
| 252 |
config, |
| 253 |
capabilities, |
| 254 |
reset_cursor_command, |
| 255 |
is_synchronized_output_set: false, |
| 256 |
background_color: None, |
| 257 |
original_background_color, |
| 258 |
}) |
| 259 |
} |
| 260 |
|
| 261 |
pub fn terminal(&self) -> &PlatformTerminal { |
| 262 |
&self.terminal |
| 263 |
} |
| 264 |
|
| 265 |
fn enable_mouse_capture(&mut self) -> io::Result<()> { |
| 266 |
if self.config.enable_mouse_capture { |
| 267 |
write!( |
| 268 |
self.terminal, |
| 269 |
"{}{}{}{}{}", |
| 270 |
decset!(MouseTracking), |
| 271 |
decset!(ButtonEventMouse), |
| 272 |
decset!(AnyEventMouse), |
| 273 |
decset!(RXVTMouse), |
| 274 |
decset!(SGRMouse), |
| 275 |
)?; |
| 276 |
} |
| 277 |
Ok(()) |
| 278 |
} |
| 279 |
|
| 280 |
fn disable_mouse_capture(&mut self) -> io::Result<()> { |
| 281 |
if self.config.enable_mouse_capture { |
| 282 |
write!( |
| 283 |
self.terminal, |
| 284 |
"{}{}{}{}{}", |
| 285 |
decreset!(MouseTracking), |
| 286 |
decreset!(ButtonEventMouse), |
| 287 |
decreset!(AnyEventMouse), |
| 288 |
decreset!(RXVTMouse), |
| 289 |
decreset!(SGRMouse), |
| 290 |
)?; |
| 291 |
} |
| 292 |
Ok(()) |
| 293 |
} |
| 294 |
|
| 295 |
fn reset_background_color(&mut self) -> io::Result<()> { |
| 296 |
write!( |
| 297 |
self.terminal, |
| 298 |
"{}", |
| 299 |
match self.original_background_color { |
| 300 |
Some(color) => Osc::ChangeDynamicColors( |
| 301 |
osc::DynamicColorNumber::TextBackgroundColor, |
| 302 |
vec![color.into()] |
| 303 |
), |
| 304 |
None => Osc::ResetDynamicColor(osc::DynamicColorNumber::TextBackgroundColor), |
| 305 |
} |
| 306 |
) |
| 307 |
} |
| 308 |
|
| 309 |
fn enable_extensions(&mut self) -> io::Result<()> { |
| 310 |
const KEYBOARD_FLAGS: csi::KittyKeyboardFlags = |
| 311 |
csi::KittyKeyboardFlags::DISAMBIGUATE_ESCAPE_CODES |
| 312 |
.union(csi::KittyKeyboardFlags::REPORT_ALTERNATE_KEYS); |
| 313 |
|
| 314 |
match self.capabilities.kitty_keyboard { |
| 315 |
KittyKeyboardSupport::None | KittyKeyboardSupport::Partial => (), |
| 316 |
KittyKeyboardSupport::Full => { |
| 317 |
write!( |
| 318 |
self.terminal, |
| 319 |
"{}", |
| 320 |
Csi::Keyboard(csi::Keyboard::PushFlags(KEYBOARD_FLAGS)) |
| 321 |
)?; |
| 322 |
} |
| 323 |
KittyKeyboardSupport::Some => { |
| 324 |
write!( |
| 325 |
self.terminal, |
| 326 |
"{}{}", |
| 327 |
|
| 328 |
Csi::Keyboard(csi::Keyboard::PushFlags(KEYBOARD_FLAGS)), |
| 329 |
|
| 330 |
|
| 331 |
Csi::Keyboard(csi::Keyboard::QueryFlags), |
| 332 |
)?; |
| 333 |
self.terminal.flush()?; |
| 334 |
|
| 335 |
let event = self.terminal.read(|event| { |
| 336 |
matches!( |
| 337 |
event, |
| 338 |
Event::Csi(Csi::Keyboard(csi::Keyboard::ReportFlags(_))) |
| 339 |
) |
| 340 |
})?; |
| 341 |
let Event::Csi(Csi::Keyboard(csi::Keyboard::ReportFlags(flags))) = event else { |
| 342 |
unreachable!(); |
| 343 |
}; |
| 344 |
if flags != KEYBOARD_FLAGS { |
| 345 |
log::info!("Turning off enhanced keyboard support because the terminal enabled different flags. Requested {KEYBOARD_FLAGS:?} but got {flags:?}"); |
| 346 |
write!( |
| 347 |
self.terminal, |
| 348 |
"{}", |
| 349 |
Csi::Keyboard(csi::Keyboard::PopFlags(1)) |
| 350 |
)?; |
| 351 |
self.terminal.flush()?; |
| 352 |
self.capabilities.kitty_keyboard = KittyKeyboardSupport::Partial; |
| 353 |
} else { |
| 354 |
log::debug!( |
| 355 |
"The terminal fully supports the requested keyboard enhancement flags" |
| 356 |
); |
| 357 |
self.capabilities.kitty_keyboard = KittyKeyboardSupport::Full; |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
if self.capabilities.theme_mode.is_some() { |
| 363 |
|
| 364 |
write!(self.terminal, "{}", decset!(Theme))?; |
| 365 |
} |
| 366 |
|
| 367 |
Ok(()) |
| 368 |
} |
| 369 |
|
| 370 |
fn disable_extensions(&mut self) -> io::Result<()> { |
| 371 |
if self.capabilities.kitty_keyboard == KittyKeyboardSupport::Full { |
| 372 |
write!( |
| 373 |
self.terminal, |
| 374 |
"{}", |
| 375 |
Csi::Keyboard(csi::Keyboard::PopFlags(1)) |
| 376 |
)?; |
| 377 |
} |
| 378 |
|
| 379 |
if self.capabilities.theme_mode.is_some() { |
| 380 |
|
| 381 |
write!(self.terminal, "{}", decreset!(Theme))?; |
| 382 |
} |
| 383 |
|
| 384 |
Ok(()) |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
fn start_synchronized_render(&mut self) -> io::Result<()> { |
| 394 |
if self.capabilities.synchronized_output && !self.is_synchronized_output_set { |
| 395 |
write!(self.terminal, "{}", decset!(SynchronizedOutput))?; |
| 396 |
self.is_synchronized_output_set = true; |
| 397 |
} |
| 398 |
Ok(()) |
| 399 |
} |
| 400 |
|
| 401 |
fn end_sychronized_render(&mut self) -> io::Result<()> { |
| 402 |
if self.is_synchronized_output_set { |
| 403 |
write!(self.terminal, "{}", decreset!(SynchronizedOutput))?; |
| 404 |
self.is_synchronized_output_set = false; |
| 405 |
} |
| 406 |
Ok(()) |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
impl Backend for TerminaBackend { |
| 411 |
fn claim(&mut self) -> io::Result<()> { |
| 412 |
self.terminal.enter_raw_mode()?; |
| 413 |
|
| 414 |
write!( |
| 415 |
self.terminal, |
| 416 |
"{}{}{}{}", |
| 417 |
|
| 418 |
decset!(ClearAndEnableAlternateScreen), |
| 419 |
decset!(BracketedPaste), |
| 420 |
decset!(FocusTracking), |
| 421 |
|
| 422 |
|
| 423 |
Csi::Edit(csi::Edit::EraseInDisplay(csi::EraseInDisplay::EraseDisplay)), |
| 424 |
)?; |
| 425 |
if let Some(color) = self.background_color { |
| 426 |
write!( |
| 427 |
self.terminal, |
| 428 |
"{}", |
| 429 |
Osc::ChangeDynamicColors( |
| 430 |
osc::DynamicColorNumber::TextBackgroundColor, |
| 431 |
vec![color.into()] |
| 432 |
) |
| 433 |
)?; |
| 434 |
} |
| 435 |
self.enable_mouse_capture()?; |
| 436 |
self.enable_extensions()?; |
| 437 |
|
| 438 |
Ok(()) |
| 439 |
} |
| 440 |
|
| 441 |
fn reconfigure(&mut self, mut config: Config) -> io::Result<()> { |
| 442 |
std::mem::swap(&mut self.config, &mut config); |
| 443 |
if self.config.enable_mouse_capture != config.enable_mouse_capture { |
| 444 |
if self.config.enable_mouse_capture { |
| 445 |
self.enable_mouse_capture()?; |
| 446 |
} else { |
| 447 |
self.disable_mouse_capture()?; |
| 448 |
} |
| 449 |
} |
| 450 |
self.capabilities.extended_underlines |= self.config.force_enable_extended_underlines; |
| 451 |
Ok(()) |
| 452 |
} |
| 453 |
|
| 454 |
fn restore(&mut self) -> io::Result<()> { |
| 455 |
self.disable_extensions()?; |
| 456 |
self.disable_mouse_capture()?; |
| 457 |
write!( |
| 458 |
self.terminal, |
| 459 |
"{}{}{}{}", |
| 460 |
&self.reset_cursor_command, |
| 461 |
decreset!(BracketedPaste), |
| 462 |
decreset!(FocusTracking), |
| 463 |
decreset!(ClearAndEnableAlternateScreen), |
| 464 |
)?; |
| 465 |
if self.background_color.is_some() { |
| 466 |
self.reset_background_color()?; |
| 467 |
} |
| 468 |
self.terminal.flush()?; |
| 469 |
self.terminal.enter_cooked_mode()?; |
| 470 |
Ok(()) |
| 471 |
} |
| 472 |
|
| 473 |
fn draw<'a, I>(&mut self, content: I) -> io::Result<()> |
| 474 |
where |
| 475 |
I: Iterator<Item = (u16, u16, &'a Cell)>, |
| 476 |
{ |
| 477 |
let mut fg = Color::Reset; |
| 478 |
let mut bg = Color::Reset; |
| 479 |
let mut underline_color = Color::Reset; |
| 480 |
let mut underline_style = UnderlineStyle::Reset; |
| 481 |
let mut modifier = Modifier::empty(); |
| 482 |
let mut last_pos: Option<(u16, u16)> = None; |
| 483 |
for (x, y, cell) in content { |
| 484 |
|
| 485 |
if !matches!(last_pos, Some(p) if x == p.0 + 1 && y == p.1) { |
| 486 |
write!( |
| 487 |
self.terminal, |
| 488 |
"{}", |
| 489 |
Csi::Cursor(csi::Cursor::Position { |
| 490 |
col: OneBased::from_zero_based(x), |
| 491 |
line: OneBased::from_zero_based(y), |
| 492 |
}) |
| 493 |
)?; |
| 494 |
} |
| 495 |
last_pos = Some((x, y)); |
| 496 |
|
| 497 |
let mut attributes = SgrAttributes::default(); |
| 498 |
if cell.fg != fg { |
| 499 |
attributes.foreground = Some(cell.fg.into()); |
| 500 |
fg = cell.fg; |
| 501 |
} |
| 502 |
if cell.bg != bg { |
| 503 |
attributes.background = Some(cell.bg.into()); |
| 504 |
bg = cell.bg; |
| 505 |
} |
| 506 |
if cell.modifier != modifier { |
| 507 |
attributes.modifiers = diff_modifiers(modifier, cell.modifier); |
| 508 |
modifier = cell.modifier; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
let mut new_underline_style = cell.underline_style; |
| 514 |
if self.capabilities.extended_underlines { |
| 515 |
if cell.underline_color != underline_color { |
| 516 |
write!( |
| 517 |
self.terminal, |
| 518 |
"{}", |
| 519 |
Csi::Sgr(csi::Sgr::UnderlineColor(cell.underline_color.into())) |
| 520 |
)?; |
| 521 |
underline_color = cell.underline_color; |
| 522 |
} |
| 523 |
} else { |
| 524 |
match new_underline_style { |
| 525 |
UnderlineStyle::Reset | UnderlineStyle::Line => (), |
| 526 |
_ => new_underline_style = UnderlineStyle::Line, |
| 527 |
} |
| 528 |
} |
| 529 |
if new_underline_style != underline_style { |
| 530 |
write!( |
| 531 |
self.terminal, |
| 532 |
"{}", |
| 533 |
Csi::Sgr(csi::Sgr::Underline(new_underline_style.into())) |
| 534 |
)?; |
| 535 |
underline_style = new_underline_style; |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
if !attributes.is_empty() { |
| 542 |
write!( |
| 543 |
self.terminal, |
| 544 |
"{}", |
| 545 |
Csi::Sgr(csi::Sgr::Attributes(attributes)) |
| 546 |
)?; |
| 547 |
} |
| 548 |
|
| 549 |
write!(self.terminal, "{}", &cell.symbol)?; |
| 550 |
} |
| 551 |
|
| 552 |
write!(self.terminal, "{}", Csi::Sgr(csi::Sgr::Reset))?; |
| 553 |
|
| 554 |
Ok(()) |
| 555 |
} |
| 556 |
|
| 557 |
fn hide_cursor(&mut self) -> io::Result<()> { |
| 558 |
write!(self.terminal, "{}", decreset!(ShowCursor)) |
| 559 |
} |
| 560 |
|
| 561 |
fn show_cursor(&mut self, kind: CursorKind) -> io::Result<()> { |
| 562 |
let style = match kind { |
| 563 |
CursorKind::Block => CursorStyle::SteadyBlock, |
| 564 |
CursorKind::Bar => CursorStyle::SteadyBar, |
| 565 |
CursorKind::Underline => CursorStyle::SteadyUnderline, |
| 566 |
CursorKind::Hidden => unreachable!(), |
| 567 |
}; |
| 568 |
write!( |
| 569 |
self.terminal, |
| 570 |
"{}{}", |
| 571 |
decset!(ShowCursor), |
| 572 |
Csi::Cursor(csi::Cursor::CursorStyle(style)), |
| 573 |
) |
| 574 |
} |
| 575 |
|
| 576 |
fn set_cursor(&mut self, x: u16, y: u16) -> io::Result<()> { |
| 577 |
let col = OneBased::from_zero_based(x); |
| 578 |
let line = OneBased::from_zero_based(y); |
| 579 |
write!( |
| 580 |
self.terminal, |
| 581 |
"{}", |
| 582 |
Csi::Cursor(csi::Cursor::Position { line, col }) |
| 583 |
) |
| 584 |
} |
| 585 |
|
| 586 |
fn clear(&mut self) -> io::Result<()> { |
| 587 |
write!( |
| 588 |
self.terminal, |
| 589 |
"{}", |
| 590 |
Csi::Edit(csi::Edit::EraseInDisplay(csi::EraseInDisplay::EraseDisplay)) |
| 591 |
) |
| 592 |
} |
| 593 |
|
| 594 |
fn start_sync(&mut self) -> io::Result<()> { |
| 595 |
self.start_synchronized_render() |
| 596 |
} |
| 597 |
|
| 598 |
fn end_sync(&mut self) -> io::Result<()> { |
| 599 |
self.end_sychronized_render() |
| 600 |
} |
| 601 |
|
| 602 |
fn size(&self) -> io::Result<Rect> { |
| 603 |
let WindowSize { rows, cols, .. } = self.terminal.get_dimensions()?; |
| 604 |
Ok(Rect::new(0, 0, cols, rows)) |
| 605 |
} |
| 606 |
|
| 607 |
fn flush(&mut self) -> io::Result<()> { |
| 608 |
self.terminal.flush() |
| 609 |
} |
| 610 |
|
| 611 |
fn supports_true_color(&self) -> bool { |
| 612 |
self.capabilities.true_color |
| 613 |
} |
| 614 |
|
| 615 |
fn get_theme_mode(&self) -> Option<theme::Mode> { |
| 616 |
self.capabilities.theme_mode |
| 617 |
} |
| 618 |
|
| 619 |
fn set_background_color(&mut self, color: Option<Color>) -> io::Result<()> { |
| 620 |
if !self.capabilities.dynamic_background_color { |
| 621 |
return Ok(()); |
| 622 |
} |
| 623 |
self.background_color = match color { |
| 624 |
Some(Color::Rgb(r, g, b)) => Some(RgbColor::new(r, g, b)), |
| 625 |
_ => None, |
| 626 |
}; |
| 627 |
if let Some(color) = self.background_color { |
| 628 |
write!( |
| 629 |
self.terminal, |
| 630 |
"{}", |
| 631 |
Osc::ChangeDynamicColors( |
| 632 |
osc::DynamicColorNumber::TextBackgroundColor, |
| 633 |
vec![color.into()] |
| 634 |
) |
| 635 |
) |
| 636 |
} else { |
| 637 |
self.reset_background_color() |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
impl Drop for TerminaBackend { |
| 643 |
fn drop(&mut self) { |
| 644 |
|
| 645 |
|
| 646 |
if !std::thread::panicking() { |
| 647 |
let _ = self.disable_extensions(); |
| 648 |
let _ = self.disable_mouse_capture(); |
| 649 |
let _ = write!( |
| 650 |
self.terminal, |
| 651 |
"{}{}{}{}", |
| 652 |
&self.reset_cursor_command, |
| 653 |
decreset!(BracketedPaste), |
| 654 |
decreset!(FocusTracking), |
| 655 |
decreset!(ClearAndEnableAlternateScreen), |
| 656 |
); |
| 657 |
if self.background_color.is_some() { |
| 658 |
let _ = self.reset_background_color(); |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
} |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
fn diff_modifiers(from: Modifier, to: Modifier) -> SgrModifiers { |
| 667 |
let mut modifiers = SgrModifiers::default(); |
| 668 |
|
| 669 |
let removed = from - to; |
| 670 |
if removed.contains(Modifier::REVERSED) { |
| 671 |
modifiers |= SgrModifiers::NO_REVERSE; |
| 672 |
} |
| 673 |
if removed.contains(Modifier::BOLD) { |
| 674 |
modifiers |= SgrModifiers::INTENSITY_NORMAL; |
| 675 |
if to.contains(Modifier::DIM) { |
| 676 |
modifiers |= SgrModifiers::INTENSITY_DIM |
| 677 |
} |
| 678 |
} |
| 679 |
if removed.contains(Modifier::DIM) { |
| 680 |
modifiers |= SgrModifiers::INTENSITY_NORMAL; |
| 681 |
} |
| 682 |
if removed.contains(Modifier::ITALIC) { |
| 683 |
modifiers |= SgrModifiers::NO_ITALIC; |
| 684 |
} |
| 685 |
if removed.contains(Modifier::CROSSED_OUT) { |
| 686 |
modifiers |= SgrModifiers::NO_STRIKE_THROUGH; |
| 687 |
} |
| 688 |
if removed.contains(Modifier::HIDDEN) { |
| 689 |
modifiers |= SgrModifiers::NO_INVISIBLE; |
| 690 |
} |
| 691 |
if removed.contains(Modifier::SLOW_BLINK) || removed.contains(Modifier::RAPID_BLINK) { |
| 692 |
modifiers |= SgrModifiers::BLINK_NONE; |
| 693 |
} |
| 694 |
|
| 695 |
let added = to - from; |
| 696 |
if added.contains(Modifier::REVERSED) { |
| 697 |
modifiers |= SgrModifiers::REVERSE; |
| 698 |
} |
| 699 |
if added.contains(Modifier::BOLD) { |
| 700 |
modifiers |= SgrModifiers::INTENSITY_BOLD; |
| 701 |
} |
| 702 |
if added.contains(Modifier::DIM) { |
| 703 |
modifiers |= SgrModifiers::INTENSITY_DIM; |
| 704 |
} |
| 705 |
if added.contains(Modifier::ITALIC) { |
| 706 |
modifiers |= SgrModifiers::ITALIC; |
| 707 |
} |
| 708 |
if added.contains(Modifier::CROSSED_OUT) { |
| 709 |
modifiers |= SgrModifiers::STRIKE_THROUGH; |
| 710 |
} |
| 711 |
if added.contains(Modifier::HIDDEN) { |
| 712 |
modifiers |= SgrModifiers::INVISIBLE; |
| 713 |
} |
| 714 |
if added.contains(Modifier::SLOW_BLINK) { |
| 715 |
modifiers |= SgrModifiers::BLINK_SLOW; |
| 716 |
} |
| 717 |
if added.contains(Modifier::RAPID_BLINK) { |
| 718 |
modifiers |= SgrModifiers::BLINK_RAPID; |
| 719 |
} |
| 720 |
|
| 721 |
modifiers |
| 722 |
} |