testdoor.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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("M", "Menu Demo")
  54. m.AddSelection("P", "Progress Bars Demo")
  55. m.AddSelection("S", "Show Panel")
  56. m.AddSelection("Q", "Quit")
  57. return m
  58. }
  59. func display_information(d *door.Door) {
  60. d.Write(door.Clrscr)
  61. keyColor := door.ColorText("BRI GREEN")
  62. sepColor := door.ColorText("BRI YEL")
  63. valColor := door.ColorText("CYAN")
  64. nice_format := func(key string, value string) string {
  65. return fmt.Sprintf("%s%-20s %s: %s%s", keyColor, key, sepColor, valColor, value) + door.CRNL
  66. }
  67. d.Write(nice_format("BBS Software", d.Config.BBSID))
  68. d.Write(nice_format("Real Name", d.Config.Real_name))
  69. d.Write(nice_format("Handle", d.Config.Handle))
  70. d.Write(nice_format("User #", strconv.Itoa(d.Config.User_number)))
  71. d.Write(nice_format("Security Level", strconv.Itoa(d.Config.Security_level)))
  72. d.Write(nice_format("Node #", strconv.Itoa(d.Config.Node)))
  73. d.Write(nice_format("Unicode", strconv.FormatBool(door.Unicode)))
  74. d.Write(nice_format("CP437", strconv.FormatBool(door.CP437)))
  75. d.Write(nice_format("Screen Size", fmt.Sprintf("%d X %d", door.Width, door.Height)))
  76. }
  77. func display_ansi(d *door.Door) {
  78. space := SPACE()
  79. d.Write(door.Clrscr)
  80. for _, line := range space {
  81. if door.Unicode {
  82. d.Write(door.CP437_to_Unicode(line) + door.CRNL)
  83. } else {
  84. d.Write(line + door.CRNL)
  85. }
  86. }
  87. }
  88. func font_demo(d *door.Door) {
  89. var output [][]byte
  90. var l int
  91. var centering string
  92. d.Write(door.Clrscr) // + door.CRNL + door.CRNL)
  93. fac := FontAmazonCyan()
  94. output, l = fac.Output("ABCDEFGHIJKL")
  95. centering = strings.Repeat(" ", (door.Width-l)/2)
  96. for _, o := range output {
  97. if door.Unicode {
  98. d.Write(fmt.Sprintf("%s%s%s", centering, CP437Bytes_to_Unicode(o), door.Reset) + door.CRNL)
  99. } else {
  100. d.Write(fmt.Sprintf("%s%s%s", centering, string(o), door.Reset) + door.CRNL)
  101. }
  102. }
  103. d.Write(door.CRNL)
  104. fab := FontAnarchyBlue()
  105. output, l = fab.Output("Bugz is Here!")
  106. centering = strings.Repeat(" ", (door.Width-l)/2)
  107. for _, o := range output {
  108. if door.Unicode {
  109. d.Write(centering + CP437Bytes_to_Unicode(o) + door.Reset + door.CRNL)
  110. } else {
  111. d.Write(centering + string(o) + door.Reset + door.CRNL)
  112. }
  113. }
  114. d.Write(door.CRNL)
  115. unchain := FontUnchained()
  116. output, l = unchain.Output("Hi There!")
  117. centering = strings.Repeat(" ", (door.Width-l)/2)
  118. for _, o := range output {
  119. if door.Unicode {
  120. d.Write(centering + CP437Bytes_to_Unicode(o) + door.Reset + door.CRNL)
  121. } else {
  122. d.Write(centering + string(o) + door.Reset + door.CRNL)
  123. }
  124. }
  125. d.Write(door.CRNL)
  126. press_a_key(d)
  127. asylum := FontAsylum()
  128. output, l = asylum.Output("Bugz ROCKS")
  129. centering = strings.Repeat(" ", (door.Width-l)/2)
  130. // centering = ""
  131. for _, o := range output {
  132. if door.Unicode {
  133. d.Write(centering + CP437Bytes_to_Unicode(o) + door.Reset + door.CRNL)
  134. } else {
  135. d.Write(centering + string(o) + door.Reset + door.CRNL)
  136. }
  137. }
  138. d.Write(door.CRNL)
  139. brain := FontBrainDmgBlu()
  140. output, l = brain.Output("I'm so BLUE")
  141. centering = strings.Repeat(" ", (door.Width-l)/2)
  142. for _, o := range output {
  143. if door.Unicode {
  144. d.Write(centering + CP437Bytes_to_Unicode(o) + door.Reset + door.CRNL)
  145. } else {
  146. d.Write(centering + string(o) + door.Reset + door.CRNL)
  147. }
  148. }
  149. d.Write(door.CRNL)
  150. boner := FontBoner()
  151. output, l = boner.Output("Welcome!")
  152. centering = strings.Repeat(" ", (door.Width-l)/2)
  153. for _, o := range output {
  154. if door.Unicode {
  155. d.Write(centering + CP437Bytes_to_Unicode(o) + door.Reset + door.CRNL)
  156. } else {
  157. d.Write(centering + string(o) + door.Reset + door.CRNL)
  158. }
  159. }
  160. d.Write(door.CRNL)
  161. press_a_key(d)
  162. descent := FontDescent()
  163. output, l = descent.Output("Meanwhile...")
  164. if l > door.Width {
  165. output, l = descent.Output("BUGZ !?!")
  166. }
  167. centering = strings.Repeat(" ", (door.Width-l)/2)
  168. for _, o := range output {
  169. if door.Unicode {
  170. d.Write(centering + CP437Bytes_to_Unicode(o) + door.Reset + door.CRNL)
  171. } else {
  172. d.Write(centering + string(o) + door.Reset + door.CRNL)
  173. }
  174. }
  175. d.Write(door.CRNL)
  176. remorse := FontRemorse()
  177. output, l = remorse.Output("Enjoy the fonts")
  178. if l > door.Width {
  179. output, l = remorse.Output("Amazing")
  180. }
  181. centering = strings.Repeat(" ", (door.Width-l)/2)
  182. for _, o := range output {
  183. if door.Unicode {
  184. d.Write(centering + CP437Bytes_to_Unicode(o) + door.Reset + door.CRNL)
  185. } else {
  186. d.Write(centering + string(o) + door.Reset + door.CRNL)
  187. }
  188. }
  189. d.Write(door.CRNL)
  190. dungeon := FontDungeon()
  191. output, l = dungeon.Output("Until NEXT time")
  192. if l > door.Width {
  193. output, l = dungeon.Output("Beware")
  194. }
  195. centering = strings.Repeat(" ", (door.Width-l)/2)
  196. for _, o := range output {
  197. if door.Unicode {
  198. d.Write(centering + CP437Bytes_to_Unicode(o) + door.Reset + door.CRNL)
  199. } else {
  200. d.Write(centering + string(o) + door.Reset + door.CRNL)
  201. }
  202. }
  203. d.Write(door.CRNL)
  204. }
  205. func input_demo(d *door.Door) {
  206. inputColor := door.ColorText("BRI WHI ON BLUE")
  207. inputColor2 := door.ColorText("BRI WHI ON GREEN")
  208. prompt := door.Line{Text: "What is YOUR Name: "}
  209. prompt.RenderF = door.RenderBlueYellow
  210. d.Write(prompt.Output() + inputColor)
  211. name := d.Input(25)
  212. d.Write(door.Reset + door.CRNL)
  213. prompt.Text = "What is Your Quest: "
  214. d.Write(prompt.Output() + inputColor2)
  215. quest := d.Input(35)
  216. d.Write(door.Reset + door.CRNL)
  217. prompt.Text = "What is your Favorite CoLoR: "
  218. d.Write(prompt.Output() + inputColor2)
  219. color := d.Input(15)
  220. d.Write(door.Reset + door.CRNL)
  221. d.Write(fmt.Sprintf("You're %s on the %s quest, and fond of %s."+door.CRNL, name, quest, color))
  222. }
  223. func progress_bars(d *door.Door) {
  224. d.Write(door.Clrscr)
  225. bar := door.BarLine{Line: door.Line{DefaultColor: door.ColorText("BOLD YELLOW")}, Width: 20, Style: door.HALF_STEP}
  226. bar2 := door.BarLine{Width: 30, Style: door.PERCENT_SPACE}
  227. bar2.ColorRange = []door.BarRange{
  228. {2500, door.ColorText("RED")},
  229. {5000, door.ColorText("BROWN")},
  230. {7500, door.ColorText("BOLD YEL")},
  231. {9500, door.ColorText("GREEN")},
  232. {10100, door.ColorText("BRI GRE")}}
  233. bar3 := door.BarLine{Width: 15, Style: door.GRADIENT, Line: door.Line{DefaultColor: door.ColorText("CYAN")}}
  234. var percentage int64
  235. bar.UpdateP = pctUpdate(&percentage)
  236. bar2.UpdateP = pctUpdate(&percentage)
  237. bar3.UpdateP = pctUpdate(&percentage)
  238. update_bars := func() {
  239. bar.Update()
  240. bar2.Update()
  241. bar3.Update()
  242. }
  243. d.Write(door.Goto(3, 12) + "Half-Step")
  244. d.Write(door.Goto(25, 12) + "% with space and Color Range")
  245. d.Write(door.Goto(57, 12) + "Gradient")
  246. bar_start := door.Goto(3, 15)
  247. for f := 0; f <= 100; f++ {
  248. percentage = int64(f * 100)
  249. update_bars()
  250. d.Write(bar_start + bar.Output() + " " + door.Reset + bar2.Output() + door.Reset + " " + bar3.Output())
  251. if d.Disconnected {
  252. // don't continue to sleep if we're disconnected
  253. break
  254. }
  255. time.Sleep(time.Millisecond * 100)
  256. }
  257. }
  258. func panel_demo(d *door.Door) {
  259. width := 55
  260. fmtStr := "%-55s"
  261. p := door.Panel{X: 5, Y: 5, Width: width, Style: door.DOUBLE, BorderColor: door.ColorText("CYAN ON BLUE"), Title: "[ Panel Demo ]"}
  262. lineColor := door.ColorText("BRIGHT WHI ON BLUE")
  263. // Add lines to the panel
  264. for _, line := range []string{"The BBS Door Panel Demo", "(C) 2021 Red Green Software, https://red-green.com"} {
  265. if door.Unicode {
  266. line = strings.Replace(line, "(C)", "\u00a9", -1)
  267. }
  268. l := door.Line{Text: fmt.Sprintf(fmtStr, line), DefaultColor: lineColor}
  269. p.Lines = append(p.Lines, l)
  270. }
  271. p.Lines = append(p.Lines, p.Spacer())
  272. p.Lines = append(p.Lines, door.Line{Text: fmt.Sprintf(fmtStr, "Welcome to golang!"), DefaultColor: lineColor})
  273. d.Write(door.Clrscr)
  274. d.Write(p.Output())
  275. }
  276. func main() {
  277. var message string
  278. fmt.Println("Starting testdoor.go")
  279. d := door.Door{}
  280. d.Init()
  281. defer func() {
  282. if err := recover(); err != nil {
  283. log.Println("FAILURE:", err)
  284. // Display error to user
  285. d.Write(fmt.Sprintf(door.Reset+door.CRNL+"Exception: %v"+door.CRNL, err))
  286. // This displays stack trace stderr
  287. debug.PrintStack()
  288. }
  289. }()
  290. bold := door.Color(1, 37, 40)
  291. bolder := door.ColorText("BLI BOLD YEL ON BLUE")
  292. d.Write("Welcome to " + bolder + "door32.sys" + door.Reset + door.CRNL + "..." + door.CRNL)
  293. key := press_a_key(&d)
  294. d.Write(fmt.Sprintf("Key %s%d / %x%s", bold, key, key, door.Reset) + door.CRNL)
  295. b := door.AlertBox("Warning: golang is in use!", 1)
  296. d.Write(door.ColorText("BRI WHI ON GREEN"))
  297. for _, line := range b {
  298. d.Write(line + door.CRNL)
  299. }
  300. d.Write(door.Reset + door.CRNL)
  301. left := d.TimeLeft()
  302. message = fmt.Sprintf("You have %0.2f minutes / %0.2f seconds remaining..."+door.CRNL, left.Minutes(), left.Seconds())
  303. d.Write(message)
  304. press_a_key(&d)
  305. mainmenu := MainMenu()
  306. var choice int
  307. for choice >= 0 {
  308. d.Write(door.Clrscr)
  309. choice = mainmenu.Choose(&d)
  310. if choice < 0 {
  311. break
  312. }
  313. option := mainmenu.GetOption(choice)
  314. // fmt.Printf("Choice: %d, Option: %c\n", choice, option)
  315. switch option {
  316. case 'A':
  317. display_ansi(&d)
  318. press_a_key(&d)
  319. case 'D':
  320. display_information(&d)
  321. press_a_key(&d)
  322. case 'F':
  323. font_demo(&d)
  324. press_a_key(&d)
  325. case 'I':
  326. d.Write(door.Reset + door.CRNL + door.CRNL)
  327. input_demo(&d)
  328. press_a_key(&d)
  329. case 'M':
  330. d.Write(door.Reset + door.CRNL + "TO DO: Provide menu of options to select from..." + door.CRNL)
  331. press_a_key(&d)
  332. case 'P':
  333. progress_bars(&d)
  334. press_a_key(&d)
  335. case 'S':
  336. panel_demo(&d)
  337. press_a_key(&d)
  338. case 'Q':
  339. choice = -1
  340. break
  341. case 'C':
  342. z := 0
  343. a := 10 / z
  344. z = a
  345. }
  346. }
  347. d.Write(door.Reset + door.CRNL)
  348. message = fmt.Sprintf("Returning to the %s BBS..."+door.CRNL, d.Config.BBSID)
  349. d.Write(message)
  350. d.WaitKey(1, 0)
  351. left = d.TimeLeft()
  352. message = fmt.Sprintf("You had %0.2f minutes / %0.2f seconds remaining!"+door.CRNL, left.Minutes(), left.Seconds())
  353. d.Write(message)
  354. left = d.TimeUsed()
  355. d.Write(fmt.Sprintf("You used %0.2f seconds / %0.2f minutes."+door.CRNL, left.Seconds(), left.Minutes()))
  356. fmt.Println("Ending testdoor.go")
  357. }