Explorar el Código

Use map for input. NMS abort on keypress.

NoMoreSecrets: It doesn't abort, but it completes the reveal.
Bugz hace 1 año
padre
commit
eca264278c
Se han modificado 3 ficheros con 123 adiciones y 55 borrados
  1. 8 0
      door/color.go
  2. 80 47
      door/input.go
  3. 35 8
      door/nomoresecrets.go

+ 8 - 0
door/color.go

@@ -6,6 +6,9 @@ import (
 	"strings"
 )
 
+// Possibly make this so it is part of an ANSI package.
+// DRY - Reuse.
+
 // Convert an array of int to \x1b[1;2;3m for ANSI Color
 // output.
 // https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
@@ -22,6 +25,11 @@ func Color(arg ...int) string {
 
 // BUG(bugz) INVERSE does not work.
 
+// Would I want to do something like:
+// BRIGHT YELLOW or ON BLUE, and have it just change the
+// color or the background?  Right now, I require the full
+// color (FG & BG).
+
 // Convert a string like "BRIGHT WHITE ON BLUE" into the expected
 // ANSI Color codes.  You can use just the first 3 letters.
 // Supports BRIGHT, BOLD, BLINK and ON (to set the background color).

+ 80 - 47
door/input.go

@@ -152,7 +152,6 @@ func ReadRune(d *Door, runeread *bufio.Reader, timeout bool) {
 }
 
 func process(d *Door, newRune bool) {
-	// This is like GetKey
 	var rlen int
 	var r, r2 rune
 	var has2 bool
@@ -236,56 +235,89 @@ func process(d *Door, newRune bool) {
 			}
 
 			if has2 {
+				doorwayMap := map[rune]Extended{'\x50': DOWN_ARROW,
+					'\x48': UP_ARROW,
+					'\x4b': LEFT_ARROW,
+					'\x4d': RIGHT_ARROW,
+					'\x47': HOME,
+					'\x4f': END,
+					'\x49': PAGE_UP,
+					'\x51': PAGE_DOWN,
+					'\x3b': F1,
+					'\x3c': F2,
+					'\x3d': F3,
+					'\x3e': F4,
+					'\x3f': F5,
+					'\x40': F6,
+					'\x41': F7,
+					'\x42': F8,
+					'\x43': F9,
+					'\x44': F10,
+					'\x45': F11,
+					'\x46': F12,
+					'\x52': INSERT,
+					'\x53': DELETE,
+				}
+
 				ArrayPop(&readerBuffer, 2)
-				switch r2 {
-				case '\x50':
-					d.readerChannel <- ReaderData{0, DOWN_ARROW, nil}
-				case '\x48':
-					d.readerChannel <- ReaderData{0, UP_ARROW, nil}
-				case '\x4b':
-					d.readerChannel <- ReaderData{0, LEFT_ARROW, nil}
-				case 0x4d:
-					d.readerChannel <- ReaderData{0, RIGHT_ARROW, nil}
-				case 0x47:
-					d.readerChannel <- ReaderData{0, HOME, nil}
-				case 0x4f:
-					d.readerChannel <- ReaderData{0, END, nil}
-				case 0x49:
-					d.readerChannel <- ReaderData{0, PAGE_UP, nil}
-				case 0x51:
-					d.readerChannel <- ReaderData{0, PAGE_DOWN, nil}
-				case 0x3b:
-					d.readerChannel <- ReaderData{0, F1, nil}
-				case 0x3c:
-					d.readerChannel <- ReaderData{0, F2, nil}
-				case 0x3d:
-					d.readerChannel <- ReaderData{0, F3, nil}
-				case 0x3e:
-					d.readerChannel <- ReaderData{0, F4, nil}
-				case 0x3f:
-					d.readerChannel <- ReaderData{0, F5, nil}
-				case 0x40:
-					d.readerChannel <- ReaderData{0, F6, nil}
-				case 0x41:
-					d.readerChannel <- ReaderData{0, F7, nil}
-				case 0x42:
-					d.readerChannel <- ReaderData{0, F8, nil}
-				case 0x43:
-					d.readerChannel <- ReaderData{0, F9, nil}
-				case 0x44:
-					d.readerChannel <- ReaderData{0, F10, nil}
-				case 0x45:
-					d.readerChannel <- ReaderData{0, F11, nil}
-				case 0x46:
-					d.readerChannel <- ReaderData{0, F12, nil}
-				case 0x52:
-					d.readerChannel <- ReaderData{0, INSERT, nil}
-				case 0x53:
-					d.readerChannel <- ReaderData{0, DELETE, nil}
-				default:
+				ext, has := doorwayMap[r2]
+				if has {
+					d.readerChannel <- ReaderData{0, ext, nil}
+				} else {
 					log.Printf("ERROR Doorway mode: 0x00 %x\n", r2)
 					d.readerChannel <- ReaderData{0, UNKNOWN, nil}
 				}
+				/*
+					switch r2 {
+					case '\x50':
+						d.readerChannel <- ReaderData{0, DOWN_ARROW, nil}
+					case '\x48':
+						d.readerChannel <- ReaderData{0, UP_ARROW, nil}
+					case '\x4b':
+						d.readerChannel <- ReaderData{0, LEFT_ARROW, nil}
+					case 0x4d:
+						d.readerChannel <- ReaderData{0, RIGHT_ARROW, nil}
+					case 0x47:
+						d.readerChannel <- ReaderData{0, HOME, nil}
+					case 0x4f:
+						d.readerChannel <- ReaderData{0, END, nil}
+					case 0x49:
+						d.readerChannel <- ReaderData{0, PAGE_UP, nil}
+					case 0x51:
+						d.readerChannel <- ReaderData{0, PAGE_DOWN, nil}
+					case 0x3b:
+						d.readerChannel <- ReaderData{0, F1, nil}
+					case 0x3c:
+						d.readerChannel <- ReaderData{0, F2, nil}
+					case 0x3d:
+						d.readerChannel <- ReaderData{0, F3, nil}
+					case 0x3e:
+						d.readerChannel <- ReaderData{0, F4, nil}
+					case 0x3f:
+						d.readerChannel <- ReaderData{0, F5, nil}
+					case 0x40:
+						d.readerChannel <- ReaderData{0, F6, nil}
+					case 0x41:
+						d.readerChannel <- ReaderData{0, F7, nil}
+					case 0x42:
+						d.readerChannel <- ReaderData{0, F8, nil}
+					case 0x43:
+						d.readerChannel <- ReaderData{0, F9, nil}
+					case 0x44:
+						d.readerChannel <- ReaderData{0, F10, nil}
+					case 0x45:
+						d.readerChannel <- ReaderData{0, F11, nil}
+					case 0x46:
+						d.readerChannel <- ReaderData{0, F12, nil}
+					case 0x52:
+						d.readerChannel <- ReaderData{0, INSERT, nil}
+					case 0x53:
+						d.readerChannel <- ReaderData{0, DELETE, nil}
+					default:
+						log.Printf("ERROR Doorway mode: 0x00 %x\n", r2)
+						d.readerChannel <- ReaderData{0, UNKNOWN, nil}
+					}
+				*/
 			} else {
 				return
 			}
@@ -437,6 +469,7 @@ func process(d *Door, newRune bool) {
 				if !newRune {
 					// Yes, this is invalid.
 					ArrayPop(&readerBuffer, extlen)
+					// TODO:  Save this somewhere.
 					log.Println("ERROR Extended:", extended)
 					d.readerChannel <- ReaderData{0, UNKNOWN, nil}
 				} else {

+ 35 - 8
door/nomoresecrets.go

@@ -110,6 +110,8 @@ Example:
 func NoMoreSecrets(output string, Door *Door, config *NoMoreSecretsConfig) {
 	// Find ANSI codes, strip color (We'll handle color)
 
+	var keyAbort bool
+
 	rand.Seed(time.Now().UnixNano())
 
 	if Unicode {
@@ -237,7 +239,7 @@ func NoMoreSecrets(output string, Door *Door, config *NoMoreSecretsConfig) {
 			return result
 		}
 
-		for i := 0; i < (config.Jumble_Sec*1000)/config.Jumble_Loop_Speed; i++ {
+		for i := 0; (i < (config.Jumble_Sec*1000)/config.Jumble_Loop_Speed) && (!keyAbort); i++ {
 			for _, pos := range charpos {
 				if work[pos] != ' ' {
 					// Safe way to handle bytes to unicode
@@ -257,14 +259,20 @@ func NoMoreSecrets(output string, Door *Door, config *NoMoreSecretsConfig) {
 			if Door.Disconnect() {
 				return
 			}
-			time.Sleep(time.Millisecond * time.Duration(config.Jumble_Loop_Speed))
+			// time.Sleep()
+			_, _, err := Door.WaitKey(time.Millisecond * time.Duration(config.Jumble_Loop_Speed))
+			if err == nil {
+				log.Println("NoMore")
+				keyAbort = true
+				break
+			}
 		}
 
 		for {
 			var revealed bool = true
 			for idx, pos := range charpos {
 				if work[pos] != ' ' {
-					if chartime[idx] > 0 {
+					if !keyAbort && (chartime[idx] > 0) {
 						if chartime[idx] < 500 {
 							if rand.Intn(3) == 0 {
 								var safe [1]byte
@@ -299,6 +307,7 @@ func NoMoreSecrets(output string, Door *Door, config *NoMoreSecretsConfig) {
 						}
 						revealed = false
 					} else {
+						chartime[idx] = 0
 						work[pos] = original[revealpos[pos]]
 					}
 				}
@@ -308,7 +317,13 @@ func NoMoreSecrets(output string, Door *Door, config *NoMoreSecretsConfig) {
 			if Door.Disconnect() {
 				return
 			}
-			time.Sleep(time.Millisecond * time.Duration(config.Reveal_Loop_Speed))
+			// time.Sleep()
+			_, _, err := Door.WaitKey(time.Millisecond * time.Duration(config.Reveal_Loop_Speed))
+			if err == nil {
+				log.Println("NoMore")
+				keyAbort = true
+			}
+
 			if revealed {
 				break
 			}
@@ -435,7 +450,7 @@ func NoMoreSecrets(output string, Door *Door, config *NoMoreSecretsConfig) {
 			return string(result)
 		}
 
-		for i := 0; i < (config.Jumble_Sec*1000)/config.Jumble_Loop_Speed; i++ {
+		for i := 0; (i < (config.Jumble_Sec*1000)/config.Jumble_Loop_Speed) && (!keyAbort); i++ {
 			for _, pos := range charpos {
 				if work[pos] != ' ' {
 					if NORANDOM {
@@ -449,14 +464,20 @@ func NoMoreSecrets(output string, Door *Door, config *NoMoreSecretsConfig) {
 			if Door.Disconnect() {
 				return
 			}
-			time.Sleep(time.Millisecond * time.Duration(config.Jumble_Loop_Speed))
+			// time.Sleep()
+			_, _, err := Door.WaitKey(time.Millisecond * time.Duration(config.Jumble_Loop_Speed))
+			if err == nil {
+				log.Println("NoMore")
+				keyAbort = true
+				break
+			}
 		}
 
 		for {
 			var revealed bool = true
 			for idx, pos := range charpos {
 				if work[pos] != ' ' {
-					if chartime[idx] > 0 {
+					if !keyAbort && (chartime[idx] > 0) {
 						if chartime[idx] < 500 {
 							if rand.Intn(3) == 0 {
 								if NORANDOM {
@@ -482,6 +503,7 @@ func NoMoreSecrets(output string, Door *Door, config *NoMoreSecretsConfig) {
 						}
 						revealed = false
 					} else {
+						chartime[idx] = 0
 						work[pos] = original[revealpos[pos]]
 					}
 				}
@@ -491,7 +513,12 @@ func NoMoreSecrets(output string, Door *Door, config *NoMoreSecretsConfig) {
 			if Door.Disconnect() {
 				return
 			}
-			time.Sleep(time.Millisecond * time.Duration(config.Reveal_Loop_Speed))
+			// time.Sleep()
+			_, _, err := Door.WaitKey(time.Millisecond * time.Duration(config.Reveal_Loop_Speed))
+			if err == nil {
+				log.Println("NoMore")
+				keyAbort = true
+			}
 			if revealed {
 				break
 			}