testdoor.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package main
  2. import (
  3. "fmt"
  4. "red-green/door"
  5. "strconv"
  6. "strings"
  7. "time"
  8. )
  9. func pctUpdate(pct *int64) func() int64 {
  10. return func() int64 {
  11. return *pct
  12. }
  13. }
  14. // Can I add a function to Door?
  15. // NO: cannot define new methods on non-local type door.Door
  16. /*
  17. func (d *door.Door) PressAKey() {
  18. d.Write(door.Reset + door.CRNL + "Press a key to continue...")
  19. d.Key()
  20. d.Write(door.CRNL)
  21. }
  22. */
  23. func press_a_key(d *door.Door) int {
  24. d.Write(door.Reset + door.CRNL + "Press a key to continue...")
  25. k := d.Key()
  26. d.Write(door.CRNL)
  27. return k
  28. }
  29. func MainMenu() door.Menu {
  30. // Make the main menu
  31. m := door.Menu{Panel: door.Panel{Width: 45,
  32. X: 5,
  33. Y: 5,
  34. Style: door.DOUBLE,
  35. Title: "[ Main Menu: ]",
  36. TitleOffset: 3,
  37. BorderColor: door.ColorText("BRI CYAN ON BLA")}}
  38. m.SelectedR = door.MakeMenuRender(door.ColorText("BOLD CYAN"),
  39. door.ColorText("BOLD BLUE"),
  40. door.ColorText("BOLD CYAN"),
  41. door.ColorText("BOLD BLUE"))
  42. m.UnselectedR = door.MakeMenuRender(door.ColorText("BOLD YEL ON BLUE"),
  43. door.ColorText("BOLD WHI ON BLUE"),
  44. door.ColorText("BOLD YEL ON BLUE"),
  45. door.ColorText("BOLD CYAN ON BLUE"))
  46. m.AddSelection("A", "ANSI Display")
  47. m.AddSelection("D", "Display Information (dropfile, screen)")
  48. m.AddSelection("I", "Input Prompt Demo")
  49. m.AddSelection("P", "Progress Bars Demo")
  50. m.AddSelection("S", "Show Panel")
  51. m.AddSelection("Q", "Quit")
  52. return m
  53. }
  54. func display_information(d *door.Door) {
  55. d.Write(door.Clrscr)
  56. keyColor := door.ColorText("BRI GREEN")
  57. sepColor := door.ColorText("BRI YEL")
  58. valColor := door.ColorText("CYAN")
  59. nice_format := func(key string, value string) string {
  60. return fmt.Sprintf("%s%-20s %s: %s%s", keyColor, key, sepColor, valColor, value) + door.CRNL
  61. }
  62. d.Write(nice_format("BBS Software", d.Config.BBSID))
  63. d.Write(nice_format("Real Name", d.Config.Real_name))
  64. d.Write(nice_format("Handle", d.Config.Handle))
  65. d.Write(nice_format("User #", strconv.Itoa(d.Config.User_number)))
  66. d.Write(nice_format("Security Level", strconv.Itoa(d.Config.Security_level)))
  67. d.Write(nice_format("Node #", strconv.Itoa(d.Config.Node)))
  68. d.Write(nice_format("Unicode", strconv.FormatBool(door.Unicode)))
  69. d.Write(nice_format("CP437", strconv.FormatBool(door.CP437)))
  70. d.Write(nice_format("Screen Size", fmt.Sprintf("%d X %d", door.Width, door.Height)))
  71. }
  72. func display_ansi(d *door.Door) {
  73. space := SPACE()
  74. d.Write(door.Clrscr)
  75. for _, line := range space {
  76. if door.Unicode {
  77. d.Write(door.CP437_to_Unicode(line) + door.CRNL)
  78. } else {
  79. d.Write(line + door.CRNL)
  80. }
  81. }
  82. }
  83. func input_demo(d *door.Door) {
  84. inputColor := door.ColorText("BRI WHI ON BLUE")
  85. inputColor2 := door.ColorText("BRI WHI ON GREEN")
  86. prompt := door.Line{Text: "What is YOUR Name: "}
  87. prompt.RenderF = door.RenderBlueYellow
  88. d.Write(prompt.Output() + inputColor)
  89. name := d.Input(25)
  90. d.Write(door.Reset + door.CRNL)
  91. prompt.Text = "What is Your Quest: "
  92. d.Write(prompt.Output() + inputColor2)
  93. quest := d.Input(35)
  94. d.Write(door.Reset + door.CRNL)
  95. prompt.Text = "What is your Favorite CoLoR: "
  96. d.Write(prompt.Output() + inputColor2)
  97. color := d.Input(15)
  98. d.Write(door.Reset + door.CRNL)
  99. d.Write(fmt.Sprintf("You're %s on the %s quest, and fond of %s."+door.CRNL, name, quest, color))
  100. }
  101. func progress_bars(d *door.Door) {
  102. d.Write(door.Clrscr)
  103. bar := door.BarLine{Line: door.Line{DefaultColor: door.ColorText("BOLD YELLOW")}, Width: 20, Style: door.HALF_STEP}
  104. bar2 := door.BarLine{Width: 30, Style: door.PERCENT_SPACE}
  105. bar2.ColorRange = []door.BarRange{
  106. {2500, door.ColorText("RED")},
  107. {5000, door.ColorText("BROWN")},
  108. {7500, door.ColorText("BOLD YEL")},
  109. {9500, door.ColorText("GREEN")},
  110. {10100, door.ColorText("BRI GRE")}}
  111. bar3 := door.BarLine{Width: 15, Style: door.GRADIENT, Line: door.Line{DefaultColor: door.ColorText("CYAN")}}
  112. var percentage int64
  113. bar.UpdateP = pctUpdate(&percentage)
  114. bar2.UpdateP = pctUpdate(&percentage)
  115. bar3.UpdateP = pctUpdate(&percentage)
  116. update_bars := func() {
  117. bar.Update()
  118. bar2.Update()
  119. bar3.Update()
  120. }
  121. d.Write(door.Goto(3, 12) + "Half-Step")
  122. d.Write(door.Goto(25, 12) + "% with space and Color Range")
  123. d.Write(door.Goto(57, 12) + "Gradient")
  124. bar_start := door.Goto(3, 15)
  125. for f := 0; f <= 100; f++ {
  126. percentage = int64(f * 100)
  127. update_bars()
  128. d.Write(bar_start + bar.Output() + " " + door.Reset + bar2.Output() + door.Reset + " " + bar3.Output())
  129. if d.Disconnected {
  130. // don't continue to sleep if we're disconnected
  131. break
  132. }
  133. time.Sleep(time.Millisecond * 100)
  134. }
  135. }
  136. func panel_demo(d *door.Door) {
  137. width := 55
  138. fmtStr := "%-55s"
  139. p := door.Panel{X: 5, Y: 5, Width: width, Style: door.DOUBLE, BorderColor: door.ColorText("CYAN ON BLUE"), Title: "[ Panel Demo ]"}
  140. lineColor := door.ColorText("BRIGHT WHI ON BLUE")
  141. // Add lines to the panel
  142. for _, line := range []string{"The BBS Door Panel Demo", "(C) 2021 Red Green Software, https://red-green.com"} {
  143. if door.Unicode {
  144. line = strings.Replace(line, "(C)", "\u00a9", -1)
  145. }
  146. l := door.Line{Text: fmt.Sprintf(fmtStr, line), DefaultColor: lineColor}
  147. p.Lines = append(p.Lines, l)
  148. }
  149. p.Lines = append(p.Lines, p.Spacer())
  150. p.Lines = append(p.Lines, door.Line{Text: fmt.Sprintf(fmtStr, "Welcome to golang!"), DefaultColor: lineColor})
  151. d.Write(door.Clrscr)
  152. d.Write(p.Output())
  153. }
  154. func main() {
  155. var message string
  156. fmt.Println("Starting testdoor.go")
  157. d := door.Door{}
  158. d.Init()
  159. bold := door.Color(1, 37, 40)
  160. bolder := door.ColorText("BLI BOLD YEL ON BLUE")
  161. d.Write("Welcome to " + bolder + "door32.sys" + door.Reset + door.CRNL + "..." + door.CRNL)
  162. key := press_a_key(&d)
  163. d.Write(fmt.Sprintf("Key %s%d / %x%s", bold, key, key, door.Reset) + door.CRNL)
  164. b := door.AlertBox("Warning: golang is in use!", 1)
  165. d.Write(door.ColorText("BRI WHI ON GREEN"))
  166. for _, line := range b {
  167. d.Write(line + door.CRNL)
  168. }
  169. d.Write(door.Reset + door.CRNL)
  170. left := d.TimeLeft()
  171. message = fmt.Sprintf("You have %0.2f minutes / %0.2f seconds remaining..."+door.CRNL, left.Minutes(), left.Seconds())
  172. d.Write(message)
  173. press_a_key(&d)
  174. mainmenu := MainMenu()
  175. var choice int
  176. for choice >= 0 {
  177. d.Write(door.Clrscr)
  178. choice = mainmenu.Choose(&d)
  179. if choice < 0 {
  180. break
  181. }
  182. option := mainmenu.GetOption(choice)
  183. // fmt.Printf("Choice: %d, Option: %c\n", choice, option)
  184. switch option {
  185. case 'A':
  186. display_ansi(&d)
  187. press_a_key(&d)
  188. case 'D':
  189. display_information(&d)
  190. press_a_key(&d)
  191. case 'I':
  192. d.Write(door.Reset + door.CRNL + door.CRNL)
  193. input_demo(&d)
  194. press_a_key(&d)
  195. case 'P':
  196. progress_bars(&d)
  197. press_a_key(&d)
  198. case 'S':
  199. panel_demo(&d)
  200. press_a_key(&d)
  201. case 'Q':
  202. choice = -1
  203. break
  204. }
  205. }
  206. d.Write(door.Reset + door.CRNL)
  207. message = fmt.Sprintf("Returning to the %s BBS..."+door.CRNL, d.Config.BBSID)
  208. d.Write(message)
  209. d.WaitKey(1, 0)
  210. left = d.TimeLeft()
  211. message = fmt.Sprintf("You had %0.2f minutes / %0.2f seconds remaining!"+door.CRNL, left.Minutes(), left.Seconds())
  212. d.Write(message)
  213. left = d.TimeUsed()
  214. d.Write(fmt.Sprintf("You used %0.2f seconds / %0.2f minutes."+door.CRNL, left.Seconds(), left.Minutes()))
  215. fmt.Println("Ending testdoor.go")
  216. }