2017-05-07 20:56:31 +00:00
|
|
|
require_relative './dialogue'
|
|
|
|
require_relative './gardner'
|
|
|
|
require_relative './utility'
|
|
|
|
|
2017-05-07 16:59:36 +00:00
|
|
|
# Planter is the module for creating or editing a tree.
|
|
|
|
module Planter
|
2017-10-14 18:39:11 +00:00
|
|
|
|
2017-05-13 23:45:19 +00:00
|
|
|
# In-memory tree
|
2017-05-07 20:56:31 +00:00
|
|
|
class Plot
|
2017-10-14 18:39:11 +00:00
|
|
|
|
2017-05-07 20:56:31 +00:00
|
|
|
# The tree, trunk, and branches
|
|
|
|
attr_accessor :tree, :trunk, :branches
|
|
|
|
|
2017-05-13 23:45:19 +00:00
|
|
|
# Edit the trunk of the tree
|
|
|
|
def edit_trunk
|
2017-05-14 18:13:50 +00:00
|
|
|
puts "Current Trunk:\n"
|
2017-10-14 18:39:11 +00:00
|
|
|
Dialogue.display_trunk(@trunk, true)
|
2017-05-21 19:56:12 +00:00
|
|
|
print "\n[ =EDITING= ](CTRL-C to abort)> "
|
2017-05-14 18:13:50 +00:00
|
|
|
STDOUT.flush
|
|
|
|
begin
|
|
|
|
new_trunk = STDIN.gets.to_s
|
|
|
|
rescue Interrupt
|
|
|
|
puts "\n**Aborting edit**\n\n"
|
|
|
|
new_trunk = @trunk["trunk"]
|
|
|
|
end
|
|
|
|
@trunk["trunk"] = new_trunk
|
2017-05-13 23:45:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Edit a branch on the tree
|
|
|
|
#
|
|
|
|
# @param branch [Integer] The number of the branch to be edited.
|
2017-05-21 19:56:12 +00:00
|
|
|
def edit_branch(branch_no)
|
|
|
|
puts "Current Branch:\n"
|
2017-10-14 18:39:11 +00:00
|
|
|
Dialogue.display_branch(@branches[branch_no], branch_no, true)
|
2017-05-21 19:56:12 +00:00
|
|
|
print "\n[ =EDITING= ](CTRL-C to abort)> "
|
|
|
|
STDOUT.flush
|
|
|
|
begin
|
|
|
|
new_branch = STDIN.gets.to_s
|
|
|
|
rescue Interrupt
|
|
|
|
puts "\n**Aborting edit**\n\n"
|
|
|
|
new_branch = @branches[branch_no]["desc"]
|
|
|
|
end
|
|
|
|
@branches[branch_no]["desc"] = new_branch
|
2017-05-13 23:45:19 +00:00
|
|
|
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
|
2017-05-07 20:56:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Utilities for editing specific parts of a tree.
|
|
|
|
class Spade
|
2017-10-14 18:39:11 +00:00
|
|
|
|
2017-05-07 20:56:31 +00:00
|
|
|
# The file we parse into a tree
|
|
|
|
attr_writer :file
|
|
|
|
|
2017-05-13 23:45:19 +00:00
|
|
|
def initialize(file)
|
2017-05-21 19:56:12 +00:00
|
|
|
@file = file
|
2017-05-13 23:45:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Establish and populate a new Plot (in-memory tree), then control the flow
|
|
|
|
# of editing the Plot
|
2017-05-07 20:56:31 +00:00
|
|
|
def plant
|
|
|
|
@plot = Plot.new
|
|
|
|
@plot.tree = @file
|
2017-05-13 19:01:28 +00:00
|
|
|
@plot.trunk = @file.shift
|
2017-05-07 20:56:31 +00:00
|
|
|
@plot.branches = Gardner.prune_branches(@file)
|
|
|
|
|
2017-05-13 23:45:19 +00:00
|
|
|
next_branch = dig(1)
|
|
|
|
until next_branch == 0 do
|
|
|
|
next_branch = dig(next_branch)
|
|
|
|
end
|
|
|
|
|
|
|
|
puts "\n#{@plot.branches[0]["desc"]}"
|
|
|
|
exit
|
2017-05-07 20:56:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Function for displaying a single branch in debug mode. We also always
|
|
|
|
# display the trunk, since otherwise it's displayed a single time then gone
|
|
|
|
# forever (until next time).
|
|
|
|
#
|
2017-05-07 20:59:24 +00:00
|
|
|
# @param branch_no [Integer] The number of the branch to be displayed.
|
2017-05-07 20:56:31 +00:00
|
|
|
def dig(branch_no)
|
|
|
|
branch = @plot.branches[branch_no]
|
|
|
|
|
2017-10-14 18:39:11 +00:00
|
|
|
Dialogue.display_trunk(@plot.trunk, true)
|
|
|
|
Dialogue.display_branch(branch, branch_no, true)
|
2017-05-13 19:01:28 +00:00
|
|
|
|
2017-05-13 23:45:19 +00:00
|
|
|
response = get_response(branch)
|
|
|
|
to_branch = parse_response(response, branch_no)
|
2017-05-07 20:56:31 +00:00
|
|
|
|
2017-05-13 23:45:19 +00:00
|
|
|
return to_branch
|
2017-05-07 20:56:31 +00:00
|
|
|
end
|
|
|
|
|
2017-05-13 23:45:19 +00:00
|
|
|
# Get a response for the displayed branch
|
2017-05-07 20:56:31 +00:00
|
|
|
#
|
2017-05-13 23:45:19 +00:00
|
|
|
# @param branch [Hash] A branch data set
|
|
|
|
# @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
|
2017-05-07 20:56:31 +00:00
|
|
|
end
|
|
|
|
|
2017-05-13 23:45:19 +00:00
|
|
|
# Parse the response from get_response
|
2017-05-07 20:56:31 +00:00
|
|
|
#
|
2017-05-13 23:45:19 +00:00
|
|
|
# @param response [String] The option selected
|
|
|
|
# @param branch_no [Integer] The currently-displayed branch
|
|
|
|
# @return [Integer] the branch to display
|
|
|
|
def parse_response(response, branch_no)
|
2017-05-14 18:13:50 +00:00
|
|
|
10.times { print "*" }
|
|
|
|
print "\n(Your choice: "
|
|
|
|
|
|
|
|
if response.to_i >= 1
|
|
|
|
print "Change to branch #{response.to_i})\n\n"
|
|
|
|
return response.to_i
|
|
|
|
|
|
|
|
end
|
2017-05-13 23:45:19 +00:00
|
|
|
|
|
|
|
case response.to_s.downcase
|
|
|
|
when "t"
|
2017-05-14 18:13:50 +00:00
|
|
|
print "Edit the trunk.)\n\n"
|
2017-05-13 23:45:19 +00:00
|
|
|
@plot.edit_trunk
|
|
|
|
return branch_no
|
|
|
|
when "a"
|
2017-05-14 18:13:50 +00:00
|
|
|
print "Add a new branch.)\n\n"
|
2017-05-13 23:45:19 +00:00
|
|
|
return branch_no
|
|
|
|
when "b"
|
2017-05-14 18:13:50 +00:00
|
|
|
print "Edit the current branch.)\n\n"
|
2017-05-13 23:45:19 +00:00
|
|
|
@plot.edit_branch(branch_no)
|
|
|
|
return branch_no
|
|
|
|
when "x"
|
2017-05-14 18:13:50 +00:00
|
|
|
print "Delete the current branch.)\n\n"
|
2017-05-13 23:45:19 +00:00
|
|
|
return branch_no
|
|
|
|
when "l"
|
2017-05-14 18:13:50 +00:00
|
|
|
print "Edit leaves of current branch.)\n\n"
|
2017-05-13 23:45:19 +00:00
|
|
|
return branch_no
|
|
|
|
when "s"
|
2017-05-14 18:13:50 +00:00
|
|
|
print "Save changes.)\n\n"
|
2017-05-13 23:45:19 +00:00
|
|
|
return branch_no
|
|
|
|
when "q"
|
2017-05-14 18:13:50 +00:00
|
|
|
print "Quit without saving.)\n\n"
|
2017-05-13 23:45:19 +00:00
|
|
|
print "Unsaved changes will be lost. Still quit? [y/n]> "
|
|
|
|
verify = STDIN.gets.chomp.to_s.downcase
|
|
|
|
|
2017-10-14 19:15:38 +00:00
|
|
|
return 0 if verify == "y"
|
|
|
|
return branch_no
|
2017-05-13 23:45:19 +00:00
|
|
|
else
|
2017-05-14 18:13:50 +00:00
|
|
|
print "Unknown option. Returning to current branch.)\n\n"
|
2017-05-13 23:45:19 +00:00
|
|
|
return branch_no
|
|
|
|
end
|
2017-05-07 20:56:31 +00:00
|
|
|
end
|
|
|
|
end
|
2017-05-07 16:59:36 +00:00
|
|
|
end
|
2017-05-07 20:56:31 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
Process:
|
|
|
|
- User selects to create/edit a tree
|
|
|
|
- If the user presented a file, use it as the tree file
|
|
|
|
- If the user failed to provide a file, create a new tree file in the current directory
|
|
|
|
|
|
|
|
- Check if the file is empty.
|
|
|
|
- If so, assume a new tree
|
|
|
|
- If not, verify formatting
|
|
|
|
|
|
|
|
- If new tree, prompt for trunk
|
|
|
|
- If existing tree, display trunk and first branch
|
|
|
|
|
|
|
|
- At this point, editing is the same
|
|
|
|
- Prompt provides options:
|
2017-05-13 23:45:19 +00:00
|
|
|
- #: Go to that branch number
|
|
|
|
- T: Modify the tree trunk
|
|
|
|
- A: Add a new branch (append to list of branches)
|
|
|
|
- B: Modify the current branch description
|
|
|
|
- X: Delete the current branch (does this renumber branches?)
|
|
|
|
- L: Modify the current leaves, respond with leaf prompt
|
|
|
|
- S: Save changes
|
|
|
|
- Q: Quit
|
|
|
|
|
|
|
|
- Example prompt:
|
|
|
|
[ 0-5,T,A,B,X,L,S,Q ]>
|
|
|
|
- Leaf prompt:
|
2017-05-07 20:56:31 +00:00
|
|
|
|
|
|
|
Details:
|
|
|
|
|
|
|
|
- Regardless of the file existing, the user will be editing a Hash, not the actual YAML file
|
2017-05-13 23:45:19 +00:00
|
|
|
- Use Gardner to build and interact with the tree
|
|
|
|
- Use Planter to modify the tree "in memory" (aka, the hash)
|
|
|
|
- Use Dialog to interact with the tree "in memory", to test-run it
|
2017-05-07 20:56:31 +00:00
|
|
|
- After each edit option, display the current branch with the new changes.
|
|
|
|
|
|
|
|
=end
|