testdoor.go 13 KB

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