Class: Agentilda::Viewer

Inherits:
Object
  • Object
show all
Defined in:
lib/agentilda/viewer.rb

Overview

Hands Markdown files to a renderer outside this process: the system viewer via open, or mdfried in the terminal.

Both paths shell out, so the class takes its two effects as lambdas and the specs assert on the commands instead of launching viewers on the box running the suite.

Constant Summary collapse

INSTALL =

What to run when mdfried is asked for but not installed. Homebrew is the one package manager the toolchain already assumes (see the Brewfile convention), so there is no fallback chain to get wrong.

"brew install mdfried >/dev/null"
DETECT =

How to notice mdfried is missing without depending on which, which differs across shells; command -v is POSIX.

"command -v mdfried >/dev/null"

Instance Method Summary collapse

Constructor Details

#initialize(launch: ->(*cmd) { system(*cmd) }, pipe: ->(cmd, &block) { IO.popen(cmd, "w", &block) }) ⇒ Viewer

Returns a new instance of Viewer.

Parameters:

  • launch (#call) (defaults to: ->(*cmd) { system(*cmd) })

    runs a command, returns truthy on success

  • pipe (#call) (defaults to: ->(cmd, &block) { IO.popen(cmd, "w", &block) })

    runs a command, yielding its stdin as an IO



22
23
24
25
26
# File 'lib/agentilda/viewer.rb', line 22

def initialize(launch: ->(*cmd) { system(*cmd) },
  pipe: ->(cmd, &block) { IO.popen(cmd, "w", &block) })
  @launch = launch
  @pipe = pipe
end

Instance Method Details

#mdfried(paths) ⇒ void

This method returns an undefined value.

Streams each file's content into mdfried, installing it first when it is missing.

Parameters:

  • paths (Array<String>)

Raises:



42
43
44
45
46
47
# File 'lib/agentilda/viewer.rb', line 42

def mdfried(paths)
  ensure_mdfried!
  Array(paths).each do |path|
    @pipe.call("mdfried") { |io| io.write(File.read(path)) }
  end
end

#open(paths) ⇒ void

This method returns an undefined value.

Opens each file in whatever the system has configured for Markdown.

Parameters:

  • paths (Array<String>)


32
33
34
# File 'lib/agentilda/viewer.rb', line 32

def open(paths)
  Array(paths).each { |path| @launch.call("open", path) }
end