Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

support ANSI Escape Sequence Select Graphic Rendition #163

Merged
merged 5 commits into from
Oct 23, 2022
Merged

support ANSI Escape Sequence Select Graphic Rendition #163

merged 5 commits into from
Oct 23, 2022

Conversation

ktr0731
Copy link
Owner

@ktr0731 ktr0731 commented Oct 23, 2022

These changes support Select Graphic Rendition, a part of ANSI Escape Sequence (so this implements #29). Now, the preview window interprets SGR parameters such as \x1b[1mfoo (bolded foo), \x1b[31mbar (bar with a red background).

The changes support almost of SGR functionality:

  • 16 colors
  • 256 colors
  • RGB colors (a.k.a. True Color)
  • Attributes (bold, dim, italic, underline, blink, reverse, and strikethrough)

For example, see the following code and screenshot:

type Track struct {
	Name      string
	AlbumName string
	Artist    string
}

var tracks = []Track{
	{"foo", "album1", "artist1"},
	{"bar", "album1", "artist1"},
	{"foo", "album2", "artist1"},
	{"baz", "album2", "artist2"},
	{"baz", "album3", "artist2"},
}

func main() {
	idx, err := fuzzyfinder.FindMulti(
		tracks,
		func(i int) string {
			return tracks[i].Name
		},
		fuzzyfinder.WithPreviewWindow(func(i, w, h int) string {
			if i == -1 {
				return ""
			}
			return fmt.Sprintf("\x1b[1;32mTrack\x1b[m: %s (%s)\n\x1b[1;31mAlbum\x1b[m: %s",
				tracks[i].Name,
				tracks[i].Artist,
				tracks[i].AlbumName)
		}))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("selected: %v\n", idx)
}

image

We also can use some libraries which provide SGR output.

package main

import (
	"fmt"
	"log"

	"github.com/logrusorgru/aurora/v4"

	fuzzyfinder "github.com/ktr0731/go-fuzzyfinder"
)

func main() {
	idx, err := fuzzyfinder.FindMulti(
		[]struct{}{},
		func(i int) string {
			return ""
		},
		fuzzyfinder.WithPreviewWindow(func(i, w, h int) string {
			return aurora.Sprintf(aurora.Cyan("Hello, %s"), aurora.Magenta("Aurora"))
		}))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("selected: %v\n", idx)
}

@ktr0731 ktr0731 self-assigned this Oct 23, 2022
@codecov
Copy link

codecov bot commented Oct 23, 2022

Codecov Report

Merging #163 (0305aa1) into master (3ecdbfe) will increase coverage by 3.21%.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #163      +/-   ##
==========================================
+ Coverage   84.46%   87.68%   +3.21%     
==========================================
  Files           5        5              
  Lines         837      828       -9     
==========================================
+ Hits          707      726      +19     
+ Misses        117       89      -28     
  Partials       13       13              

@ktr0731 ktr0731 merged commit e22fee8 into master Oct 23, 2022
@ktr0731 ktr0731 deleted the sgr branch October 23, 2022 07:48
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant