testdoor.go 15 KB

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