Module: AbideDevUtils::Output

Defined in:
lib/abide_dev_utils/output.rb

Constant Summary collapse

FWRITER =
AbideDevUtils::Files::Writer.new

Class Method Summary collapse

Class Method Details

.json(in_obj, console: false, file: nil, pretty: true, **_) ⇒ Object



39
40
41
42
43
44
# File 'lib/abide_dev_utils/output.rb', line 39

def self.json(in_obj, console: false, file: nil, pretty: true, **_)
  AbideDevUtils::Validate.hashable(in_obj)
  json_out = pretty ? JSON.pretty_generate(in_obj) : JSON.generate(in_obj)
  simple(json_out) if console
  FWRITER.write_json(json_out, file: file) unless file.nil?
end


30
31
32
# File 'lib/abide_dev_utils/output.rb', line 30

def self.print(msg, stream: $stdout, **_)
  stream.print msg
end

.progress(title: 'Progress', start: 0, total: 100, format: nil, **_) ⇒ Object



70
71
72
# File 'lib/abide_dev_utils/output.rb', line 70

def self.progress(title: 'Progress', start: 0, total: 100, format: nil, **_)
  ProgressBar.create(title: title, starting_at: start, total: total, format: format)
end

.simple(msg, stream: $stdout, **_) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/abide_dev_utils/output.rb', line 21

def self.simple(msg, stream: $stdout, **_)
  case msg
  when Hash
    stream.puts JSON.pretty_generate(msg)
  else
    stream.puts msg
  end
end

.simple_section_separator(section_text, sepchar: '#', width: 60, **_) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/abide_dev_utils/output.rb', line 13

def self.simple_section_separator(section_text, sepchar: '#', width: 60, **_)
  section_text = section_text.to_s
  section_text = section_text[0..width - 4] if section_text.length > width
  section_text = " #{section_text} "
  section_sep_line = sepchar * width
  [section_sep_line, section_text.center(width, sepchar), section_sep_line].join("\n")
end

.text(msg, console: false, file: nil, **_) ⇒ Object



34
35
36
37
# File 'lib/abide_dev_utils/output.rb', line 34

def self.text(msg, console: false, file: nil, **_)
  simple(msg) if console
  FWRITER.write_text(msg, file: file) unless file.nil?
end

.yaml(in_obj, console: false, file: nil, stringify: false, **_) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/abide_dev_utils/output.rb', line 46

def self.yaml(in_obj, console: false, file: nil, stringify: false, **_)
  yaml_out = if in_obj.is_a? String
               in_obj
             else
               AbideDevUtils::Validate.hashable(in_obj)
               if stringify
                 JSON.parse(JSON.generate(in_obj)).to_yaml
               else
                 # Use object's #to_yaml method if it exists, convert to hash if not
                 in_obj.respond_to?(:to_yaml) ? in_obj.to_yaml : in_obj.to_h.to_yaml
               end
             end
  simple(yaml_out) if console
  FWRITER.write_yaml(yaml_out, file: file) unless file.nil?
end

.yml(in_obj, console: false, file: nil, **_) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/abide_dev_utils/output.rb', line 62

def self.yml(in_obj, console: false, file: nil, **_)
  AbideDevUtils::Validate.hashable(in_obj)
  # Use object's #to_yaml method if it exists, convert to hash if not
  yml_out = in_obj.respond_to?(:to_yaml) ? in_obj.to_yaml : in_obj.to_h.to_yaml
  simple(yml_out) if console
  FWRITER.write_yml(yml_out, file: file) unless file.nil?
end