-
Notifications
You must be signed in to change notification settings - Fork 20
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
feat: Add sixel sequence object #269
Open
CannibalVox
wants to merge
1
commit into
charmbracelet:main
Choose a base branch
from
CannibalVox:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 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,73 @@ | ||
package ansi | ||
|
||
import ( | ||
"bytes" | ||
"strconv" | ||
) | ||
|
||
type SixelSequence struct { | ||
PixelWidth int | ||
PixelHeight int | ||
ImageWidth int | ||
ImageHeight int | ||
PaletteString string | ||
PixelString string | ||
} | ||
|
||
var _ Sequence = &SixelSequence{} | ||
|
||
// Clone returns a copy of the DCS sequence. | ||
func (s SixelSequence) Clone() Sequence { | ||
return SixelSequence{ | ||
PixelWidth: s.PixelWidth, | ||
PixelHeight: s.PixelHeight, | ||
ImageWidth: s.ImageWidth, | ||
ImageHeight: s.ImageHeight, | ||
PaletteString: s.PaletteString, | ||
PixelString: s.PixelString, | ||
} | ||
} | ||
|
||
// String returns a string representation of the sequence. | ||
// The string will always be in the 7-bit format i.e (ESC P p..p i..i f <data> ESC \). | ||
func (s SixelSequence) String() string { | ||
return s.buffer().String() | ||
} | ||
|
||
// Bytes returns the byte representation of the sequence. | ||
// The bytes will always be in the 7-bit format i.e (ESC P p..p i..i F <data> ESC \). | ||
func (s SixelSequence) Bytes() []byte { | ||
return s.buffer().Bytes() | ||
} | ||
|
||
func (s SixelSequence) buffer() *bytes.Buffer { | ||
var b bytes.Buffer | ||
|
||
b.WriteByte(ESC) | ||
// P<a>;<b>;<c>q | ||
// a = pixel aspect ratio (deprecated) | ||
// b = how to color unfilled pixels, 1 = transparent | ||
// (0 means background color but terminals seem to draw it arbitrarily making it useless) | ||
// c = horizontal grid size, I think everyone ignores this | ||
b.WriteString("P0;1;0q") | ||
// "<a>;<b>;<c>;<d> | ||
// a = pixel width | ||
// b = pixel height | ||
// c = image width in pixels | ||
// d = image height in pixels | ||
b.WriteByte('"') | ||
b.WriteString(strconv.Itoa(s.PixelWidth)) | ||
b.WriteByte(';') | ||
b.WriteString(strconv.Itoa(s.PixelHeight)) | ||
b.WriteByte(';') | ||
b.WriteString(strconv.Itoa(s.ImageWidth)) | ||
b.WriteByte(';') | ||
b.WriteString(strconv.Itoa(s.ImageHeight)) | ||
b.WriteString(s.PaletteString) | ||
b.WriteString(s.PixelString) | ||
// ST | ||
b.WriteByte(ESC) | ||
b.WriteByte('\\') | ||
|
||
return &b | ||
} |
This file contains 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,19 @@ | ||
package ansi | ||
|
||
import "testing" | ||
|
||
func TestSixelSequence(t *testing.T) { | ||
expectedResult := "\x1bP0;1;0q\"3;4;5;6PALETTEPIXELS\x1b\\" | ||
result := (SixelSequence{ | ||
PixelWidth: 3, | ||
PixelHeight: 4, | ||
ImageWidth: 5, | ||
ImageHeight: 6, | ||
PaletteString: "PALETTE", | ||
PixelString: "PIXELS", | ||
}).String() | ||
if result != expectedResult { | ||
t.Errorf("Expected sixel sequence to output %q but it output %q.", expectedResult, result) | ||
return | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sixel is just a DCS command. This should be a
type SixelImage struct
that conforms toimage.Image
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sixels don't really lend themselves to random access by pixel- Image.At() would basically have to be O(N) where N is the number of bytes in the encoded sixel image.