1+ // Package main implements tlock, a terminal lock screen for macOS with
2+ // Touch ID and password authentication.
13package main
24
35/*
@@ -129,7 +131,7 @@ func centerText(text string, width int) string {
129131
130132func centerBlock (block string , width int ) string {
131133 lines := strings .Split (block , "\n " )
132- var result []string
134+ result := make ( []string , 0 , len ( lines ))
133135 for _ , line := range lines {
134136 result = append (result , centerText (line , width ))
135137 }
@@ -387,10 +389,10 @@ const (
387389)
388390
389391type gridCell struct {
390- state int
391- wormIdx int
392+ state int
393+ wormIdx int
392394 trailAge int
393- color lipgloss.Color
395+ color lipgloss.Color
394396}
395397
396398type worm struct {
@@ -399,8 +401,8 @@ type worm struct {
399401 length int // current drawn length (grows from 0 to cap)
400402 dir int // 0=up, 1=right, 2=down, 3=left
401403 color lipgloss.Color
402- turnCool int // ticks before next turn allowed
403- minRun int // minimum straight cells before turning
404+ turnCool int // ticks before next turn allowed
405+ minRun int // minimum straight cells before turning
404406}
405407
406408// Retro phosphor CRT palette
@@ -421,8 +423,10 @@ var wormColors = []lipgloss.Color{
421423}
422424
423425// Direction deltas: up, right, down, left (cardinal only, in grid coords)
424- var dx = []int {0 , 1 , 0 , - 1 }
425- var dy = []int {- 1 , 0 , 1 , 0 }
426+ var (
427+ dx = []int {0 , 1 , 0 , - 1 }
428+ dy = []int {- 1 , 0 , 1 , 0 }
429+ )
426430
427431// Trail fade stages
428432var trailBlocks = []string {"\u2588 " , "\u2593 " , "\u2592 " , "\u2591 " }
@@ -500,7 +504,10 @@ func runWormDemo(numWorms int) bool {
500504 // Shuffle colors and assign — no two adjacent worms share a color
501505 shuffled := make ([]lipgloss.Color , len (wormColors ))
502506 copy (shuffled , wormColors )
503- rand .Shuffle (len (shuffled ), func (a , b int ) { shuffled [a ], shuffled [b ] = shuffled [b ], shuffled [a ] })
507+ rand .Shuffle (
508+ len (shuffled ),
509+ func (a , b int ) { shuffled [a ], shuffled [b ] = shuffled [b ], shuffled [a ] },
510+ )
504511 assignedColors := make ([]lipgloss.Color , numWorms )
505512 colorIdx := 0
506513 for j := range assignedColors {
@@ -525,7 +532,7 @@ func runWormDemo(numWorms int) bool {
525532 for attempts := 0 ; attempts < 500 ; attempts ++ {
526533 sx = rand .Intn (gridW - 4 ) + 2
527534 sy = rand .Intn (gridH - 4 ) + 2
528- clear := true
535+ open := true
529536 // Check the full body line + 1 cell padding around it
530537 for j := - 1 ; j <= bodyLen ; j ++ {
531538 for pad := - 1 ; pad <= 1 ; pad ++ {
@@ -539,20 +546,20 @@ func runWormDemo(numWorms int) bool {
539546 }
540547 if cx < 0 || cx >= gridW || cy < 0 || cy >= gridH {
541548 if j >= 0 && j < bodyLen {
542- clear = false
549+ open = false
543550 }
544551 continue
545552 }
546553 if grid [cy ][cx ].state != cellEmpty {
547- clear = false
554+ open = false
548555 break
549556 }
550557 }
551- if ! clear {
558+ if ! open {
552559 break
553560 }
554561 }
555- if clear {
562+ if open {
556563 break
557564 }
558565 }
@@ -587,8 +594,9 @@ func runWormDemo(numWorms int) bool {
587594 startKeyReader := func () {
588595 go func () {
589596 buf := make ([]byte , 1 )
590- os .Stdin .Read (buf )
591- keyCh <- buf [0 ]
597+ if _ , err := os .Stdin .Read (buf ); err == nil {
598+ keyCh <- buf [0 ]
599+ }
592600 }()
593601 }
594602 startKeyReader ()
@@ -763,10 +771,18 @@ func runWormDemo(numWorms int) bool {
763771}
764772
765773func main () {
766- snake := flag .Bool ("snake" , false , "Screensaver on immediately (shortcut for --screensaver --screensaver-delay 0)" )
774+ snake := flag .Bool (
775+ "snake" ,
776+ false ,
777+ "Screensaver on immediately (shortcut for --screensaver --screensaver-delay 0)" ,
778+ )
767779 snakeCount := flag .Int ("snake-count" , 0 , "Number of worms (0 = auto based on terminal size)" )
768780 screensaver := flag .Bool ("screensaver" , false , "Enable xlock-style worm screensaver" )
769- screensaverDelay := flag .Int ("screensaver-delay" , 30 , "Seconds idle before screensaver starts (0 = immediate)" )
781+ screensaverDelay := flag .Int (
782+ "screensaver-delay" ,
783+ 30 ,
784+ "Seconds idle before screensaver starts (0 = immediate)" ,
785+ )
770786 flag .Parse ()
771787
772788 fd := int (os .Stdin .Fd ())
@@ -775,7 +791,7 @@ func main() {
775791 fmt .Fprintf (os .Stderr , "failed to set raw mode: %v\n " , err )
776792 os .Exit (1 )
777793 }
778- defer term .Restore (fd , oldState )
794+ defer func () { _ = term .Restore (fd , oldState ) }( )
779795
780796 hideCursor ()
781797 defer showCursor ()
0 commit comments