Module: GithubCLI::Util
- Extended by:
- Util
- Included in:
- Command::Completion, Formatters::Table, Util
- Defined in:
- lib/github_cli/util.rb
Instance Method Summary collapse
-
#convert_value(value) ⇒ Object
Attempts to convert value object to string.
- #convert_values(values) ⇒ Object
-
#flatten_hash(hash, prefix = nil) ⇒ Object
Converts deeply nested hash into only one level structure.
-
#longest_common_prefix(string_1, string_2) ⇒ Object
Compares two strings to find common prefix.
-
#pad(string, width, options = {}) ⇒ Object
Pads a string padder - padding character align - align :left, :right, :center.
-
#truncate(string, width, options = {}) ⇒ Object
Shortens string :trailing - trailing character in place of cutout string.
Instance Method Details
#convert_value(value) ⇒ Object
Attempts to convert value object to string
33 34 35 36 37 38 39 40 41 |
# File 'lib/github_cli/util.rb', line 33 def convert_value(value) case value when true then "true" when false then "false" when Hash then convert_value(value.values) when Array then value.map(&:to_s) else value.to_s end end |
#convert_values(values) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/github_cli/util.rb', line 23 def convert_values(values) values_copy = values.dup collected = [] values_copy.inject([]) do |collected, val| collected << convert_value(val) end end |
#flatten_hash(hash, prefix = nil) ⇒ Object
Converts deeply nested hash into only one level structure
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/github_cli/util.rb', line 9 def flatten_hash(hash, prefix=nil) new_hash ||= {} hash.each do |key, val| key = prefix ? :"#{prefix}_#{key}" : key case val when Hash new_hash.update flatten_hash(val, key) else new_hash[key] = val end end return new_hash end |
#longest_common_prefix(string_1, string_2) ⇒ Object
Compares two strings to find common prefix
86 87 88 |
# File 'lib/github_cli/util.rb', line 86 def longest_common_prefix(string_1, string_2) ("#{string_1}\0#{string_2}").match(/^(.*).*\0\1/i).to_a[1] end |
#pad(string, width, options = {}) ⇒ Object
Pads a string padder - padding character align - align :left, :right, :center
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/github_cli/util.rb', line 61 def pad(string, width, ={}) supported = [:left, :right, :center] padder = [:padder] || ' ' align = [:align] || :left chars = string.to_s.chars.to_a if chars.length < width string = case :"#{align}" when :left string + (padder * (width - chars.length)) when :right (padder * (width - chars.length)) + string when :center right = ((pad_length = width - chars.length).to_f / 2).ceil left = pad_length - right (padder * left) + string + (padder * right) else raise ArgumentError, "Alignment must be one of: #{supported.join(' ')}" end end string end |
#truncate(string, width, options = {}) ⇒ Object
Shortens string :trailing - trailing character in place of cutout string
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/github_cli/util.rb', line 46 def truncate(string, width, ={}) trailing = [:trailing] || '…' chars = string.to_s.chars.to_a if chars.length < width && chars.length > 3 chars.join elsif chars.length > 3 (chars[0, width - trailing.length].join) + trailing end end |