nomoresecrets.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package door
  2. import (
  3. "log"
  4. "math/rand"
  5. "time"
  6. "unicode"
  7. )
  8. /*
  9. No More Secrets - from "Sneakers"
  10. https://github.com/bartobri/no-more-secrets
  11. */
  12. type NoMoreSecretsConfig struct {
  13. Jumble_Sec int // in sec
  14. Jumble_Loop_Speed int // in ms
  15. Reveal_Loop_Speed int // in ms
  16. Max_Time int // Max value before reveal (per character)
  17. Color string // Color to use before reveal
  18. }
  19. // The default configuration for NoMoreSecrets
  20. var NoMoreSecretsDefault NoMoreSecretsConfig
  21. // Initialize the Defaults
  22. func init() {
  23. NoMoreSecretsDefault = NoMoreSecretsConfig{
  24. Jumble_Sec: 2,
  25. Jumble_Loop_Speed: 35,
  26. Reveal_Loop_Speed: 50,
  27. Max_Time: 5000,
  28. Color: ColorText("CYAN ON BLACK"),
  29. }
  30. }
  31. // Get random character code for jumble
  32. // We use chr 0x21 - 0xdf (excluding 0x7f)
  33. func getRandom() byte {
  34. // 0x7f = backspace / rubout
  35. var rb byte = 0x7f
  36. for rb == 0x7f {
  37. rb = byte(rand.Intn(0xdf-0x21) + 0x21)
  38. }
  39. return rb
  40. }
  41. /*
  42. NoMoreSecrets - render output as random, then fade in the original text.
  43. Example:
  44. func About_Example_NoMoreSecrets(d *door.Door) {
  45. W := 60
  46. center_x := (door.Width - W) / 2
  47. center_y := (door.Height - 16) / 2
  48. about := door.Panel{X: center_x,
  49. Y: center_y,
  50. Width: W,
  51. Style: door.SINGLE_DOUBLE,
  52. BorderColor: door.ColorText("BOLD YELLOW ON BLUE"),
  53. }
  54. about.Lines = append(about.Lines, door.Line{Text: fmt.Sprintf("%*s", -W, "About This Door"),
  55. DefaultColor: door.ColorText("BOLD CYAN ON BLUE")})
  56. about.Lines = append(about.Lines, about.Spacer())
  57. about.Lines = append(about.Lines, door.Line{Text: fmt.Sprintf("%*s", -W, "Test Door written in go, using go door.")})
  58. var copyright string = "(C) 2022 Bugz, Red Green Software"
  59. if door.Unicode {
  60. copyright = strings.Replace(copyright, "(C)", "\u00a9", -1)
  61. }
  62. about.Lines = append(about.Lines,
  63. door.Line{Text: fmt.Sprintf("%*s", -W, copyright),
  64. DefaultColor: door.ColorText("BOLD WHITE ON BLUE")})
  65. for _, text := range []string{"",
  66. "This door was written by Bugz.",
  67. "",
  68. "It is written in Go, understands CP437 and unicode, adapts",
  69. "to screen sizes, uses door32.sys, supports TheDraw Fonts,",
  70. "and runs on Linux and Windows."} {
  71. about.Lines = append(about.Lines, door.Line{Text: fmt.Sprintf("%*s", -W, text)})
  72. }
  73. better := door.NoMoreSecretsDefault
  74. better.Color = door.ColorText("BOLD CYAN ON BLUE")
  75. door.NoMoreSecrets(about.Output(), d, &better)
  76. }
  77. */
  78. func NoMoreSecrets(output string, Door *Door, config *NoMoreSecretsConfig) {
  79. // Find ANSI codes, strip color (We'll handle color)
  80. rand.Seed(time.Now().UnixNano())
  81. if Unicode {
  82. var original []rune = []rune(output) // original / master
  83. var work []rune = make([]rune, 0) // work copy we modify
  84. var workpos int = 0 // where we are in the work copy
  85. var charpos []int // where characters are we can change
  86. var chartime []int // character time / reveal timeout
  87. var revealpos map[int]int // workpos to original position
  88. var colormap map[int]string // index to color end position (workpos)
  89. var coloridx []int // index of positions (map isn't ordered)
  90. var lastcolor []int // LastColor tracking for keeping colors proper
  91. var currentANSI string // ANSI Escape code we've extracted
  92. var ANSIchar rune // Last character of the ANSI Escape code
  93. colormap = make(map[int]string)
  94. revealpos = make(map[int]int)
  95. coloridx = make([]int, 0)
  96. // default color = "reset"
  97. lastcolor = make([]int, 1)
  98. lastcolor[0] = 0
  99. // Not using range, I want to be able to look ahead and modify
  100. // x.
  101. for x := 0; x < len(original); x++ {
  102. var char rune = original[x]
  103. if char == '\x1b' {
  104. // ANSI Code
  105. currentANSI = "\x1b["
  106. if original[x+1] != '[' {
  107. log.Println("NoMoreSecrets: Found \\x1b not followed by [!")
  108. }
  109. x += 2
  110. for {
  111. currentANSI += string(original[x])
  112. if unicode.IsLetter(original[x]) {
  113. ANSIchar = original[x]
  114. break
  115. }
  116. x++
  117. }
  118. // log.Printf("curentANSI: END @ %d [%#v]\n", x, currentANSI[1:])
  119. // Is this a color code?
  120. if ANSIchar == 'm' {
  121. // Yes, don't store in work. Process code.
  122. Door.UpdateLastColor(currentANSI, &lastcolor)
  123. colormap[workpos] = Color(lastcolor...)
  124. coloridx = append(coloridx, workpos)
  125. // log.Printf("Added %d with %s\n", workpos, colormap[workpos][1:])
  126. } else {
  127. // Not a color code. Add to work.
  128. var ANSIrunes []rune = []rune(currentANSI)
  129. work = append(work, ANSIrunes...)
  130. workpos += len(ANSIrunes)
  131. }
  132. currentANSI = ""
  133. } else {
  134. // Not escape, so what is it?
  135. if unicode.IsPrint(char) {
  136. if char == ' ' {
  137. work = append(work, char)
  138. chartime = append(chartime, 0)
  139. } else {
  140. work = append(work, char)
  141. chartime = append(chartime, rand.Intn(config.Max_Time+1))
  142. }
  143. charpos = append(charpos, workpos)
  144. revealpos[workpos] = x
  145. workpos++
  146. } else {
  147. // control code, CR NL.
  148. work = append(work, char)
  149. workpos++
  150. }
  151. }
  152. }
  153. // jumble loop
  154. var renderF func() string = func() string {
  155. var result string
  156. var lastcolor string
  157. var pos int = 0
  158. for idx, char := range work {
  159. _, found := revealpos[idx]
  160. if found {
  161. for charpos[pos] != idx {
  162. pos++
  163. }
  164. // This is a character
  165. if chartime[pos] != 0 && char != ' ' {
  166. if lastcolor != config.Color {
  167. result += config.Color
  168. lastcolor = config.Color
  169. }
  170. } else {
  171. // look up the color in the colormap
  172. var best string
  173. // use the coloridx, lookup in colormap
  174. for _, cpos := range coloridx {
  175. if cpos > idx {
  176. break
  177. }
  178. best = colormap[cpos]
  179. }
  180. if lastcolor != best {
  181. result += best
  182. lastcolor = best
  183. }
  184. }
  185. }
  186. result += string(char)
  187. }
  188. return result
  189. }
  190. for i := 0; i < (config.Jumble_Sec*1000)/config.Jumble_Loop_Speed; i++ {
  191. for _, pos := range charpos {
  192. if work[pos] != ' ' {
  193. // Safe way to handle bytes to unicode
  194. var rb byte = getRandom()
  195. var safe []byte = []byte{rb}
  196. var rndchar string = CP437_to_Unicode(string(safe))
  197. work[pos] = []rune(rndchar)[0]
  198. }
  199. }
  200. Door.Write(renderF())
  201. if Door.Disconnect() {
  202. return
  203. }
  204. time.Sleep(time.Millisecond * time.Duration(config.Jumble_Loop_Speed))
  205. }
  206. for {
  207. var revealed bool = true
  208. for idx, pos := range charpos {
  209. if work[pos] != ' ' {
  210. if chartime[idx] > 0 {
  211. if chartime[idx] < 500 {
  212. if rand.Intn(3) == 0 {
  213. var safe []byte = []byte{getRandom()}
  214. var rndchar string = CP437_to_Unicode(string(safe))
  215. work[pos] = []rune(rndchar)[0]
  216. }
  217. } else {
  218. if rand.Intn(10) == 0 {
  219. var safe []byte = []byte{getRandom()}
  220. var rndchar string = CP437_to_Unicode(string(safe))
  221. work[pos] = []rune(rndchar)[0]
  222. }
  223. }
  224. if chartime[idx] < config.Reveal_Loop_Speed {
  225. chartime[idx] = 0
  226. } else {
  227. chartime[idx] -= config.Reveal_Loop_Speed
  228. }
  229. revealed = false
  230. } else {
  231. work[pos] = original[revealpos[pos]]
  232. }
  233. }
  234. }
  235. Door.Write(renderF())
  236. if Door.Disconnect() {
  237. return
  238. }
  239. time.Sleep(time.Millisecond * time.Duration(config.Reveal_Loop_Speed))
  240. if revealed {
  241. break
  242. }
  243. }
  244. } else {
  245. // CP437
  246. var original []byte = []byte(output) // original / master
  247. var work []byte // work copy we modify
  248. var workpos int = 0 // where are we in the work copy
  249. var charpos []int // where characters are we can change
  250. var chartime []int // character time / reveal timeout
  251. var revealpos map[int]int // workpos to original position
  252. var colormap map[int]string // index to color end position (workpos)
  253. var coloridx []int // index of positions (map isn't ordered)
  254. var lastcolor []int // LastColor tracking for keeping color proper
  255. var currentANSI string // ANSI Escape code we've extracted
  256. var ANSIchar byte // Last character of the ANSI Escape code
  257. work = make([]byte, 0)
  258. colormap = make(map[int]string)
  259. revealpos = make(map[int]int)
  260. coloridx = make([]int, 0)
  261. // default color = "reset"
  262. lastcolor = make([]int, 1)
  263. lastcolor[0] = 0
  264. // Not using range, I want to be able to look ahead and modify
  265. // x.
  266. for x := 0; x < len(original); x++ {
  267. var char byte = original[x]
  268. if char == '\x1b' {
  269. // ANSI Code
  270. currentANSI = "\x1b["
  271. if original[x+1] != '[' {
  272. log.Println("NoMoreSecrets: Found \\x1b not followed by [!")
  273. }
  274. x += 2
  275. for {
  276. currentANSI += string(original[x])
  277. if unicode.IsLetter(rune(original[x])) {
  278. ANSIchar = original[x]
  279. break
  280. }
  281. x++
  282. }
  283. // Is this a color code?
  284. if ANSIchar == 'm' {
  285. // Yes, don't store in work. Process code.
  286. Door.UpdateLastColor(currentANSI, &lastcolor)
  287. colormap[workpos] = Color(lastcolor...)
  288. coloridx = append(coloridx, workpos)
  289. } else {
  290. // Not a color code. Add to work.
  291. work = append(work, []byte(currentANSI)...)
  292. workpos += len(currentANSI)
  293. }
  294. currentANSI = ""
  295. } else {
  296. // Not escape, so what is it?
  297. if unicode.IsPrint(rune(char)) {
  298. if char == ' ' {
  299. work = append(work, char)
  300. chartime = append(chartime, 0)
  301. } else {
  302. work = append(work, char)
  303. chartime = append(chartime, rand.Intn(config.Max_Time+1))
  304. }
  305. charpos = append(charpos, workpos)
  306. revealpos[workpos] = x
  307. workpos++
  308. } else {
  309. // control code, CR NL.
  310. work = append(work, char)
  311. workpos++
  312. }
  313. }
  314. }
  315. // jumble loop
  316. var renderF func() string = func() string {
  317. var result []byte
  318. var lastcolor string
  319. var pos int = 0
  320. for idx, char := range work {
  321. _, found := revealpos[idx]
  322. if found {
  323. for charpos[pos] != idx {
  324. pos++
  325. }
  326. // This is a character
  327. if chartime[pos] != 0 && char != ' ' {
  328. if lastcolor != config.Color {
  329. result = append(result, []byte(config.Color)...)
  330. lastcolor = config.Color
  331. }
  332. } else {
  333. // look up the color in the colormap
  334. var best string
  335. for _, cpos := range coloridx {
  336. if cpos > idx {
  337. break
  338. }
  339. best = colormap[cpos]
  340. }
  341. if lastcolor != best {
  342. result = append(result, []byte(best)...)
  343. lastcolor = best
  344. }
  345. }
  346. }
  347. result = append(result, char)
  348. }
  349. return string(result)
  350. }
  351. for i := 0; i < (config.Jumble_Sec*1000)/config.Jumble_Loop_Speed; i++ {
  352. for _, pos := range charpos {
  353. if work[pos] != ' ' {
  354. work[pos] = getRandom()
  355. }
  356. }
  357. Door.Write(renderF())
  358. if Door.Disconnect() {
  359. return
  360. }
  361. time.Sleep(time.Millisecond * time.Duration(config.Jumble_Loop_Speed))
  362. }
  363. for {
  364. var revealed bool = true
  365. for idx, pos := range charpos {
  366. if work[pos] != ' ' {
  367. if chartime[idx] > 0 {
  368. if chartime[idx] < 500 {
  369. if rand.Intn(3) == 0 {
  370. work[pos] = getRandom()
  371. }
  372. } else {
  373. if rand.Intn(10) == 0 {
  374. work[pos] = getRandom()
  375. }
  376. }
  377. if chartime[idx] < config.Reveal_Loop_Speed {
  378. chartime[idx] = 0
  379. } else {
  380. chartime[idx] -= config.Reveal_Loop_Speed
  381. }
  382. revealed = false
  383. } else {
  384. work[pos] = original[revealpos[pos]]
  385. }
  386. }
  387. }
  388. Door.Write(renderF())
  389. if Door.Disconnect() {
  390. return
  391. }
  392. time.Sleep(time.Millisecond * time.Duration(config.Reveal_Loop_Speed))
  393. if revealed {
  394. break
  395. }
  396. }
  397. }
  398. }