Class: RubyCode::Views::Formatter::FetchSummary

Inherits:
Object
  • Object
show all
Defined in:
lib/rubycode/views/formatter/fetch_summary.rb

Overview

Builds fetch result summary display

Class Method Summary collapse

Class Method Details

.build(result:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rubycode/views/formatter/fetch_summary.rb', line 10

def self.build(result:)
  pastel = Pastel.new
   = result. || {}
  content = result.content || ""

  lines = build_header_lines(pastel, , content)
  lines.concat(build_preview_lines(pastel, content))
  lines << ""

  lines.join("\n")
end

.build_header_lines(pastel, metadata, content) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rubycode/views/formatter/fetch_summary.rb', line 22

def self.build_header_lines(pastel, , content)
  url = [:url] || "Unknown URL"
  size_kb = (content.bytesize / 1024.0).round(1)
  size_bytes = content.bytesize

  [
    "",
    "   #{pastel.cyan("📥")} Fetched from: #{pastel.bold(url)}",
    "   #{pastel.dim("Status:")} Success",
    "   #{pastel.dim("Size:")} #{size_kb} KB (#{size_bytes} bytes)"
  ]
end

.build_preview_lines(pastel, content) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/rubycode/views/formatter/fetch_summary.rb', line 35

def self.build_preview_lines(pastel, content)
  first_line = content.lines.first&.strip || ""
  return [] unless first_line.length.positive?

  [
    "",
    pastel.dim("   Preview:"),
    pastel.dim("   #{truncate(first_line, 100)}")
  ]
end

.truncate(text, max_length) ⇒ Object



46
47
48
49
50
# File 'lib/rubycode/views/formatter/fetch_summary.rb', line 46

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

  "#{text[0...(max_length - 3)]}..."
end