Skip to content

Commit 1796735

Browse files
authored
avoid cnorm on certain terminals (#10769)
using a terminfo's cnorm doesn't reset the cursor for many terminals, see issue: #10089
1 parent 730e684 commit 1796735

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

helix-tui/src/backend/crossterm.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,33 @@ use std::{
2323
fmt,
2424
io::{self, Write},
2525
};
26+
use termini::TermInfo;
2627

2728
fn term_program() -> Option<String> {
28-
std::env::var("TERM_PROGRAM").ok()
29+
// Some terminals don't set $TERM_PROGRAM
30+
match std::env::var("TERM_PROGRAM") {
31+
Err(_) => std::env::var("TERM").ok(),
32+
Ok(term_program) => Some(term_program),
33+
}
2934
}
3035
fn vte_version() -> Option<usize> {
3136
std::env::var("VTE_VERSION").ok()?.parse().ok()
3237
}
38+
fn reset_cursor_approach(terminfo: TermInfo) -> String {
39+
let mut reset_str = "\x1B[0 q".to_string();
40+
41+
if let Some(termini::Value::Utf8String(se_str)) = terminfo.extended_cap("Se") {
42+
reset_str.push_str(se_str);
43+
};
44+
45+
reset_str.push_str(
46+
terminfo
47+
.utf8_string_cap(termini::StringCapability::CursorNormal)
48+
.unwrap_or(""),
49+
);
50+
51+
reset_str
52+
}
3353

3454
/// Describes terminal capabilities like extended underline, truecolor, etc.
3555
#[derive(Clone, Debug)]
@@ -69,10 +89,7 @@ impl Capabilities {
6989
|| t.extended_cap("Su").is_some()
7090
|| vte_version() >= Some(5102)
7191
|| matches!(term_program().as_deref(), Some("WezTerm")),
72-
reset_cursor_command: t
73-
.utf8_string_cap(termini::StringCapability::CursorNormal)
74-
.unwrap_or("\x1B[0 q")
75-
.to_string(),
92+
reset_cursor_command: reset_cursor_approach(t),
7693
},
7794
}
7895
}

0 commit comments

Comments
 (0)