123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
-
-
-
-
-
-
-
-
-
-
-
- package gortty
-
- import (
- "bytes"
- "context"
- "fmt"
- )
-
-
- type Encoder struct {
- input chan rune
- output chan byte
- autoWrap bool
- autoCRLF bool
- charset *CodeSet
- crlfThresh int
- crlfForceThresh int
- shiftThresh int
- callback Callback
- }
-
-
- func NewEncoder(ctx context.Context, input chan rune, output chan byte, charset *CodeSet) *Encoder {
- e := new(Encoder)
- if input == nil {
- e.input = make(chan rune)
- } else {
- e.input = input
- }
- e.output = output
- e.charset = charset
- e.crlfThresh = 60
- e.crlfForceThresh = 72
- e.shiftThresh = 70
- go e.run(ctx)
- return e
- }
-
-
- func (e *Encoder) EncodeString(s string) {
- for _, c := range s {
- e.input <- c
- }
-
- }
-
-
- func (e *Encoder) SetAutoCRLF(autoCRLF bool) {
- e.autoCRLF = autoCRLF
- }
-
-
- func (e *Encoder) SetCallback(callback Callback) {
- e.callback = callback
- }
-
- func (e *Encoder) run(ctx context.Context) {
-
- var modCase int
- var charsWithoutCRLF int
- var charsWithoutShift int
- for {
- select {
- case <-ctx.Done():
- return
- case r := <-e.input:
- c := e.charset.toCode(r)
-
-
-
- if r == '\r' || r == '\n' {
- charsWithoutCRLF = 0
- }
-
-
- if c.shift == shiftWhitespace {
-
-
- if modCase == shiftUnknown {
- e.output <- e.charset.unshift()
- }
-
- modCase = shiftWhitespace
- } else if c.shift != modCase {
-
-
- if c.shift == shiftLetter {
- e.output <- e.charset.unshift()
- } else {
- e.output <- e.charset.shift()
- }
-
-
- modCase = c.shift
-
-
-
- charsWithoutShift = 0
- }
-
-
-
-
-
- if charsWithoutShift > e.shiftThresh {
- if modCase == shiftWhitespace {
- e.output <- e.charset.unshift()
- } else if modCase == shiftLetter {
- e.output <- e.charset.unshift()
- } else {
- e.output <- e.charset.shift()
- }
-
-
- charsWithoutShift = 0
- }
-
-
-
- if !e.autoWrap {
-
- e.output <- c.toByte()
-
- if e.autoCRLF {
- if r == '\n' {
- e.output <- e.charset.toByte('\r')
- } else if r == '\r' {
- e.output <- e.charset.toByte('\n')
- }
- }
-
- } else if (charsWithoutCRLF > e.crlfThresh) && (modCase == shiftWhitespace) {
-
- e.output <- e.charset.toByte('\r')
- e.output <- e.charset.toByte('\n')
- charsWithoutCRLF = 0
- } else if charsWithoutCRLF > e.crlfForceThresh {
-
- e.output <- e.charset.toByte('\r')
- e.output <- e.charset.toByte('\n')
- e.output <- c.toByte()
- charsWithoutCRLF = 0
- } else {
-
- e.output <- c.toByte()
-
- if e.autoCRLF {
- if r == '\n' {
- e.output <- e.charset.toByte('\r')
- } else if r == '\r' {
- e.output <- e.charset.toByte('\n')
- }
- }
-
- }
-
- charsWithoutShift++
- charsWithoutCRLF++
-
- }
- }
-
- }
-
- func (e *Encoder) Write(p []byte) (n int, err error) {
-
- runes := bytes.Runes(p)
-
- fmt.Print(string(runes))
-
- for _, r := range runes {
- e.input <- r
- }
- return len(p), nil
- }
|