testdoor.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "log"
  6. "os"
  7. "red-green/door"
  8. "runtime/debug"
  9. "runtime/pprof"
  10. "time"
  11. // "net/http"
  12. // _ "net/http/pprof"
  13. )
  14. const ENABLE_PROFILER bool = false
  15. // "runtime/pprof"
  16. // Max X value we can use before hitting MemStats panel.
  17. var MaxX int = 0
  18. func press_keys(d *door.Door) {
  19. d.WriteA(door.Reset, door.CRNL, "Press some keys... <ENTER> to exit.")
  20. var r rune
  21. var ex door.Extended
  22. var err error
  23. for (r != 0x0d) && (err == nil) {
  24. r, ex, err = d.WaitKey(door.Inactivity)
  25. if ex == door.MOUSE {
  26. m, ok := d.GetMouse()
  27. if ok {
  28. // var m door.MouseInfo = door.Mouse
  29. d.WriteA(fmt.Sprintf("M %d (%d,%d) ", m.Button, m.X, m.Y))
  30. }
  31. } else {
  32. if ex == door.NOP {
  33. d.WriteA(fmt.Sprintf("%d (%x) ", r, r))
  34. } else {
  35. d.WriteA(fmt.Sprintf("<%s> ", ex.String()))
  36. }
  37. }
  38. }
  39. d.WriteA(door.Reset, door.CRNL)
  40. }
  41. func press_a_key(d *door.Door) error {
  42. var err error
  43. var ex door.Extended
  44. d.WriteA(door.Reset, door.CRNL, "Press a key, or LEFT mouse click to continue...")
  45. for {
  46. _, ex, err = d.WaitKey(door.Inactivity)
  47. if ex == door.MOUSE {
  48. m, ok := d.GetMouse()
  49. if ok {
  50. if m.Button == 1 {
  51. break
  52. }
  53. }
  54. } else {
  55. break
  56. }
  57. }
  58. d.WriteA(door.CRNL)
  59. return err
  60. }
  61. func main() {
  62. var message string
  63. if ENABLE_PROFILER {
  64. fp1, err1 := os.Create("cpu.pro")
  65. if err1 != nil {
  66. log.Fatal(err1)
  67. }
  68. pprof.StartCPUProfile(fp1)
  69. defer pprof.StopCPUProfile()
  70. defer func() {
  71. fp2, err2 := os.Create("memory.pro")
  72. if err2 != nil {
  73. log.Fatal(err2)
  74. }
  75. pprof.WriteHeapProfile(fp2)
  76. }()
  77. }
  78. /*
  79. go func() {
  80. http.ListenAndServe(":6060", nil)
  81. }()
  82. */
  83. var d door.Door = door.Door{}
  84. d.Init("testdoor")
  85. defer func() {
  86. if err := recover(); err != nil {
  87. // This displays stack trace stderr
  88. debug.PrintStack()
  89. fmt.Println("ERROR:", err)
  90. log.Println("FAILURE:", err)
  91. // Display error to user
  92. d.WriteA(fmt.Sprintf(door.Reset+door.CRNL+"Exception: %v"+door.CRNL, err))
  93. }
  94. }()
  95. defer d.Close()
  96. // Updaters work best when the screen doesn't scroll, so start
  97. // us off at the very top.
  98. d.WriteA(door.Clrscr)
  99. // Start spinrite effects
  100. var ticker *time.Ticker = time.NewTicker(time.Millisecond * time.Duration(100)) // 100
  101. var spin door.SpinRiteMsg = door.SpinRiteMsgInit(15, 5,
  102. door.ColorText("RED ON GREEN"),
  103. []string{"RED", "GREEN", "SOFTWARE"})
  104. /*
  105. var spin2 door.SpinRite = door.SpinRiteInit(13, 5,
  106. door.ColorText("BRI CYA ON BLUE"))
  107. */
  108. // Add in the GoRoutine Status panel
  109. MaxX = door.Width - 20
  110. var goPanel door.Panel = door.Panel{X: door.Width - 19,
  111. Y: 3,
  112. Width: 16,
  113. Style: door.DOUBLE,
  114. Title: "] GoRuntime [",
  115. BorderColor: door.ColorText("BOLD YELL"),
  116. }
  117. const DisplayGoRoutines bool = true
  118. if DisplayGoRoutines {
  119. goLineUpdater := func(u *bytes.Buffer) {
  120. u.Reset()
  121. u.Write(GoRoutinesStatus())
  122. // log.Println(status)
  123. }
  124. var goLine *door.Line = &door.Line{
  125. Text: &bytes.Buffer{},
  126. UpdateF: goLineUpdater,
  127. Width: goPanel.Width,
  128. DefaultColor: door.ColorText("CYAN"),
  129. }
  130. // goLine.Update() // Force Update
  131. goPanel.Lines = append(goPanel.Lines, goLine)
  132. }
  133. // Display Legend on GoRuntime panel
  134. const DisplayGoRoutineLegend bool = false
  135. // Display Memory
  136. const DisplayMemory bool = true
  137. var MemoryStats map[string]*bytes.Buffer = make(map[string]*bytes.Buffer)
  138. if DisplayGoRoutineLegend {
  139. // Line for legend.
  140. goPanel.Lines = append(goPanel.Lines, &door.Line{Width: goPanel.Width})
  141. }
  142. if DisplayMemory {
  143. memColor := door.ColorText("BRI BLUE")
  144. memoryUpdater := func(u *bytes.Buffer) {
  145. if u == nil {
  146. u = &bytes.Buffer{}
  147. }
  148. u.Reset()
  149. Memory(&MemoryStats)
  150. fmt.Fprintf(u, "%-8s%8s", "Sys", MemoryStats["Sys"].Bytes())
  151. }
  152. // memoryUpdater(&bytes.Buffer{})
  153. goPanel.Lines = append(goPanel.Lines, &door.Line{UpdateF: memoryUpdater,
  154. Width: goPanel.Width,
  155. DefaultColor: memColor})
  156. heapUpdater := func(u *bytes.Buffer) {
  157. if u == nil {
  158. u = &bytes.Buffer{}
  159. }
  160. u.Reset()
  161. fmt.Fprintf(u, "%-8s%8s", "HeapSys", MemoryStats["Heap"].Bytes())
  162. }
  163. goPanel.Lines = append(goPanel.Lines, &door.Line{UpdateF: heapUpdater,
  164. Width: goPanel.Width,
  165. DefaultColor: memColor})
  166. stackUpdater := func(u *bytes.Buffer) {
  167. u.Reset()
  168. fmt.Fprintf(u, "%-8s%8s", "StackSys", MemoryStats["StackSys"].Bytes())
  169. }
  170. goPanel.Lines = append(goPanel.Lines, &door.Line{UpdateF: stackUpdater,
  171. Width: goPanel.Width,
  172. DefaultColor: memColor})
  173. gcUpdater := func(u *bytes.Buffer) {
  174. u.Reset()
  175. fmt.Fprintf(u, "%-8s%8s", "NumGC", MemoryStats["NumGC"].Bytes())
  176. }
  177. goPanel.Lines = append(goPanel.Lines, &door.Line{UpdateF: gcUpdater,
  178. Width: goPanel.Width,
  179. DefaultColor: memColor})
  180. }
  181. var lIndex int = 0
  182. var legendCount int = 0
  183. const legendUpdate = 20
  184. go func() {
  185. defer func() {
  186. r := recover()
  187. if r != nil {
  188. log.Printf("** FAIL: %s, %#v\n", r, r)
  189. }
  190. }()
  191. // var output string
  192. var legend []string = []string{
  193. "R=run r=Runnable",
  194. "S=Select s=Syscall",
  195. "Chan <read >write",
  196. "Z=Sleep P=Preempt",
  197. "I=Idle D=Dead",
  198. "W=Wait C=Copystk",
  199. }
  200. if DisplayGoRoutineLegend {
  201. goPanel.Lines[1].Text = bytes.NewBuffer([]byte(legend[0]))
  202. }
  203. for range ticker.C {
  204. // output = door.SavePos + door.Goto(door.Width-16, 1) + spin.Output() +
  205. // door.Goto(door.Width-15, 3) + spin2.Output() + door.RestorePos
  206. if DisplayGoRoutineLegend {
  207. legendCount++
  208. if legendCount >= legendUpdate {
  209. legendCount = 0
  210. lIndex++
  211. if lIndex == len(legend) {
  212. lIndex = 0
  213. }
  214. goPanel.Lines[1].Text = bytes.NewBuffer([]byte(legend[lIndex]))
  215. }
  216. }
  217. if goPanel.X == 0 {
  218. goPanel.X = door.Width - 40
  219. }
  220. goPanel.Update()
  221. //output = door.Goto(door.Width-16, 1) + spin.Output() + goPanel.Output()
  222. /*
  223. +
  224. door.Goto(door.Width-15, 3) + spin2.Output()
  225. */
  226. if !d.Disconnect() {
  227. // d.Update(output)
  228. d.WriteA(door.SavePos, door.Goto(door.Width-16, 1), spin.Output(), goPanel.Output(), door.RestorePos)
  229. } else {
  230. log.Println("Ticker.Stop: Disconnected!")
  231. ticker.Stop()
  232. return
  233. }
  234. }
  235. log.Println("range Ticker.C stopped.")
  236. }()
  237. var wopr door.WOPR
  238. wopr.Init(d.StartTime, d.TimeOut, "")
  239. wopr.ElapsedPanel.X = door.Width - 19
  240. wopr.ElapsedPanel.Y = door.Height - 15
  241. wopr.RemainingPanel.X = door.Width - 19
  242. wopr.RemainingPanel.Y = door.Height - 8
  243. wopr.Animate(&d)
  244. // bold := door.Color(1, 37, 40)
  245. var bolder string = door.ColorText("BOLD YEL ON BLUE")
  246. d.WriteA("Welcome to ", bolder, "go door go TestDoor.", door.Reset, door.CRNL, "...", door.CRNL)
  247. d.EnableMouse(door.Normal)
  248. press_a_key(&d)
  249. d.WriteA(door.CRNL)
  250. var b []string
  251. if door.CP437 {
  252. b = door.AlertBox("Alert: go \xfb is in use!", 1)
  253. } else {
  254. b = door.AlertBox("Alert: go \u221a is in use!", 1)
  255. }
  256. warningColor := door.ColorText("BRI WHI ON GREEN")
  257. for _, line := range b {
  258. // Prevent color bleeding.
  259. d.WriteA(warningColor, line, door.Reset, door.CRNL)
  260. }
  261. d.WriteA(door.Reset, door.CRNL)
  262. var left time.Duration = d.TimeLeft()
  263. message = fmt.Sprintf("You have %0.2f minutes / %0.2f seconds remaining..."+door.CRNL, left.Minutes(), left.Seconds())
  264. d.WriteA(message)
  265. press_a_key(&d)
  266. var mainmenu door.Menu = MainMenu()
  267. var choice int
  268. for choice >= 0 {
  269. d.WriteA(door.Clrscr, door.HideCursor)
  270. choice = mainmenu.Choose(&d)
  271. d.WriteA(door.ShowCursor)
  272. if choice < 0 {
  273. break
  274. }
  275. option := mainmenu.GetOption(choice)
  276. wopr.Stop()
  277. // Clear WOPR panels.
  278. d.WriteA(door.Reset, wopr.Clear())
  279. r, b := mainmenu.Panel.RightBottomPos()
  280. d.WriteA(door.Goto(r, b))
  281. // fmt.Printf("Choice: %d, Option: %c\n", choice, option)
  282. switch option {
  283. case 'A':
  284. display_ansi(&d)
  285. press_a_key(&d)
  286. case 'D':
  287. display_information(&d)
  288. press_a_key(&d)
  289. case 'F':
  290. font_demo(&d)
  291. press_a_key(&d)
  292. case 'I':
  293. d.WriteA(door.Reset, door.CRNL, door.CRNL)
  294. input_demo(&d)
  295. press_a_key(&d)
  296. case 'M':
  297. // Why is this so far down on the screen? (Scrolls)
  298. d.WriteA(door.Reset, door.CRNL, "TO DO: Provide menu of options to select from...", door.CRNL)
  299. press_a_key(&d)
  300. case 'P':
  301. progress_bars(&d)
  302. press_a_key(&d)
  303. case 'S':
  304. panel_demo(&d)
  305. press_a_key(&d)
  306. case 'T':
  307. about_test_door(&d)
  308. press_a_key(&d)
  309. case 'W':
  310. width_demo(&d)
  311. case 'Q':
  312. // This is also far down on the screen ...
  313. choice = -1
  314. case 'C': // Disabled
  315. var a, z int
  316. z = 0
  317. a = 10 / z
  318. z = a
  319. _ = a
  320. _ = z
  321. }
  322. wopr.Animate(&d)
  323. }
  324. // d.Write("\x1b[?1000l") // disable mouse
  325. // d.Write("\x1b[?1002l")
  326. d.DisableMouse()
  327. d.WriteA(door.Reset, door.CRNL)
  328. if d.Config.BBSID != "" {
  329. message = fmt.Sprintf("Returning to the %s BBS..."+door.CRNL, d.Config.BBSID)
  330. } else {
  331. message = "Returning to the BBS..." + door.CRNL
  332. }
  333. d.WriteA(message)
  334. d.WaitKey(time.Second)
  335. left = d.TimeLeft()
  336. ticker.Stop()
  337. message = fmt.Sprintf("You had %0.2f minutes remaining!"+door.CRNL, left.Minutes())
  338. d.WriteA(message)
  339. left = d.TimeUsed()
  340. d.WriteA(fmt.Sprintf("You used %0.2f seconds / %0.2f minutes."+door.CRNL, left.Seconds(), left.Minutes()))
  341. fmt.Println("Ending testdoor.go")
  342. }