Skip to content

Instantly share code, notes, and snippets.

@paul-d-ray
Last active December 30, 2025 20:38
Show Gist options
  • Select an option

  • Save paul-d-ray/86bc4f9ced6e907167e76771903b028d to your computer and use it in GitHub Desktop.

Select an option

Save paul-d-ray/86bc4f9ced6e907167e76771903b028d to your computer and use it in GitHub Desktop.
Nushell Sine Wave

Sine Wave using Nushell

Sine Wave

# Sine wave ASCII plot in NuShell with color using match (correct syntax for guards)
# written by Grok with some manual syntax corrections and changing the colors to use nushell colors

 const PI = 3.14159265359
 const WIDTH = 180
 const HEIGHT = 30
 const PERIODS = 2

 # Loop over y from top (HEIGHT/2) down to bottom (-HEIGHT/2)
 for y in (($HEIGHT / 2)..($HEIGHT / -2)) {
     mut line = ""

     for x in 0..<$WIDTH {
         let angle = ($x / $WIDTH * 2 * $PI * $PERIODS)
         let sine = ([$angle] | math sin | into float | get 0)
         let plot_y = ($sine * ($HEIGHT / 2 - 1) | math round | into int)

         if $plot_y == $y {
             # Normalize angle to 0..2π
             let phase = ($angle mod (2 * $PI))

             # Pick color based on phase using proper match guard syntax
             let color = match $phase {
                 $p if ($p < ($PI / 2)) => {ansi green}    # green    (0 to π/2)
                 $p if ($p < $PI)       => {ansi yellow}   # yellow   (π/2 to π)
                 $p if ($p < (3 * $PI / 2)) => {ansi blue} # blue (π to 3π/2)
                 _                      => {ansi magenta}  # magenta  (3π/2 to 2π)
             }

             $line = ($line + $color + "*" + "\e[0m")
         } else {
             $line = ($line + " ")
         }
     }

     print $line
 }

Vertical Sine Wave

# PR 20251216_2200
# help from Grok
# Vertical Sine Wave 
# Saw Dave on Dave's Garage, where he was making code in CBasic
#   convert this to Nushell, 0.108 
#   had Grok help add the color code.

loop {
  seq 0.0 0.1 100.0 | each { |a|
    let s = ($a | math sin)
    let pos = (40 + (40 * $s) | math round)

    let color = match $s {
      $s if ($s >= 0.66 and $s <= 1.0) => { ansi red }
      $s if ($s >= 0.33 and $s < 0.66) => { ansi yellow }
      $s if ($s >= 0.0 and $s < 0.33) => { ansi green }
      $s if ($s >= -0.33 and  $s < 0.0) => { ansi cyan }
      $s if ($s >= -0.66 and  $s < -0.33) => { ansi blue }
      _ => { ansi magenta }
    }

    '' | fill --width $pos --character ' ' | print --no-newline
    print $"($color)*(ansi reset)"
    sleep 50ms
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment