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
class Cli < Thor

View file

@ -1,24 +1,12 @@
# 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
# app
class App
def initialize
@tree = Node.new('untitled')
@tree = Noozoid::Models::Node.new('untitled')
end
def run
@ -31,11 +19,16 @@ module Noozoid
# Curses.start_color
Curses.init_screen
Curses.refresh
@main_window = MainWidget.new(self)
# attach models to views
@main_window = MainView.new(self)
@main_window.draw
@main_window.wait
rescue StandardError
rescue StandardError => e
Curses.close_screen
warn e.message unless e.nil?
warn e.backtrace unless e.nil?
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_relative '../config'
require_relative '../utils/auto_ajust'
module Noozoid
module Gui
# HelpWidget
class HelpWidget
# HelpView
class HelpView
TITLE_STRING = 'Noozoid help'
QUIT_STRING = 'Press q to close'
def initialize(parent)
@parent = parent
@win = Curses::Window.new(
Curses.lines / 2, Curses.cols / 2,
Curses.lines / 4, Curses.cols / 4
)
@display = false
end
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.setpos(0, @win.maxx / 4)
@win.attron(Curses::A_REVERSE)
@ -23,6 +30,14 @@ module Noozoid
@win.attroff(Curses::A_REVERSE)
fill_content
@win.refresh
@display = true
end
def hide
@win.clear
@win.refresh
@win.close
@display = false
end
def fill_content
@ -42,11 +57,9 @@ module Noozoid
def wait
loop do
key = Curses.getch
@win.refresh if key == Curses::Key::RESIZE
break if key == 'q'
end
@win.clear
@win.refresh
@win.close
end
end
end

View file

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

View file

@ -7,26 +7,12 @@
#
# Press `h` key when running for help.
require 'noozoid/cli'
require 'noozoid'
Noozoid::Cli.start(ARGV)
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
puts '= Commands ='
puts ''