Skip to content
Snippets Groups Projects

python curses test

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Fjen Undso
    Edited
    c.py 1.37 KiB
    #!/usr/bin/env python3
    from curses import wrapper
    import curses
    import time
    
    
    class ActiveNumber:
        def __init__(self, window, row=0, col=0, number=0, active=False):
            self.window = window
            self.row = row
            self.col = col
            self.number = number
            self.active = active
    
        def draw(self):
            attr = curses.A_NORMAL
            if self.active:
                attr = curses.A_STANDOUT
            self.window.move(self.row, self.col)
            self.window.addstr("<")
            self.window.addstr(str(self.number).zfill(3), attr)
            self.window.addstr(">")
            self.window.refresh()
    
        def incr(self):
            self.number += 1 if self.number < 255 else self.number
    
        def decr(self):
            self.number -= 1 if self.number > 0 else self.number
    
    def progress(window):
        # TODO: make it good
        win = window.subwin(10, 3, 0, 0)
        win.border(0)
        win.addstr(1, 1, "#")
        win.refresh()
    
    def main(stdscr):
        stdscr.clear()
        # hide cursor
        curses.curs_set(0)
    
        #progress(stdscr)
        n1 = ActiveNumber(stdscr)
        n1.draw()
    
        stdscr.refresh()
        while True:
            k = stdscr.getkey()
            if str(k) == 'KEY_DOWN':
                n1.active = False
            if str(k) == 'KEY_UP':
                n1.active = True
            if str(k) == 'KEY_RIGHT':
                n1.incr()
            if str(k) == 'KEY_LEFT':
                n1.decr()
            n1.draw()
    
    wrapper(main)
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment