Переглянути джерело

Updated: box and color organized.

Steve Thielemann 3 роки тому
батько
коміт
1e61216496
5 змінених файлів з 209 додано та 188 видалено
  1. 1 1
      Makefile
  2. 96 0
      door/box.go
  3. 109 0
      door/color.go
  4. 0 186
      door/door.go
  5. 3 1
      testdoor/testdoor.go

+ 1 - 1
Makefile

@@ -5,6 +5,6 @@ all: door32 testdoor/testdoor
 door32: door32.c
 	gcc -o door32 door32.c
 
-testdoor/testdoor: testdoor/testdoor.go door/door.go
+testdoor/testdoor: testdoor/testdoor.go door/*.go
 	cd testdoor; go build testdoor.go
 

+ 96 - 0
door/box.go

@@ -0,0 +1,96 @@
+package door
+
+import "strings"
+
+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) Row(text string) string {
+	var style *BoxStyle
+	if Unicode {
+		style = &unicode_boxes[b.Style]
+	} else {
+		style = &boxes[b.Style]
+	}
+	return style.side + text + style.side
+}
+
+func (b *Box) Middle() string {
+	var style *BoxStyle
+	if Unicode {
+		style = &unicode_boxes[b.Style]
+	} else {
+		style = &boxes[b.Style]
+	}
+	return style.middle_left + strings.Repeat(style.top, b.Width) + style.middle_right
+}
+
+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
+}

+ 109 - 0
door/color.go

@@ -0,0 +1,109 @@
+package door
+
+import (
+	"fmt"
+	"strings"
+)
+
+func Color(arg ...int) string {
+	var result string = "\x1b["
+	for i := range arg {
+		result += fmt.Sprintf("%d;", arg[i])
+	}
+	result = result[:len(result)-1]
+	result += "m"
+	return result
+}
+
+func ColorText(color string) string {
+	// split on spaces, uppercase, match first 3 letter
+	var result []int
+	var bg bool
+
+	result = append(result, 0)
+
+	parts := strings.Fields(strings.ToUpper(color))
+	for _, part := range parts {
+		switch part {
+		case "BLACK", "BLA":
+			if bg {
+				result = append(result, 40)
+			} else {
+				result = append(result, 30)
+			}
+
+		case "RED":
+			if bg {
+				result = append(result, 41)
+			} else {
+				result = append(result, 31)
+			}
+
+		case "GREEN", "GRE":
+			if bg {
+				result = append(result, 42)
+			} else {
+				result = append(result, 32)
+			}
+
+		case "BROWN", "BRO":
+			if bg {
+				result = append(result, 43)
+			} else {
+				result = append(result, 33)
+			}
+
+		case "YELLOW", "YEL":
+			if bg {
+				result = append(result, 43)
+			} else {
+				result = append(result, 33)
+			}
+
+		case "BLUE", "BLU":
+			if bg {
+				result = append(result, 44)
+			} else {
+				result = append(result, 34)
+			}
+
+		case "MAGENTA", "MAG":
+			if bg {
+				result = append(result, 45)
+			} else {
+				result = append(result, 35)
+			}
+
+		case "CYAN", "CYA":
+			if bg {
+				result = append(result, 46)
+			} else {
+				result = append(result, 36)
+			}
+
+		case "WHITE", "WHI":
+			if bg {
+				result = append(result, 47)
+			} else {
+				result = append(result, 37)
+			}
+
+		case "BOLD", "BOL", "BRIGHT", "BRI":
+			result = append(result, 1)
+
+		case "ON":
+			bg = true
+
+		case "BLINK", "BLI":
+			result = append(result, 5)
+
+		case "INVERT", "INVERSE", "INV":
+			result = append(result, 7)
+		default:
+			fmt.Println("ColorText Unknown:", part)
+		}
+
+	}
+	// fmt.Println("ColorText:", result)
+	return Color(result...)
+}

+ 0 - 186
door/door.go

@@ -29,192 +29,6 @@ g00r00                       Line 7 : User's handle/alias
 1                            Line 11: Current node number
 */
 
-func Color(arg ...int) string {
-	var result string = "\x1b["
-	for i := range arg {
-		result += fmt.Sprintf("%d;", arg[i])
-	}
-	result = result[:len(result)-1]
-	result += "m"
-	return result
-}
-
-func ColorText(color string) string {
-	// split on spaces, uppercase, match first 3 letter
-	var result []int
-	var bg bool
-
-	result = append(result, 0)
-
-	parts := strings.Fields(strings.ToUpper(color))
-	for _, part := range parts {
-		switch part {
-		case "BLACK", "BLA":
-			if bg {
-				result = append(result, 40)
-			} else {
-				result = append(result, 30)
-			}
-
-		case "RED":
-			if bg {
-				result = append(result, 41)
-			} else {
-				result = append(result, 31)
-			}
-
-		case "GREEN", "GRE":
-			if bg {
-				result = append(result, 42)
-			} else {
-				result = append(result, 32)
-			}
-
-		case "BROWN", "BRO":
-			if bg {
-				result = append(result, 43)
-			} else {
-				result = append(result, 33)
-			}
-
-		case "YELLOW", "YEL":
-			if bg {
-				result = append(result, 43)
-			} else {
-				result = append(result, 33)
-			}
-
-		case "BLUE", "BLU":
-			if bg {
-				result = append(result, 44)
-			} else {
-				result = append(result, 34)
-			}
-
-		case "MAGENTA", "MAG":
-			if bg {
-				result = append(result, 45)
-			} else {
-				result = append(result, 35)
-			}
-
-		case "CYAN", "CYA":
-			if bg {
-				result = append(result, 46)
-			} else {
-				result = append(result, 36)
-			}
-
-		case "WHITE", "WHI":
-			if bg {
-				result = append(result, 47)
-			} else {
-				result = append(result, 37)
-			}
-
-		case "BOLD", "BOL", "BRIGHT", "BRI":
-			result = append(result, 1)
-
-		case "ON":
-			bg = true
-
-		case "BLINK", "BLI":
-			result = append(result, 5)
-
-		case "INVERT", "INVERSE", "INV":
-			result = append(result, 7)
-		default:
-			fmt.Println("ColorText Unknown:", part)
-		}
-
-	}
-	// fmt.Println("ColorText:", result)
-	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 + 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

+ 3 - 1
testdoor/testdoor.go

@@ -22,7 +22,9 @@ func main() {
 	// or %-20s
 	message = fmt.Sprintf("%20s", "SHAZAM!")
 
-	d.Write(b.Middle(message) + door.CRNL)
+	d.Write(b.Row(message) + door.CRNL)
+	d.Write(b.Middle() + door.CRNL)
+	d.Write(b.Row(fmt.Sprintf("%-20s", "Meow?")) + door.CRNL)
 	d.Write(b.Bottom() + door.CRNL)
 
 	d.Write("Returning you to the BBS..." + door.CRNL)