sanitize key name/code preparation in tscreen constructor
2 files changed, 29 insertions(+), 32 deletions(-)

M style.go
M tscreen.go
M style.go +3 -0
@@ 49,6 49,9 @@ const attrAll = AttrBold | AttrBlink | A
 // would be replaced with a palette number and palette index.
 //
 // To use Style, just declare a variable of its type.
+//
+// todo: put rgb flags to colors, so all fg data have the same offset to
+// their bg counterpart
 type Style int64
 
 // StyleDefault represents a default style, based upon the context.

          
M tscreen.go +26 -32
@@ 51,9 51,7 @@ func NewTerminfoScreen() (Screen, error)
 	}
 	t := &tScreen{ti: ti}
 
-	t.keyexist = make(map[Key]bool)
-	t.keycodes = make(map[string]*tKeyCode)
-	prepareKeys(t)
+	t.keyexist, t.keycodes = prepareKeys(ti)
 	t.buildAcsMap()
 	t.sigwinch = make(chan os.Signal, 10)
 	t.fallback = make(map[rune]string)

          
@@ 144,13 142,16 @@ type tScreen struct {
 	// in, out  *os.File
 	// baud     int
 	//	tiosp     *termiosPrivate
-	term *terminal
+	term     *terminal
+	evch     chan Event
+	sigwinch chan os.Signal
+	keyexist map[Key]bool
+	keycodes map[string]*tKeyCode
 
 	sync.Mutex
 
 	h, w  int
 	cells CellBuffer
-
 	// current style when drawing. if the style between cells does not change
 	// no style attributes have to be written to the terminal
 	//	curstyle Style

          
@@ 158,15 159,10 @@ type tScreen struct {
 	fini    bool
 	cursorx int
 	cursory int
+	quit    chan struct{}
+	indoneq chan struct{}
 
 	// maybe
-	//	style     Style
-	evch      chan Event
-	sigwinch  chan os.Signal
-	quit      chan struct{}
-	indoneq   chan struct{}
-	keyexist  map[Key]bool
-	keycodes  map[string]*tKeyCode
 	keychan   chan []byte
 	keytimer  *time.Timer
 	keyexpire time.Time

          
@@ 189,16 185,6 @@ type tScreen struct {
 	buttondn  bool
 }
 
-func (t *tScreen) prepareKey(key Key, mod ModMask, val string) {
-	if val != "" {
-		// Do not overrride codes that already exist
-		if _, exist := t.keycodes[val]; !exist {
-			t.keyexist[key] = true
-			t.keycodes[val] = &tKeyCode{key: key, mod: mod}
-		}
-	}
-}
-
 func (t *tScreen) Fini() {
 	ti := t.ti
 	t.Lock()

          
@@ 211,11 197,12 @@ func (t *tScreen) Fini() {
 	t.DisableMouse()
 	t.clear = false
 	t.fini = true
-	t.Unlock()
 
 	if t.quit != nil {
 		close(t.quit)
+		t.quit = nil
 	}
+	t.Unlock()
 
 	signal.Stop(t.sigwinch)
 

          
@@ 1191,14 1178,12 @@ func (t *tScreen) HasKey(k Key) bool {
 
 func (t *tScreen) Resize(int, int, int, int) {}
 
-func prepareKeys(t *tScreen) {
-	ti := t.ti
+func prepareKeys(ti *terminfo.Terminfo) (map[Key]bool, map[string]*tKeyCode) {
 
 	type key struct {
 		Key
 		Name string
 	}
-
 	type combo struct {
 		ModMask
 		keys []key

          
@@ 1207,7 1192,7 @@ func prepareKeys(t *tScreen) {
 	keys := []combo{
 		{ModNone,
 			[]key{
-				{KeyBackspace, t.ti.KeyBackspace},
+				{KeyBackspace, ti.KeyBackspace},
 				{KeyF1, ti.KeyF1},
 				{KeyF2, ti.KeyF2},
 				{KeyF3, ti.KeyF3},

          
@@ 1391,10 1376,18 @@ func prepareKeys(t *tScreen) {
 			}},
 		}...)
 	}
-
+	keyexist := make(map[Key]bool)
+	keycodes := make(map[string]*tKeyCode)
 	for _, combo := range keys {
 		for _, k := range combo.keys {
-			t.prepareKey(k.Key, combo.ModMask, k.Name)
+			if k.Name == "" {
+				continue
+			}
+			// Do not overrride codes that already exist
+			if _, exist := keycodes[k.Name]; !exist {
+				keyexist[k.Key] = true
+				keycodes[k.Name] = &tKeyCode{key: k.Key, mod: combo.ModMask}
+			}
 		}
 	}
 

          
@@ 1406,13 1399,13 @@ outer:
 		// when parsing this we don't want to fast path handling
 		// of it, but instead wait a bit before parsing it as in
 		// isolation.
-		for esc := range t.keycodes {
+		for esc := range keycodes {
 			if []byte(esc)[0] == byte(i) {
 				continue outer
 			}
 		}
 
-		t.keyexist[Key(i)] = true
+		keyexist[Key(i)] = true
 
 		mod := ModCtrl
 		switch Key(i) {

          
@@ 1420,6 1413,7 @@ outer:
 			// directly typeable- no control sequence
 			mod = ModNone
 		}
-		t.keycodes[string(rune(i))] = &tKeyCode{key: Key(i), mod: mod}
+		keycodes[string(rune(i))] = &tKeyCode{key: Key(i), mod: mod}
 	}
+	return keyexist, keycodes
 }