planter.rb: Implement editor logic

- Added flow for working with editor:
  - Allow for changing branches
  - Recognize all options, with ease of implementing more
  - Allow for exiting the editor
- Move assignment into initialize
This commit is contained in:
Bill Niblock 2017-05-13 19:45:19 -04:00
parent 1c6206c3cd
commit 4b33abcd75

View file

@ -4,11 +4,30 @@ require_relative './utility'
# Planter is the module for creating or editing a tree. # Planter is the module for creating or editing a tree.
module Planter module Planter
# A data class # In-memory tree
class Plot class Plot
# The tree, trunk, and branches # The tree, trunk, and branches
attr_accessor :tree, :trunk, :branches attr_accessor :tree, :trunk, :branches
# Edit the trunk of the tree
def edit_trunk
puts "Edit trunk"
end
# Edit a branch on the tree
#
# @param branch [Integer] The number of the branch to be edited.
def edit_branch(branch)
puts "Edit current branch"
end
# Edit a leaf on a branch, grasshopper
#
# @param branch [Integer] The number of the branch to be edited.
# @param leaf [Hash] The leaf hash to be edited.
def edit_leaf(branch, leaf)
end
end end
# Utilities for editing specific parts of a tree. # Utilities for editing specific parts of a tree.
@ -16,15 +35,25 @@ module Planter
# The file we parse into a tree # The file we parse into a tree
attr_writer :file attr_writer :file
# Establish a new Plot, which is basically an object for storing information def initialize(file)
# for us. From here, we start gardening. @file=file
end
# Establish and populate a new Plot (in-memory tree), then control the flow
# of editing the Plot
def plant def plant
@plot = Plot.new @plot = Plot.new
@plot.tree = @file @plot.tree = @file
@plot.trunk = @file.shift @plot.trunk = @file.shift
@plot.branches = Gardner.prune_branches(@file) @plot.branches = Gardner.prune_branches(@file)
dig(1) next_branch = dig(1)
until next_branch == 0 do
next_branch = dig(next_branch)
end
puts "\n#{@plot.branches[0]["desc"]}"
exit
end end
# Function for displaying a single branch in debug mode. We also always # Function for displaying a single branch in debug mode. We also always
@ -38,28 +67,76 @@ module Planter
Gardner.display_trunk(@plot.trunk) Gardner.display_trunk(@plot.trunk)
Gardner.display_branch(branch, branch_no, true) Gardner.display_branch(branch, branch_no, true)
response = get_response(branch)
to_branch = parse_response(response, branch_no)
return to_branch
end end
# Edit the trunk of the tree # Get a response for the displayed branch
def edit_trunk
end
# Edit a branch on the tree
# #
# @param branch [Integer] The number of the branch to be edited. # @param branch [Hash] A branch data set
def edit_branch(branch) # @return [Integer] the next branch
def get_response(branch)
total_branches = @plot.branches.count - 1
valid_options = ["1-#{total_branches}","t","a","b","x","l","s","q"]
print_options = valid_options.join(",")
print "\n[#{print_options}]> "
STDOUT.flush
response = STDIN.gets.chomp.to_s.downcase
until valid_options.include?(response) or response.to_i.between?(1,total_branches)
print "[## Invalid response. "
print "Valid options are #{print_options}"
print "\n[#{print_options}]> "
response = STDIN.gets.chomp.to_s.downcase
end
return response
end end
# Edit a leaf on a branch, grasshopper # Parse the response from get_response
# #
# @param branch [Integer] The number of the branch to be edited. # @param response [String] The option selected
# @param leaf [Hash] The leaf hash to be edited. # @param branch_no [Integer] The currently-displayed branch
def edit_leaf(branch, leaf) # @return [Integer] the branch to display
def parse_response(response, branch_no)
return response.to_i if response.to_i >= 1
case response.to_s.downcase
when "t"
@plot.edit_trunk
return branch_no
when "a"
puts "Add new branch"
return branch_no
when "b"
@plot.edit_branch(branch_no)
return branch_no
when "x"
puts "Delete current branch"
return branch_no
when "l"
puts "Edit leaves"
return branch_no
when "s"
puts "Save changes"
return branch_no
when "q"
print "Unsaved changes will be lost. Still quit? [y/n]> "
verify = STDIN.gets.chomp.to_s.downcase
return 0 if verify == "y"
return branch_no
else
puts "Something else!"
return branch_no
end
end end
end end
end end
@ -80,24 +157,26 @@ Process:
- At this point, editing is the same - At this point, editing is the same
- Prompt provides options: - Prompt provides options:
- B # - Go to Branch # - #: Go to that branch number
- T - Edit Trunk - T: Modify the tree trunk
- D - Edit Current Branch description - A: Add a new branch (append to list of branches)
- X - Delete Current Branch - B: Modify the current branch description
- L A - Add a new Leaf - X: Delete the current branch (does this renumber branches?)
- L # D - Edit Leaf # description - L: Modify the current leaves, respond with leaf prompt
- L # B - Edit Leaf # branch destination - S: Save changes
- L # X - Remove Leaf # - Q: Quit
- S - Save Changes (write to the file)
- Q - Quit without saving - Example prompt:
[ 0-5,T,A,B,X,L,S,Q ]>
- Leaf prompt:
[ Leaves:
Details: Details:
- Regardless of the file existing, the user will be editing a Hash, not the actual YAML file - Regardless of the file existing, the user will be editing a Hash, not the actual YAML file
- Use Gardner to build the tree (either existing or skeleton) - Use Gardner to build and interact with the tree
- Use Dialog to interact with the tree - Use Planter to modify the tree "in memory" (aka, the hash)
- Overwrite prompt with Planter prompt - Use Dialog to interact with the tree "in memory", to test-run it
- On changes, re-build the tree, and restart dialogue from most recent branch
- After each edit option, display the current branch with the new changes. - After each edit option, display the current branch with the new changes.
=end =end