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
|
||||||
|
@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.curs_set(0)
|
||||||
|
# Curses.start_color
|
||||||
|
Curses.init_screen
|
||||||
Curses.refresh
|
Curses.refresh
|
||||||
@main_window = MainWindow.new
|
@main_window = MainWidget.new(self)
|
||||||
while true do
|
@main_window.draw
|
||||||
@main_window.refresh
|
@main_window.wait
|
||||||
@main_window.loop
|
rescue StandardError
|
||||||
end
|
|
||||||
rescue Exception => e
|
|
||||||
Curses.close_screen
|
Curses.close_screen
|
||||||
puts e
|
end
|
||||||
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("|", "-")
|
|
||||||
win2.refresh
|
|
||||||
2.upto(win2.maxx - 3) do |i|
|
|
||||||
win2.setpos(win2.maxy / 2, i)
|
|
||||||
win2 << "*"
|
|
||||||
win2.refresh
|
|
||||||
sleep 0.05
|
|
||||||
end
|
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)
|
||||||
|
class MainWidget
|
||||||
|
HEADER_HELP_STR = "noozoid #{Noozoid::VERSION} " \
|
||||||
|
'~ Use the arrow keys to navigate, press ? for help'
|
||||||
|
|
||||||
|
def initialize(parent)
|
||||||
|
@parent = parent
|
||||||
@win = Curses::Window.new(Curses.lines, Curses.cols, 0, 0)
|
@win = Curses::Window.new(Curses.lines, Curses.cols, 0, 0)
|
||||||
# @win.box(?|, ?-)
|
# @win.box('|', '-')
|
||||||
# @win.refresh
|
@win.refresh
|
||||||
@title = 'untitled'
|
@title = 'untitled'
|
||||||
end
|
end
|
||||||
|
|
||||||
def title=(value)
|
def title=(value)
|
||||||
@title = value
|
@title = value
|
||||||
self.refresh
|
draw_header_title
|
||||||
|
@win.refresh
|
||||||
end
|
end
|
||||||
|
|
||||||
def draw_header
|
def draw_header_help
|
||||||
top_str = "noozoid #{Noozoid::VERSION} ~ Use the arrow keys to navigate, press ? for help"
|
|
||||||
@win.attron(Curses::A_REVERSE)
|
@win.attron(Curses::A_REVERSE)
|
||||||
@win.setpos(0, 0)
|
@win.setpos(0, 0)
|
||||||
@win.addstr(" " * Curses.cols)
|
@win.addstr(' ' * Curses.cols)
|
||||||
@win.setpos(0, 0)
|
@win.setpos(0, 0)
|
||||||
@win.addstr(top_str)
|
@win.addstr(HEADER_HELP_STR)
|
||||||
@win.attroff(Curses::A_REVERSE)
|
@win.attroff(Curses::A_REVERSE)
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw_header_title
|
||||||
@win.setpos(1, 0)
|
@win.setpos(1, 0)
|
||||||
@win.addstr('-' * Curses.cols)
|
@win.addstr('-' * Curses.cols)
|
||||||
@win.setpos(1, 4)
|
@win.setpos(1, 4)
|
||||||
@win.addstr(" #{@title} ")
|
@win.addstr(" #{@title} ")
|
||||||
end
|
end
|
||||||
|
|
||||||
def refresh
|
def draw
|
||||||
self.draw_header
|
draw_header_help
|
||||||
|
draw_header_title
|
||||||
@win.refresh
|
@win.refresh
|
||||||
end
|
end
|
||||||
|
|
||||||
def loop
|
def wait
|
||||||
|
loop do
|
||||||
key = Curses.getch
|
key = Curses.getch
|
||||||
@win.setpos(10, 1)
|
|
||||||
@win.addstr(" #{key.to_s} ")
|
|
||||||
case key
|
case key
|
||||||
when ??
|
when Curses::Key::LEFT
|
||||||
subwin = HelpWidget.new
|
@win.setpos(10, 1)
|
||||||
subwin.loop
|
@win.addstr('LEFT')
|
||||||
when ?q
|
when Curses::Key::PPAGE
|
||||||
raise "Exit"
|
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