Module: Mighost::Formatting

Included in:
PlainUI
Defined in:
lib/mighost/formatting.rb

Class Method Summary collapse

Class Method Details

.format_age(created_at) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mighost/formatting.rb', line 39

def format_age(created_at)
  return "-" unless created_at

  seconds = Time.now - created_at
  return "-" if seconds < 0

  days = (seconds / 86400).to_i
  months = (days / 30.0).to_i
  years = (days / 365.0).to_i

  if years >= 1
    remaining_months = ((days - years * 365) / 30.0).to_i
    if remaining_months.positive?
      "#{years}y #{remaining_months}mo"
    else
      "#{years}y"
    end
  elsif months >= 1
    "#{months}mo"
  elsif days >= 1
    "#{days}d"
  else
    "<1d"
  end
end

.format_captured_at(value) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/mighost/formatting.rb', line 31

def format_captured_at(value)
  case value
  when String then value[0, 10]
  when Time then value.strftime("%Y-%m-%d")
  else "-"
  end
end

.format_name(filename) ⇒ Object



15
16
17
18
19
20
# File 'lib/mighost/formatting.rb', line 15

def format_name(filename)
  return "(unknown)" unless filename

  name = filename.sub(/^\d+_/, "").sub(/\.rb$/, "")
  truncate(name, 40)
end

.format_recovery(source) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/mighost/formatting.rb', line 22

def format_recovery(source)
  case source
  when :snapshot then "snapshot"
  when :branch then "git"
  when :worktree then "worktree"
  else "-"
  end
end

.format_version(version) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/mighost/formatting.rb', line 7

def format_version(version)
  if version =~ /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/
    "#{$1}-#{$2}-#{$3} #{$4}:#{$5}"
  else
    version.to_s
  end
end

.parse_version_range(param, available_versions) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/mighost/formatting.rb', line 71

def parse_version_range(param, available_versions)
  return [] if available_versions.empty?

  case param
  when /^\.\.(.+)$/ # ..bbb (up to)
    upper = $1
    available_versions.select { |v| v <= upper }
  when /^(.+)\.\.$/ # aaa.. (from onwards)
    lower = $1
    available_versions.select { |v| v >= lower }
  when /^(.+)\.\.(.+)$/ # aaa..bbb (range)
    lower, upper = $1, $2
    available_versions.select { |v| v.between?(lower, upper) }
  else # single version
    available_versions.include?(param) ? [param] : []
  end
end

.truncate(str, max_length) ⇒ Object



65
66
67
68
69
# File 'lib/mighost/formatting.rb', line 65

def truncate(str, max_length)
  return str if str.length <= max_length

  "#{str[0, max_length - 3]}..."
end