Fix base navigation
This commit is contained in:
parent
69675eec38
commit
12df4f909c
5 changed files with 135 additions and 116 deletions
|
@ -7,22 +7,26 @@ module Noozoid
|
||||||
[:nav_next, 'Navigate to next sibling'],
|
[:nav_next, 'Navigate to next sibling'],
|
||||||
[:nav_previous, 'Navigate to previous sibling'],
|
[:nav_previous, 'Navigate to previous sibling'],
|
||||||
[:nav_root, 'Navigate to tree root'],
|
[:nav_root, 'Navigate to tree root'],
|
||||||
|
[:nav_none, ''],
|
||||||
[:node_create, 'Create node'],
|
[:node_create, 'Create node'],
|
||||||
[:node_delete, 'Delete selected node'],
|
[:node_delete, 'Delete selected node'],
|
||||||
[:node_toggle, 'Toggle node'],
|
[:node_toggle, 'Toggle node'],
|
||||||
|
[:node_none, ''],
|
||||||
[:main_help, 'Show this help'],
|
[:main_help, 'Show this help'],
|
||||||
[:main_quit, 'Exit program']
|
[:main_quit, 'Exit program']
|
||||||
]
|
]
|
||||||
|
|
||||||
DEFAULT_KEYS = {
|
DEFAULT_KEYS = {
|
||||||
nav_parent: 'h',
|
nav_parent: 'LEFT',
|
||||||
nav_child: 'l',
|
nav_child: 'RIGHT',
|
||||||
nav_next: 'j',
|
nav_next: 'DOWN',
|
||||||
nav_previous: 'k',
|
nav_previous: 'UP',
|
||||||
nav_root: 'r',
|
nav_root: 'r',
|
||||||
|
nav_none: '',
|
||||||
node_create: 'a',
|
node_create: 'a',
|
||||||
node_delete: 'd',
|
node_delete: 'd',
|
||||||
node_toggle: 'v',
|
node_toggle: 'v',
|
||||||
|
node_none: '',
|
||||||
main_quit: 'q',
|
main_quit: 'q',
|
||||||
main_help: '?'
|
main_help: '?'
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,55 +1,47 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'curses'
|
require 'curses'
|
||||||
|
|
||||||
module Noozoid
|
module Noozoid
|
||||||
|
# Gui
|
||||||
module Gui
|
module Gui
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
require_relative 'widgets/help_widget'
|
require_relative 'widgets/help_widget'
|
||||||
require_relative 'widgets/main_widget'
|
require_relative 'widgets/main_widget'
|
||||||
|
require_relative 'node'
|
||||||
|
|
||||||
module Noozoid
|
module Noozoid
|
||||||
|
# Gui
|
||||||
module Gui
|
module Gui
|
||||||
def start
|
# app
|
||||||
Curses.init_screen
|
class App
|
||||||
# invisible cursor
|
def initialize
|
||||||
Curses.curs_set(0)
|
@tree = Node.new('untitled')
|
||||||
Curses.refresh
|
end
|
||||||
@main_window = MainWindow.new
|
|
||||||
while true do
|
def run
|
||||||
@main_window.refresh
|
Curses.noecho
|
||||||
@main_window.loop
|
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
|
end
|
||||||
rescue Exception => e
|
|
||||||
Curses.close_screen
|
|
||||||
puts e
|
|
||||||
end
|
end
|
||||||
|
|
||||||
=begin
|
def self.start
|
||||||
win2 = Curses::Window.new(Curses.lines / 2 - 1, Curses.cols / 2 - 1,
|
app = App.new
|
||||||
Curses.lines / 2, Curses.cols / 2)
|
app.run
|
||||||
win2.box("|", "-")
|
end
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,14 +1,8 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Noozoid
|
module Noozoid
|
||||||
# Individual node type
|
# Individual node type
|
||||||
class Node
|
class Node
|
||||||
|
|
||||||
protected
|
|
||||||
|
|
||||||
attr_accessor :parent
|
|
||||||
|
|
||||||
public
|
|
||||||
|
|
||||||
attr_accessor :name, :open
|
attr_accessor :name, :open
|
||||||
attr_reader :children, :parent
|
attr_reader :children, :parent
|
||||||
|
|
||||||
|
@ -24,34 +18,33 @@ module Noozoid
|
||||||
child.parent = self
|
child.parent = self
|
||||||
end
|
end
|
||||||
|
|
||||||
def [](i)
|
def [](index)
|
||||||
@children[i]
|
@children[index]
|
||||||
end
|
end
|
||||||
|
|
||||||
def toggle!
|
def toggle!
|
||||||
@open = !@open
|
@open = !@open
|
||||||
end
|
end
|
||||||
|
|
||||||
def >>(n = 1)
|
def >>(num = 1)
|
||||||
return nil if @parent.nil?
|
return nil if @parent.nil?
|
||||||
|
|
||||||
idx = @parent.children.index(self)
|
idx = @parent.children.index(self)
|
||||||
return nil if idx.nil?
|
return nil if idx.nil?
|
||||||
|
|
||||||
@parent[(idx + n) % @parent.children.length]
|
@parent[(idx + num) % @parent.children.length]
|
||||||
end
|
end
|
||||||
|
|
||||||
def <<(n = 1)
|
def <<(num = 1)
|
||||||
self >> -n
|
self >> -num
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove
|
def remove
|
||||||
@parent.children.delete(self) unless @parent.nil?
|
@parent&.children&.delete(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
def children?
|
def children?
|
||||||
!@children.empty?
|
!@children.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,42 +1,46 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'curses'
|
||||||
require_relative '../config'
|
require_relative '../config'
|
||||||
|
|
||||||
module Noozoid
|
module Noozoid
|
||||||
module Gui
|
module Gui
|
||||||
|
# HelpWidget
|
||||||
class HelpWidget
|
class HelpWidget
|
||||||
def initialize
|
TITLE_STRING = 'Noozoid help'
|
||||||
@win = Curses::Window.new(
|
QUIT_STRING = 'Press q to close'
|
||||||
Curses.lines / 2,
|
|
||||||
Curses.cols / 2,
|
|
||||||
Curses.lines / 4,
|
|
||||||
Curses.cols / 4
|
|
||||||
)
|
|
||||||
@win.box(?|, ?-)
|
|
||||||
@win.setpos(0, 1)
|
|
||||||
@win.addstr(' Noozoid help ')
|
|
||||||
|
|
||||||
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
|
@win.refresh
|
||||||
end
|
end
|
||||||
|
|
||||||
def fill
|
def fill_content
|
||||||
t = 2
|
t = 2
|
||||||
Noozoid::Config::HELP_KEYS.each do |key, desc|
|
Noozoid::Config::HELP_KEYS.each do |key, desc|
|
||||||
@win.setpos(t, 7)
|
@win.setpos(t, 2)
|
||||||
@win.addstr(Noozoid::Config::DEFAULT_KEYS[key])
|
@win.addstr(Noozoid::Config::DEFAULT_KEYS[key].rjust(7))
|
||||||
@win.setpos(t, 10)
|
@win.setpos(t, 11)
|
||||||
@win.addstr(desc)
|
@win.addstr(desc)
|
||||||
|
|
||||||
t += 1
|
t += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
str = 'Press q to close'
|
@win.setpos(@win.maxy - 2, @win.maxx - QUIT_STRING.size - 2)
|
||||||
@win.setpos(@win.maxy - 2, @win.maxx - str.size - 2)
|
@win.addstr(QUIT_STRING)
|
||||||
@win.addstr(str)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def loop
|
def wait
|
||||||
while true
|
loop do
|
||||||
key = Curses.getch
|
key = Curses.getch
|
||||||
break if key == 'q'
|
break if key == 'q'
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,48 +1,74 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require_relative '../version'
|
require_relative '../version'
|
||||||
|
|
||||||
module Noozoid::Gui
|
module Noozoid
|
||||||
class MainWindow
|
module Gui
|
||||||
def initialize
|
# Main widget (full screen)
|
||||||
@win = Curses::Window.new(Curses.lines, Curses.cols, 0, 0)
|
class MainWidget
|
||||||
# @win.box(?|, ?-)
|
HEADER_HELP_STR = "noozoid #{Noozoid::VERSION} " \
|
||||||
# @win.refresh
|
'~ Use the arrow keys to navigate, press ? for help'
|
||||||
@title = 'untitled'
|
|
||||||
end
|
|
||||||
|
|
||||||
def title=(value)
|
def initialize(parent)
|
||||||
@title = value
|
@parent = parent
|
||||||
self.refresh
|
@win = Curses::Window.new(Curses.lines, Curses.cols, 0, 0)
|
||||||
end
|
# @win.box('|', '-')
|
||||||
|
@win.refresh
|
||||||
|
@title = 'untitled'
|
||||||
|
end
|
||||||
|
|
||||||
def draw_header
|
def title=(value)
|
||||||
top_str = "noozoid #{Noozoid::VERSION} ~ Use the arrow keys to navigate, press ? for help"
|
@title = value
|
||||||
@win.attron(Curses::A_REVERSE)
|
draw_header_title
|
||||||
@win.setpos(0, 0)
|
@win.refresh
|
||||||
@win.addstr(" " * Curses.cols)
|
end
|
||||||
@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 refresh
|
def draw_header_help
|
||||||
self.draw_header
|
@win.attron(Curses::A_REVERSE)
|
||||||
@win.refresh
|
@win.setpos(0, 0)
|
||||||
end
|
@win.addstr(' ' * Curses.cols)
|
||||||
|
@win.setpos(0, 0)
|
||||||
|
@win.addstr(HEADER_HELP_STR)
|
||||||
|
@win.attroff(Curses::A_REVERSE)
|
||||||
|
end
|
||||||
|
|
||||||
def loop
|
def draw_header_title
|
||||||
key = Curses.getch
|
@win.setpos(1, 0)
|
||||||
@win.setpos(10, 1)
|
@win.addstr('-' * Curses.cols)
|
||||||
@win.addstr(" #{key.to_s} ")
|
@win.setpos(1, 4)
|
||||||
case key
|
@win.addstr(" #{@title} ")
|
||||||
when ??
|
end
|
||||||
subwin = HelpWidget.new
|
|
||||||
subwin.loop
|
def draw
|
||||||
when ?q
|
draw_header_help
|
||||||
raise "Exit"
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue