testdoor.go 14 KB

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