From 252ad93d5e4a1034167badfed67da2f1c6d7ed71 Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sun, 7 May 2017 23:43:35 -0400 Subject: [PATCH 01/21] sapling/utility.rb: Minor style refactoring --- lib/sapling.rb | 4 +--- lib/sapling/utility.rb | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/sapling.rb b/lib/sapling.rb index 890f562..c80903f 100644 --- a/lib/sapling.rb +++ b/lib/sapling.rb @@ -11,11 +11,9 @@ require_relative 'sapling/utility' # Sapling is the main module for the program. From here, the rest of the world # starts building. module Sapling - # CLI is the class for option parsing, and the gateway to the program, on the # command line class CLI - # Option parsing, and gateway to either reading and traversing a tree, or # editing/creating a tree. def talk(options) @@ -71,7 +69,7 @@ module Sapling end - # Hacky way of dealing with bad options + # Handle bad options gracefully begin opt_parser.parse!(options) rescue OptionParser::InvalidOption diff --git a/lib/sapling/utility.rb b/lib/sapling/utility.rb index 12b13f1..576122a 100644 --- a/lib/sapling/utility.rb +++ b/lib/sapling/utility.rb @@ -55,5 +55,4 @@ def verify_tree(file) end results.include?(false) ? false : true - end From e27cd43e7c3221eb0744b1ec584aeabca33be143 Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sun, 7 May 2017 23:46:19 -0400 Subject: [PATCH 02/21] dialogue.rb: Refactor - Abstract functionality from talk into individual functions: - display_trunk: Displays the trunk of the branch. - display_branch: Displays the branch and option list - get_response: Displays the conversation prompt, and gets the response - Of note, this allows the editor to make use of talk functionality --- lib/sapling/dialogue.rb | 72 ++++++++++++++++++++++++++++------------- 1 file changed, 50 insertions(+), 22 deletions(-) diff --git a/lib/sapling/dialogue.rb b/lib/sapling/dialogue.rb index bbae8b9..35acec9 100644 --- a/lib/sapling/dialogue.rb +++ b/lib/sapling/dialogue.rb @@ -2,13 +2,11 @@ require_relative './gardner' # Dialogue is the module for traversing an existing tree. module Dialogue - - # Spealer holds the functionality for viewing and going through a dialogue + # Speaker holds the functionality for viewing and going through a dialogue # tree. class Speaker # The file, which should be a dialogue tree YAML file. attr_accessor :file - # Status of verbose/debug mode. True = on; false = off. attr_accessor :debug @@ -20,26 +18,26 @@ module Dialogue # Conversation handles navigating the tree, until the option to end is # reached. def conversation() - tree = Gardner.grow(@file) + tree = Gardner.prune_trunk(@file) - 10.times { print "*" } - puts "\n[ Branch: 1 ]" if @debug - next_branch = talk(tree[1]) + display_trunk(tree[0]) + branches = Gardner.prune_branches(tree[1]) + + next_branch = 1 until next_branch == 0 do - puts "\n[ Branch: #{next_branch} ]" if @debug - next_branch = talk(tree[next_branch]) + next_branch = talk(branches[next_branch], next_branch) end - puts "\n#{tree[0]["desc"]}" + puts "\n#{branches[0]["desc"]}" exit end - # Talk displays a branch, the options, and prompts for a response + # Talk displays a branch, the options, and prompts for a response. # # @param branch [Hash] A branch data set + # @param branch_no [Integer] The branch number # @return [Integer] The number of the next branch - def talk(branch) - + def talk(branch, branch_no) # If there are no options on this branch, we assume it's a terminal # branch. Return 0, and end the program. if branch["options"].empty? @@ -47,13 +45,50 @@ module Dialogue return 0 end - valid_options = branch["options"].keys.join(", ") + display_branch(branch, branch_no) + response = get_response(branch) + + unless response == 0 + puts "\n" + 10.times { print "*" } + puts "\n(Your choice: #{branch["options"][response].keys[0]})" + response = branch["options"][response].values[0].to_i + end + + return response + end + + # Format and display the trunk + # + # @param trunk [Hash] The trunk hash + def display_trunk(trunk) + 40.times { print "-" } + puts "\n#{trunk["trunk"]}" + 40.times { print "-" } + puts "\n" + end + + # Format and display a branch + # + # @param branch [Hash] A branch data set + # @param branch_no [Integer] The branch number + def display_branch(branch, branch_no) + puts "\n[ Branch: #{branch_no} ]" if @debug puts "\n#{branch["desc"]}\n\n" + branch["options"].each_pair do |k,v| puts "\t#{k}: #{v.keys[0]}" puts "\t\t [ Goes to branch #{v.values[0]} ]" if @debug end + end + + # Get a response for the displayed branch + # + # @param branch [Hash] A branch data set + # @return [Integer] the next branch + def get_response(branch) + valid_options = branch["options"].keys.join(", ") print "\n[#{valid_options}]> " STDOUT.flush @@ -66,14 +101,7 @@ module Dialogue response = STDIN.gets.chomp.to_i end - - puts "\n" - 10.times { print "*" } - return 0 if response == 0 - puts "\n(Your choice: #{branch["options"][response].keys[0]})" - return branch["options"][response].values[0].to_i + return response end - end - end From a2dd7e3b1cd57623ae33fb341a30489d04c3dbdc Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sun, 7 May 2017 23:49:12 -0400 Subject: [PATCH 03/21] gardner.rb: Refactor - Remove functionality that doesn't belong in this module: - Move printing of trunk to the dialogue class - Remove tree generation entirely; let other modules generate trees as they need them - Style formatting --- lib/sapling/gardner.rb | 35 +++++++---------------------------- 1 file changed, 7 insertions(+), 28 deletions(-) diff --git a/lib/sapling/gardner.rb b/lib/sapling/gardner.rb index 5437883..8ae7ccf 100644 --- a/lib/sapling/gardner.rb +++ b/lib/sapling/gardner.rb @@ -2,11 +2,10 @@ require 'yaml' # Gardner is the module for working with a dialogue tree file module Gardner - - # Parse the branch + # Parse the tree array into an array of numbered branches, and ordered leaves. # # @param tree [Array] The dialogue tree - # @return [Array] The array of options on the branch. + # @return [Array] An array of numbered branches, with numbered leaves def self.prune_branches(tree) branches = { 0 => { "desc" => "Thanks for using Sapling!" } } tree.each do |b| @@ -16,13 +15,12 @@ module Gardner end return branches - end - # Parse the options + # Parse the leaves of a branch into a numbered hash of options. # # @param leaves [Array] The option of leaf hashes - # @return [Hash] A has of options + # @return [Hash] A numbered hash of options def self.prune_leaves(leaves) x = 1 options = {} @@ -35,34 +33,15 @@ module Gardner end return options - end - # Parse the trunk - # The trunk is like the introduction to the tree. + # Parse the trunk of the tree. # # @param tree [Hash] The entire tree - # @return [Hash] The tree without the trunk + # @return [Array] The trunk, and the remainder of the tree def self.prune_trunk(tree) trunk = tree.shift - 40.times { print "-" } - puts "\n#{trunk["trunk"]}" - 40.times { print "-" } - puts "\n" - - return tree + return [trunk,tree] end - - # The main method for Sapling. From here, the tree is grown. - # - # @param tree [File] The dialogue tree file - # @return [Hash] The final, constructed data set - def self.grow(tree) - trunk = Gardner.prune_trunk(tree) - branches = Gardner.prune_branches(trunk) - - return branches - end - end From ccf8a3b5f68bc9f401f35834b18eefea6301ac03 Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sun, 7 May 2017 23:52:13 -0400 Subject: [PATCH 04/21] planter.rb: Style refactor updates --- lib/sapling/planter.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/sapling/planter.rb b/lib/sapling/planter.rb index 4c30739..ac1f82c 100644 --- a/lib/sapling/planter.rb +++ b/lib/sapling/planter.rb @@ -4,7 +4,7 @@ require_relative './utility' # Planter is the module for creating or editing a tree. module Planter - + # A data class class Plot # The tree, trunk, and branches attr_accessor :tree, :trunk, :branches @@ -13,7 +13,6 @@ module Planter # Utilities for editing specific parts of a tree. class Spade - # The file we parse into a tree attr_writer :file From 5f7da9d55a22865ba9e07c0050fee8f3e48aaf0c Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sat, 13 May 2017 14:59:16 -0400 Subject: [PATCH 05/21] dialogue.rb: Refactor display code - Dialogue should be responsible for the flow of a tree, not displaying parts of it. With the exception of the prompt, all display responsibilities fall to Gardner --- lib/sapling/dialogue.rb | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/lib/sapling/dialogue.rb b/lib/sapling/dialogue.rb index 35acec9..33060d8 100644 --- a/lib/sapling/dialogue.rb +++ b/lib/sapling/dialogue.rb @@ -20,7 +20,7 @@ module Dialogue def conversation() tree = Gardner.prune_trunk(@file) - display_trunk(tree[0]) + Gardner.display_trunk(tree[0]) branches = Gardner.prune_branches(tree[1]) next_branch = 1 @@ -45,7 +45,7 @@ module Dialogue return 0 end - display_branch(branch, branch_no) + Gardner.display_branch(branch, branch_no, @debug) response = get_response(branch) @@ -59,30 +59,6 @@ module Dialogue return response end - # Format and display the trunk - # - # @param trunk [Hash] The trunk hash - def display_trunk(trunk) - 40.times { print "-" } - puts "\n#{trunk["trunk"]}" - 40.times { print "-" } - puts "\n" - end - - # Format and display a branch - # - # @param branch [Hash] A branch data set - # @param branch_no [Integer] The branch number - def display_branch(branch, branch_no) - puts "\n[ Branch: #{branch_no} ]" if @debug - puts "\n#{branch["desc"]}\n\n" - - branch["options"].each_pair do |k,v| - puts "\t#{k}: #{v.keys[0]}" - puts "\t\t [ Goes to branch #{v.values[0]} ]" if @debug - end - end - # Get a response for the displayed branch # # @param branch [Hash] A branch data set From 280d016d38ce6efff59726737147a3212ae660b1 Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sat, 13 May 2017 15:00:36 -0400 Subject: [PATCH 06/21] gardner.rb: Move display code from dialogue --- lib/sapling/gardner.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/sapling/gardner.rb b/lib/sapling/gardner.rb index 8ae7ccf..c09a759 100644 --- a/lib/sapling/gardner.rb +++ b/lib/sapling/gardner.rb @@ -44,4 +44,29 @@ module Gardner return [trunk,tree] end + + # Format and display the trunk + # + # @param trunk [Hash] The trunk hash + def self.display_trunk(trunk) + 40.times { print "-" } + puts "\n#{trunk["trunk"]}" + 40.times { print "-" } + puts "\n" + end + + # Format and display a branch and the options + # + # @param branch [Hash] A branch data set + # @param branch_no [Integer] The branch number + # @param debug [Boolean] Status of showing debug information + def self.display_branch(branch, branch_no, debug) + puts "\n[ Branch: #{branch_no} ]" if debug + puts "\n#{branch["desc"]}\n\n" + + branch["options"].each_pair do |k,v| + puts "\t#{k}: #{v.keys[0]}" + puts "\t\t[ Goes to branch #{v.values[0]} ]\n" if debug + end + end end From 802074a5130aa286c3dd6deac87b3626b0cf30ff Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sat, 13 May 2017 15:01:28 -0400 Subject: [PATCH 07/21] planter.rb: Utilize display code now in Gardner --- lib/sapling/planter.rb | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/lib/sapling/planter.rb b/lib/sapling/planter.rb index ac1f82c..3c3138d 100644 --- a/lib/sapling/planter.rb +++ b/lib/sapling/planter.rb @@ -21,7 +21,7 @@ module Planter def plant @plot = Plot.new @plot.tree = @file - @plot.trunk = @file.shift.values[0] + @plot.trunk = @file.shift @plot.branches = Gardner.prune_branches(@file) dig(1) @@ -35,22 +35,9 @@ module Planter def dig(branch_no) branch = @plot.branches[branch_no] - # Print the trunk - 40.times { print "-" } - puts "\n[ Trunk ]\n#{@plot.trunk}" - 40.times { print "-" } - puts "\n" - 10.times { print "*" } + Gardner.display_trunk(@plot.trunk) + Gardner.display_branch(branch, branch_no, true) - # Print the branch and options - puts "\n[ Branch: #{branch_no} ]" - puts "\n#{branch["desc"]}\n\n" - unless branch["options"].empty? - branch["options"].each_pair do |k,v| - puts "\t#{k}: #{v.keys[0]}" - puts "\t\t [ Goes to branch #{v.values[0]} ]" - end - end end # Edit the trunk of the tree From 2e2fea9e58a7736f23f6e90af9dfbb07141fe049 Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sat, 13 May 2017 15:55:33 -0400 Subject: [PATCH 08/21] dialogue.rb: Update comment --- lib/sapling/dialogue.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/sapling/dialogue.rb b/lib/sapling/dialogue.rb index 33060d8..6de064c 100644 --- a/lib/sapling/dialogue.rb +++ b/lib/sapling/dialogue.rb @@ -2,8 +2,7 @@ require_relative './gardner' # Dialogue is the module for traversing an existing tree. module Dialogue - # Speaker holds the functionality for viewing and going through a dialogue - # tree. + # Speaker holds the functionality for going through a dialogue tree. class Speaker # The file, which should be a dialogue tree YAML file. attr_accessor :file From 4c726b079d71db604775e3b70e70c423420503ff Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sat, 13 May 2017 19:42:42 -0400 Subject: [PATCH 09/21] sapling.rb: Modify start options - Reduce some LOC by moving some assignments into the initialize functions of both Dialogue and Planter --- lib/sapling.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/sapling.rb b/lib/sapling.rb index c80903f..254b35e 100644 --- a/lib/sapling.rb +++ b/lib/sapling.rb @@ -41,8 +41,7 @@ module Sapling end puts "Welcome to Sapling, a Dialogue Tree Utility.\n" - speaker = Dialogue::Speaker.new - speaker.file = YAML.load_file(ARGV[0]) + speaker = Dialogue::Speaker.new(YAML.load_file(ARGV[0]),false) speaker.conversation end @@ -62,8 +61,7 @@ module Sapling end puts "Welcome to Sapling, a Dialogue Tree Utility.\n" - gardner = Planter::Spade.new - gardner.file = tree + gardner = Planter::Spade.new(tree) gardner.plant end From 1c6206c3cd10703679466db3769904d1710ec8cf Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sat, 13 May 2017 19:43:48 -0400 Subject: [PATCH 10/21] dialogue.rb: Move assignments into initialize --- lib/sapling/dialogue.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/sapling/dialogue.rb b/lib/sapling/dialogue.rb index 6de064c..641b4f1 100644 --- a/lib/sapling/dialogue.rb +++ b/lib/sapling/dialogue.rb @@ -9,9 +9,9 @@ module Dialogue # Status of verbose/debug mode. True = on; false = off. attr_accessor :debug - def initialize - @file = "" - @debug = false + def initialize(file="", debug=false) + @file = file + @debug = debug end # Conversation handles navigating the tree, until the option to end is From 4b33abcd757a2526390220b120a77fbaaadd39f9 Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sat, 13 May 2017 19:45:19 -0400 Subject: [PATCH 11/21] 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 --- lib/sapling/planter.rb | 139 ++++++++++++++++++++++++++++++++--------- 1 file changed, 109 insertions(+), 30 deletions(-) diff --git a/lib/sapling/planter.rb b/lib/sapling/planter.rb index 3c3138d..aa67dd6 100644 --- a/lib/sapling/planter.rb +++ b/lib/sapling/planter.rb @@ -4,11 +4,30 @@ require_relative './utility' # Planter is the module for creating or editing a tree. module Planter - # A data class + # In-memory tree class Plot # The tree, trunk, and 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 # Utilities for editing specific parts of a tree. @@ -16,15 +35,25 @@ module Planter # The file we parse into a tree attr_writer :file - # Establish a new Plot, which is basically an object for storing information - # for us. From here, we start gardening. + def initialize(file) + @file=file + end + + # Establish and populate a new Plot (in-memory tree), then control the flow + # of editing the Plot def plant @plot = Plot.new @plot.tree = @file @plot.trunk = @file.shift @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 # 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_branch(branch, branch_no, true) + response = get_response(branch) + to_branch = parse_response(response, branch_no) + + return to_branch end - # Edit the trunk of the tree - def edit_trunk - - end - - # Edit a branch on the tree + # Get a response for the displayed branch # - # @param branch [Integer] The number of the branch to be edited. - def edit_branch(branch) + # @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 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 leaf [Hash] The leaf hash to be edited. - def edit_leaf(branch, leaf) + # @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) + 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 @@ -80,24 +157,26 @@ Process: - At this point, editing is the same - Prompt provides options: - - B # - Go to Branch # - - T - Edit Trunk - - D - Edit Current Branch description - - X - Delete Current Branch - - L A - Add a new Leaf - - L # D - Edit Leaf # description - - L # B - Edit Leaf # branch destination - - L # X - Remove Leaf # - - S - Save Changes (write to the file) - - Q - Quit without saving + - #: 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: + [ Leaves: Details: - 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 Dialog to interact with the tree - - Overwrite prompt with Planter prompt - - On changes, re-build the tree, and restart dialogue from most recent branch +- 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 - After each edit option, display the current branch with the new changes. =end From 03d09105f3e5a09c05b54ae5f5d7bbc45cc18ddc Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sun, 14 May 2017 14:12:32 -0400 Subject: [PATCH 12/21] gardner.rb: Add debug options and information --- lib/sapling/gardner.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/sapling/gardner.rb b/lib/sapling/gardner.rb index c09a759..3a75dbb 100644 --- a/lib/sapling/gardner.rb +++ b/lib/sapling/gardner.rb @@ -48,8 +48,10 @@ module Gardner # Format and display the trunk # # @param trunk [Hash] The trunk hash - def self.display_trunk(trunk) + # @param debug [Boolean] The status of showing debug information + def self.display_trunk(trunk, debug=false) 40.times { print "-" } + puts "\n[ Trunk ]\n" if debug puts "\n#{trunk["trunk"]}" 40.times { print "-" } puts "\n" @@ -60,7 +62,7 @@ module Gardner # @param branch [Hash] A branch data set # @param branch_no [Integer] The branch number # @param debug [Boolean] Status of showing debug information - def self.display_branch(branch, branch_no, debug) + def self.display_branch(branch, branch_no, debug=false) puts "\n[ Branch: #{branch_no} ]" if debug puts "\n#{branch["desc"]}\n\n" From 78f9837b5a92dea26f4b3c756106c3f5ae4f1d46 Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sun, 14 May 2017 14:13:20 -0400 Subject: [PATCH 13/21] dialogue.rb: Add debug value for Gardner --- lib/sapling/dialogue.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sapling/dialogue.rb b/lib/sapling/dialogue.rb index 641b4f1..0e6823c 100644 --- a/lib/sapling/dialogue.rb +++ b/lib/sapling/dialogue.rb @@ -19,7 +19,7 @@ module Dialogue def conversation() tree = Gardner.prune_trunk(@file) - Gardner.display_trunk(tree[0]) + Gardner.display_trunk(tree[0], false) branches = Gardner.prune_branches(tree[1]) next_branch = 1 From 1af5c9a53c1f987b3667c82c0f8ce1a9373540b5 Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sun, 14 May 2017 14:13:50 -0400 Subject: [PATCH 14/21] planter.rb: Improve editor and options - Editor: Enable trunk editing - General: Add value for Gardner debug mode - General: Improve visability of option chosen - General: Improve display --- lib/sapling/planter.rb | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/lib/sapling/planter.rb b/lib/sapling/planter.rb index aa67dd6..e24f681 100644 --- a/lib/sapling/planter.rb +++ b/lib/sapling/planter.rb @@ -11,7 +11,17 @@ module Planter # Edit the trunk of the tree def edit_trunk - puts "Edit trunk" + puts "Current Trunk:\n" + Gardner.display_trunk(@trunk, true) + print "[ =EDITING= ](CTRL-C to abort)> " + 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 end # Edit a branch on the tree @@ -64,7 +74,7 @@ module Planter def dig(branch_no) branch = @plot.branches[branch_no] - Gardner.display_trunk(@plot.trunk) + Gardner.display_trunk(@plot.trunk, true) Gardner.display_branch(branch, branch_no, true) response = get_response(branch) @@ -102,28 +112,38 @@ module Planter # @param branch_no [Integer] The currently-displayed branch # @return [Integer] the branch to display def parse_response(response, branch_no) - return response.to_i if response.to_i >= 1 + 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 case response.to_s.downcase when "t" + print "Edit the trunk.)\n\n" @plot.edit_trunk return branch_no when "a" - puts "Add new branch" + print "Add a new branch.)\n\n" return branch_no when "b" + print "Edit the current branch.)\n\n" @plot.edit_branch(branch_no) return branch_no when "x" - puts "Delete current branch" + print "Delete the current branch.)\n\n" return branch_no when "l" - puts "Edit leaves" + print "Edit leaves of current branch.)\n\n" return branch_no when "s" - puts "Save changes" + print "Save changes.)\n\n" return branch_no when "q" + print "Quit without saving.)\n\n" print "Unsaved changes will be lost. Still quit? [y/n]> " verify = STDIN.gets.chomp.to_s.downcase @@ -131,14 +151,11 @@ module Planter return branch_no else - puts "Something else!" + print "Unknown option. Returning to current branch.)\n\n" return branch_no end end - - end - end =begin @@ -169,7 +186,6 @@ Process: - Example prompt: [ 0-5,T,A,B,X,L,S,Q ]> - Leaf prompt: - [ Leaves: Details: From 7b1b0541c16aaa7bbfbd2f9107d8e1f253fc5488 Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sun, 21 May 2017 15:56:12 -0400 Subject: [PATCH 15/21] planter.rb: Implement branch desc. editing --- lib/sapling/planter.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/sapling/planter.rb b/lib/sapling/planter.rb index e24f681..abaeb64 100644 --- a/lib/sapling/planter.rb +++ b/lib/sapling/planter.rb @@ -13,7 +13,7 @@ module Planter def edit_trunk puts "Current Trunk:\n" Gardner.display_trunk(@trunk, true) - print "[ =EDITING= ](CTRL-C to abort)> " + print "\n[ =EDITING= ](CTRL-C to abort)> " STDOUT.flush begin new_trunk = STDIN.gets.to_s @@ -27,8 +27,18 @@ module Planter # 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" + def edit_branch(branch_no) + puts "Current Branch:\n" + Gardner.display_branch(@branches[branch_no], branch_no, true) + 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 end # Edit a leaf on a branch, grasshopper @@ -46,7 +56,7 @@ module Planter attr_writer :file def initialize(file) - @file=file + @file = file end # Establish and populate a new Plot (in-memory tree), then control the flow From d0ebc379238576a4c7247dd370cae13ffa5e4487 Mon Sep 17 00:00:00 2001 From: Bill Nibz Date: Sat, 14 Oct 2017 14:38:19 -0400 Subject: [PATCH 16/21] Dialogue: Move display back in --- lib/sapling/dialogue.rb | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/sapling/dialogue.rb b/lib/sapling/dialogue.rb index 0e6823c..5ccc010 100644 --- a/lib/sapling/dialogue.rb +++ b/lib/sapling/dialogue.rb @@ -2,6 +2,34 @@ require_relative './gardner' # Dialogue is the module for traversing an existing tree. module Dialogue + + # Format and display the trunk + # + # @param trunk [Hash] The trunk hash + # @param debug [Boolean] The status of showing debug information + def self.display_trunk(trunk, debug=false) + 40.times { print "-" } + puts "\n[ Trunk ]\n" if debug + puts "\n#{trunk["trunk"]}" + 40.times { print "-" } + puts "\n" + end + + # Format and display a branch and the options + # + # @param branch [Hash] A branch data set + # @param branch_no [Integer] The branch number + # @param debug [Boolean] Status of showing debug information + def self.display_branch(branch, branch_no, debug=false) + puts "\n[ Branch: #{branch_no} ]" if debug + puts "\n#{branch["desc"]}\n\n" + + branch["options"].each_pair do |k,v| + puts "\t#{k}: #{v.keys[0]}" + puts "\t\t[ Goes to branch #{v.values[0]} ]\n" if debug + end + end + # Speaker holds the functionality for going through a dialogue tree. class Speaker # The file, which should be a dialogue tree YAML file. @@ -19,7 +47,7 @@ module Dialogue def conversation() tree = Gardner.prune_trunk(@file) - Gardner.display_trunk(tree[0], false) + Dialogue.display_trunk(tree[0], false) branches = Gardner.prune_branches(tree[1]) next_branch = 1 @@ -44,7 +72,7 @@ module Dialogue return 0 end - Gardner.display_branch(branch, branch_no, @debug) + Dialogue.display_branch(branch, branch_no, @debug) response = get_response(branch) From 783fb886fd16376b63a07e1fe4c0921443d4e854 Mon Sep 17 00:00:00 2001 From: Bill Nibz Date: Sat, 14 Oct 2017 14:38:41 -0400 Subject: [PATCH 17/21] Gardner: Remove display functionality --- lib/sapling/gardner.rb | 28 +--------------------------- 1 file changed, 1 insertion(+), 27 deletions(-) diff --git a/lib/sapling/gardner.rb b/lib/sapling/gardner.rb index 3a75dbb..e6184bf 100644 --- a/lib/sapling/gardner.rb +++ b/lib/sapling/gardner.rb @@ -2,6 +2,7 @@ require 'yaml' # Gardner is the module for working with a dialogue tree file module Gardner + # Parse the tree array into an array of numbered branches, and ordered leaves. # # @param tree [Array] The dialogue tree @@ -44,31 +45,4 @@ module Gardner return [trunk,tree] end - - # Format and display the trunk - # - # @param trunk [Hash] The trunk hash - # @param debug [Boolean] The status of showing debug information - def self.display_trunk(trunk, debug=false) - 40.times { print "-" } - puts "\n[ Trunk ]\n" if debug - puts "\n#{trunk["trunk"]}" - 40.times { print "-" } - puts "\n" - end - - # Format and display a branch and the options - # - # @param branch [Hash] A branch data set - # @param branch_no [Integer] The branch number - # @param debug [Boolean] Status of showing debug information - def self.display_branch(branch, branch_no, debug=false) - puts "\n[ Branch: #{branch_no} ]" if debug - puts "\n#{branch["desc"]}\n\n" - - branch["options"].each_pair do |k,v| - puts "\t#{k}: #{v.keys[0]}" - puts "\t\t[ Goes to branch #{v.values[0]} ]\n" if debug - end - end end From dfc41175272a8065d21658da2a52da37e20eb692 Mon Sep 17 00:00:00 2001 From: Bill Nibz Date: Sat, 14 Oct 2017 14:39:11 -0400 Subject: [PATCH 18/21] Planter: Gardner to Dialogue --- lib/sapling/planter.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/sapling/planter.rb b/lib/sapling/planter.rb index abaeb64..8881117 100644 --- a/lib/sapling/planter.rb +++ b/lib/sapling/planter.rb @@ -4,15 +4,17 @@ require_relative './utility' # Planter is the module for creating or editing a tree. module Planter + # In-memory tree class Plot + # The tree, trunk, and branches attr_accessor :tree, :trunk, :branches # Edit the trunk of the tree def edit_trunk puts "Current Trunk:\n" - Gardner.display_trunk(@trunk, true) + Dialogue.display_trunk(@trunk, true) print "\n[ =EDITING= ](CTRL-C to abort)> " STDOUT.flush begin @@ -29,7 +31,7 @@ module Planter # @param branch [Integer] The number of the branch to be edited. def edit_branch(branch_no) puts "Current Branch:\n" - Gardner.display_branch(@branches[branch_no], branch_no, true) + Dialogue.display_branch(@branches[branch_no], branch_no, true) print "\n[ =EDITING= ](CTRL-C to abort)> " STDOUT.flush begin @@ -52,6 +54,7 @@ module Planter # Utilities for editing specific parts of a tree. class Spade + # The file we parse into a tree attr_writer :file @@ -84,8 +87,8 @@ module Planter def dig(branch_no) branch = @plot.branches[branch_no] - Gardner.display_trunk(@plot.trunk, true) - Gardner.display_branch(branch, branch_no, true) + Dialogue.display_trunk(@plot.trunk, true) + Dialogue.display_branch(branch, branch_no, true) response = get_response(branch) to_branch = parse_response(response, branch_no) @@ -157,9 +160,7 @@ module Planter 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 + return 0 if verify == "y" else branch_no else print "Unknown option. Returning to current branch.)\n\n" return branch_no From ca5ddf7bafe56c8a8c649e66e57c477f8dc0a9be Mon Sep 17 00:00:00 2001 From: Bill Nibz Date: Sat, 14 Oct 2017 15:14:41 -0400 Subject: [PATCH 19/21] Re-organization --- lib/{sapling => }/dialogue.rb | 0 lib/{sapling => }/gardner.rb | 0 lib/{sapling => }/planter.rb | 0 lib/{sapling => }/utility.rb | 0 lib/sapling.rb => sapling.rb | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename lib/{sapling => }/dialogue.rb (100%) rename lib/{sapling => }/gardner.rb (100%) rename lib/{sapling => }/planter.rb (100%) rename lib/{sapling => }/utility.rb (100%) rename lib/sapling.rb => sapling.rb (100%) diff --git a/lib/sapling/dialogue.rb b/lib/dialogue.rb similarity index 100% rename from lib/sapling/dialogue.rb rename to lib/dialogue.rb diff --git a/lib/sapling/gardner.rb b/lib/gardner.rb similarity index 100% rename from lib/sapling/gardner.rb rename to lib/gardner.rb diff --git a/lib/sapling/planter.rb b/lib/planter.rb similarity index 100% rename from lib/sapling/planter.rb rename to lib/planter.rb diff --git a/lib/sapling/utility.rb b/lib/utility.rb similarity index 100% rename from lib/sapling/utility.rb rename to lib/utility.rb diff --git a/lib/sapling.rb b/sapling.rb similarity index 100% rename from lib/sapling.rb rename to sapling.rb From b7cea51f7aaa10be650bcc79d578126f94177997 Mon Sep 17 00:00:00 2001 From: Bill Nibz Date: Sat, 14 Oct 2017 15:15:38 -0400 Subject: [PATCH 20/21] Planter: Modify conditional logic --- lib/planter.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/planter.rb b/lib/planter.rb index 8881117..df38ee0 100644 --- a/lib/planter.rb +++ b/lib/planter.rb @@ -160,7 +160,8 @@ module Planter print "Unsaved changes will be lost. Still quit? [y/n]> " verify = STDIN.gets.chomp.to_s.downcase - return 0 if verify == "y" else branch_no + return 0 if verify == "y" + return branch_no else print "Unknown option. Returning to current branch.)\n\n" return branch_no From 4b930fb67b9e36f8aef987a9df363b0fe29af963 Mon Sep 17 00:00:00 2001 From: Bill Nibz Date: Sat, 14 Oct 2017 15:15:54 -0400 Subject: [PATCH 21/21] Sapling: Dynamic local requires --- sapling.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/sapling.rb b/sapling.rb index 254b35e..5e3c9ae 100644 --- a/sapling.rb +++ b/sapling.rb @@ -3,10 +3,7 @@ require 'optparse' require 'yaml' -require_relative 'sapling/dialogue' -require_relative 'sapling/gardner' -require_relative 'sapling/planter' -require_relative 'sapling/utility' +Dir[File.join(__dir__, 'lib', '*.rb')].each { |file| require file } # Sapling is the main module for the program. From here, the rest of the world # starts building.