From 1c0f1fb778ced17cc61a1cce69e8afa9ab166f5a Mon Sep 17 00:00:00 2001 From: Felix Hanley Date: Sat, 16 Sep 2023 21:55:13 +0700 Subject: Allow filtering non-graphic --- generator.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'generator.go') diff --git a/generator.go b/generator.go index 881ce45..d80bfb1 100644 --- a/generator.go +++ b/generator.go @@ -3,6 +3,7 @@ package bechars import ( "fmt" "strings" + "unicode" "src.userspace.com.au/lexer" ) @@ -22,6 +23,7 @@ type Generator struct { tok *lexer.Token maxRune *rune minRune *rune + graphic bool } // Option functions allows configuration of the generator. @@ -54,6 +56,14 @@ func MinRune(r rune) Option { } } +// OnlyGraphic filters non-visible characters +func OnlyGraphic(b bool) Option { + return func(g *Generator) error { + g.graphic = b + return nil + } +} + func ensureRangeLimits(g *Generator) { if g.maxRune == nil { maxRune := '\u007F' @@ -197,6 +207,10 @@ func (g *Generator) filter(in, exclude string) string { if strings.ContainsRune(exclude, r) { continue } + if g.graphic && !unicode.IsGraphic(r) { + fmt.Println("non-graphic", r) + continue + } out.WriteRune(r) } return out.String() -- cgit v1.2.3