Repository: helix-editor/helix
File: helix-tui/src/backend/crossterm.rs (Rust)

Code before:
    match std::env::var("TERM_PROGRAM") {
        Err(_) => std::env::var("TERM").ok(),
        Ok(term_program) => Some(term_program),
    }
}
fn vte_version() -> Option<usize> {
    std::env::var("VTE_VERSION").ok()?.parse().ok()
}
fn reset_cursor_approach(terminfo: TermInfo, fallback: &str) -> String {
    let mut reset_str: String = Default::default();
    if let termini::Value::Utf8String(reset_str_se) = terminfo
        .extended_cap("Se")
        .unwrap_or(termini::Value::Utf8String(fallback))
    {
        reset_str.push_str(reset_str_se);
    }

    reset_str.push_str(
        terminfo
            .utf8_string_cap(termini::StringCapability::CursorNormal)
            .unwrap(),
    );

    match term_program().as_deref() {
        // some terminals don't reset the cursor with either cnorm or Se
        Some("tmux") => {
            reset_str.push_str(fallback);
            reset_str
        }
        _ => reset_str,
    }
}

/// Describes terminal capabilities like extended underline, truecolor, etc.
#[derive(Clone, Debug)]
struct Capabilities {
    /// Support for undercurled, underdashed, etc.
    has_extended_underlines: bool,
    /// Support for resetting the cursor style back to normal.
    reset_cursor_command: String,
}

impl Default for Capabilities {
    fn default() -> Self {
        Self {
            has_extended_underlines: false,
            reset_cursor_command: "\x1B[0 q".to_string(),
        }
    }
}


Review comment (security):
So tmux sets both cnorm and SE and both don't contain that reset sequence? 

I guess it's because it passes that escape sequence trough but doesn't know about it itself?

I would really like to avoid special cases like this. I think what would work is if you always prepend the fallback sequence. So basically `format!("{fallback}{Se}{cnorm}")` (with this approach Se and cnorm won't need a fallback). 

It's a bit of a hack but it should work. Emitting the escape sequence multiple times is fine. 

Code after:
    match std::env::var("TERM_PROGRAM") {
        Err(_) => std::env::var("TERM").ok(),
        Ok(term_program) => Some(term_program),
    }
}
fn vte_version() -> Option<usize> {
    std::env::var("VTE_VERSION").ok()?.parse().ok()
}
fn reset_cursor_approach(terminfo: TermInfo) -> String {
    let mut reset_str = "\x1B[0 q".to_string();

    if let Some(termini::Value::Utf8String(se_str)) = terminfo.extended_cap("Se") {
        reset_str.push_str(se_str);
    };

    reset_str.push_str(
        terminfo
            .utf8_string_cap(termini::StringCapability::CursorNormal)
            .unwrap_or(""),
    );

    reset_str
}

/// Describes terminal capabilities like extended underline, truecolor, etc.
#[derive(Clone, Debug)]
struct Capabilities {
    /// Support for undercurled, underdashed, etc.
    has_extended_underlines: bool,
    /// Support for resetting the cursor style back to normal.
    reset_cursor_command: String,
}

impl Default for Capabilities {
    fn default() -> Self {
        Self {
            has_extended_underlines: false,
            reset_cursor_command: "\x1B[0 q".to_string(),
        }
    }
}

impl Capabilities {
    /// Detect capabilities from the terminfo database located based
    /// on the $TERM environment variable. If detection fails, returns
    /// a default value where no capability is supported, or just undercurl
    /// if config.undercurl is set.
    pub fn from_env_or_default(config: &EditorConfig) -> Self {
        match termini::TermInfo::from_env() {
            Err(_) => Capabilities {
                has_extended_underlines: config.undercurl,