nomoresecrets.go 12 KB

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