Browse Source

Updated demo, renamed Variables so accessible.

Steve Thielemann 3 years ago
parent
commit
32a1324a6c
4 changed files with 209 additions and 130 deletions
  1. 9 0
      door/box.go
  2. 21 21
      door/door.go
  3. 11 0
      door/menu.go
  4. 168 109
      testdoor/testdoor.go

+ 9 - 0
door/box.go

@@ -99,3 +99,12 @@ func (b *Box) Bottom() string {
 	}
 	return style.bottom_left + strings.Repeat(style.top, b.Width) + style.bottom_right
 }
+
+func AlertBox(text string, style int) []string {
+	var results []string
+	b := Box{Width: len(text), Style: style}
+	results = append(results, b.Top())
+	results = append(results, b.Row(text))
+	results = append(results, b.Bottom())
+	return results
+}

+ 21 - 21
door/door.go

@@ -61,17 +61,17 @@ type DropfileConfig struct {
 	comm_handle    int
 	baudrate       int
 	BBSID          string
-	user_number    int
-	real_name      string
-	handle         string
+	User_number    int
+	Real_name      string
+	Handle         string
 	security_level int
 	time_left      int
 	emulation      int
-	node_number    int
+	Node           int
 }
 
 type Door struct {
-	config       DropfileConfig
+	Config       DropfileConfig
 	READFD       int
 	WRITEFD      int
 	Disconnected bool
@@ -110,24 +110,24 @@ func (d *Door) ReadDropfile(filename string) {
 		lines = append(lines, line)
 	}
 
-	d.config.comm_type, err = strconv.Atoi(lines[0])
-	d.config.comm_handle, err = strconv.Atoi(lines[1])
-	d.config.baudrate, err = strconv.Atoi(lines[2])
-	d.config.BBSID = lines[3]
-	d.config.user_number, err = strconv.Atoi(lines[4])
-	d.config.real_name = lines[5]
-	d.config.handle = lines[6]
-	d.config.security_level, err = strconv.Atoi(lines[7])
-	d.config.time_left, err = strconv.Atoi(lines[8])
-	d.config.emulation, err = strconv.Atoi(lines[9])
-	d.config.node_number, err = strconv.Atoi(lines[10])
-
-	d.READFD = d.config.comm_handle
-	d.WRITEFD = d.config.comm_handle
+	d.Config.comm_type, err = strconv.Atoi(lines[0])
+	d.Config.comm_handle, err = strconv.Atoi(lines[1])
+	d.Config.baudrate, err = strconv.Atoi(lines[2])
+	d.Config.BBSID = lines[3]
+	d.Config.User_number, err = strconv.Atoi(lines[4])
+	d.Config.Real_name = lines[5]
+	d.Config.Handle = lines[6]
+	d.Config.security_level, err = strconv.Atoi(lines[7])
+	d.Config.time_left, err = strconv.Atoi(lines[8])
+	d.Config.emulation, err = strconv.Atoi(lines[9])
+	d.Config.Node, err = strconv.Atoi(lines[10])
+
+	d.READFD = d.Config.comm_handle
+	d.WRITEFD = d.Config.comm_handle
 
 	// Calculate the time when time expires.
 	d.StartTime = time.Now()
-	d.TimeOut = time.Now().Add(time.Duration(d.config.time_left) * time.Minute)
+	d.TimeOut = time.Now().Add(time.Duration(d.Config.time_left) * time.Minute)
 }
 
 // Detect client terminal capabilities, Unicode, CP437, Full_CP437,
@@ -216,7 +216,7 @@ func (d *Door) Init() {
 
 	d.ReadDropfile(dropfile)
 
-	fmt.Printf("BBS %s, User %s / Handle %s / File %d\n", d.config.BBSID, d.config.real_name, d.config.handle, d.config.comm_handle)
+	fmt.Printf("BBS %s, User %s / Handle %s / File %d\n", d.Config.BBSID, d.Config.Real_name, d.Config.Handle, d.Config.comm_handle)
 	d.detect()
 }
 

+ 11 - 0
door/menu.go

@@ -1,6 +1,7 @@
 package door
 
 import (
+	"fmt"
 	"strings"
 	"unicode"
 )
@@ -66,6 +67,11 @@ func MakeMenuRender(bracketColor, optionColor, upperColor, lowerColor string) fu
 func (m *Menu) AddSelection(key string, text string) {
 	key = key[:1] // Make sure it is just 1 character
 	m.Options = append(m.Options, rune(key[0]))
+
+	if len(text)+4 > m.Width {
+		panic(fmt.Sprintf("Menu (not wide enough) Width %d : text size %d + 4 = %d\n", m.Width, len(text), len(text)+4))
+	}
+
 	linetext := "[" + key + "] " + text + strings.Repeat(" ", m.Width-(4+len(text)))
 	m.Lines = append(m.Lines, Line{Text: linetext, RenderF: m.UnselectedR})
 }
@@ -93,6 +99,11 @@ func (m *Menu) Build() {
 }
 */
 
+// Get the character that was pressed from the choice
+func (m *Menu) GetOption(choice int) rune {
+	return m.Options[choice-1]
+}
+
 func (m *Menu) Choose(d *Door) int {
 	var changed []int
 	updated := true

+ 168 - 109
testdoor/testdoor.go

@@ -3,6 +3,7 @@ package main
 import (
 	"fmt"
 	"red-green/door"
+	"strconv"
 	"time"
 )
 
@@ -12,98 +13,31 @@ func pctUpdate(pct *int64) func() int64 {
 	}
 }
 
-func press_a_key(d *door.Door) {
+// Can I add a function to Door?
+// NO:  cannot define new methods on non-local type door.Door
+
+/*
+func (d *door.Door) PressAKey() {
 	d.Write(door.Reset + door.CRNL + "Press a key to continue...")
 	d.Key()
 	d.Write(door.CRNL)
 }
+*/
 
-func main() {
-	fmt.Println("Starting testdoor.go")
-	d := door.Door{}
-
-	d.Init()
-	reset := door.Reset
-	bold := door.Color(1, 37, 40)
-	bolder := door.ColorText("BLI BOLD YEL ON BLUE")
-	d.Write("Welcome to " + bolder + "door32.sys" + reset + door.CRNL + "..." + door.CRNL)
-	key := d.Key()
-	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)
-	// or %-20s
-	message = fmt.Sprintf("%20s", "SHAZAM!")
-
-	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)
-
-	left := d.TimeLeft()
-
-	message = fmt.Sprintf("You have %0.2f minutes / %0.2f seconds remaining..."+door.CRNL, left.Minutes(), left.Seconds())
-	d.Write(message)
-
-	inputColor := door.ColorText("BRI WHI ON BLUE")
-
-	prompt := door.Line{Text: "What is YOUR Name: "}
-	prompt.RenderF = door.RenderBlueYellow
-	d.Write(prompt.Output() + inputColor)
-	// d.Write("What is your name: " + inputColor)
-	name := d.Input(25)
-	d.Write(door.Reset + door.CRNL)
-
-	if false {
-		bar := door.BarLine{Line: door.Line{DefaultColor: door.ColorText("BOLD YELLOW")}, Width: 20, Style: door.HALF_STEP}
-
-		bar2 := door.BarLine{Width: 30, Style: door.PERCENT_SPACE}
-
-		bar2.ColorRange = []door.BarRange{
-			{2500, door.ColorText("RED")},
-			{5000, door.ColorText("BROWN")},
-			{7500, door.ColorText("BOLD YEL")},
-			{9500, door.ColorText("GREEN")},
-			{10100, door.ColorText("BRI GRE")}}
-
-		bar3 := door.BarLine{Width: 15, Style: door.GRADIENT, Line: door.Line{DefaultColor: door.ColorText("CYAN")}}
-
-		var percentage int64
-		bar.UpdateP = pctUpdate(&percentage)
-		bar2.UpdateP = pctUpdate(&percentage)
-		bar3.UpdateP = pctUpdate(&percentage)
-
-		bar_start := door.Goto(3, 15)
-
-		for f := 0; f <= 100; f++ {
-			percentage = int64(f * 100)
-			d.Write(bar_start)
-			bar.Update()
-			d.Write(bar.Output())
-			d.Write("  " + door.Reset)
-			bar2.Update()
-			d.Write(bar2.Output())
-			d.Write(door.Reset + "  ")
-			bar3.Update()
-			d.Write(bar3.Output())
-
-			if d.Disconnected {
-				break
-			}
-
-			time.Sleep(time.Millisecond * 100)
-			// d.WaitKey(0, 55000)
-		}
-
-	}
-
-	press_a_key(&d)
+func press_a_key(d *door.Door) int {
+	d.Write(door.Reset + door.CRNL + "Press a key to continue...")
+	k := d.Key()
+	d.Write(door.CRNL)
+	return k
+}
 
-	m := door.Menu{Panel: door.Panel{Width: 25,
+func MainMenu() door.Menu {
+	// Make the main menu
+	m := door.Menu{Panel: door.Panel{Width: 45,
 		X:           5,
 		Y:           5,
 		Style:       door.DOUBLE,
-		Title:       "Please Select:",
+		Title:       "[ Main Menu: ]",
 		TitleOffset: 3,
 		BorderColor: door.ColorText("BRI CYAN ON BLA")}}
 	m.SelectedR = door.MakeMenuRender(door.ColorText("BOLD CYAN"),
@@ -114,32 +48,36 @@ func main() {
 		door.ColorText("BOLD WHI ON BLUE"),
 		door.ColorText("BOLD YEL ON BLUE"),
 		door.ColorText("BOLD CYAN ON BLUE"))
-	m.AddSelection("1", "Play A Game")
-	m.AddSelection("A", "Ask BUGZ for Help")
-	m.AddSelection("D", "Drop to DOS")
+
+	m.AddSelection("A", "ANSI Display")
+	m.AddSelection("D", "Display Information (dropfile, screen)")
+	m.AddSelection("I", "Input Prompt Demo")
+	m.AddSelection("P", "Progress Bars Demo")
+
 	m.AddSelection("Q", "Quit")
-	/*
-		m.MenuOptions = []door.MenuOption{{rune('1'), "Play A Game"},
-			{rune('A'), "Ask BUGZ for Help"},
-			{rune('D'), "Drop to DOS"},
-			{rune('Q'), "Quit"}}
-		m.Build()
-	*/
-
-	// Yes, the render functions are working
-	/*
-		test := door.Line{Text: "[T] Testing Render...", RenderF: m.SelectedR}
-		d.Write(test.Output() + door.Reset + door.CRNL)
-		test.RenderF = m.UnselectedR
-		d.Write(test.Output() + door.Reset + door.CRNL)
-		d.Write(door.Reset + door.CRNL + "Press a key to continue...")
-		d.Key()
-	*/
+	return m
+}
 
+func display_information(d *door.Door) {
 	d.Write(door.Clrscr)
 
-	item := m.Choose(&d)
+	keyColor := door.ColorText("BRI GREEN")
+	sepColor := door.ColorText("BRI YEL")
+	valColor := door.ColorText("CYAN")
+	nice_format := func(key string, value string) string {
+		return fmt.Sprintf("%s%-20s %s: %s%s", keyColor, key, sepColor, valColor, value) + door.CRNL
+	}
+
+	d.Write(nice_format("BBS Software", d.Config.BBSID))
+	d.Write(nice_format("Real Name", d.Config.Real_name))
+	d.Write(nice_format("Handle", d.Config.Handle))
+	d.Write(nice_format("Node #", strconv.Itoa(d.Config.Node)))
+	d.Write(nice_format("Unicode", strconv.FormatBool(door.Unicode)))
+	d.Write(nice_format("CP437", strconv.FormatBool(door.CP437)))
+	d.Write(nice_format("Screen Size", fmt.Sprintf("%d X %d", door.Width, door.Height)))
+}
 
+func display_ansi(d *door.Door) {
 	SPACE := [...]string{
 		"\x1b[?7h\x1b[255D\x1b[0;1;32m\xdc\x1b[42m\xb2\xb2\xb2\xb2\xdb\x1b[40m\xdc \x1b[42m\xb1\xb1\xb2\xb2\xdb\xdb\x1b[4C\xb1\xb2\xb2\xdb\x1b[4C\xb1\xb2\xb2\xdb\xdb\x1b[3C\xb1\xb2\xb2\xdb\xdb\x1b[11C\xb1\xb2\xb2\xdb\x1b[4C\xb1\xb2\xb2\xdb\xdb\x1b[3C\xb1\xb2\xb2\xdb\xdb\x1b[40m",
 		"\x1b[42m\xb1\xb1\x1b[4C\xdb\x1b[2C\xb1\xb1\x1b[2C\xdb\xdb\x1b[2C\xb0\xb1\x1b[2C\xb2\xdb\x1b[2C\xb0\xb1\x1b[3C\xdb\xdb\x1b[1C\xb0\xb1\x1b[3C\xdb\xdb\x1b[9C\xb0\xb1\x1b[2C\xb2\xdb\x1b[2C\xb0\xb1\x1b[3C\xdb\xdb\x1b[1C\xb0\xb1\x1b[3C\xdb\xdb\x1b[40m",
@@ -161,17 +99,138 @@ func main() {
 			d.Write(line + door.CRNL)
 		}
 	}
+}
+
+func input_demo(d *door.Door) {
+	inputColor := door.ColorText("BRI WHI ON BLUE")
+	inputColor2 := door.ColorText("BRI WHI ON GREEN")
+	prompt := door.Line{Text: "What is YOUR Name: "}
+	prompt.RenderF = door.RenderBlueYellow
+	d.Write(prompt.Output() + inputColor)
+	name := d.Input(25)
+	d.Write(door.Reset + door.CRNL)
+	prompt.Text = "What is Your Quest: "
+	d.Write(prompt.Output() + inputColor2)
+	quest := d.Input(35)
+	d.Write(door.Reset + door.CRNL)
+	prompt.Text = "What is your Favorite CoLoR: "
+	d.Write(prompt.Output() + inputColor2)
+	color := d.Input(15)
+	d.Write(door.Reset + door.CRNL)
+	d.Write(fmt.Sprintf("You're %s on the %s quest, and fond of %s."+door.CRNL, name, quest, color))
+}
+
+func progress_bars(d *door.Door) {
+	d.Write(door.Clrscr)
+
+	bar := door.BarLine{Line: door.Line{DefaultColor: door.ColorText("BOLD YELLOW")}, Width: 20, Style: door.HALF_STEP}
+	bar2 := door.BarLine{Width: 30, Style: door.PERCENT_SPACE}
+
+	bar2.ColorRange = []door.BarRange{
+		{2500, door.ColorText("RED")},
+		{5000, door.ColorText("BROWN")},
+		{7500, door.ColorText("BOLD YEL")},
+		{9500, door.ColorText("GREEN")},
+		{10100, door.ColorText("BRI GRE")}}
+
+	bar3 := door.BarLine{Width: 15, Style: door.GRADIENT, Line: door.Line{DefaultColor: door.ColorText("CYAN")}}
+
+	var percentage int64
+	bar.UpdateP = pctUpdate(&percentage)
+	bar2.UpdateP = pctUpdate(&percentage)
+	bar3.UpdateP = pctUpdate(&percentage)
+
+	d.Write(door.Reset + "Half-Step, Percentage with space and Color Range, Gradient..." + door.CRNL)
+
+	bar_start := door.Goto(3, 15)
+
+	for f := 0; f <= 100; f++ {
+		percentage = int64(f * 100)
+
+		bar.Update()
+		bar2.Update()
+		bar3.Update()
+
+		d.Write(bar_start + bar.Output() + "  " + door.Reset + bar2.Output() + door.Reset + "  " + bar3.Output())
+
+		if d.Disconnected {
+			break
+		}
+
+		time.Sleep(time.Millisecond * 100)
+	}
 
-	d.Write(door.Reset + door.CRNL + door.CRNL)
-	d.Write(fmt.Sprintf("You chose %d from the menu."+door.CRNL, item))
+}
 
-	press_a_key(&d)
+func main() {
+	var message string
+
+	fmt.Println("Starting testdoor.go")
+	d := door.Door{}
+
+	d.Init()
+
+	bold := door.Color(1, 37, 40)
+	bolder := door.ColorText("BLI BOLD YEL ON BLUE")
+	d.Write("Welcome to " + bolder + "door32.sys" + door.Reset + door.CRNL + "..." + door.CRNL)
+	key := press_a_key(&d)
+	d.Write(fmt.Sprintf("Key %s%d / %x%s", bold, key, key, door.Reset) + door.CRNL)
+
+	b := door.AlertBox("Warning: golang is in use!", 1)
+	d.Write(door.ColorText("BRI WHI ON GREEN"))
+	for _, line := range b {
+		d.Write(line + door.CRNL)
+	}
+	d.Write(door.Reset + door.CRNL)
+
+	left := d.TimeLeft()
 
-	message = fmt.Sprintf("Returning %s to the BBS..."+door.CRNL, name)
+	message = fmt.Sprintf("You have %0.2f minutes / %0.2f seconds remaining..."+door.CRNL, left.Minutes(), left.Seconds())
 	d.Write(message)
 
-	d.WaitKey(3, 0)
+	press_a_key(&d)
+
+	mainmenu := MainMenu()
+	var choice int
+
+	for choice >= 0 {
+		d.Write(door.Clrscr)
+		choice = mainmenu.Choose(&d)
+
+		if choice < 0 {
+			break
+		}
+		option := mainmenu.GetOption(choice)
+
+		// fmt.Printf("Choice: %d, Option: %c\n", choice, option)
+
+		switch option {
+		case 'A':
+			display_ansi(&d)
+			press_a_key(&d)
+		case 'D':
+			display_information(&d)
+			press_a_key(&d)
+		case 'I':
+			d.Write(door.Reset + door.CRNL + door.CRNL)
+			input_demo(&d)
+			press_a_key(&d)
+		case 'P':
+			progress_bars(&d)
+			press_a_key(&d)
+		case 'Q':
+			choice = -1
+			break
+		}
+
+	}
+
+	d.Write(door.Reset + door.CRNL)
+
+	message = fmt.Sprintf("Returning to the %s BBS..."+door.CRNL, d.Config.BBSID)
+	d.Write(message)
 
+	d.WaitKey(2, 0)
 	left = d.TimeLeft()
 
 	message = fmt.Sprintf("You had %0.2f minutes / %0.2f seconds remaining!"+door.CRNL, left.Minutes(), left.Seconds())