nomoresecrets.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. time.Sleep(time.Millisecond * time.Duration(config.Jumble_Loop_Speed))
  202. }
  203. for {
  204. var revealed bool = true
  205. for idx, pos := range charpos {
  206. if work[pos] != ' ' {
  207. if chartime[idx] > 0 {
  208. if chartime[idx] < 500 {
  209. if rand.Intn(3) == 0 {
  210. var safe []byte = []byte{getRandom()}
  211. var rndchar string = CP437_to_Unicode(string(safe))
  212. work[pos] = []rune(rndchar)[0]
  213. }
  214. } else {
  215. if rand.Intn(10) == 0 {
  216. var safe []byte = []byte{getRandom()}
  217. var rndchar string = CP437_to_Unicode(string(safe))
  218. work[pos] = []rune(rndchar)[0]
  219. }
  220. }
  221. if chartime[idx] < config.Reveal_Loop_Speed {
  222. chartime[idx] = 0
  223. } else {
  224. chartime[idx] -= config.Reveal_Loop_Speed
  225. }
  226. revealed = false
  227. } else {
  228. work[pos] = original[revealpos[pos]]
  229. }
  230. }
  231. }
  232. Door.Write(renderF())
  233. time.Sleep(time.Millisecond * time.Duration(config.Reveal_Loop_Speed))
  234. if revealed {
  235. break
  236. }
  237. }
  238. } else {
  239. // CP437
  240. var original []byte = []byte(output) // original / master
  241. var work []byte // work copy we modify
  242. var workpos int = 0 // where are we in the work copy
  243. var charpos []int // where characters are we can change
  244. var chartime []int // character time / reveal timeout
  245. var revealpos map[int]int // workpos to original position
  246. var colormap map[int]string // index to color end position (workpos)
  247. var coloridx []int // index of positions (map isn't ordered)
  248. var lastcolor []int // LastColor tracking for keeping color proper
  249. var currentANSI string // ANSI Escape code we've extracted
  250. var ANSIchar byte // Last character of the ANSI Escape code
  251. work = make([]byte, 0)
  252. colormap = make(map[int]string)
  253. revealpos = make(map[int]int)
  254. coloridx = make([]int, 0)
  255. // default color = "reset"
  256. lastcolor = make([]int, 1)
  257. lastcolor[0] = 0
  258. // Not using range, I want to be able to look ahead and modify
  259. // x.
  260. for x := 0; x < len(original); x++ {
  261. var char byte = original[x]
  262. if char == '\x1b' {
  263. // ANSI Code
  264. currentANSI = "\x1b["
  265. if original[x+1] != '[' {
  266. log.Println("NoMoreSecrets: Found \\x1b not followed by [!")
  267. }
  268. x += 2
  269. for {
  270. currentANSI += string(original[x])
  271. if unicode.IsLetter(rune(original[x])) {
  272. ANSIchar = original[x]
  273. break
  274. }
  275. x++
  276. }
  277. // Is this a color code?
  278. if ANSIchar == 'm' {
  279. // Yes, don't store in work. Process code.
  280. Door.UpdateLastColor(currentANSI, &lastcolor)
  281. colormap[workpos] = Color(lastcolor...)
  282. coloridx = append(coloridx, workpos)
  283. } else {
  284. // Not a color code. Add to work.
  285. work = append(work, []byte(currentANSI)...)
  286. workpos += len(currentANSI)
  287. }
  288. currentANSI = ""
  289. } else {
  290. // Not escape, so what is it?
  291. if unicode.IsPrint(rune(char)) {
  292. if char == ' ' {
  293. work = append(work, char)
  294. chartime = append(chartime, 0)
  295. } else {
  296. work = append(work, char)
  297. chartime = append(chartime, rand.Intn(config.Max_Time+1))
  298. }
  299. charpos = append(charpos, workpos)
  300. revealpos[workpos] = x
  301. workpos++
  302. } else {
  303. // control code, CR NL.
  304. work = append(work, char)
  305. workpos++
  306. }
  307. }
  308. }
  309. // jumble loop
  310. var renderF func() string = func() string {
  311. var result []byte
  312. var lastcolor string
  313. var pos int = 0
  314. for idx, char := range work {
  315. _, found := revealpos[idx]
  316. if found {
  317. for charpos[pos] != idx {
  318. pos++
  319. }
  320. // This is a character
  321. if chartime[pos] != 0 && char != ' ' {
  322. if lastcolor != config.Color {
  323. result = append(result, []byte(config.Color)...)
  324. lastcolor = config.Color
  325. }
  326. } else {
  327. // look up the color in the colormap
  328. var best string
  329. for _, cpos := range coloridx {
  330. if cpos > idx {
  331. break
  332. }
  333. best = colormap[cpos]
  334. }
  335. if lastcolor != best {
  336. result = append(result, []byte(best)...)
  337. lastcolor = best
  338. }
  339. }
  340. }
  341. result = append(result, char)
  342. }
  343. return string(result)
  344. }
  345. for i := 0; i < (config.Jumble_Sec*1000)/config.Jumble_Loop_Speed; i++ {
  346. for _, pos := range charpos {
  347. if work[pos] != ' ' {
  348. work[pos] = getRandom()
  349. }
  350. }
  351. Door.Write(renderF())
  352. time.Sleep(time.Millisecond * time.Duration(config.Jumble_Loop_Speed))
  353. }
  354. for {
  355. var revealed bool = true
  356. for idx, pos := range charpos {
  357. if work[pos] != ' ' {
  358. if chartime[idx] > 0 {
  359. if chartime[idx] < 500 {
  360. if rand.Intn(3) == 0 {
  361. work[pos] = getRandom()
  362. }
  363. } else {
  364. if rand.Intn(10) == 0 {
  365. work[pos] = getRandom()
  366. }
  367. }
  368. if chartime[idx] < config.Reveal_Loop_Speed {
  369. chartime[idx] = 0
  370. } else {
  371. chartime[idx] -= config.Reveal_Loop_Speed
  372. }
  373. revealed = false
  374. } else {
  375. work[pos] = original[revealpos[pos]]
  376. }
  377. }
  378. }
  379. Door.Write(renderF())
  380. time.Sleep(time.Millisecond * time.Duration(config.Reveal_Loop_Speed))
  381. if revealed {
  382. break
  383. }
  384. }
  385. }
  386. }