NASutils Knowledge BaseUnofficialPolski

BASIC · projectA menu with boxes in yabasic

This program gives your NAS a small menu in the SSH window: four entries in a double frame, a highlighted bar, and a box beside the menu with memory, volumes or uptime. You move with the arrow keys or the numbers 1 to 4.

Programming on the NAS › BASIC in 30 lessons


  ╔═ NAS toolbox ════════╗
  ║                      ║
  ║  1  Memory           ║
  ║  2  Volumes          ║
  ║  3  Uptime and load  ║
  ║  4  Quit             ║
  ║                      ║
  ╚══════════════════════╝












  ↑ ↓ or 1-4 choose   Enter opens   q quits
The menu with the bar on Memory.

  ╔═ NAS toolbox ════════╗  ╔═ Memory ═══════════════════════════════════════╗
  ║                      ║  ║                                                ║
  ║  1  Memory           ║  ║  Total       15.4 GiB                          ║
  ║  2  Volumes          ║  ║  Used        5.6 GiB    ██████░░░░░░░░░░ 36 %  ║
  ║  3  Uptime and load  ║  ║  Available   9.8 GiB                           ║
  ║  4  Quit             ║  ║  Swap used   3.0 GiB                           ║
  ║                      ║  ║                                                ║
  ╚══════════════════════╝  ╚════════════════════════════════════════════════╝












  Esc or q goes back
The Memory box: 15.4 GiB in total, 5.6 GiB used with a green bar at 36 %, and the swap in use.

  ╔═ NAS toolbox ════════╗  ╔═ Volumes ══════════════════════════════════════╗
  ║                      ║  ║                                                ║
  ║  1  Memory           ║  ║  CACHEDEV1_DATA  1.30 TiB   ███░░░░░░░░░ 21 %  ║
  ║  2  Volumes          ║  ║  CACHEDEV2_DATA  1.20 TiB   ██░░░░░░░░░░ 14 %  ║
  ║  3  Uptime and load  ║  ║                                                ║
  ║  4  Quit             ║  ╚════════════════════════════════════════════════╝
  ║                      ║
  ╚══════════════════════╝












  Esc or q goes back
The Volumes box: two data volumes with their sizes and bars at 21 % and 14 %.

  ╔═ NAS toolbox ════════╗  ╔═ Uptime and load ══════════════════════════════╗
  ║                      ║  ║                                                ║
  ║  1  Memory           ║  ║  Up for      1 d 13 h 10 min                   ║
  ║  2  Volumes          ║  ║  Load        1.17  0.90  0.88  1 / 5 / 15 min  ║
  ║  3  Uptime and load  ║  ║  Threads     4315                              ║
  ║  4  Quit             ║  ║                                                ║
  ║                      ║  ╚════════════════════════════════════════════════╝
  ╚══════════════════════╝












  Esc or q goes back
The Uptime and load box: 1 d 13 h 10 min, the three load averages and 4315 threads.

Download it on the NAS

curl -fsSLO https://kb.nasutils.net/projects/basic/menu.yab

menu.yab Download menu.yab

rem NAS toolbox: a framed menu that opens boxes with live facts of the NAS.
rem Run: yabasic menu.yab   Arrows or 1-4 choose, Enter opens, Esc or q goes back and quits.
rem yabasic's print at() shows only ASCII, so the screen goes to /dev/tty as escape codes.

proc$ = peek$("env", "PROC") : if proc$ = "" proc$ = "/proc"
dim item$(4) : item$(1) = "Memory" : item$(2) = "Volumes" : item$(3) = "Uptime and load" : item$(4) = "Quit"
dim fact$(12), word$(8), line$(1)
clear screen
pick = 1
repeat
  menu("↑ ↓ or 1-4 choose   Enter opens   q quits")
  k$ = inkey$
  if k$ = "up" and pick > 1 pick = pick - 1
  if k$ = "down" and pick < 4 pick = pick + 1
  if instr("1234", k$) and len(k$) = 1 then pick = val(k$) : k$ = "enter" : endif
  if k$ = "enter" and pick < 4 then
    menu("Esc or q goes back")
    show(pick)
    repeat : b$ = inkey$ : until b$ = "esc" or b$ = "q"
  endif
