testdoor.go 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "red-green/door"
  6. "runtime/debug"
  7. "strconv"
  8. "strings"
  9. "time"
  10. // "net/http"
  11. // _ "net/http/pprof"
  12. "github.com/mitchellh/go-wordwrap"
  13. )
  14. //go:generate sh -c "font-util extract -f 'Amazon Cyan,Serpent,Unchained,Asylum,ArmageonRed,BrainDmgBlu,Boner,Descent,Remorse,Dungeon' ../TDFONTS.TDF ../TDFONTS2.TDF ../TDFONTS9.TDF > fonts.go"
  15. //go:generate sh -c "font-util extract -f Armageddon -c 7,1 -c 4,2 ../TDFONTS2.TDF > rgfont.go; sed -i 's/Armageddon/RedGreen/g' rgfont.go"
  16. func pctUpdate(pct *int64) func() int64 {
  17. return func() int64 {
  18. return *pct
  19. }
  20. }
  21. func press_keys(d *door.Door) {
  22. d.Write(door.Reset + door.CRNL + "Press some keys... <ENTER> to exit.")
  23. var r rune
  24. var ex door.Extended
  25. var err error
  26. for (r != 0x0d) && (err == nil) {
  27. r, ex, err = d.WaitKey(door.Inactivity)
  28. if ex == door.MOUSE {
  29. m, ok := d.GetMouse()
  30. if ok {
  31. // var m door.MouseInfo = door.Mouse
  32. d.Write(fmt.Sprintf("M %d (%d,%d) ", m.Button, m.X, m.Y))
  33. }
  34. } else {
  35. if ex == door.NOP {
  36. d.Write(fmt.Sprintf("%d (%x) ", r, r))
  37. } else {
  38. d.Write(fmt.Sprintf("<%s> ", ex.String()))
  39. }
  40. }
  41. }
  42. d.Write(door.Reset + door.CRNL)
  43. }
  44. func press_a_key(d *door.Door) error {
  45. var err error
  46. var ex door.Extended
  47. d.Write(door.Reset + door.CRNL + "Press a key, or LEFT mouse click to continue...")
  48. for {
  49. _, ex, err = d.WaitKey(door.Inactivity)
  50. if ex == door.MOUSE {
  51. m, ok := d.GetMouse()
  52. if ok {
  53. if m.Button == 1 {
  54. break
  55. }
  56. }
  57. } else {
  58. break
  59. }
  60. }
  61. d.Write(door.CRNL)
  62. return err
  63. }
  64. func about_test_door(d *door.Door) {
  65. var W int = 60
  66. var center_x int = (door.Width - W) / 2
  67. var center_y int = (door.Height - 16) / 2
  68. var about door.Panel = door.Panel{X: center_x,
  69. Y: center_y,
  70. Width: W,
  71. Style: door.SINGLE_DOUBLE,
  72. BorderColor: door.ColorText("BOLD YELLOW ON BLUE"),
  73. }
  74. about.Lines = append(about.Lines, door.Line{Text: fmt.Sprintf("%*s", -W, "About This Door"),
  75. DefaultColor: door.ColorText("BOLD CYAN ON BLUE")})
  76. about.Lines = append(about.Lines, about.Spacer())
  77. about.Lines = append(about.Lines, door.Line{Text: fmt.Sprintf("%*s", -W, "Test Door written in go, using go door.")})
  78. var copyright string = "(C) 2022 Bugz, Red Green Software"
  79. if door.Unicode {
  80. copyright = strings.Replace(copyright, "(C)", "\u00a9", -1)
  81. }
  82. about.Lines = append(about.Lines,
  83. door.Line{Text: copyright, Width: W,
  84. DefaultColor: door.ColorText("BOLD WHITE ON BLUE")})
  85. for _, text := range []string{"",
  86. "This door was written by Bugz.",
  87. "",
  88. "It is written in Go, detects CP437/unicode, detects screen",
  89. "size, supports door.sys & door32.sys, supports TheDraw Fonts",
  90. "(the fonts are compiled into the door), has NoMoreSecrets",
  91. "and SpinRite effects, and runs on Linux",
  92. "and sometimes even Windows..."} {
  93. about.Lines = append(about.Lines, door.Line{Text: text, Width: W})
  94. }
  95. var better door.NoMoreSecretsConfig = door.NoMoreSecretsDefault
  96. better.Jumble_Loop_Speed = 75 // 35
  97. better.Reveal_Loop_Speed = 100 // 50
  98. better.Color = door.ColorText("BRI CYAN ON BLUE")
  99. door.NoMoreSecrets(about.Output(), d, &better)
  100. }
  101. func MainMenu() door.Menu {
  102. // Make the main menu
  103. // Width was 45
  104. var menu door.Menu = door.Menu{Panel: door.Panel{Width: 35,
  105. X: 2,
  106. Y: 5,
  107. Style: door.DOUBLE,
  108. Title: "[ Main Menu: ]",
  109. TitleOffset: 3,
  110. BorderColor: door.ColorText("BRI CYAN ON BLA")}}
  111. menu.SelectedR = door.MakeMenuRender(door.ColorText("BOLD CYAN"),
  112. door.ColorText("BOLD BLUE"),
  113. door.ColorText("BOLD WHITE"),
  114. door.ColorText("BOLD CYAN"))
  115. menu.UnselectedR = door.MakeMenuRender(door.ColorText("BOLD YEL ON BLUE"),
  116. door.ColorText("BOLD WHI ON BLUE"),
  117. door.ColorText("BOLD YEL ON BLUE"),
  118. door.ColorText("BOLD CYAN ON BLUE"))
  119. menu.AddSelection("A", "ANSI Display")
  120. // m.AddSelection("C", "Crash")
  121. menu.AddSelection("D", "Display Information")
  122. menu.AddSelection("F", "Font Demo")
  123. menu.AddSelection("I", "Input Prompt Demo")
  124. menu.AddSelection("M", "Menu Demo")
  125. menu.AddSelection("P", "Progress Bars Demo")
  126. menu.AddSelection("S", "Show Panel")
  127. menu.AddSelection("T", "Test Door About")
  128. menu.AddSelection("W", "Screen Width")
  129. menu.AddSelection("Q", "Quit")
  130. var descriptions []string = []string{
  131. // 12345678901234567890123456789012345678901234567890
  132. "Display an ANSI file. It is compiled into the door itself.",
  133. // "Crash go, see a handled error.", // The error shows up in the logs.
  134. "Display dropfile information.",
  135. "Display TheDraw Fonts. Font information is compiled into the door.",
  136. "Input some values, while updating the time.",
  137. "Isn't this is a menu?",
  138. "Display progress bar styles: half step, display percentage, and gradient.",
  139. "Show multiple panels. Panels can be mouse drag/drop around.",
  140. "Show about the door, using NoMoreSecrets effect.",
  141. "Examples using the full width of the screen.",
  142. "Exit this door.",
  143. }
  144. var widthLeft int = door.Width - (menu.Width + menu.X + 2)
  145. var panelWidth int = widthLeft - (2 + 2)
  146. var maxLines int = 1
  147. var maxLineLength int
  148. // Calculate the max lines needed for each line.
  149. for _, line := range descriptions {
  150. var wrapped string = wordwrap.WrapString(line, uint(panelWidth))
  151. var lines int = len(strings.Split(wrapped, "\n"))
  152. if lines > maxLines {
  153. maxLines = lines
  154. }
  155. if len(line) > maxLineLength {
  156. maxLineLength = len(line)
  157. }
  158. }
  159. if maxLines == 1 {
  160. // Ok! Everything fits into one line, SO use max line length as width of panel.
  161. panelWidth = maxLineLength
  162. }
  163. // Position the description panel beside the menu. m.Width + m.X + 2 (1 one to give it a space)
  164. var descPanel door.Panel = door.Panel{X: menu.Width + menu.X + 2 + 1, Y: menu.Y, Width: panelWidth, Style: door.SINGLE, BorderColor: door.ColorText("WHI ON BLU")}
  165. for x := 0; x < maxLines; x++ {
  166. descPanel.Lines = append(descPanel.Lines, door.Line{Text: "", Width: panelWidth})
  167. }
  168. menu.Activated = func(item int, d *door.Door) {
  169. var line string = descriptions[item]
  170. var lines []string = strings.Split(wordwrap.WrapString(line, uint(panelWidth)), "\n")
  171. for idx := range descPanel.Lines {
  172. if idx >= len(lines) {
  173. descPanel.Lines[idx].Text = ""
  174. } else {
  175. descPanel.Lines[idx].Text = lines[idx]
  176. }
  177. }
  178. d.Write(door.SavePos + descPanel.Output() + door.RestorePos)
  179. }
  180. return menu
  181. }
  182. func display_information(d *door.Door) {
  183. d.Write(door.Clrscr)
  184. var headerColor string = door.ColorText("BRI CYAN")
  185. var keyColor string = door.ColorText("BRI GREEN")
  186. var sepColor string = door.ColorText("BRI YEL")
  187. var valColor string = door.ColorText("CYAN")
  188. var nice_format func(string, string) string = func(key string, value string) string {
  189. return fmt.Sprintf("%s%-20s %s: %s%s", keyColor, key, sepColor, valColor, value) + door.CRNL
  190. }
  191. var offset string
  192. var header string = "DropFile: "
  193. offset = strings.Repeat(" ", len(header))
  194. d.Write(headerColor + header)
  195. d.Write(nice_format("Comm Type", strconv.Itoa(d.Config.Comm_type)))
  196. if d.Config.BBSID != "" {
  197. d.Write(offset + nice_format("BBS Software", d.Config.BBSID))
  198. }
  199. d.Write(offset + nice_format("Time Left", strconv.Itoa(d.Config.Time_left)))
  200. d.Write(offset + nice_format("Real Name", d.Config.Real_name))
  201. // d.Write(nice_format("Comm Handle", strconv.Itoa(d.Config.Comm_handle)))
  202. d.Write(offset + nice_format("Handle", d.Config.Handle))
  203. d.Write(offset + nice_format("User #", strconv.Itoa(d.Config.User_number)))
  204. d.Write(offset + nice_format("Security Level", strconv.Itoa(d.Config.Security_level)))
  205. d.Write(offset + nice_format("Node #", strconv.Itoa(d.Config.Node)))
  206. header = "Detected: "
  207. offset = strings.Repeat(" ", len(header))
  208. d.Write(door.CRNL + headerColor + header)
  209. d.Write(nice_format("Unicode", strconv.FormatBool(door.Unicode)))
  210. d.Write(offset + nice_format("CP437", strconv.FormatBool(door.CP437)))
  211. d.Write(offset + nice_format("Full CP437", strconv.FormatBool(door.Full_CP437)))
  212. d.Write(offset + nice_format("Screen Size", fmt.Sprintf("%d X %d", door.Width, door.Height)))
  213. var time time.Duration = d.TimeLeft()
  214. d.Write(offset + nice_format("Door Time Left", fmt.Sprintf("%d Hours, %d Minutes, %d Seconds", int(time.Hours()), int(time.Minutes())%60, int(time.Seconds())%60)))
  215. time = d.TimeUsed()
  216. d.Write(offset + nice_format("Door Time Used", fmt.Sprintf("%d Minutes, %d Seconds", int(time.Minutes()), int(time.Seconds())%60)))
  217. press_a_key(d)
  218. d.Write(door.Clrscr + door.CRNL + door.CRNL + door.CRNL)
  219. modules := GetModules()
  220. header = "Build: "
  221. offset = strings.Repeat(" ", len(header))
  222. d.Write(headerColor + header)
  223. gover, gitver, goarch, goos := GetVersion()
  224. d.Write(nice_format("go version", gover))
  225. d.Write(offset + nice_format("git commit", gitver))
  226. d.Write(offset + nice_format("Arch", goarch))
  227. d.Write(offset + nice_format("OS", goos))
  228. for mod, version := range modules {
  229. d.Write(offset + nice_format(mod, version))
  230. }
  231. }
  232. func display_ansi(d *door.Door) {
  233. var art []string = ANSIGrowl()
  234. d.Write(door.Clrscr)
  235. for _, line := range art {
  236. d.Write(line + door.CRNL)
  237. }
  238. }
  239. func font_demo(d *door.Door) {
  240. var output []string
  241. var l int
  242. var centering string
  243. var now time.Time = time.Now()
  244. // I have checks here (but not all are complete), that verify the font
  245. // output doesn't exceed the size of the screen.
  246. d.Write(door.Clrscr + door.CRNL) // + door.CRNL + door.CRNL)
  247. var fac door.ColorFont = FontAmazonCyan()
  248. output, l = fac.Output(now.Format("Jan Mon"))
  249. // Size check: Is this too big for the screen?
  250. /*
  251. if l > door.Width {
  252. output, l = fac.Output("Jan")
  253. }
  254. */
  255. if l < door.Width {
  256. centering = ""
  257. // centering = strings.Repeat(" ", (door.Width-l)/2)
  258. for _, o := range output {
  259. d.Write(fmt.Sprintf("%s%s%s", centering, o, door.Reset) + door.CRNL)
  260. }
  261. d.Write(door.CRNL)
  262. }
  263. var patch door.ColorMap = fac.Scan(6)
  264. // log.Printf("Patch: %#v\n", patch)
  265. fac.Modify(4, patch)
  266. output, l = fac.Output(now.Format("Monday"))
  267. centering = strings.Repeat(" ", (door.Width-l)/2)
  268. for _, o := range output {
  269. d.Write(fmt.Sprintf("%s%s%s", centering, o, door.Reset) + door.CRNL)
  270. }
  271. d.Write(door.CRNL)
  272. fac.Modify(1, patch)
  273. output, l = fac.Output(now.Format("January")) // 3:04:05 PM"))
  274. centering = strings.Repeat(" ", (door.Width-l)/2)
  275. for _, o := range output {
  276. d.Write(fmt.Sprintf("%s%s%s", centering, o, door.Reset) + door.CRNL)
  277. }
  278. d.Write(door.CRNL)
  279. press_a_key(d)
  280. // Anarchy Blue - no digits
  281. var fab door.ColorFont = FontSerpent()
  282. output, l = fab.Output("Date/Time:")
  283. if l < door.Width {
  284. centering = strings.Repeat(" ", (door.Width-l)/2)
  285. for _, o := range output {
  286. d.Write(centering + o + door.Reset + door.CRNL)
  287. }
  288. d.Write(door.CRNL)
  289. }
  290. var unchain door.ColorFont = FontUnchained()
  291. now = time.Now()
  292. output, l = unchain.Output(now.Format("01/02/2006"))
  293. if l < door.Width {
  294. centering = strings.Repeat(" ", (door.Width-l)/2)
  295. for _, o := range output {
  296. d.Write(centering + o + door.Reset + door.CRNL)
  297. }
  298. d.Write(door.CRNL)
  299. }
  300. output, l = unchain.Output(now.Format("3:04:05 PM"))
  301. if l > door.Width {
  302. output, l = unchain.Output("Meow")
  303. }
  304. if l < door.Width {
  305. centering = strings.Repeat(" ", (door.Width-l)/2)
  306. for _, o := range output {
  307. d.Write(centering + o + door.Reset + door.CRNL)
  308. }
  309. d.Write(door.CRNL)
  310. }
  311. press_a_key(d)
  312. var asylum door.ColorFont = FontAsylum()
  313. output, l = asylum.Output("Bugz ROCKS")
  314. if l > door.Width {
  315. output, l = asylum.Output("Aslym")
  316. }
  317. if l < door.Width {
  318. centering = strings.Repeat(" ", (door.Width-l)/2)
  319. for _, o := range output {
  320. d.Write(centering + o + door.Reset + door.CRNL)
  321. }
  322. d.Write(door.CRNL)
  323. }
  324. var brain door.ColorFont = FontBrainDmgBlu()
  325. output, l = brain.Output("I'm so BLUE")
  326. if l > door.Width {
  327. output, l = brain.Output("Blue")
  328. }
  329. if l < door.Width {
  330. centering = strings.Repeat(" ", (door.Width-l)/2)
  331. for _, o := range output {
  332. d.Write(centering + o + door.Reset + door.CRNL)
  333. }
  334. d.Write(door.CRNL)
  335. }
  336. var boner door.ColorFont = FontBoner()
  337. output, l = boner.Output("Welcome!")
  338. if l < door.Width {
  339. centering = strings.Repeat(" ", (door.Width-l)/2)
  340. for _, o := range output {
  341. d.Write(centering + o + door.Reset + door.CRNL)
  342. }
  343. d.Write(door.CRNL)
  344. }
  345. press_a_key(d)
  346. var descent door.ColorFont = FontDescent()
  347. output, l = descent.Output("Meanwhile...")
  348. if l > door.Width {
  349. output, l = descent.Output("BUGZ")
  350. }
  351. if l < door.Width {
  352. centering = strings.Repeat(" ", (door.Width-l)/2)
  353. for _, o := range output {
  354. d.Write(centering + o + door.Reset + door.CRNL)
  355. }
  356. d.Write(door.CRNL)
  357. }
  358. var remorse door.ColorFont = FontRemorse()
  359. output, l = remorse.Output("Enjoy the fonts")
  360. if l > door.Width {
  361. output, l = remorse.Output("Amazing")
  362. }
  363. if l < door.Width {
  364. centering = strings.Repeat(" ", (door.Width-l)/2)
  365. for _, o := range output {
  366. d.Write(centering + o + door.Reset + door.CRNL)
  367. }
  368. d.Write(door.CRNL)
  369. }
  370. var dungeon door.ColorFont = FontDungeon()
  371. output, l = dungeon.Output("Until NEXT time")
  372. if l > door.Width {
  373. output, l = dungeon.Output("Beware")
  374. }
  375. if l < door.Width {
  376. centering = strings.Repeat(" ", (door.Width-l)/2)
  377. for _, o := range output {
  378. d.Write(centering + o + door.Reset + door.CRNL)
  379. }
  380. d.Write(door.CRNL)
  381. }
  382. /*
  383. redgreen := FontArmageddon()
  384. white := redgreen.Scan(7)
  385. blue := redgreen.Scan(4)
  386. redgreen.Modify(1, white)
  387. redgreen.Modify(2, blue)
  388. */
  389. redgreen := FontRedGreen()
  390. output, l = redgreen.Output("Red-Green")
  391. if l < door.Width {
  392. press_a_key(d)
  393. centering = strings.Repeat(" ", (door.Width-l)/2)
  394. for _, o := range output {
  395. d.Write(centering + o + door.Reset + door.CRNL)
  396. }
  397. d.Write(door.CRNL)
  398. output, l = redgreen.Output("Software")
  399. centering = strings.Repeat(" ", (door.Width-l)/2)
  400. for _, o := range output {
  401. d.Write(centering + o + door.Reset + door.CRNL)
  402. }
  403. d.Write(door.CRNL)
  404. }
  405. }
  406. func input_demo(d *door.Door) {
  407. var ticker *time.Ticker = time.NewTicker(time.Second)
  408. var StopIt = make(chan bool)
  409. go func() {
  410. for {
  411. select {
  412. case <-StopIt:
  413. return
  414. case t := <-ticker.C:
  415. const tf = "January 2, 2006 03:04:05 PM MST"
  416. output := door.SavePos + door.Goto(5, 2) + door.ColorText("BRI WHI ON CYAN") + t.Format(tf) + door.RestorePos
  417. d.Write(output)
  418. }
  419. }
  420. }()
  421. var inputColor string = door.ColorText("BRI WHI ON BLUE")
  422. var inputColor2 string = door.ColorText("BRI WHI ON GREEN")
  423. var prompt door.Line = door.Line{Text: "What is YOUR Name: "}
  424. prompt.RenderF = door.RenderBlueYellow
  425. d.Write(prompt.Output() + inputColor)
  426. var name string = d.Input(25)
  427. d.Write(door.Reset + door.CRNL)
  428. prompt.Text = "What is Your Quest: "
  429. d.Write(prompt.Output() + inputColor2)
  430. var quest string = d.Input(35)
  431. d.Write(door.Reset + door.CRNL)
  432. prompt.Text = "What is your Favorite CoLoR: "
  433. d.Write(prompt.Output() + inputColor)
  434. var color string = d.Input(15)
  435. d.Write(door.Reset + door.CRNL)
  436. ticker.Stop()
  437. StopIt <- true
  438. d.Write(fmt.Sprintf("You're %s on the %s quest, and fond of %s."+door.CRNL, name, quest, color))
  439. }
  440. func progress_bars(d *door.Door) {
  441. d.Write(door.Clrscr)
  442. var bar door.BarLine = door.BarLine{Line: door.Line{DefaultColor: door.ColorText("BOLD YELLOW")}, Width: 20, Style: door.HALF_STEP}
  443. var bar2 door.BarLine = door.BarLine{Width: 30, Style: door.SOLID, PercentStyle: door.PERCENT_SPACE}
  444. bar2.ColorRange = []door.BarRange{
  445. {Percent: 2500, Color: door.ColorText("RED")},
  446. {Percent: 5000, Color: door.ColorText("BROWN")},
  447. {Percent: 7500, Color: door.ColorText("BOLD YEL")},
  448. {Percent: 9500, Color: door.ColorText("GREEN")},
  449. {Percent: 10100, Color: door.ColorText("BRI GRE")}}
  450. var bar3 door.BarLine = door.BarLine{Width: 15, Style: door.GRADIENT, Line: door.Line{DefaultColor: door.ColorText("CYAN")}}
  451. var percentage int64
  452. bar.UpdateP = pctUpdate(&percentage)
  453. bar2.UpdateP = pctUpdate(&percentage)
  454. bar3.UpdateP = pctUpdate(&percentage)
  455. update_bars := func() {
  456. bar.Update()
  457. bar2.Update()
  458. bar3.Update()
  459. }
  460. d.Write(door.Goto(3, 12) + "Half-Step")
  461. d.Write(door.Goto(25, 12) + "% with space and Color Range")
  462. d.Write(door.Goto(57, 12) + "Gradient")
  463. d.Write(door.HideCursor)
  464. bar_start := door.Goto(3, 15)
  465. for f := 0; f <= 100; f++ {
  466. d.Write(door.Goto(3, 10) + door.Reset + fmt.Sprintf("Value: %d", f))
  467. percentage = int64(f * 100)
  468. update_bars()
  469. d.Write(bar_start + bar.Output() + " " + door.Reset + bar2.Output() + door.Reset + " " + bar3.Output())
  470. if d.Disconnect() {
  471. // don't continue to sleep if we're disconnected
  472. break
  473. }
  474. time.Sleep(time.Millisecond * 100)
  475. }
  476. d.Write(door.ShowCursor)
  477. }
  478. func width_demo(d *door.Door) {
  479. var w int = door.Width
  480. var panel door.Panel = door.Panel{X: 1, Y: 1, Width: w}
  481. var lineColor string = door.ColorText("WHI")
  482. var line string
  483. for y := 1; y <= door.Height; y++ {
  484. if y%10 == 0 {
  485. line = strings.Repeat("1234567890", w/10)
  486. for x := len(line); x < w; x++ {
  487. line += strconv.Itoa((x + 1) % 10)
  488. }
  489. } else {
  490. line = ""
  491. for x := 1; x < w; x++ {
  492. if x%10 == 0 {
  493. line += strconv.Itoa(y % 10)
  494. } else {
  495. line += " "
  496. }
  497. }
  498. }
  499. var l door.Line = door.Line{Text: line, DefaultColor: lineColor}
  500. panel.Lines = append(panel.Lines, l)
  501. }
  502. var message string = fmt.Sprintf("Screen Size: %d X %d", door.Width, door.Height)
  503. d.Write(panel.Output())
  504. // Output alert on top of panel
  505. var cx, cy int
  506. cx = (door.Width - len(message) + 2) / 2
  507. cy = (door.Height - 3) / 2
  508. var alert []string = door.AlertBox(message, 1)
  509. d.Write(door.ColorText("BRI YEL ON BLUE"))
  510. for idx, ab := range alert {
  511. d.Write(door.Goto(cx, cy+idx) + ab)
  512. }
  513. d.Write(door.Reset + panel.GotoEnd())
  514. // Pause for key
  515. d.WaitKey(door.Inactivity)
  516. panel.Lines = make([]door.Line, 0)
  517. var background string = "BUGZ Test Door in GO "
  518. var bl int = len(background)
  519. for y := 1; y <= door.Height; y++ {
  520. offset := (y - 1) % bl
  521. line = background[offset:]
  522. for len(line) < w {
  523. if len(line)+bl <= w {
  524. line += background
  525. } else {
  526. line += background[0 : w-len(line)]
  527. }
  528. }
  529. var l door.Line = door.Line{Text: line, RenderF: door.RenderBlueYellow}
  530. panel.Lines = append(panel.Lines, l)
  531. }
  532. d.Write(panel.Output())
  533. d.WaitKey(door.Inactivity)
  534. }
  535. type TrackPanels struct {
  536. Panel *door.Panel
  537. XPos int
  538. YPos int
  539. BColor string
  540. }
  541. func FindPanel(m door.Mouse, panels []TrackPanels) int {
  542. for idx, p := range panels {
  543. hit, _, _ := p.Panel.Within(int(m.X), int(m.Y))
  544. if hit {
  545. return idx
  546. }
  547. }
  548. return -1
  549. }
  550. func panel_demo(d *door.Door) {
  551. var width int = 55
  552. var panel door.Panel = door.Panel{X: 5, Y: 5, Width: width, Style: door.DOUBLE, BorderColor: door.ColorText("CYAN ON BLUE"), Title: "[ Panel Demo ]"}
  553. var moveColor string = door.ColorText("CYAN ON BLACK")
  554. var lineColor string = door.ColorText("BRIGHT WHI ON BLUE")
  555. // Add lines to the panel
  556. for _, line := range []string{"The BBS Door Panel Demo", "(C) 2021 Red Green Software, https://red-green.com"} {
  557. if door.Unicode {
  558. line = strings.Replace(line, "(C)", "\u00a9", -1)
  559. }
  560. var l door.Line = door.Line{Text: line, Width: width, DefaultColor: lineColor}
  561. panel.Lines = append(panel.Lines, l)
  562. }
  563. panel.Lines = append(panel.Lines, panel.Spacer())
  564. panel.Lines = append(panel.Lines, door.Line{Text: "Welcome to golang!", Width: width, DefaultColor: lineColor})
  565. width = 10
  566. var single door.Panel = door.Panel{X: 6, Y: 12, Width: width, Style: door.SINGLE, BorderColor: door.ColorText("WHITE ON BLUE"), Title: "< Single >"}
  567. single.Lines = append(single.Lines, door.Line{Text: "Example", Width: width, DefaultColor: door.ColorText("WHI ON BLACK")})
  568. single.Lines = append(single.Lines, single.Spacer())
  569. single.Lines = append(single.Lines, door.Line{Text: "More Text", Width: width, DefaultColor: door.ColorText("BRI GREEN ON BLACK")})
  570. width = 15
  571. var double_single door.Panel = door.Panel{X: 26, Y: 12, Width: width, Style: door.DOUBLE_SINGLE, BorderColor: door.ColorText("BRI CYAN ON GREEN"), Title: "Double", TitleOffset: 3}
  572. double_single.Lines = append(double_single.Lines, door.Line{Text: "Double / Single", Width: width, DefaultColor: door.ColorText("BRI WHI ON GREEN")})
  573. double_single.Lines = append(double_single.Lines, double_single.Spacer())
  574. double_single.Lines = append(double_single.Lines, door.Line{Text: "Some Other Text", Width: width, DefaultColor: door.ColorText("BRI CYAN ON GREEN")})
  575. var single_double door.Panel = door.Panel{X: 46, Y: 12, Width: width, Style: door.SINGLE_DOUBLE, BorderColor: door.ColorText("BRI YELL ON RED")}
  576. single_double.Lines = append(single_double.Lines, door.Line{Text: "Single / Double", Width: width, DefaultColor: door.ColorText("BRI WHI ON RED")})
  577. single_double.Lines = append(single_double.Lines, single_double.Spacer())
  578. single_double.Lines = append(single_double.Lines, door.Line{Text: "Text Goes Here ", Width: width, DefaultColor: door.ColorText("BRI GREEN ON RED")})
  579. d.Write(door.Clrscr)
  580. d.Write(panel.Output())
  581. d.Write(single.Output())
  582. d.Write(double_single.Output())
  583. d.Write(single_double.Output())
  584. d.Write(door.Goto(1, 20) + door.Reset + "Use MOUSE to click/drag panels, Right-Click Exits, R to Reset, Q to quit...")
  585. var panels []TrackPanels = []TrackPanels{
  586. {&panel, panel.X, panel.Y, panel.BorderColor},
  587. {&single, single.X, single.Y, single.BorderColor},
  588. {&double_single, double_single.X, double_single.Y, single.BorderColor},
  589. {&single_double, single_double.X, single_double.Y, single_double.BorderColor}}
  590. var movingPanel *door.Panel
  591. var moveX, moveY int
  592. var panelColor string
  593. for {
  594. key, ex, err := d.WaitKey(door.Inactivity)
  595. if err != nil {
  596. return
  597. }
  598. if ex == door.MOUSE {
  599. m, ok := d.GetMouse()
  600. if ok {
  601. // Process Mouse Event
  602. if m.Button == 3 {
  603. // Exit on right click
  604. return
  605. }
  606. if m.Button == 1 {
  607. idx := FindPanel(m, panels)
  608. if idx != -1 {
  609. movingPanel = panels[idx].Panel
  610. panelColor = movingPanel.BorderColor
  611. moveX = int(m.X)
  612. moveY = int(m.Y)
  613. } else {
  614. continue
  615. }
  616. // Should we do something to the panel? Yes!
  617. movingPanel.BorderColor = moveColor
  618. d.Update(movingPanel.Output())
  619. } else if m.Button == 4 {
  620. if movingPanel != nil {
  621. PR, PB := movingPanel.RightBottomPos()
  622. log.Printf("Panel (%d,%d) End(%d,%d) W %d, L %d\n", movingPanel.X, movingPanel.Y, PR, PB, movingPanel.Width, movingPanel.Length())
  623. // Ok, panel is move!
  624. d.Update(movingPanel.Clear())
  625. // Restore panel border
  626. movingPanel.BorderColor = panelColor
  627. // move panel
  628. movingPanel.X -= (moveX - int(m.X))
  629. movingPanel.Y -= (moveY - int(m.Y))
  630. // sanity checks
  631. if movingPanel.X < 1 {
  632. movingPanel.X = 1
  633. }
  634. // var edgeX bool
  635. if movingPanel.X+movingPanel.Width >= door.Width {
  636. movingPanel.X = door.Width - movingPanel.Width - 1
  637. // edgeX = true
  638. }
  639. if movingPanel.Y < 1 {
  640. movingPanel.Y = 1
  641. }
  642. // var edgeY bool
  643. if movingPanel.Y+movingPanel.Length() >= door.Height {
  644. movingPanel.Y = door.Height - movingPanel.Length() - 1
  645. // edgeY = true
  646. }
  647. PR, PB = movingPanel.RightBottomPos()
  648. log.Printf("Panel Now (%d,%d) End (%d,%d) %d, %d\n", movingPanel.X, movingPanel.Y, PR, PB, movingPanel.Width, movingPanel.Length()) //, edgeX, edgeY)
  649. // If panel is at the end of the screen -- it scrolls (syncterm)
  650. // This "fixes" it. (Maybe a better way would be to not use last
  651. // line on the screen?)
  652. // Or another way:
  653. if PR == door.Width && PB == door.Height {
  654. movingPanel.X--
  655. }
  656. /*
  657. if edgeX && edgeY {
  658. movingPanel.X--
  659. log.Printf("Panel adjust X\n")
  660. }
  661. */
  662. d.Update(movingPanel.Output())
  663. }
  664. movingPanel = nil
  665. }
  666. }
  667. } else {
  668. if (key == 'Q') || (key == 'q') || (key == '\x1b') {
  669. return
  670. }
  671. if (key == 'R') || (key == 'r') {
  672. for _, p := range panels {
  673. d.Update(p.Panel.Clear())
  674. }
  675. for _, p := range panels {
  676. p.Panel.X = p.XPos
  677. p.Panel.Y = p.YPos
  678. p.Panel.BorderColor = p.BColor
  679. d.Update(p.Panel.Output())
  680. }
  681. }
  682. if key == '0' {
  683. d.Write(panels[0].Panel.GotoEnd())
  684. }
  685. if key == '1' {
  686. d.Write(panels[1].Panel.GotoEnd())
  687. }
  688. }
  689. }
  690. }
  691. func main() {
  692. var message string
  693. /*
  694. go func() {
  695. http.ListenAndServe(":6060", nil)
  696. }()
  697. */
  698. var d door.Door = door.Door{}
  699. d.Init("testdoor")
  700. defer func() {
  701. if err := recover(); err != nil {
  702. // This displays stack trace stderr
  703. debug.PrintStack()
  704. fmt.Println("ERROR:", err)
  705. log.Println("FAILURE:", err)
  706. // Display error to user
  707. d.Write(fmt.Sprintf(door.Reset+door.CRNL+"Exception: %v"+door.CRNL, err))
  708. }
  709. }()
  710. defer d.Close()
  711. // Updaters work best when the screen doesn't scroll, so start
  712. // us off at the very top.
  713. d.Write(door.Clrscr)
  714. // Start spinrite effects
  715. var ticker *time.Ticker = time.NewTicker(time.Millisecond * time.Duration(100))
  716. var spin door.SpinRiteMsg = door.SpinRiteMsgInit(15, 5,
  717. door.ColorText("RED ON GREEN"),
  718. []string{"RED", "GREEN", "SOFTWARE"})
  719. var spin2 door.SpinRite = door.SpinRiteInit(13, 5,
  720. door.ColorText("BRI CYA ON BLUE"))
  721. // Add in the GoRoutine Status panel
  722. var goPanel door.Panel = door.Panel{X: 0,
  723. Y: 1,
  724. Width: 18,
  725. Style: door.DOUBLE,
  726. Title: "] GoRoutines [",
  727. BorderColor: door.ColorText("BOLD YEL ON BLU"),
  728. }
  729. goLineUpdater := func() string {
  730. status := GoRoutinesStatus()
  731. // log.Println(status)
  732. return status
  733. }
  734. var goLine door.Line = door.Line{
  735. UpdateF: goLineUpdater,
  736. Width: goPanel.Width,
  737. }
  738. // goLine.Update() // Force Update
  739. goPanel.Lines = append(goPanel.Lines, goLine)
  740. goPanel.Lines = append(goPanel.Lines, door.Line{Width: goPanel.Width})
  741. var lIndex int = 0
  742. var legendCount int = 0
  743. const legendUpdate = 20
  744. go func() {
  745. var output string
  746. var legend []string = []string{
  747. "R=run r=Runnable",
  748. "S=Select s=Syscall",
  749. "Chan <read >write",
  750. "Z=Sleep P=Preempt",
  751. "I=Idle D=Dead",
  752. "W=Wait C=Copystk",
  753. }
  754. goPanel.Lines[1].Text = legend[0]
  755. for range ticker.C {
  756. // output = door.SavePos + door.Goto(door.Width-16, 1) + spin.Output() +
  757. // door.Goto(door.Width-15, 3) + spin2.Output() + door.RestorePos
  758. legendCount++
  759. if legendCount >= legendUpdate {
  760. legendCount = 0
  761. lIndex++
  762. if lIndex == len(legend) {
  763. lIndex = 0
  764. }
  765. goPanel.Lines[1].Text = legend[lIndex]
  766. }
  767. if goPanel.X == 0 {
  768. goPanel.X = door.Width - 40
  769. }
  770. goPanel.Update()
  771. output = door.Goto(door.Width-16, 1) + spin.Output() +
  772. door.Goto(door.Width-15, 3) + spin2.Output() + goPanel.Output()
  773. if !d.Disconnect() {
  774. d.Update(output)
  775. } else {
  776. ticker.Stop()
  777. return
  778. }
  779. }
  780. }()
  781. var wopr door.WOPR
  782. wopr.Init(d.StartTime, d.TimeOut, "") // TimeUsed(), d.TimeLeft(), "")
  783. wopr.ElapsedPanel.X = door.Width - 19
  784. wopr.ElapsedPanel.Y = door.Height - 15
  785. wopr.RemainingPanel.X = door.Width - 19
  786. wopr.RemainingPanel.Y = door.Height - 8
  787. wopr.Animate(&d)
  788. // bold := door.Color(1, 37, 40)
  789. var bolder string = door.ColorText("BOLD YEL ON BLUE")
  790. d.Write("Welcome to " + bolder + "go door go TestDoor." + door.Reset + door.CRNL + "..." + door.CRNL)
  791. d.EnableMouse(door.Normal)
  792. press_a_key(&d)
  793. d.Write(door.CRNL)
  794. var b []string
  795. if door.CP437 {
  796. b = door.AlertBox("Alert: go \xfb is in use!", 1)
  797. } else {
  798. b = door.AlertBox("Alert: go \u221a is in use!", 1)
  799. }
  800. warningColor := door.ColorText("BRI WHI ON GREEN")
  801. for _, line := range b {
  802. // Prevent color bleeding.
  803. d.Write(warningColor + line + door.Reset + door.CRNL)
  804. }
  805. d.Write(door.Reset + door.CRNL)
  806. var left time.Duration = d.TimeLeft()
  807. message = fmt.Sprintf("You have %0.2f minutes / %0.2f seconds remaining..."+door.CRNL, left.Minutes(), left.Seconds())
  808. d.Write(message)
  809. press_a_key(&d)
  810. var mainmenu door.Menu = MainMenu()
  811. var choice int
  812. for choice >= 0 {
  813. d.Write(door.Clrscr + door.HideCursor)
  814. choice = mainmenu.Choose(&d)
  815. d.Write(door.ShowCursor)
  816. if choice < 0 {
  817. break
  818. }
  819. option := mainmenu.GetOption(choice)
  820. wopr.Stop()
  821. // Clear WOPR panels.
  822. d.Write(door.Reset + wopr.Clear())
  823. r, b := mainmenu.Panel.RightBottomPos()
  824. d.Write(door.Goto(r, b))
  825. // fmt.Printf("Choice: %d, Option: %c\n", choice, option)
  826. switch option {
  827. case 'A':
  828. display_ansi(&d)
  829. press_a_key(&d)
  830. case 'D':
  831. display_information(&d)
  832. press_a_key(&d)
  833. case 'F':
  834. font_demo(&d)
  835. press_a_key(&d)
  836. case 'I':
  837. d.Write(door.Reset + door.CRNL + door.CRNL)
  838. input_demo(&d)
  839. press_a_key(&d)
  840. case 'M':
  841. // Why is this so far down on the screen? (Scrolls)
  842. d.Write(door.Reset + door.CRNL + "TO DO: Provide menu of options to select from..." + door.CRNL)
  843. press_a_key(&d)
  844. case 'P':
  845. progress_bars(&d)
  846. press_a_key(&d)
  847. case 'S':
  848. panel_demo(&d)
  849. press_a_key(&d)
  850. case 'T':
  851. about_test_door(&d)
  852. press_a_key(&d)
  853. case 'W':
  854. width_demo(&d)
  855. case 'Q':
  856. // This is also far down on the screen ...
  857. choice = -1
  858. case 'C': // Disabled
  859. var a, z int
  860. z = 0
  861. a = 10 / z
  862. z = a
  863. _ = a
  864. _ = z
  865. }
  866. wopr.Animate(&d)
  867. }
  868. // d.Write("\x1b[?1000l") // disable mouse
  869. // d.Write("\x1b[?1002l")
  870. d.DisableMouse()
  871. d.Write(door.Reset + door.CRNL)
  872. if d.Config.BBSID != "" {
  873. message = fmt.Sprintf("Returning to the %s BBS..."+door.CRNL, d.Config.BBSID)
  874. } else {
  875. message = "Returning to the BBS..." + door.CRNL
  876. }
  877. d.Write(message)
  878. d.WaitKey(time.Second)
  879. left = d.TimeLeft()
  880. ticker.Stop()
  881. message = fmt.Sprintf("You had %0.2f minutes remaining!"+door.CRNL, left.Minutes())
  882. d.Write(message)
  883. left = d.TimeUsed()
  884. d.Write(fmt.Sprintf("You used %0.2f seconds / %0.2f minutes."+door.CRNL, left.Seconds(), left.Minutes()))
  885. fmt.Println("Ending testdoor.go")
  886. }