testdoor.go 12 KB

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