Steve Thielemann 3 years ago
parent
commit
ae630a1af9
2 changed files with 143 additions and 3 deletions
  1. 138 3
      door/door.go
  2. 5 0
      testdoor/testdoor.go

+ 138 - 3
door/door.go

@@ -132,6 +132,89 @@ func ColorText(color string) string {
 	return Color(result...)
 }
 
+type BoxStyle struct {
+	top_left     string
+	top_right    string
+	top          string
+	side         string
+	bottom_left  string
+	bottom_right string
+	middle_left  string
+	middle_right string
+}
+
+var boxes = [4]BoxStyle{
+	/*
+		┌──┐
+		│  │
+		├──┤
+		└──┘
+	*/
+	BoxStyle{"\xda", "\xbf", "\xc4", "\xb3", "\xc0", "\xd9", "\xc3", "\xb4"},
+	/*
+		╔══╗
+		║  ║
+		╠══╣
+		╚══╝
+	*/
+	BoxStyle{"\xc9", "\xbb", "\xcd", "\xba", "\xc8", "\xbc", "\xcc", "\xb9"},
+	/*
+		╒══╕
+		│  │
+		╞══╡
+		╘══╛
+	*/
+	BoxStyle{"\xd6", "\xb8", "\xcd", "\xb3", "\xd4", "\xbe", "\xc6", "\xb5"},
+	/*
+		╓──╖
+		║  ║
+		╟──╢
+		╙──╜
+	*/
+	BoxStyle{"\xd6", "\xb7", "\xc4", "\xba", "\xd3", "\xbd", "\xc7", "\xb6"}}
+
+var unicode_boxes = []BoxStyle{
+	BoxStyle{"\u250c", "\u2510", "\u2500", "\u2502", "\u2514", "\u2518", "\u251c", "\u2524"},
+	BoxStyle{"\u2554", "\u2557", "\u2550", "\u2551", "\u255a", "\u255d", "\u2560", "\u2563"},
+	BoxStyle{"\u2553", "\u2556", "\u2500", "\u2551", "\u2559", "\u255c", "\u255f", "\u2562"},
+	BoxStyle{"\u2552", "\u2555", "\u2550", "\u2502", "\u2558", "\u255b", "\u255e", "\u2561"},
+}
+
+type Box struct {
+	Width int
+	Style int
+}
+
+func (b *Box) Top() string {
+	var style *BoxStyle
+	if Unicode {
+		style = &unicode_boxes[b.Style]
+	} else {
+		style = &boxes[b.Style]
+	}
+	return style.top_left + strings.Repeat(style.top, b.Width) + style.top_right
+}
+
+func (b *Box) Middle(text string) string {
+	var style *BoxStyle
+	if Unicode {
+		style = &unicode_boxes[b.Style]
+	} else {
+		style = &boxes[b.Style]
+	}
+	return style.side + text + strings.Repeat(" ", b.Width-len(text)) + style.side
+}
+
+func (b *Box) Bottom() string {
+	var style *BoxStyle
+	if Unicode {
+		style = &unicode_boxes[b.Style]
+	} else {
+		style = &boxes[b.Style]
+	}
+	return style.bottom_left + strings.Repeat(style.top, b.Width) + style.bottom_right
+}
+
 var Reset string = Color(0)
 var READFD int
 var WRITEFD int
@@ -212,6 +295,12 @@ func (d *Door) HasKey() bool {
 	return true
 }
 
+var Unicode bool
+var CP437 bool
+var Full_CP437 bool
+var Height int
+var Width int
+
 func (d *Door) detect() {
 	// if d.config.comm_handle == 0 {
 	// d.Write("\377\375\042\377\373\001") // fix telnet client
@@ -225,18 +314,64 @@ func (d *Door) detect() {
 	time.Sleep(250 * time.Millisecond)
 	// read everything
 	// telnet term isn't in RAW mode, so keys are buffer until <CR>
+	var results string
 
 	if true { // d.HasKey() {
 		buffer := make([]byte, 100)
 		r, err := syscall.Read(d.READFD, buffer)
-		results := string(buffer[:r])
-		results = strings.Replace(results, "\x1b", "^", -1)
-		fmt.Println("DETECT:", r, err, results)
+		results = string(buffer[:r])
+		output := strings.Replace(results, "\x1b", "^[", -1)
+		fmt.Println("DETECT:", r, err, output)
 	} else {
 		// local telnet echos the reply  :()
 		fmt.Println("DETECT: Nothing received.")
+		return
 	}
 
+	if ((strings.Index(results, "1;1R") != -1) ||
+		(strings.Index(results, "1;3R") != -1)) &&
+		((strings.Index(results, "2:2R") != -1) ||
+			(strings.Index(results, "2;3R") != -1)) {
+		Unicode = true
+	} else {
+		Unicode = false
+		CP437 = true
+	}
+	if strings.Index(results, "1;3R") != -1 {
+		Full_CP437 = true
+	}
+
+	// get screen size
+	var err error
+
+	pos := strings.LastIndex(results, "\x1b")
+	if pos != -1 {
+		pos++
+		if results[pos] == '[' {
+			pos++
+			results = results[pos:]
+			pos = strings.Index(results, ";")
+			if pos != -1 {
+				height := results[:pos]
+				// Height, err = strconv.Atoi(results)
+				Height, err = strconv.Atoi(height)
+				pos++
+				results = results[pos:]
+
+				pos = strings.Index(results, "R")
+				if pos != -1 {
+					width := results[:pos]
+
+					Width, err = strconv.Atoi(width)
+					fmt.Printf("Width: %s, %d, %v\n", results, Width, err)
+				}
+			} else {
+				Height = 0
+				Width = 0
+			}
+		}
+	}
+	fmt.Printf("Unicode %v Screen: %d X %d\n", Unicode, Width, Height)
 }
 
 func (d *Door) Init() {

+ 5 - 0
testdoor/testdoor.go

@@ -17,6 +17,11 @@ func main() {
 	key := d.Getch()
 	message := fmt.Sprintf("Key %s%d / %x%s"+door.CRNL, bold, key, key, reset)
 	d.Write(message)
+	b := door.Box{20, 1}
+	d.Write(b.Top() + door.CRNL)
+	d.Write(b.Middle("SHAZAM!") + door.CRNL)
+	d.Write(b.Bottom() + door.CRNL)
+
 	d.Write("Returning you to the BBS..." + door.CRNL)
 	d.SleepKey(3)
 	fmt.Println("Ending testdoor.go")