Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fish]/[misc] omf, concat, + tree printer code
- Loading branch information
Kay Rhodes
committed
Jul 10, 2017
1 parent
edf4cdc
commit 3b1327f
Showing
3 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |