123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637 |
- package main
- import (
- "fmt"
- "log"
- "red-green/door"
- "runtime/debug"
- "strconv"
- "strings"
- "time"
- )
- func pctUpdate(pct *int64) func() int64 {
- return func() int64 {
- return *pct
- }
- }
- // 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 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
- }
- func about_test_door(d *door.Door) {
- W := 60
- center_x := (door.Width - W) / 2
- center_y := (door.Height - 16) / 2
- about := door.Panel{X: center_x,
- Y: center_y,
- Width: W,
- Style: door.SINGLE_DOUBLE,
- BorderColor: door.ColorText("BOLD YELLOW ON BLUE"),
- }
- about.Lines = append(about.Lines, door.Line{Text: fmt.Sprintf("%*s", -W, "About This Door"),
- DefaultColor: door.ColorText("BOLD CYAN ON BLUE")})
- about.Lines = append(about.Lines, about.Spacer())
- about.Lines = append(about.Lines, door.Line{Text: fmt.Sprintf("%*s", -W, "Test Door written in go, using go door.")})
- var copyright string = "(C) 2022 Bugz, Red Green Software"
- if door.Unicode {
- copyright = strings.Replace(copyright, "(C)", "\u00a9", -1)
- }
- about.Lines = append(about.Lines,
- door.Line{Text: fmt.Sprintf("%*s", -W, copyright),
- DefaultColor: door.ColorText("BOLD WHITE ON BLUE")})
- for _, text := range []string{"",
- "This door was written by Bugz.",
- "",
- "It is written in Go, understands CP437 and unicode, adapts",
- "to screen sizes, uses door32.sys, supports TheDraw Fonts,",
- "and runs on Linux and Windows."} {
- about.Lines = append(about.Lines, door.Line{Text: fmt.Sprintf("%*s", -W, text)})
- }
- better := door.NoMoreSecretsDefault
- better.Color = door.ColorText("BRI CYAN ON BLUE")
- door.NoMoreSecrets(about.Output(), d, &better)
- }
- func MainMenu() door.Menu {
- // Make the main menu
- m := door.Menu{Panel: door.Panel{Width: 45,
- X: 5,
- Y: 5,
- Style: door.DOUBLE,
- Title: "[ Main Menu: ]",
- TitleOffset: 3,
- BorderColor: door.ColorText("BRI CYAN ON BLA")}}
- m.SelectedR = door.MakeMenuRender(door.ColorText("BOLD CYAN"),
- door.ColorText("BOLD BLUE"),
- door.ColorText("BOLD WHITE"),
- door.ColorText("BOLD CYAN"))
- m.UnselectedR = door.MakeMenuRender(door.ColorText("BOLD YEL ON BLUE"),
- door.ColorText("BOLD WHI ON BLUE"),
- door.ColorText("BOLD YEL ON BLUE"),
- door.ColorText("BOLD CYAN ON BLUE"))
- m.AddSelection("A", "ANSI Display")
- m.AddSelection("C", "Crash")
- m.AddSelection("D", "Display Information (dropfile, screen)")
- m.AddSelection("F", "Font Demo")
- m.AddSelection("I", "Input Prompt Demo")
- m.AddSelection("M", "Menu Demo")
- m.AddSelection("P", "Progress Bars Demo")
- m.AddSelection("S", "Show Panel")
- m.AddSelection("T", "Test Door About")
- m.AddSelection("W", "Screen Width")
- m.AddSelection("Q", "Quit")
- return m
- }
- func display_information(d *door.Door) {
- d.Write(door.Clrscr)
- 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("Time Left", strconv.Itoa(d.Config.Time_left)))
- d.Write(nice_format("Real Name", d.Config.Real_name))
- d.Write(nice_format("Comm Type", strconv.Itoa(d.Config.Comm_type)))
- d.Write(nice_format("Comm Handle", strconv.Itoa(d.Config.Comm_handle)))
- d.Write(nice_format("Baudrate", strconv.Itoa(d.Config.Baudrate)))
- d.Write(nice_format("Handle", d.Config.Handle))
- d.Write(nice_format("User #", strconv.Itoa(d.Config.User_number)))
- d.Write(nice_format("Security Level", strconv.Itoa(d.Config.Security_level)))
- d.Write(nice_format("Node #", strconv.Itoa(d.Config.Node)))
- d.Write(nice_format("Emulation", strconv.Itoa(d.Config.Emulation)))
- d.Write(nice_format("Unicode", strconv.FormatBool(door.Unicode)))
- d.Write(nice_format("CP437", strconv.FormatBool(door.CP437)))
- d.Write(nice_format("Full CP437", strconv.FormatBool(door.Full_CP437)))
- d.Write(nice_format("Screen Size", fmt.Sprintf("%d X %d", door.Width, door.Height)))
- }
- func display_ansi(d *door.Door) {
- art := ANSIGrowl()
- d.Write(door.Clrscr)
- for _, line := range art {
- d.Write(line + door.CRNL)
- }
- }
- func font_demo(d *door.Door) {
- var output []string
- var l int
- var centering string
- d.Write(door.Clrscr) // + door.CRNL + door.CRNL)
- fac := FontAmazonCyan()
- output, l = fac.Output("ABCDEFGHIJKL")
- if l > door.Width {
- output, l = fac.Output("Cyan")
- }
- if l < door.Width {
- centering = strings.Repeat(" ", (door.Width-l)/2)
- for _, o := range output {
- d.Write(fmt.Sprintf("%s%s%s", centering, o, door.Reset) + door.CRNL)
- }
- d.Write(door.CRNL)
- }
- patch := fac.Scan(6)
- // log.Printf("Patch: %#v\n", patch)
- fac.Modify(4, patch)
- output, l = fac.Output("Blue")
- centering = strings.Repeat(" ", (door.Width-l)/2)
- for _, o := range output {
- d.Write(fmt.Sprintf("%s%s%s", centering, o, door.Reset) + door.CRNL)
- }
- d.Write(door.CRNL)
- fac.Modify(1, patch)
- output, l = fac.Output("Red")
- centering = strings.Repeat(" ", (door.Width-l)/2)
- for _, o := range output {
- d.Write(fmt.Sprintf("%s%s%s", centering, o, door.Reset) + door.CRNL)
- }
- d.Write(door.CRNL)
- press_a_key(d)
- fab := FontAnarchyBlue()
- output, l = fab.Output("Bugz is Here!")
- if l > door.Width {
- output, l = fab.Output("Hello")
- }
- if l < door.Width {
- centering = strings.Repeat(" ", (door.Width-l)/2)
- for _, o := range output {
- d.Write(centering + o + door.Reset + door.CRNL)
- }
- d.Write(door.CRNL)
- }
- unchain := FontUnchained()
- output, l = unchain.Output("Hi There!")
- if l > door.Width {
- output, l = unchain.Output("Meow")
- }
- if l < door.Width {
- centering = strings.Repeat(" ", (door.Width-l)/2)
- for _, o := range output {
- d.Write(centering + o + door.Reset + door.CRNL)
- }
- d.Write(door.CRNL)
- }
- press_a_key(d)
- asylum := FontAsylum()
- output, l = asylum.Output("Bugz ROCKS")
- if l > door.Width {
- output, l = asylum.Output("Aslym")
- }
- if l < door.Width {
- centering = strings.Repeat(" ", (door.Width-l)/2)
- for _, o := range output {
- d.Write(centering + o + door.Reset + door.CRNL)
- }
- d.Write(door.CRNL)
- }
- brain := FontBrainDmgBlu()
- output, l = brain.Output("I'm so BLUE")
- if l > door.Width {
- output, l = brain.Output("Blue")
- }
- if l < door.Width {
- centering = strings.Repeat(" ", (door.Width-l)/2)
- for _, o := range output {
- d.Write(centering + o + door.Reset + door.CRNL)
- }
- d.Write(door.CRNL)
- }
- boner := FontBoner()
- output, l = boner.Output("Welcome!")
- if l < door.Width {
- centering = strings.Repeat(" ", (door.Width-l)/2)
- for _, o := range output {
- d.Write(centering + o + door.Reset + door.CRNL)
- }
- d.Write(door.CRNL)
- }
- press_a_key(d)
- descent := FontDescent()
- output, l = descent.Output("Meanwhile...")
- if l > door.Width {
- output, l = descent.Output("BUGZ")
- }
- if l < door.Width {
- centering = strings.Repeat(" ", (door.Width-l)/2)
- for _, o := range output {
- d.Write(centering + o + door.Reset + door.CRNL)
- }
- d.Write(door.CRNL)
- }
- remorse := FontRemorse()
- output, l = remorse.Output("Enjoy the fonts")
- if l > door.Width {
- output, l = remorse.Output("Amazing")
- }
- if l < door.Width {
- centering = strings.Repeat(" ", (door.Width-l)/2)
- for _, o := range output {
- d.Write(centering + o + door.Reset + door.CRNL)
- }
- d.Write(door.CRNL)
- }
- dungeon := FontDungeon()
- output, l = dungeon.Output("Until NEXT time")
- if l > door.Width {
- output, l = dungeon.Output("Beware")
- }
- if l < door.Width {
- centering = strings.Repeat(" ", (door.Width-l)/2)
- for _, o := range output {
- d.Write(centering + o + door.Reset + door.CRNL)
- }
- d.Write(door.CRNL)
- }
- /*
- redgreen := FontArmageddon()
- white := redgreen.Scan(7)
- blue := redgreen.Scan(4)
- redgreen.Modify(1, white)
- redgreen.Modify(2, blue)
- */
- redgreen := FontRedGreen()
- output, l = redgreen.Output("Red-Green")
- if l < door.Width {
- press_a_key(d)
- centering = strings.Repeat(" ", (door.Width-l)/2)
- for _, o := range output {
- d.Write(centering + o + door.Reset + door.CRNL)
- }
- d.Write(door.CRNL)
- output, l = redgreen.Output("Software")
- centering = strings.Repeat(" ", (door.Width-l)/2)
- for _, o := range output {
- d.Write(centering + o + door.Reset + door.CRNL)
- }
- d.Write(door.CRNL)
- }
- }
- func input_demo(d *door.Door) {
- ticker := time.NewTicker(time.Second)
- go func() {
- for t := range ticker.C {
- const tf = "January 2, 2006 03:04:05 PM MST"
- output := door.SavePos + door.Goto(5, 2) + door.ColorText("BRI WHI ON GREEN") + t.Format(tf) + door.RestorePos
- d.Write(output)
- }
- }()
- 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)
- ticker.Stop()
- 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.SOLID, PercentStyle: 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)
- update_bars := func() {
- bar.Update()
- bar2.Update()
- bar3.Update()
- }
- d.Write(door.Goto(3, 12) + "Half-Step")
- d.Write(door.Goto(25, 12) + "% with space and Color Range")
- d.Write(door.Goto(57, 12) + "Gradient")
- d.Write(door.HideCursor)
- bar_start := door.Goto(3, 15)
- for f := 0; f <= 100; f++ {
- percentage = int64(f * 100)
- update_bars()
- d.Write(bar_start + bar.Output() + " " + door.Reset + bar2.Output() + door.Reset + " " + bar3.Output())
- if d.Disconnect() {
- // don't continue to sleep if we're disconnected
- break
- }
- time.Sleep(time.Millisecond * 100)
- }
- d.Write(door.ShowCursor)
- }
- func width_demo(d *door.Door) {
- w := door.Width
- panel := door.Panel{X: 1, Y: 1, Width: w}
- lineColor := door.ColorText("WHI")
- var line string
- for y := 1; y <= door.Height; y++ {
- if y%10 == 0 {
- line = strings.Repeat("1234567890", w/10)
- for x := len(line); x < w; x++ {
- line += strconv.Itoa((x + 1) % 10)
- }
- } else {
- line = ""
- for x := 1; x < w; x++ {
- if x%10 == 0 {
- line += strconv.Itoa(y % 10)
- } else {
- line += " "
- }
- }
- }
- l := door.Line{Text: line, DefaultColor: lineColor}
- panel.Lines = append(panel.Lines, l)
- }
- message := fmt.Sprintf("Screen Size: %d X %d", door.Width, door.Height)
- d.Write(panel.Output())
- // Output alert on top of panel
- cx := (door.Width - len(message) + 2) / 2
- cy := (door.Height - 3) / 2
- alert := door.AlertBox(message, 1)
- d.Write(door.ColorText("BRI YEL ON BLUE"))
- for idx, ab := range alert {
- d.Write(door.Goto(cx, cy+idx) + ab)
- }
- d.Write(door.Reset + panel.GotoEnd())
- // Pause for key
- d.Key()
- panel.Lines = make([]door.Line, 0)
- background := "BUGZ Test Door in GO "
- bl := len(background)
- for y := 1; y <= door.Height; y++ {
- offset := (y - 1) % bl
- line = background[offset:]
- for len(line) < w {
- if len(line)+bl <= w {
- line += background
- } else {
- line += background[0 : w-len(line)]
- }
- }
- l := door.Line{Text: line, RenderF: door.RenderBlueYellow}
- panel.Lines = append(panel.Lines, l)
- }
- d.Write(panel.Output())
- d.Key()
- }
- func panel_demo(d *door.Door) {
- width := 55
- fmtStr := "%-55s"
- p := door.Panel{X: 5, Y: 5, Width: width, Style: door.DOUBLE, BorderColor: door.ColorText("CYAN ON BLUE"), Title: "[ Panel Demo ]"}
- lineColor := door.ColorText("BRIGHT WHI ON BLUE")
- // Add lines to the panel
- for _, line := range []string{"The BBS Door Panel Demo", "(C) 2021 Red Green Software, https://red-green.com"} {
- if door.Unicode {
- line = strings.Replace(line, "(C)", "\u00a9", -1)
- }
- l := door.Line{Text: fmt.Sprintf(fmtStr, line), DefaultColor: lineColor}
- p.Lines = append(p.Lines, l)
- }
- p.Lines = append(p.Lines, p.Spacer())
- p.Lines = append(p.Lines, door.Line{Text: fmt.Sprintf(fmtStr, "Welcome to golang!"), DefaultColor: lineColor})
- single := door.Panel{X: 6, Y: 12, Width: 10, Style: door.SINGLE, BorderColor: door.ColorText("WHITE ON BLUE"), Title: "< Single >"}
- single.Lines = append(single.Lines, door.Line{Text: "Example ", DefaultColor: door.ColorText("WHI ON BLACK")})
- single.Lines = append(single.Lines, single.Spacer())
- single.Lines = append(single.Lines, door.Line{Text: "More Text ", DefaultColor: door.ColorText("BRI GREEN ON BLACK")})
- ds := door.Panel{X: 26, Y: 12, Width: 15, Style: door.DOUBLE_SINGLE, BorderColor: door.ColorText("BRI CYAN ON GREEN"), Title: "Double", TitleOffset: 3}
- ds.Lines = append(ds.Lines, door.Line{Text: "Double / Single", DefaultColor: door.ColorText("BRI WHI ON GREEN")})
- ds.Lines = append(ds.Lines, ds.Spacer())
- ds.Lines = append(ds.Lines, door.Line{Text: "Some Other Text", DefaultColor: door.ColorText("BRI CYAN ON GREEN")})
- sd := door.Panel{X: 46, Y: 12, Width: 15, Style: door.SINGLE_DOUBLE, BorderColor: door.ColorText("BRI YELL ON RED")}
- sd.Lines = append(sd.Lines, door.Line{Text: "Single / Double", DefaultColor: door.ColorText("BRI WHI ON RED")})
- sd.Lines = append(sd.Lines, sd.Spacer())
- sd.Lines = append(sd.Lines, door.Line{Text: "Text Goes Here ", DefaultColor: door.ColorText("BRI GREEN ON RED")})
- d.Write(door.Clrscr)
- d.Write(p.Output())
- d.Write(single.Output())
- d.Write(ds.Output())
- d.Write(sd.Output())
- }
- func main() {
- var message string
- fmt.Println("Starting testdoor.go")
- d := door.Door{}
- d.Init("testdoor")
- defer func() {
- if err := recover(); err != nil {
- log.Println("FAILURE:", err)
- // Display error to user
- d.Write(fmt.Sprintf(door.Reset+door.CRNL+"Exception: %v"+door.CRNL, err))
- // This displays stack trace stderr
- debug.PrintStack()
- }
- }()
- // defer d.Close()
- ticker := time.NewTicker(time.Second)
- go func() {
- for t := range ticker.C {
- const tf = "03:04:05 PM"
- var timeinfo string = fmt.Sprintf("(%3.1f mins)", d.TimeLeft().Minutes())
- // maxlen = 12 + 7 + 5 = 24
- output := door.SavePos + door.Goto(door.Width-25, 0) + door.ColorText("BRI WHI ON BLUE") + " " + t.Format(tf) + " " + timeinfo + " " + door.RestorePos
- if !d.Disconnect() {
- d.Write(output)
- } else {
- ticker.Stop()
- return
- }
- }
- }()
- 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("You have %0.2f minutes / %0.2f seconds remaining..."+door.CRNL, left.Minutes(), left.Seconds())
- d.Write(message)
- press_a_key(&d)
- mainmenu := MainMenu()
- var choice int
- for choice >= 0 {
- d.Write(door.Clrscr + door.HideCursor)
- choice = mainmenu.Choose(&d)
- d.Write(door.ShowCursor)
- 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 'F':
- font_demo(&d)
- press_a_key(&d)
- case 'I':
- d.Write(door.Reset + door.CRNL + door.CRNL)
- input_demo(&d)
- press_a_key(&d)
- case 'M':
- d.Write(door.Reset + door.CRNL + "TO DO: Provide menu of options to select from..." + door.CRNL)
- press_a_key(&d)
- case 'P':
- progress_bars(&d)
- press_a_key(&d)
- case 'S':
- panel_demo(&d)
- press_a_key(&d)
- case 'T':
- about_test_door(&d)
- press_a_key(&d)
- case 'W':
- width_demo(&d)
- case 'Q':
- choice = -1
- case 'C':
- z := 0
- a := 10 / z
- z = a
- }
- }
- d.Write(door.Reset + door.CRNL)
- message = fmt.Sprintf("Returning to the %s BBS..."+door.CRNL, d.Config.BBSID)
- d.Write(message)
- d.WaitKey(1, 0)
- left = d.TimeLeft()
- ticker.Stop()
- message = fmt.Sprintf("You had %0.2f minutes / %0.2f seconds remaining!"+door.CRNL, left.Minutes(), left.Seconds())
- d.Write(message)
- left = d.TimeUsed()
- d.Write(fmt.Sprintf("You used %0.2f seconds / %0.2f minutes."+door.CRNL, left.Seconds(), left.Minutes()))
- fmt.Println("Ending testdoor.go")
- }
|