Skip to content

Commit

Permalink
Merge pull request #300 from mattrobenolt/ghostty
Browse files Browse the repository at this point in the history
Add support for ghostty terminal
  • Loading branch information
swsnr authored Nov 12, 2024
2 parents 051bbbf + 0d950a3 commit 01a75db
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Use `cargo release` to create a new release.

## [Unreleased]

### Added
- Added support for images in Ghostty terminal (See [GH-300]).

### Removed
- Remove a few dependencies: `mime_guess` and `system_proxy` (see [GH-297]).
- mdcat no longer supports building against the `onig` regex library (see [GH-297]).
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Then it
| [kitty] ||| ✓³ | |
| [WezTerm] ||| ✓³ | |
| [VSCode] ||| ✓³ | |
| [Ghostty] ||| ✓³ | |

1) mdcat requires that the terminal supports strikethrough formatting and [inline links][osc8].
It will not render strikethrough text and links correctly on terminals that don't support these (e.g. the Linux text console)
Expand All @@ -57,6 +58,7 @@ Not supported:
[resvg]: https://github.com/RazrFalcon/resvg
[SVG support]: https://github.com/RazrFalcon/resvg#svg-support
[VSCode]: https://code.visualstudio.com/
[Ghostty]: https://mitchellh.com/ghostty

## Usage

Expand Down
2 changes: 2 additions & 0 deletions mdcat.1.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ It understands the following values.
+
* `wezterm`: WezTerm. Note that WezTerm sets `$TERM` to `xterm-256color` by default, and only uses `wezterm` for `$TERM` if explicitly configured to do so.
* `xterm-kitty`: kitty
* `xterm-ghostty`: Ghostty
+
For all other values `mdcat` proceeds to check `$TERM_PROGRAM`.

Expand All @@ -155,6 +156,7 @@ TERM_PROGRAM::
* `iTerm.app`: iTerm2
* `WezTerm`: WezTerm
* `vscode`: VSCode integrated terminal, but only if `$TERM_PROGRAM_VERSION` indicates a sufficient version to support all required features..
* `ghostty`: Ghostty
+
For all other values `mdcat` proceeds to check `$TERMINOLOGY`.

Expand Down
29 changes: 29 additions & 0 deletions pulldown-cmark-mdcat/src/terminal/detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pub enum TerminalProgram {
///
/// Since version 1.80 it supports images with the iTerm2 protocol.
VSCode,
/// Ghostty.
///
/// See <https://mitchellh.com/ghostty> for more information.
Ghostty,
}

impl Display for TerminalProgram {
Expand All @@ -58,6 +62,7 @@ impl Display for TerminalProgram {
TerminalProgram::Kitty => "kitty",
TerminalProgram::WezTerm => "WezTerm",
TerminalProgram::VSCode => "vscode",
TerminalProgram::Ghostty => "ghostty",
};
write!(f, "{name}")
}
Expand All @@ -80,6 +85,7 @@ impl TerminalProgram {
match std::env::var("TERM").ok().as_deref() {
Some("wezterm") => Some(Self::WezTerm),
Some("xterm-kitty") => Some(Self::Kitty),
Some("xterm-ghostty") => Some(Self::Ghostty),
_ => None,
}
}
Expand All @@ -88,6 +94,7 @@ impl TerminalProgram {
match std::env::var("TERM_PROGRAM").ok().as_deref() {
Some("WezTerm") => Some(Self::WezTerm),
Some("iTerm.app") => Some(Self::ITerm2),
Some("ghostty") => Some(Self::Ghostty),
Some("vscode")
if get_term_program_major_minor_version()
.map_or(false, |version| (1, 80) <= version) =>
Expand Down Expand Up @@ -121,6 +128,8 @@ impl TerminalProgram {
/// - [`TerminalProgram::WezTerm`] if `$TERM` is `wezterm`.
/// - [`TerminalProgram::WezTerm`] if `$TERM_PROGRAM` is `WezTerm`.
/// - [`TerminalProgram::ITerm2`] if `$TERM_PROGRAM` is `iTerm.app`.
/// - [`TerminalProgram::Ghostty`] if `$TERM` is `xterm-ghostty`.
/// - [`TerminalProgram::Ghostty`] if `$TERM_PROGRAM` is `ghostty`.
/// - [`TerminalProgram::Terminology`] if `$TERMINOLOGY` is `1`.
/// - [`TerminalProgram::Ansi`] otherwise.
pub fn detect() -> Self {
Expand Down Expand Up @@ -156,6 +165,8 @@ impl TerminalProgram {
TerminalProgram::VSCode => {
ansi.with_image_capability(ImageCapability::ITerm2(ITerm2Protocol))
}
TerminalProgram::Ghostty => ansi
.with_image_capability(ImageCapability::Kitty(self::kitty::KittyGraphicsProtocol)),
}
}
}
Expand Down Expand Up @@ -222,6 +233,24 @@ mod tests {
);
}

#[test]
pub fn detect_term_ghostty() {
with_vars(vec![("TERM", Some("xterm-ghostty"))], || {
assert_eq!(TerminalProgram::detect(), TerminalProgram::Ghostty)
})
}

#[test]
pub fn detect_term_program_ghostty() {
with_vars(
vec![
("TERM", Some("xterm-256color")),
("TERM_PROGRAM", Some("ghostty")),
],
|| assert_eq!(TerminalProgram::detect(), TerminalProgram::Ghostty),
)
}

#[test]
pub fn detect_ansi() {
with_vars(
Expand Down

0 comments on commit 01a75db

Please sign in to comment.