Skip to content
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(term): ansi: implement ANSI aware truncation #50

Merged
merged 1 commit into from
Mar 14, 2024

Conversation

aymanbagabas
Copy link
Member

@aymanbagabas aymanbagabas commented Mar 13, 2024

This implements an ANSI and wide-characters aware truncation algorithm that uses the newly merged ANSI parser state machine and the fantastic library uniseg.

Since this is using the ANSI state machine, it's compatible with CSI m
(SGR) style sequence, OSC 8 (hyperlinks), and basically any other
escape sequence supported in the state machine (DCS, ESC, SOS, PM, APC).

Related: muesli/reflow#71

Benchmarks against reflow truncate.String:

goos: darwin
goarch: amd64
pkg: github.com/charmbracelet/x/exp/term/ansi
cpu: Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
BenchmarkTruncateString-16              41309161                30.61 ns/op           65 B/op          1 allocs/op
BenchmarkReflowTruncateString-16        10123755               119.9 ns/op           291 B/op          3 allocs/op
PASS
func BenchmarkTruncateString(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		b.ReportAllocs()
		b.ResetTimer()
		for pb.Next() {
			Truncate("foo", 2, "")
		}
	})
}

func BenchmarkReflowTruncateString(b *testing.B) {
	b.RunParallel(func(pb *testing.PB) {
		b.ReportAllocs()
		b.ResetTimer()
		for pb.Next() {
			truncate.String("foo", 2)
		}
	})
}

@aymanbagabas aymanbagabas force-pushed the term/ansi_truncate branch 2 times, most recently from 2c9a780 to eefdc37 Compare March 13, 2024 17:01
This implements an ANSI and wide-characters aware truncation algorithm
that uses the newly merged [ANSI parser state machine][statemachine] and
the fantastic library uniseg.

Since this is using the ANSI state machine, it's compatible with `CSI m`
(SGR) style sequence, `OSC 8` (hyperlinks), and basically any other
escape sequence supported in the state machine (DCS, ESC, SOS, PM, APC).

Related: muesli/reflow#71

[statemachine]: https://github.com/charmbracelet/x/blob/main/exp/term/ansi/parser/transition_table.go
@aymanbagabas aymanbagabas merged commit a83b192 into main Mar 14, 2024
8 checks passed
@aymanbagabas aymanbagabas deleted the term/ansi_truncate branch March 14, 2024 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants