Reorganize code tree

This commit is contained in:
Glenn Y. Rolland 2021-01-02 13:57:42 +01:00
parent 76a9316e5c
commit b230729de5
16 changed files with 118 additions and 101 deletions

View file

@ -1,6 +0,0 @@
require "noozoid/version"
module Noozoid
class Error < StandardError; end
# Your code goes here...
end

View file

@ -1,50 +0,0 @@
# frozen_string_literal: true
module Noozoid
# Individual node type
class Node
attr_accessor :name, :open
attr_reader :children, :parent
def initialize(name = 'untitled')
@name = name
@children = []
@parent = nil
@open = true
end
def []=(child)
@children.push(child)
child.parent = self
end
def [](index)
@children[index]
end
def toggle!
@open = !@open
end
def >>(num = 1)
return nil if @parent.nil?
idx = @parent.children.index(self)
return nil if idx.nil?
@parent[(idx + num) % @parent.children.length]
end
def <<(num = 1)
self >> -num
end
def remove
@parent&.children&.delete(self)
end
def children?
!@children.empty?
end
end
end

View file

@ -1,8 +1,4 @@
require 'thor'
require_relative 'gui'
module Noozoid module Noozoid
class Cli < Thor class Cli < Thor

View file

@ -1,24 +1,12 @@
# frozen_string_literal: true # 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 module Noozoid
# Gui # Gui
module Gui module Gui
# app # app
class App class App
def initialize def initialize
@tree = Node.new('untitled') @tree = Noozoid::Models::Node.new('untitled')
end end
def run def run
@ -31,11 +19,16 @@ module Noozoid
# Curses.start_color # Curses.start_color
Curses.init_screen Curses.init_screen
Curses.refresh Curses.refresh
@main_window = MainWidget.new(self)
# attach models to views
@main_window = MainView.new(self)
@main_window.draw @main_window.draw
@main_window.wait @main_window.wait
rescue StandardError
rescue StandardError => e
Curses.close_screen Curses.close_screen
warn e.message unless e.nil?
warn e.backtrace unless e.nil?
end end
end end

71
src/lib/models/node.rb Normal file
View file

@ -0,0 +1,71 @@
# frozen_string_literal: true
require 'observer'
module Noozoid
# Individual node type
module Models
# Proper output of a tree
# module PrettyPrint
# def self.tree(subtree, current, indent = 0)
# print ' ' * indent
# print subtree == current ? '> ' : '- '
# print subtree.name + "\n"
# return unless subtree.open
# subtree.children.each do |child|
# tree(child, current, indent + 2)
# end
# end
# end
# Mindmap node
class Node
include Observable
attr_accessor :name, :content, :open
attr_reader :children, :parent
def initialize(name = 'untitled')
@name = name
@content = nil
@children = []
@parent = nil
@open = true
end
def []=(child)
@children.push(child)
child.parent = self
end
def [](index)
@children[index]
end
def toggle!
@open = !@open
end
def >>(num = 1)
return nil if @parent.nil?
idx = @parent.children.index(self)
return nil if idx.nil?
@parent[(idx + num) % @parent.children.length]
end
def <<(num = 1)
self >> -num
end
def remove
@parent&.children&.delete(self)
end
def children?
!@children.empty?
end
end
end
end

View file

@ -0,0 +1,12 @@
module Noozoid
module Utils
module AutoAdjust
def self.set(ideal, min, max)
target = ideal
target = min if target < min
target = max if target > max
target
end
end
end
end

View file

@ -2,20 +2,27 @@
require 'curses' require 'curses'
require_relative '../config' require_relative '../config'
require_relative '../utils/auto_ajust'
module Noozoid module Noozoid
module Gui module Gui
# HelpWidget # HelpView
class HelpWidget class HelpView
TITLE_STRING = 'Noozoid help' TITLE_STRING = 'Noozoid help'
QUIT_STRING = 'Press q to close' QUIT_STRING = 'Press q to close'
def initialize(parent) def initialize(parent)
@parent = parent @parent = parent
@win = Curses::Window.new( @display = false
Curses.lines / 2, Curses.cols / 2, end
Curses.lines / 4, Curses.cols / 4
) def show
lines_size = Utils::AutoAdjust.set(Curses.lines / 2, 10, 20)
lines_top = (Curses.lines - lines_size) / 2
cols_size = Utils::AutoAdjust.set(Curses.cols / 2, 20, 60)
cols_left = (Curses.cols - cols_size) / 2
@win = Curses::Window.new(lines_size, cols_size, lines_top, cols_left)
@win.box('|', '-') @win.box('|', '-')
@win.setpos(0, @win.maxx / 4) @win.setpos(0, @win.maxx / 4)
@win.attron(Curses::A_REVERSE) @win.attron(Curses::A_REVERSE)
@ -23,6 +30,14 @@ module Noozoid
@win.attroff(Curses::A_REVERSE) @win.attroff(Curses::A_REVERSE)
fill_content fill_content
@win.refresh @win.refresh
@display = true
end
def hide
@win.clear
@win.refresh
@win.close
@display = false
end end
def fill_content def fill_content
@ -42,11 +57,9 @@ module Noozoid
def wait def wait
loop do loop do
key = Curses.getch key = Curses.getch
@win.refresh if key == Curses::Key::RESIZE
break if key == 'q' break if key == 'q'
end end
@win.clear
@win.refresh
@win.close
end end
end end
end end

View file

@ -5,7 +5,7 @@ require_relative '../version'
module Noozoid module Noozoid
module Gui module Gui
# Main widget (full screen) # Main widget (full screen)
class MainWidget class MainView
HEADER_HELP_STR = "noozoid #{Noozoid::VERSION} " \ HEADER_HELP_STR = "noozoid #{Noozoid::VERSION} " \
'~ Use the arrow keys to navigate, press ? for help' '~ Use the arrow keys to navigate, press ? for help'
@ -49,6 +49,8 @@ module Noozoid
loop do loop do
key = Curses.getch key = Curses.getch
case key case key
when Curses::Key::RESIZE
@win.refresh
when Curses::Key::LEFT when Curses::Key::LEFT
@win.setpos(10, 1) @win.setpos(10, 1)
@win.addstr('LEFT') @win.addstr('LEFT')
@ -59,7 +61,7 @@ module Noozoid
Curses.setpos(10, 1) Curses.setpos(10, 1)
Curses.addstr('Page Dn') Curses.addstr('Page Dn')
when '?' when '?'
subwin = HelpWidget.new(self) subwin = HelpView.new(self)
subwin.wait subwin.wait
when 'q' when 'q'
break break

View file

@ -7,26 +7,12 @@
# #
# Press `h` key when running for help. # Press `h` key when running for help.
require 'noozoid/cli' require 'noozoid'
Noozoid::Cli.start(ARGV) Noozoid::Cli.start(ARGV)
exit 0 exit 0
# Proper output of a tree
module PrettyPrint
def self.tree(subtree, current, indent = 0)
print ' ' * indent
print subtree == current ? '> ' : '- '
print subtree.name + "\n"
return unless subtree.open
subtree.children.each do |child|
tree(child, current, indent + 2)
end
end
end
def print_help def print_help
puts '= Commands =' puts '= Commands ='
puts '' puts ''