Module: Ralph::Helpers

Instance Method Summary collapse

Instance Method Details

#check_completion(output, promise) ⇒ Object



55
56
57
58
# File 'lib/ralph/helpers.rb', line 55

def check_completion(output, promise)
  pattern = /<promise>\s*#{escape_regex(promise)}\s*<\/promise>/i
  output.match?(pattern)
end

#escape_regex(str) ⇒ Object



9
10
11
# File 'lib/ralph/helpers.rb', line 9

def escape_regex(str)
  Regexp.escape(str)
end

#format_duration(ms) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ralph/helpers.rb', line 13

def format_duration(ms)
  total_seconds = [0, (ms / 1000).floor].max
  hours = total_seconds / 3600
  minutes = (total_seconds % 3600) / 60
  seconds = total_seconds % 60

  if hours > 0
    "#{hours}:#{minutes.to_s.rjust(2, '0')}:#{seconds.to_s.rjust(2, '0')}"
  else
    "#{minutes}:#{seconds.to_s.rjust(2, '0')}"
  end
end

#format_duration_long(ms) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ralph/helpers.rb', line 26

def format_duration_long(ms)
  total_seconds = [0, (ms / 1000).floor].max
  hours = total_seconds / 3600
  minutes = (total_seconds % 3600) / 60
  seconds = total_seconds % 60

  if hours > 0
    "#{hours}h #{minutes}m #{seconds}s"
  elsif minutes > 0
    "#{minutes}m #{seconds}s"
  else
    "#{seconds}s"
  end
end

#format_tool_summary(tool_counts, max_items = 6) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ralph/helpers.rb', line 41

def format_tool_summary(tool_counts, max_items = 6)
  if tool_counts.empty?
    ""
  else
    entries = tool_counts.sort_by { |_, v| -v }
    shown = entries.first(max_items)
    remaining = entries.length - shown.length

    shown.map { |name, count| "#{name} #{count}" }
      .tap { |parts| parts << "+#{remaining} more" if remaining > 0 }
      .then { |parts| parts.join("") }
  end
end

#now_msObject



72
73
74
# File 'lib/ralph/helpers.rb', line 72

def now_ms
  (Time.now.to_f * 1000).to_i
end

#strip_ansi(input) ⇒ Object



5
6
7
# File 'lib/ralph/helpers.rb', line 5

def strip_ansi(input)
  input.gsub(/\x1B\[[0-9;]*m/, "")
end

#which(cmd) ⇒ Object

Cross-platform which



61
62
63
64
65
66
67
68
69
70
# File 'lib/ralph/helpers.rb', line 61

def which(cmd)
  exts = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : [""]
  ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end
  nil
end