until k$ = "esc" or k$ = "q" or (k$ = "enter" and pick = 4)
send(e$("0") + chr$(27) + "[2J" + at$(1, 1))
exit

sub menu(hint$)
  local s$, i
  s$ = e$("0") + chr$(27) + "[2J" + box$(3, 3, 24, 8, "NAS toolbox")
  for i = 1 to 4
    s$ = s$ + at$(5, 4 + i)
    if i = pick s$ = s$ + e$("30;43")
    s$ = s$ + pad$(" " + str$(i) + "  " + item$(i), 20) + e$("0")
  next i
  send(s$ + at$(3, 23) + e$("33") + hint$ + e$("0"))
end sub

sub show(n)
  local s$, i, rows
  rows = facts(n)
  s$ = box$(29, 3, 50, rows + 4, item$(n))
  for i = 1 to rows
    s$ = s$ + at$(32, 4 + i) + fact$(i)
  next i
  send(s$)
end sub

sub facts(n)
  local total, avail, a$, i, f
  if n = 1 then
    total = meminfo("MemTotal") : avail = meminfo("MemAvailable")
    fact$(1) = pad$("Total", 12) + gib$(total)
    fact$(2) = pad$("Used", 12) + pad$(gib$(total - avail), 11) + bar$((total - avail) / total, 16)
    fact$(3) = pad$("Available", 12) + gib$(avail)
    fact$(4) = pad$("Swap used", 12) + gib$(meminfo("SwapTotal") - meminfo("SwapFree"))
    return 4
  elsif n = 2 then
    a$ = peek$("env", "DF")
    if a$ = "" then a$ = system$("df -kP 2>/dev/null") else a$ = system$("cat '" + a$ + "'") : endif
    for i = 2 to token(a$, line$(), chr$(10))
      if token(line$(i), word$(), " ") = 6 and left$(word$(6), 7) = "/share/" and f < 8 then
        f = f + 1
        fact$(f) = pad$(mid$(word$(6), 8), 16) + pad$(gib$(val(word$(2))), 11) + bar$(val(word$(3)) / val(word$(2)), 12)
      endif
    next i
    if f = 0 then fact$(1) = "No volume under /share" : f = 1 : endif
    return f
  else
    a$ = first$(proc$ + "/uptime") : i = val(a$)
    fact$(1) = pad$("Up for", 12) + str$(int(i / 86400)) + " d " + str$(mod(int(i / 3600), 24)) + " h " + str$(mod(int(i / 60), 60)) + " min"
    a$ = first$(proc$ + "/loadavg") : f = token(a$, word$(), " ")
    fact$(2) = pad$("Load", 12) + word$(1) + "  " + word$(2) + "  " + word$(3) + e$("33") + "  1 / 5 / 15 min" + e$("0")
    fact$(3) = pad$("Threads", 12) + mid$(word$(4), instr(word$(4), "/") + 1)
    return 3
  endif
end sub

sub first$(file$)
  local a$
  open #2, file$, "r" : line input #2 a$ : close #2
  return a$
end sub

sub meminfo(key$)
  local a$, v
  open #2, proc$ + "/meminfo", "r"
  while not eof(#2)
    line input #2 a$
    if left$(a$, len(key$) + 1) = key$ + ":" v = val(mid$(a$, len(key$) + 2))
  wend
  close #2
  return v
end sub

sub bar$(part, width)
  local n, c$
  n = int(part * width + 0.5)
  c$ = "32"
  if part > 0.7 c$ = "33"
  if part > 0.9 c$ = "31"
  return e$(c$) + rep$("█", n) + e$("0") + rep$("░", width - n) + " " + str$(int(part * 100 + 0.5)) + " %"
end sub

sub gib$(kb)
  if kb >= 1024 * 1024 * 1024 return str$(kb / 1024 / 1024 / 1024, "%.2f") + " TiB"
  return str$(kb / 1024 / 1024, "%.1f") + " GiB"
end sub

sub box$(x, y, w, h, title$)
  local s$, i
  s$ = e$("33") + at$(x, y) + "╔" + rep$("═", w - 2) + "╗"
  for i = 1 to h - 2 : s$ = s$ + at$(x, y + i) + "║" + rep$(" ", w - 2) + "║" : next i
  s$ = s$ + at$(x, y + h - 1) + "╚" + rep$("═", w - 2) + "╝"
  return s$ + at$(x + 2, y) + e$("1;33") + " " + title$ + " " + e$("0")
end sub

sub width(s$)
  local i, n, c
  for i = 1 to len(s$)
    c = asc(mid$(s$, i, 1))
    if c < 128 or c >= 192 n = n + 1
  next i
  return n
end sub

sub pad$(s$, n) : return s$ + rep$(" ", n - width(s$)) : end sub
sub rep$(s$, n) : local r$, i : for i = 1 to n : r$ = r$ + s$ : next i : return r$ : end sub
sub e$(code$) : return chr$(27) + "[" + code$ + "m" : end sub
sub at$(x, y) : return chr$(27) + "[" + str$(y) + ";" + str$(x) + "H" : end sub
sub send(s$) : open #1, "/dev/tty", "w" : print #1 s$; : close #1 : end sub

What you learn

  • clear screen turns on yabasic's key reading, and inkey$ then returns one key: "up", "down", "enter", "esc" or the letter.
  • chr$(27) + "[5;3H" moves the cursor to line 5, column 3, and chr$(27) + "[33m" turns the text amber.
  • A whole screen goes into one string and out to /dev/tty with one print #1, so it appears at once.
  • token() cuts a line of df -kP into its six fields, and val() turns a field into a number.

How it works

yabasic draws its own screen with curses, and that screen shows a byte above 127 as a code such as ~U. The frame characters ═ and ║ are three bytes each in UTF-8, so the program does not use print at() for them. It builds every picture as a string of escape codes and frame characters and writes it to /dev/tty, the terminal of the SSH session. clear screen stays at the start, because inkey$ works only after it.

The main loop draws the menu, waits for a key and moves pick. A number key selects its entry and opens it at once. Enter on an entry draws the menu again with a new hint and calls show, which draws a box at column 29 with the lines that facts returns. The program then waits for Esc or q and draws the menu again.

facts reads the NAS. Memory in use is MemTotal minus MemAvailable from /proc/meminfo. The volumes are the lines of df -kP mounted under /share, each with its size and a bar of the used space. Uptime is the first number of /proc/uptime in seconds; the load and the number of threads come from /proc/loadavg.

Notes to the lines

  • 5peek$("env", "PROC") reads an environment variable. The test points PROC at sample files; on the NAS it is empty and the program reads /proc.
  • 8clear screen comes before the first inkey$, which stops the program with an error without it.
  • 13–15The arrows move the bar between 1 and 4. A number key sets pick and acts like Enter.
  • 19The box stays until Esc or q; other keys do nothing.
  • 22–23The program clears the screen and ends with exit. Without exit, yabasic shows "Program done, press RETURN" after a program that used clear screen.
  • 30The highlighted entry gets black letters on amber: 30;43 in one escape code.
  • 57Without DF the program runs df -kP. -P keeps each mount on one line, and 2>/dev/null hides errors.
  • 59A line counts when it has six fields and its mount point starts with /share/.
  • 68The seconds of uptime become days, hours and minutes with int() and mod().
  • 71The fourth field of /proc/loadavg reads like 3/4315: running and all threads. The part after / is the count.
  • 93–100bar$ fills a share of the width with █ and the rest with ░; green turns amber above 70 % and red above 90 %.
  • 107–113box$ returns a double frame as escape codes and writes the title over the top edge.
  • 115–122width counts the letters of a UTF-8 text: every byte except those from 128 to 191, which continue a letter. pad$ uses it, so the columns line up.
  • 128send opens /dev/tty, prints the string without a new line and closes the file, which sends it out.

Run it

yabasic menu.yab

↑ and ↓ or the keys 1 to 4 choose an entry, Enter opens its box, Esc or q goes back, and q or Quit ends the program.

Try it yourself

Add a fifth entry "CPU" before Quit that shows the model name line of /proc/cpuinfo; every 4 that counts the entries then becomes 5. Change the 16 in the call of bar$ on line 51 to 24 and widen the box to match.

More projects in BASIC