pastebin - collaborative debugging tool
rovema.kpaste.net RSS


simple console ksh93 sinus plotter #001
Posted by Anonymous on Mon 7th Nov 2022 13:57
raw | new post
modification of post by Anonymous (view diff)

  1. #!/usr/bin/ksh93
  2.  
  3. compound c=(
  4.         compound tty=(
  5.                 integer max_x
  6.                 integer max_y
  7.         )
  8.  
  9.         compound sample_data=(
  10.                 float -a vals
  11.                 float min=0
  12.                 float max=0
  13.         )
  14.        
  15.         float min_data_shift
  16.         float max_data_shift
  17.         float data_scale_to_tty_max_y
  18. )
  19.  
  20. integer i
  21.  
  22.  
  23. #
  24. # get terminal info
  25. #
  26. (( c.tty.max_x=$(tput cols)-2 ))
  27. (( c.tty.max_y=$(tput lines)-2 ))
  28.  
  29. #
  30. # collect samples data
  31. #
  32. integer max_i=${c.tty.max_x}
  33. for ((i=0 ; i < max_i ; i++ )) ; do
  34.         (( c.sample_data.vals[i]=sin(i/5.)*sin(i/20.)*sin(i/80.)*sin(i/320.) ))
  35. done
  36.  
  37. #
  38. # get minimum/maximum value of all data samples
  39. #
  40. # we use $ for i in "${!@}" instead of $ for ((i=0 ; i < ... ; i++ )) # syntax
  41. # here so we can handle sparse data/sparse arrays
  42. #
  43. for i in "${!c.sample_data.vals[@]}" ; do
  44.         (( c.sample_data.min=fmin(c.sample_data.vals[i], c.sample_data.min) ))
  45.         (( c.sample_data.max=fmax(c.sample_data.vals[i], c.sample_data.max) ))
  46. done
  47.  
  48. #
  49. # calculations for output scaling etc
  50. #
  51. (( c.min_data_shift = c.sample_data.min * -1.0 ))
  52. (( c.max_data_shift = c.min_data_shift + c.sample_data.max ))
  53. (( c.data_scale_to_tty_max_y = c.tty.max_y / c.max_data_shift ))
  54.  
  55. #
  56. # output
  57. #
  58. tput clear
  59. for i in "${!c.sample_data.vals[@]}" ; do
  60.         tput cup $(( int( c.data_scale_to_tty_max_y * (c.min_data_shift+c.sample_data.vals[i]) ) )) $i
  61.         printf '*'
  62. done
  63.  
  64. # move cursor to bottom, and then print a newline
  65. tput cup ${c.tty.max_y} ${c.tty.max_x} ; print
  66.  
  67. #print -v c
  68.  
  69. # EOF.

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at