ncurses

In short, it is a library of functions that manages an application's display on character-cell terminals.

Example in Python

   1 import curses
   2 import time
   3 import subprocess
   4 import threading
   5 
   6 class Update(threading.Thread):
   7     def __init__(self,w1,w2,w3):
   8         threading.Thread.__init__(self)
   9         self.__w1 = w1
  10         self.__w2 = w2
  11         self.__w3 = w3
  12         self.__running = False
  13 
  14     def run(self):
  15         self.__stop = False
  16         self.__running = True
  17         while self.__stop==False:
  18             out = subprocess.check_output(["uptime"])
  19             splitted = out.strip().split()
  20 
  21             one_minute = splitted[9].replace(",","")
  22             five_minutes = splitted[10].replace(",","")
  23             fifteen_minutes = splitted[11].replace(",","")            
  24 
  25             self.__w1.addstr(0,0,"one minute")
  26             self.__w1.addstr(0,16,one_minute)
  27             self.__w1.refresh()
  28             self.__w2.addstr(0,0,"five minutes") 
  29             self.__w2.addstr(0,16,five_minutes)
  30             self.__w2.refresh() 
  31             self.__w3.addstr(0,0,"fifteen minutes") 
  32             self.__w3.addstr(0,16,fifteen_minutes )
  33             self.__w3.refresh()
  34 
  35             time.sleep(2)
  36         self.__running = False
  37 
  38     def stop(self):
  39         self.__stop = True
  40 
  41     def is_running(self):
  42         return self.__running
  43 
  44 class Screen(object):
  45 
  46     def __init__(self):
  47         self.__stdscr = curses.initscr()
  48         curses.noecho()
  49         curses.cbreak()
  50         self.__stdscr.keypad(1)
  51 
  52     def get_std_scr(self):
  53         return self.__stdscr
  54 
  55     def exit(self):
  56         curses.nocbreak()
  57         self.__stdscr.keypad(0)
  58         curses.echo()
  59         curses.endwin()
  60 
  61     def create_window(self,x,y,width,height):
  62         return curses.newwin(height, width, y, x)
  63 
  64 if __name__=='__main__':
  65     screen = None
  66     exception = ""
  67 
  68     try:
  69         screen = Screen()
  70         w1 = screen.create_window(0,0,30,1)
  71         w2 = screen.create_window(30,0,30,1)
  72         w3 = screen.create_window(60,0,30,1)
  73         w4 = screen.create_window(0,1,30,1)
  74         t = Update(w1,w2,w3)
  75         t.start()
  76         loop = True
  77 
  78         while loop:
  79             key=screen.get_std_scr().getch()
  80             if key == ord('q'):
  81                 w4.addstr(0,0,"Exiting ...") 
  82                 w4.refresh()
  83                 t.stop()
  84                 while t.is_running(): 
  85                     time.sleep(1)
  86                 loop = False
  87     except Exception as ex:
  88         exception = str(ex)
  89     finally:
  90         screen.exit()
  91     print(exception)

ncurses (last edited 2019-03-08 18:19:26 by localhost)