From 12df4f909ceab5b472b4681cba1a01cd09c3ff07 Mon Sep 17 00:00:00 2001 From: "Glenn Y. Rolland" Date: Mon, 30 Dec 2019 01:53:25 +0100 Subject: [PATCH] Fix base navigation --- lib/noozoid/config.rb | 12 ++-- lib/noozoid/gui.rb | 66 ++++++++---------- lib/noozoid/node.rb | 23 +++---- lib/noozoid/widgets/help_widget.rb | 46 +++++++------ lib/noozoid/widgets/main_widget.rb | 104 ++++++++++++++++++----------- 5 files changed, 135 insertions(+), 116 deletions(-) diff --git a/lib/noozoid/config.rb b/lib/noozoid/config.rb index 0014837..c27e453 100644 --- a/lib/noozoid/config.rb +++ b/lib/noozoid/config.rb @@ -7,22 +7,26 @@ module Noozoid [:nav_next, 'Navigate to next sibling'], [:nav_previous, 'Navigate to previous sibling'], [:nav_root, 'Navigate to tree root'], + [:nav_none, ''], [:node_create, 'Create node'], [:node_delete, 'Delete selected node'], [:node_toggle, 'Toggle node'], + [:node_none, ''], [:main_help, 'Show this help'], [:main_quit, 'Exit program'] ] DEFAULT_KEYS = { - nav_parent: 'h', - nav_child: 'l', - nav_next: 'j', - nav_previous: 'k', + nav_parent: 'LEFT', + nav_child: 'RIGHT', + nav_next: 'DOWN', + nav_previous: 'UP', nav_root: 'r', + nav_none: '', node_create: 'a', node_delete: 'd', node_toggle: 'v', + node_none: '', main_quit: 'q', main_help: '?' } diff --git a/lib/noozoid/gui.rb b/lib/noozoid/gui.rb index 86fa345..46e6b51 100644 --- a/lib/noozoid/gui.rb +++ b/lib/noozoid/gui.rb @@ -1,55 +1,47 @@ +# frozen_string_literal: true require 'curses' module Noozoid + # Gui module Gui end end require_relative 'widgets/help_widget' require_relative 'widgets/main_widget' +require_relative 'node' module Noozoid + # Gui module Gui - def start - Curses.init_screen - # invisible cursor - Curses.curs_set(0) - Curses.refresh - @main_window = MainWindow.new - while true do - @main_window.refresh - @main_window.loop + # app + class App + def initialize + @tree = Node.new('untitled') + end + + def run + Curses.noecho + Curses.nonl + Curses.stdscr.keypad(true) + # Curses.raw + # Curses.stdscr.nodelay = 1 + Curses.curs_set(0) + # Curses.start_color + Curses.init_screen + Curses.refresh + @main_window = MainWidget.new(self) + @main_window.draw + @main_window.wait + rescue StandardError + Curses.close_screen end - rescue Exception => e - Curses.close_screen - puts e end -=begin - win2 = Curses::Window.new(Curses.lines / 2 - 1, Curses.cols / 2 - 1, - Curses.lines / 2, Curses.cols / 2) - win2.box("|", "-") - win2.refresh - 2.upto(win2.maxx - 3) do |i| - win2.setpos(win2.maxy / 2, i) - win2 << "*" - win2.refresh - sleep 0.05 - end - - # Clearing windows each in turn - sleep 0.5 - win1.clear - win1.refresh - win1.close - sleep 0.5 - win2.clear - win2.refresh - win2.close - sleep 0.5 -=end - - module_function :start + def self.start + app = App.new + app.run + end end end diff --git a/lib/noozoid/node.rb b/lib/noozoid/node.rb index 858aaf9..6d2ecce 100644 --- a/lib/noozoid/node.rb +++ b/lib/noozoid/node.rb @@ -1,14 +1,8 @@ +# frozen_string_literal: true module Noozoid # Individual node type class Node - - protected - - attr_accessor :parent - - public - attr_accessor :name, :open attr_reader :children, :parent @@ -24,34 +18,33 @@ module Noozoid child.parent = self end - def [](i) - @children[i] + def [](index) + @children[index] end def toggle! @open = !@open end - def >>(n = 1) + def >>(num = 1) return nil if @parent.nil? idx = @parent.children.index(self) return nil if idx.nil? - @parent[(idx + n) % @parent.children.length] + @parent[(idx + num) % @parent.children.length] end - def <<(n = 1) - self >> -n + def <<(num = 1) + self >> -num end def remove - @parent.children.delete(self) unless @parent.nil? + @parent&.children&.delete(self) end def children? !@children.empty? end - end end diff --git a/lib/noozoid/widgets/help_widget.rb b/lib/noozoid/widgets/help_widget.rb index f8b07c5..ef74d93 100644 --- a/lib/noozoid/widgets/help_widget.rb +++ b/lib/noozoid/widgets/help_widget.rb @@ -1,42 +1,46 @@ +# frozen_string_literal: true +require 'curses' require_relative '../config' module Noozoid module Gui + # HelpWidget class HelpWidget - def initialize - @win = Curses::Window.new( - Curses.lines / 2, - Curses.cols / 2, - Curses.lines / 4, - Curses.cols / 4 - ) - @win.box(?|, ?-) - @win.setpos(0, 1) - @win.addstr(' Noozoid help ') + TITLE_STRING = 'Noozoid help' + QUIT_STRING = 'Press q to close' - self.fill + def initialize(parent) + @parent = parent + @win = Curses::Window.new( + Curses.lines / 2, Curses.cols / 2, + Curses.lines / 4, Curses.cols / 4 + ) + @win.box('|', '-') + @win.setpos(0, @win.maxx / 4) + @win.attron(Curses::A_REVERSE) + @win.addstr(TITLE_STRING.center(@win.maxx / 2)) + @win.attroff(Curses::A_REVERSE) + fill_content @win.refresh end - def fill + def fill_content t = 2 Noozoid::Config::HELP_KEYS.each do |key, desc| - @win.setpos(t, 7) - @win.addstr(Noozoid::Config::DEFAULT_KEYS[key]) - @win.setpos(t, 10) + @win.setpos(t, 2) + @win.addstr(Noozoid::Config::DEFAULT_KEYS[key].rjust(7)) + @win.setpos(t, 11) @win.addstr(desc) - t += 1 end - str = 'Press q to close' - @win.setpos(@win.maxy - 2, @win.maxx - str.size - 2) - @win.addstr(str) + @win.setpos(@win.maxy - 2, @win.maxx - QUIT_STRING.size - 2) + @win.addstr(QUIT_STRING) end - def loop - while true + def wait + loop do key = Curses.getch break if key == 'q' end diff --git a/lib/noozoid/widgets/main_widget.rb b/lib/noozoid/widgets/main_widget.rb index b44fd4a..3143f83 100644 --- a/lib/noozoid/widgets/main_widget.rb +++ b/lib/noozoid/widgets/main_widget.rb @@ -1,48 +1,74 @@ +# frozen_string_literal: true + require_relative '../version' -module Noozoid::Gui - class MainWindow - def initialize - @win = Curses::Window.new(Curses.lines, Curses.cols, 0, 0) - # @win.box(?|, ?-) - # @win.refresh - @title = 'untitled' - end +module Noozoid + module Gui + # Main widget (full screen) + class MainWidget + HEADER_HELP_STR = "noozoid #{Noozoid::VERSION} " \ + '~ Use the arrow keys to navigate, press ? for help' - def title=(value) - @title = value - self.refresh - end + def initialize(parent) + @parent = parent + @win = Curses::Window.new(Curses.lines, Curses.cols, 0, 0) + # @win.box('|', '-') + @win.refresh + @title = 'untitled' + end - def draw_header - top_str = "noozoid #{Noozoid::VERSION} ~ Use the arrow keys to navigate, press ? for help" - @win.attron(Curses::A_REVERSE) - @win.setpos(0, 0) - @win.addstr(" " * Curses.cols) - @win.setpos(0, 0) - @win.addstr(top_str) - @win.attroff(Curses::A_REVERSE) - @win.setpos(1, 0) - @win.addstr('-' * Curses.cols) - @win.setpos(1, 4) - @win.addstr(" #{@title} ") - end + def title=(value) + @title = value + draw_header_title + @win.refresh + end - def refresh - self.draw_header - @win.refresh - end + def draw_header_help + @win.attron(Curses::A_REVERSE) + @win.setpos(0, 0) + @win.addstr(' ' * Curses.cols) + @win.setpos(0, 0) + @win.addstr(HEADER_HELP_STR) + @win.attroff(Curses::A_REVERSE) + end - def loop - key = Curses.getch - @win.setpos(10, 1) - @win.addstr(" #{key.to_s} ") - case key - when ?? - subwin = HelpWidget.new - subwin.loop - when ?q - raise "Exit" + def draw_header_title + @win.setpos(1, 0) + @win.addstr('-' * Curses.cols) + @win.setpos(1, 4) + @win.addstr(" #{@title} ") + end + + def draw + draw_header_help + draw_header_title + @win.refresh + end + + def wait + loop do + key = Curses.getch + case key + when Curses::Key::LEFT + @win.setpos(10, 1) + @win.addstr('LEFT') + when Curses::Key::PPAGE + Curses.setpos(10, 1) + Curses.addstr('Page Up') + when Curses::Key::NPAGE + Curses.setpos(10, 1) + Curses.addstr('Page Dn') + when '?' + subwin = HelpWidget.new(self) + subwin.wait + when 'q' + break + else + @win.setpos(10, 10) + @win.addstr("#{key} ") + end + @win.refresh + end end end end