Module: GithubCLI::Util

Extended by:
Util
Included in:
Command::Completion, Formatters::Table, Util
Defined in:
lib/github_cli/util.rb

Instance Method Summary collapse

Instance Method Details

#convert_value(value) ⇒ Object

Attempts to convert value object to string



37
38
39
40
41
42
43
44
45
# File 'lib/github_cli/util.rb', line 37

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



27
28
29
30
31
32
33
# File 'lib/github_cli/util.rb', line 27

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

#hash_without!(hash, keys) ⇒ Object



23
24
25
# File 'lib/github_cli/util.rb', line 23

def hash_without!(hash, keys)
  hash.reject! { |key| keys.include?(key) }
end

#longest_common_prefix(string_1, string_2) ⇒ Object

Compares two strings to find common prefix



90
91
92
# File 'lib/github_cli/util.rb', line 90

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/github_cli/util.rb', line 65

def pad(string, width, options={})
  supported = [:left, :right, :center]
  padder = options[:padder] || ' '
  align  = options[: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



50
51
52
53
54
55
56
57
58
59
# File 'lib/github_cli/util.rb', line 50

def truncate(string, width, options={})
  trailing = options[: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