diff --git a/lib/noozoid.rb b/lib/noozoid.rb deleted file mode 100644 index 9b6274a..0000000 --- a/lib/noozoid.rb +++ /dev/null @@ -1,6 +0,0 @@ -require "noozoid/version" - -module Noozoid - class Error < StandardError; end - # Your code goes here... -end diff --git a/lib/noozoid/node.rb b/lib/noozoid/node.rb deleted file mode 100644 index 6d2ecce..0000000 --- a/lib/noozoid/node.rb +++ /dev/null @@ -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 diff --git a/lib/noozoid/actions/node_create.rb b/src/lib/actions/node_create.rb similarity index 100% rename from lib/noozoid/actions/node_create.rb rename to src/lib/actions/node_create.rb diff --git a/lib/noozoid/actions/node_delete.rb b/src/lib/actions/node_delete.rb similarity index 100% rename from lib/noozoid/actions/node_delete.rb rename to src/lib/actions/node_delete.rb diff --git a/lib/noozoid/actions/node_edit.rb b/src/lib/actions/node_edit.rb similarity index 100% rename from lib/noozoid/actions/node_edit.rb rename to src/lib/actions/node_edit.rb diff --git a/lib/noozoid/actions/node_move.rb b/src/lib/actions/node_move.rb similarity index 100% rename from lib/noozoid/actions/node_move.rb rename to src/lib/actions/node_move.rb diff --git a/lib/noozoid/actions/node_rename.rb b/src/lib/actions/node_rename.rb similarity index 100% rename from lib/noozoid/actions/node_rename.rb rename to src/lib/actions/node_rename.rb diff --git a/lib/noozoid/cli.rb b/src/lib/cli.rb similarity index 78% rename from lib/noozoid/cli.rb rename to src/lib/cli.rb index bb1ba30..7f8985c 100644 --- a/lib/noozoid/cli.rb +++ b/src/lib/cli.rb @@ -1,8 +1,4 @@ -require 'thor' - -require_relative 'gui' - module Noozoid class Cli < Thor diff --git a/lib/noozoid/config.rb b/src/lib/config.rb similarity index 100% rename from lib/noozoid/config.rb rename to src/lib/config.rb diff --git a/lib/noozoid/gui.rb b/src/lib/gui.rb similarity index 66% rename from lib/noozoid/gui.rb rename to src/lib/gui.rb index 46e6b51..294d798 100644 --- a/lib/noozoid/gui.rb +++ b/src/lib/gui.rb @@ -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 diff --git a/src/lib/models/node.rb b/src/lib/models/node.rb new file mode 100644 index 0000000..ad676f5 --- /dev/null +++ b/src/lib/models/node.rb @@ -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 diff --git a/src/lib/utils/auto_ajust.rb b/src/lib/utils/auto_ajust.rb new file mode 100644 index 0000000..94fd482 --- /dev/null +++ b/src/lib/utils/auto_ajust.rb @@ -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 diff --git a/lib/noozoid/version.rb b/src/lib/version.rb similarity index 100% rename from lib/noozoid/version.rb rename to src/lib/version.rb diff --git a/lib/noozoid/widgets/help_widget.rb b/src/lib/views/help.rb similarity index 65% rename from lib/noozoid/widgets/help_widget.rb rename to src/lib/views/help.rb index ef74d93..ddbdba3 100644 --- a/lib/noozoid/widgets/help_widget.rb +++ b/src/lib/views/help.rb @@ -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 diff --git a/lib/noozoid/widgets/main_widget.rb b/src/lib/views/main.rb similarity index 93% rename from lib/noozoid/widgets/main_widget.rb rename to src/lib/views/main.rb index 3143f83..75174f6 100644 --- a/lib/noozoid/widgets/main_widget.rb +++ b/src/lib/views/main.rb @@ -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 diff --git a/exe/noozoid b/src/main.cr similarity index 85% rename from exe/noozoid rename to src/main.cr index 6346281..a657c0b 100755 --- a/exe/noozoid +++ b/src/main.cr @@ -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 ''