-
Notifications
You must be signed in to change notification settings - Fork 5
Use cell width for image width to make terminal output predictable #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| using Sixel.Terminal.Models; | ||
|
|
||
| namespace Sixel.Terminal; | ||
|
|
||
| public class Compatibility | ||
| { | ||
| /// <summary> | ||
| /// Memory-caches the result of the terminal supporting sixel graphics. | ||
| /// </summary> | ||
| private static bool? _terminalSupportsSixel; | ||
|
|
||
| /// <summary> | ||
| /// Memory-caches the result of the terminal cell size. | ||
| /// </summary> | ||
| private static CellSize? _cellSize; | ||
|
|
||
| /// <summary> | ||
| /// Get the cell size of the terminal in pixel-sixel size. | ||
| /// The response to the command will look like [6;20;10t where the 20 is height and 10 is width. | ||
| /// I think the 6 is the terminal class, which is not used here. | ||
| /// </summary> | ||
| /// <returns>The number of pixel sixels that will fit in a single character cell.</returns> | ||
| public static CellSize GetCellSize() | ||
| { | ||
| if (_cellSize != null) | ||
| { | ||
| return _cellSize; | ||
| } | ||
|
|
||
| var response = GetControlSequenceResponse("[16t"); | ||
|
|
||
| try | ||
| { | ||
| var parts = response.Split(';', 't'); | ||
| _cellSize = new CellSize { | ||
| PixelWidth = int.Parse(parts[2]), | ||
| PixelHeight = int.Parse(parts[1]) | ||
| }; | ||
| } | ||
| catch | ||
| { | ||
| // Return the default Windows Terminal size if we can't get the size from the terminal. | ||
| _cellSize = new CellSize { | ||
| PixelWidth = 10, | ||
| PixelHeight = 20 | ||
| }; | ||
| // TODO: Write this to a normal powershell warning stream. | ||
| Console.Error.WriteLine("Could not get terminal cell size, using default size 10x20."); | ||
| } | ||
|
|
||
| return _cellSize; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Check if the terminal supports sixel graphics. | ||
| /// This is done by sending the terminal a Device Attributes request. | ||
| /// If the terminal responds with a response that contains ";4;" then it supports sixel graphics. | ||
| /// https://vt100.net/docs/vt510-rm/DA1.html | ||
| /// </summary> | ||
| /// <returns>True if the terminal supports sixel graphics, false otherwise.</returns> | ||
| public static bool TerminalSupportsSixel() | ||
| { | ||
| if (_terminalSupportsSixel.HasValue) | ||
| { | ||
| return _terminalSupportsSixel.Value; | ||
| } | ||
|
|
||
| _terminalSupportsSixel = GetControlSequenceResponse("[c").Contains(";4;"); | ||
|
|
||
| return _terminalSupportsSixel.Value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Send a control sequence to the terminal and read back the response from STDIN. | ||
| /// </summary> | ||
| /// <param name="controlSequence"></param> | ||
| /// <returns>The response from the terminal.</returns> | ||
| private static string GetControlSequenceResponse(string controlSequence) | ||
| { | ||
| char? c; | ||
| var response = string.Empty; | ||
|
|
||
| Console.Write($"{Constants.Escape}{controlSequence}"); | ||
| do | ||
| { | ||
| c = Console.ReadKey(true).KeyChar; | ||
| response += c; | ||
| } while (c != 'c' && Console.KeyAvailable); | ||
|
|
||
| return response; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| namespace Sixel.Terminal; | ||
|
|
||
| public static class Constants | ||
| { | ||
| /// <summary> | ||
| /// The character to use when entering a terminal escape code sequence. | ||
| /// </summary> | ||
| public const string Escape = "\u001b"; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| namespace Sixel.Terminal.Models; | ||
|
|
||
| public class CellSize | ||
| { | ||
| /// <summary> | ||
| /// The width of a cell in pixels. | ||
| /// </summary> | ||
| public int PixelWidth { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The height of a cell in pixels. | ||
| /// This isn't used for anything yet but this would be required for something like spectre console that needs to work around the size of the rendered sixel image. | ||
| /// </summary> | ||
| public int PixelHeight { get; set; } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.