testdoor.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package main
  2. import (
  3. "fmt"
  4. "red-green/door"
  5. "strconv"
  6. "time"
  7. )
  8. func pctUpdate(pct *int64) func() int64 {
  9. return func() int64 {
  10. return *pct
  11. }
  12. }
  13. // Can I add a function to Door?
  14. // NO: cannot define new methods on non-local type door.Door
  15. /*
  16. func (d *door.Door) PressAKey() {
  17. d.Write(door.Reset + door.CRNL + "Press a key to continue...")
  18. d.Key()
  19. d.Write(door.CRNL)
  20. }
  21. */
  22. func press_a_key(d *door.Door) int {
  23. d.Write(door.Reset + door.CRNL + "Press a key to continue...")
  24. k := d.Key()
  25. d.Write(door.CRNL)
  26. return k
  27. }
  28. func MainMenu() door.Menu {
  29. // Make the main menu
  30. m := door.Menu{Panel: door.Panel{Width: 45,
  31. X: 5,
  32. Y: 5,
  33. Style: door.DOUBLE,
  34. Title: "[ Main Menu: ]",
  35. TitleOffset: 3,
  36. BorderColor: door.ColorText("BRI CYAN ON BLA")}}
  37. m.SelectedR = door.MakeMenuRender(door.ColorText("BOLD CYAN"),
  38. door.ColorText("BOLD BLUE"),
  39. door.ColorText("BOLD CYAN"),
  40. door.ColorText("BOLD BLUE"))
  41. m.UnselectedR = door.MakeMenuRender(door.ColorText("BOLD YEL ON BLUE"),
  42. door.ColorText("BOLD WHI ON BLUE"),
  43. door.ColorText("BOLD YEL ON BLUE"),
  44. door.ColorText("BOLD CYAN ON BLUE"))
  45. m.AddSelection("A", "ANSI Display")
  46. m.AddSelection("D", "Display Information (dropfile, screen)")
  47. m.AddSelection("I", "Input Prompt Demo")
  48. m.AddSelection("P", "Progress Bars Demo")
  49. m.AddSelection("S", "Show Panel")
  50. m.AddSelection("Q", "Quit")
  51. return m
  52. }
  53. func display_information(d *door.Door) {
  54. d.Write(door.Clrscr)
  55. keyColor := door.ColorText("BRI GREEN")
  56. sepColor := door.ColorText("BRI YEL")
  57. valColor := door.ColorText("CYAN")
  58. nice_format := func(key string, value string) string {
  59. return fmt.Sprintf("%s%-20s %s: %s%s", keyColor, key, sepColor, valColor, value) + door.CRNL
  60. }
  61. d.Write(nice_format("BBS Software", d.Config.BBSID))
  62. d.Write(nice_format("Real Name", d.Config.Real_name))
  63. d.Write(nice_format("Handle", d.Config.Handle))
  64. d.Write(nice_format("Node #", strconv.Itoa(d.Config.Node)))
  65. d.Write(nice_format("Unicode", strconv.FormatBool(door.Unicode)))
  66. d.Write(nice_format("CP437", strconv.FormatBool(door.CP437)))
  67. d.Write(nice_format("Screen Size", fmt.Sprintf("%d X %d", door.Width, door.Height)))
  68. }
  69. func display_ansi(d *door.Door) {
  70. SPACE := [...]string{
  71. "\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",
  72. "\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",
  73. "\x1b[42m\xb0\xb1\x1b[40m\xdc\xdc\xdc\xdc \x1b[42m\xb0\xb1\x1b[2C\xb2\xdb\x1b[1C \xb0\x1b[4C\xb2\xdb\x1b[1C\xb0\xb0\x1b[6C\xb0\xb0\x1b[0;32m\xdc\xdc\x1b[11C\x1b[42m \x1b[1m\xb0\x1b[4C\xb2\xdb\x1b[1C\xb0\xb0\x1b[6C\xb0\xb0\x1b[0;32m\xdc\xdc",
  74. " \x1b[1m\xdf\xdf\xdf\xdf\x1b[42m\xb2\xb2\x1b[2C\xb0\xb0\xb1\xb1\xb2\x1b[2C \xb0\xb0\xb1\xb1\xb2\xb2\x1b[1C \xb0\x1b[6C \xb0\x1b[0;32m\xdf\xdf\x1b[11C\x1b[42m \x1b[1m\xb0\xb0\xb1\xb1\xb2\xb2\x1b[1C \xb0\x1b[6C \xb0\x1b[0;32m\xdf\xdf",
  75. "\xdb \x1b[1;42m\xb1\xb2\x1b[2C \xb0\x1b[5C \x1b[4C\xb1\xb2\x1b[1C \x1b[3C\xb1\xb2\x1b[1C \x1b[3C\xb1\xb2\x1b[8C \x1b[4C\xb1\xb2\x1b[1C \x1b[3C\xb1\xb2\x1b[1C \x1b[3C\xb1\xb2\x1b[40m",
  76. "\x1b[0;32m\xdf\x1b[42m \x1b[40m\xdb\x1b[1;42m\xb0\xb0\xb1\x1b[0;32m\xdf \x1b[42m \x1b[7C \x1b[2C\x1b[1m\xb0\xb1\x1b[3C \xb0\xb0\xb1\x1b[3C \xb0\xb0\xb1\x1b[10C \x1b[2C\xb0\xb1\x1b[3C \xb0\xb0\xb1\x1b[3C \xb0\xb0\xb1\x1b[40m",
  77. "",
  78. "",
  79. "\x1b[37mBugz in space, with a deck of cards",
  80. "\x1b[0m",
  81. }
  82. d.Write(door.Clrscr)
  83. for _, line := range SPACE {
  84. if door.Unicode {
  85. d.Write(door.CP437_to_Unicode(line) + door.CRNL)
  86. } else {
  87. d.Write(line + door.CRNL)
  88. }
  89. }
  90. }
  91. func input_demo(d *door.Door) {
  92. inputColor := door.ColorText("BRI WHI ON BLUE")
  93. inputColor2 := door.ColorText("BRI WHI ON GREEN")
  94. prompt := door.Line{Text: "What is YOUR Name: "}
  95. prompt.RenderF = door.RenderBlueYellow
  96. d.Write(prompt.Output() + inputColor)
  97. name := d.Input(25)
  98. d.Write(door.Reset + door.CRNL)
  99. prompt.Text = "What is Your Quest: "
  100. d.Write(prompt.Output() + inputColor2)
  101. quest := d.Input(35)
  102. d.Write(door.Reset + door.CRNL)
  103. prompt.Text = "What is your Favorite CoLoR: "
  104. d.Write(prompt.Output() + inputColor2)
  105. color := d.Input(15)
  106. d.Write(door.Reset + door.CRNL)
  107. d.Write(fmt.Sprintf("You're %s on the %s quest, and fond of %s."+door.CRNL, name, quest, color))
  108. }
  109. func progress_bars(d *door.Door) {
  110. d.Write(door.Clrscr)
  111. bar := door.BarLine{Line: door.Line{DefaultColor: door.ColorText("BOLD YELLOW")}, Width: 20, Style: door.HALF_STEP}
  112. bar2 := door.BarLine{Width: 30, Style: door.PERCENT_SPACE}
  113. bar2.ColorRange = []door.BarRange{
  114. {2500, door.ColorText("RED")},
  115. {5000, door.ColorText("BROWN")},
  116. {7500, door.ColorText("BOLD YEL")},
  117. {9500, door.ColorText("GREEN")},
  118. {10100, door.ColorText("BRI GRE")}}
  119. bar3 := door.BarLine{Width: 15, Style: door.GRADIENT, Line: door.Line{DefaultColor: door.ColorText("CYAN")}}
  120. var percentage int64
  121. bar.UpdateP = pctUpdate(&percentage)
  122. bar2.UpdateP = pctUpdate(&percentage)
  123. bar3.UpdateP = pctUpdate(&percentage)
  124. d.Write(door.Reset + "Half-Step, Percentage with space and Color Range, Gradient..." + door.CRNL)
  125. bar_start := door.Goto(3, 15)
  126. for f := 0; f <= 100; f++ {
  127. percentage = int64(f * 100)
  128. bar.Update()
  129. bar2.Update()
  130. bar3.Update()
  131. d.Write(bar_start + bar.Output() + " " + door.Reset + bar2.Output() + door.Reset + " " + bar3.Output())
  132. if d.Disconnected {
  133. break
  134. }
  135. time.Sleep(time.Millisecond * 100)
  136. }
  137. }
  138. func panel_demo(d *door.Door) {
  139. width := 55
  140. fmtStr := "%-55s"
  141. p := door.Panel{X: 5, Y: 5, Width: width, Style: door.DOUBLE, BorderColor: door.ColorText("CYAN ON BLUE"), Title: "[ Panel Demo ]"}
  142. lineColor := door.ColorText("BRIGHT WHI ON BLUE")
  143. // Add lines to the panel
  144. for _, line := range []string{"The BBS Door Panel Demo", "(C) 2021 Red Green Software, https://red-green.com"} {
  145. l := door.Line{Text: fmt.Sprintf(fmtStr, line), DefaultColor: lineColor}
  146. p.Lines = append(p.Lines, l)
  147. }
  148. p.Lines = append(p.Lines, p.Spacer())
  149. p.Lines = append(p.Lines, door.Line{Text: fmt.Sprintf(fmtStr, "Welcome to golang!"), DefaultColor: lineColor})
  150. d.Write(door.Clrscr)
  151. d.Write(p.Output())
  152. }
  153. func main() {
  154. var message string
  155. fmt.Println("Starting testdoor.go")
  156. d := door.Door{}
  157. d.Init()
  158. bold := door.Color(1, 37, 40)
  159. bolder := door.ColorText("BLI BOLD YEL ON BLUE")
  160. d.Write("Welcome to " + bolder + "door32.sys" + door.Reset + door.CRNL + "..." + door.CRNL)
  161. key := press_a_key(&d)
  162. d.Write(fmt.Sprintf("Key %s%d / %x%s", bold, key, key, door.Reset) + door.CRNL)
  163. b := door.AlertBox("Warning: golang is in use!", 1)
  164. d.Write(door.ColorText("BRI WHI ON GREEN"))
  165. for _, line := range b {
  166. d.Write(line + door.CRNL)
  167. }
  168. d.Write(door.Reset + door.CRNL)
  169. left := d.TimeLeft()
  170. message = fmt.Sprintf("You have %0.2f minutes / %0.2f seconds remaining..."+door.CRNL, left.Minutes(), left.Seconds())
  171. d.Write(message)
  172. press_a_key(&d)
  173. mainmenu := MainMenu()
  174. var choice int
  175. for choice >= 0 {
  176. d.Write(door.Clrscr)
  177. choice = mainmenu.Choose(&d)
  178. if choice < 0 {
  179. break
  180. }
  181. option := mainmenu.GetOption(choice)
  182. // fmt.Printf("Choice: %d, Option: %c\n", choice, option)
  183. switch option {
  184. case 'A':
  185. display_ansi(&d)
  186. press_a_key(&d)
  187. case 'D':
  188. display_information(&d)
  189. press_a_key(&d)
  190. case 'I':
  191. d.Write(door.Reset + door.CRNL + door.CRNL)
  192. input_demo(&d)
  193. press_a_key(&d)
  194. case 'P':
  195. progress_bars(&d)
  196. press_a_key(&d)
  197. case 'S':
  198. panel_demo(&d)
  199. press_a_key(&d)
  200. case 'Q':
  201. choice = -1
  202. break
  203. }
  204. }
  205. d.Write(door.Reset + door.CRNL)
  206. message = fmt.Sprintf("Returning to the %s BBS..."+door.CRNL, d.Config.BBSID)
  207. d.Write(message)
  208. d.WaitKey(2, 0)
  209. left = d.TimeLeft()
  210. message = fmt.Sprintf("You had %0.2f minutes / %0.2f seconds remaining!"+door.CRNL, left.Minutes(), left.Seconds())
  211. d.Write(message)
  212. left = d.TimeUsed()
  213. d.Write(fmt.Sprintf("You used %0.2f seconds, or %0.2f minutes."+door.CRNL, left.Seconds(), left.Minutes()))
  214. fmt.Println("Ending testdoor.go")
  215. }