Skip to content

Commit

Permalink
[fish]/[misc] omf, concat, + tree printer code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kay Rhodes committed Jul 10, 2017
1 parent edf4cdc commit 3b1327f
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .config/fish/conf.d/omf.fish
@@ -0,0 +1,7 @@
# Path to Oh My Fish install.
set -q XDG_DATA_HOME
and set -gx OMF_PATH "$XDG_DATA_HOME/omf"
or set -gx OMF_PATH "$HOME/.local/share/omf"

# Load Oh My Fish configuration.
source $OMF_PATH/init.fish
10 changes: 10 additions & 0 deletions .config/fish/functions/concat.fish
@@ -0,0 +1,10 @@
function concat
set delimiter $argv[1]
echo "delimiter->$delimiter"
set string ""
for x in $argv[2..-1]
set string "$string$delimiter$x"
end
echo $string
end

49 changes: 49 additions & 0 deletions useful_code/ruby/util/tree_printer.rb
@@ -0,0 +1,49 @@
module Util
class TreePrinter
attr_accessor :is_branch
attr_accessor :get_children
attr_accessor :format_node
attr_accessor :skip_root_node

def initialize
@arms = Hash.new("| ")
@arms[""] = ""
@arms["`"] = " "
@out = ""
@is_branch = proc{|node| node.has_children? }
@get_children = proc{|node| node.children}
@format_node = proc{|node| node.to_s}
@skip_root_node = false
end

def visit(path, leader, tie, arm, node)
if(@root == node) && skip_root_node
# skip
else
node_str = @format_node.call(node)
@out << "#{leader}#{arm}#{tie}#{node_str}\n"
end
visitChildren(node, leader + @arms[arm])
@out
end

def visitChildren(path, leader)
kids = []
if(@root == path) && skip_root_node
kids = path
else
return unless @is_branch.call(path)
kids = @get_children.call(path)
end
return if kids.empty?
arms = Array.new(kids.length - 1, "|") << "`"
pairs = kids.zip(arms)
pairs.each { |e| visit(path, leader, "-- ", e[1], e[0]) }
end

def format(root)
@root = root
visit root, "", "", "", root
end
end
end

0 comments on commit 3b1327f

Please sign in to comment.