Module: Unmagic::Browser::Console::Banner

Defined in:
lib/unmagic/browser/console/banner.rb

Overview

What the console says for itself on the way in, and when asked for help.

Constant Summary collapse

SECTIONS =

The verb table, by heading: each row is what to type and what it does.

{
  "open" => [
    ['open "https://example.com"', "open a page โ€” every other verb drives it"],
    ["visit url", "go somewhere else in the same session"],
    ["back / forward / reload", "the browser buttons"],
    ["close", "let the browser go"],
    ["use :cloudflare", "switch drivers (:local, :remote, :cloudflare)"],
  ],
  "act" => [
    ['fill_in "Email", with: "me@example.com"', "by label, placeholder, name or id"],
    ['click "Sign in"', "anything โ€” button, link, div"],
    ['click_button "Go" / click_link "More"', "when you want to be specific"],
    ['select "Australia", from: "Country"', "by the label a reader sees"],
    ["check / uncheck / choose", "boxes and radios"],
    ["press :enter", "at whatever has focus"],
  ],
  "read" => [
    ["text / title / url", "the page as prose"],
    ["html / markdown", "the page as source, highlighted"],
    ["screenshot", "drawn right here, and saved to /tmp"],
    ["screenshot full_page: true", "the whole scrollable page"],
    ["pdf", "written to /tmp"],
    ['js "document.title"', "run JavaScript in the page"],
  ],
  "find" => [
    ['find ".cart"', "one element โ€” waits for it"],
    ['all ".product"', "every match, right now"],
    ['within(".checkout") { click "Pay" }', "scope the verbs inside"],
    ['wait_for text: "Welcome"', "block until it shows up"],
    ['has_text? "Welcome"', "ask, and get an answer now"],
  ],
  "emulate" => [
    ["emulate color_scheme: :dark", "and screenshot it"],
    ['emulate device: "iPhone 13"', "viewport, user agent and touch"],
    ["emulate viewport: { width: 1440, height: 900 }", ""],
  ],
}.freeze
ONE_OFFS =

The four verbs that take a URL work without a session too, which is worth saying once rather than five times in the table.

"markdown, html, screenshot and pdf take a URL for a one-off grab: " \
'markdown "https://example.com"'

Class Method Summary collapse

Class Method Details

.helpString

Returns the full list of verbs.

Returns:

  • (String)

    the full list of verbs



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/unmagic/browser/console/banner.rb', line 65

def help
  lines = [""]
  SECTIONS.each do |heading, rows|
    lines << "  #{Terminal.paint(Terminal::GREEN, heading)}"
    width = rows.map { |verb, _| verb.length }.max
    rows.each do |verb, note|
      lines << "    #{Terminal.paint(Terminal::BLUE, verb.ljust(width))}  #{Terminal.dim(note)}".rstrip
    end
    lines << ""
  end
  lines << "  #{Terminal.dim(ONE_OFFS)}"
  lines << ""
  lines.join("\n")
end

.renderString

Returns the banner shown at startup.

Returns:

  • (String)

    the banner shown at startup



55
56
57
58
59
60
61
62
# File 'lib/unmagic/browser/console/banner.rb', line 55

def render
  [
    "",
    "  #{Terminal.paint(Terminal::BOLD, "unmagic-browser")} #{Terminal.dim(Unmagic::Browser::VERSION)}",
    "  #{Terminal.dim("a browser you can type at ยท `help` for the verbs")}",
    "",
  ].join("\n")
end