Class: Pcut::CLI
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#delimiter ⇒ Object
Returns the value of attribute delimiter.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#joiner ⇒ Object
Returns the value of attribute joiner.
-
#keep_quote ⇒ Object
Returns the value of attribute keep_quote.
-
#preview ⇒ Object
Returns the value of attribute preview.
-
#quote ⇒ Object
Returns the value of attribute quote.
-
#skip_continuous_delimiters ⇒ Object
Returns the value of attribute skip_continuous_delimiters.
-
#vertical ⇒ Object
Returns the value of attribute vertical.
Instance Method Summary collapse
- #index_labeL(index) ⇒ Object
-
#initialize ⇒ CLI
constructor
A new instance of CLI.
- #parse_field(str) ⇒ Object
- #query_label(index, queries) ⇒ Object
- #set_joiner(char) ⇒ Object
- #start(filepath) ⇒ Object
Constructor Details
#initialize ⇒ CLI
Returns a new instance of CLI.
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/pcut/cli.rb', line 25 def initialize @delimiter = "\t" @joiner = @delimiter @fields = [] @quote = nil @keep_quote = false @preview = false @vertical = false @color = true @skip_continuous_delimiters = false end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
14 15 16 |
# File 'lib/pcut/cli.rb', line 14 def color @color end |
#delimiter ⇒ Object
Returns the value of attribute delimiter.
14 15 16 |
# File 'lib/pcut/cli.rb', line 14 def delimiter @delimiter end |
#fields ⇒ Object
Returns the value of attribute fields.
14 15 16 |
# File 'lib/pcut/cli.rb', line 14 def fields @fields end |
#joiner ⇒ Object
Returns the value of attribute joiner.
14 15 16 |
# File 'lib/pcut/cli.rb', line 14 def joiner @joiner end |
#keep_quote ⇒ Object
Returns the value of attribute keep_quote.
14 15 16 |
# File 'lib/pcut/cli.rb', line 14 def keep_quote @keep_quote end |
#preview ⇒ Object
Returns the value of attribute preview.
14 15 16 |
# File 'lib/pcut/cli.rb', line 14 def preview @preview end |
#quote ⇒ Object
Returns the value of attribute quote.
14 15 16 |
# File 'lib/pcut/cli.rb', line 14 def quote @quote end |
#skip_continuous_delimiters ⇒ Object
Returns the value of attribute skip_continuous_delimiters.
14 15 16 |
# File 'lib/pcut/cli.rb', line 14 def skip_continuous_delimiters @skip_continuous_delimiters end |
#vertical ⇒ Object
Returns the value of attribute vertical.
14 15 16 |
# File 'lib/pcut/cli.rb', line 14 def vertical @vertical end |
Instance Method Details
#index_labeL(index) ⇒ Object
151 152 153 154 155 156 |
# File 'lib/pcut/cli.rb', line 151 def index_labeL(index) return "" unless @preview (@color ? red : '') + "[" + (@color ? yellow : '' ) + "#{index}" + (@color ? red : '') + "]" + (@color ? reset : '') end |
#parse_field(str) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/pcut/cli.rb', line 49 def parse_field(str) targets = [] begin field_parser = Pcut::LineParser.new field_parser.set_delimiter(",") field_parser.set_quote_guard("[") field_parser.keep_quotes = true fields = field_parser.parse(str) fields.each do |field| index = nil queries = [] query_parser = Pcut::LineParser.new query_parser.set_delimiter(".") query_parser.set_quote_guard("[") query_parser.keep_quotes = true if field =~ /\./ result = query_parser.parse(field) first = result.shift index = Pcut::RangeIndex.parse(first) result.each do |query_str| queries << Pcut::Query.parse(query_str) end else index = Pcut::RangeIndex.parse(field) end targets << [index, queries] end rescue => e STDOUT.puts e.to_s exit(1) end targets end |
#query_label(index, queries) ⇒ Object
158 159 160 161 162 163 164 165 166 |
# File 'lib/pcut/cli.rb', line 158 def query_label(index, queries) return "" unless @preview query_str = queries.each { |q| q.to_s }.join(".") (@color ? red : '') + "[" + (@color ? yellow : '' ) + "#{index}" + (@color ? green : '' ) + ".#{query_str}" + (@color ? red : '') + "]" + (@color ? reset : '') end |
#set_joiner(char) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/pcut/cli.rb', line 37 def set_joiner(char) if char == "TAB" @joiner = "\t" return end if char.size != 1 STDERR.puts "join char must be 1 byte char or 'TAB'" exit(1) end @joiner = char end |
#start(filepath) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/pcut/cli.rb', line 87 def start(filepath) begin parser = Pcut::LineParser.new parser.set_delimiter(@delimiter) parser.skip_continuous_delimiters = @skip_continuous_delimiters @quote.each_char { |c| parser.set_quote_guard(c) } if @quote parser.keep_quotes = true if @keep_quote rescue => e STDOUT.puts e.to_s exit(1) end io = nil if filepath begin if File.directory?(filepath) raise ArgumentError, "#{filepath} is directory" end io = File.open(filepath, 'r') rescue => e $stdout.puts e.to_s exit(1) end end io ||= STDIN io.each_line do |line| line.chomp! buff = [] # selected fields result = parser.parse(line) joiner = @vertical ? "\n" : @joiner # apply -f fields specification # (value pickup) if !@fields.empty? @fields.each do |setting| index = setting[0] queries = setting[1] value = Pcut::RangeCollector.collect(result, index).join(@joiner) if queries.empty? buff << index_labeL(index.index) + (value || "") else queries.each do |query| last if value.nil? sliced = Pcut::RangeCollector.collect( value.split(query.delimiter), query.index) value = sliced.join(query.delimiter) end buff << query_label(index.index, queries) + (value || "") end end # no @fields is "all the fields" else result.each_with_index do |field, index| buff << index_labeL(index + 1) + (field || "") end end puts buff.join(joiner) puts "" if @vertical end exit(0) end |