From bd8ad0002610d16ded59141870f8d30345739288 Mon Sep 17 00:00:00 2001 From: "Glenn Y. Rolland" Date: Sat, 2 Jan 2021 22:36:55 +0100 Subject: [PATCH] Add basic window management code for NCurses.cr --- src/lib/tui.cr | 75 ++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/src/lib/tui.cr b/src/lib/tui.cr index 0646423..dd825a1 100644 --- a/src/lib/tui.cr +++ b/src/lib/tui.cr @@ -16,7 +16,7 @@ module Noozoid NCurses.start_color # define background color - pair = NCurses::ColorPair.new(1).init(NCurses::Color::RED, NCurses::Color::BLACK) + pair = NCurses::ColorPair.new(1).init(NCurses::Color::WHITE, NCurses::Color::BLACK) NCurses.bkgd(pair) NCurses.curs_set(0) @@ -30,17 +30,47 @@ module Noozoid NCurses.move(x:0, y: 2) NCurses.addstr(NCurses.curses_version) - - # attach models to views - #@main_window = MainView.new(self) - #@main_window.draw - #@main_window.wait - NCurses.notimeout(true) x = NCurses.maxx / 2 y = NCurses.maxy / 2 + # NCurses.box(v: '|', h: '=') + + routes = { + 'q'.ord => -> do + NCurses.endwin + exit 0 + return nil + end, + + NCurses::KeyCode::ESC => -> do + NCurses.endwin + exit 0 + return nil + end, + + NCurses::KeyCode::LEFT => -> do + x -= 1 + x = 0 if x < 0 + end, + + NCurses::KeyCode::RIGHT => -> do + x += 1 + x = NCurses.maxx - 1 if x >= NCurses.maxx + end, + + NCurses::KeyCode::UP => -> do + y -= 1 + y = 0 if y < 0 + end, + + NCurses::KeyCode::DOWN => -> do + y += 1 + y = NCurses.maxy - 2 if y >= NCurses.maxy - 1 + end + } + loop do NCurses.erase NCurses.move(x: x, y: y) @@ -48,36 +78,9 @@ module Noozoid NCurses.refresh key = NCurses.getch - - case key - when 'q', NCurses::KeyCode::ESC - break - - when NCurses::KeyCode::LEFT - x -= 1 - x = 0 if x < 0 - - when NCurses::KeyCode::RIGHT - x += 1 - x = NCurses.maxx - 1 if x >= NCurses.maxx - - when NCurses::KeyCode::UP - y -= 1 - y = 0 if y < 0 - - when NCurses::KeyCode::DOWN - y += 1 - y = NCurses.maxy - 2 if y >= NCurses.maxy - 1 - - end + routes[key].call if routes.keys.includes? key + # ctx = { a: 1 } end - - NCurses.endwin - - #rescue StandardError => e - #Curses.close_screen - #warn e.message unless e.nil? - #warn e.backtrace unless e.nil? end end