testdoor.go 8.1 KB

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