Module: PromptObjects::CLI::Helpers

Defined in:
lib/prompt_objects/cli/helpers.rb

Overview

Shared helpers for CLI commands.

Class Method Summary collapse

Class Method Details

.load_all_objects(runtime, env_path) ⇒ Object

Load all prompt objects from an environment into a runtime.



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

def load_all_objects(runtime, env_path)
  objects_dir = File.join(env_path, "objects")
  return unless Dir.exist?(objects_dir)

  Dir.glob(File.join(objects_dir, "*.md")).each do |path|
    runtime.load_prompt_object(path)
  rescue StandardError => e
    $stderr.puts "Warning: Failed to load #{path}: #{e.message}"
  end

  runtime.registry.prompt_objects.each do |po|
    runtime.load_dependencies(po)
  end
end

.open_in_browser(url) ⇒ Object

Open a URL in the platform browser.



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/prompt_objects/cli/helpers.rb', line 57

def open_in_browser(url)
  case RUBY_PLATFORM
  when /darwin/
    system("open", url)
  when /linux/
    system("xdg-open", url)
  when /mswin|mingw|cygwin/
    system("start", url)
  else
    $stderr.puts "Open this URL in your browser: #{url}"
  end
end

Print a list of bus events in the standard one-line format.



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

def print_events(events)
  if events.empty?
    puts "No events found."
    return
  end

  events.each do |e|
    ts = e[:timestamp] || e["timestamp"]
    ts = ts.is_a?(Time) ? ts.strftime("%H:%M:%S") : ts&.split("T")&.last&.split(".")&.first
    from = e[:from] || e["from"]
    to = e[:to] || e["to"]
    summary = e[:summary] || e["summary"]

    line = "#{ts}  #{from} -> #{to}: #{summary}"
    line = line[0, 120] + "..." if line.length > 123
    puts line
  end
end

.program_nameObject

The name the CLI was invoked as (e.g. "prompt_objects" or "poop"). Used in usage/help text so each executable describes itself.



11
12
13
14
# File 'lib/prompt_objects/cli/helpers.rb', line 11

def program_name
  name = File.basename($PROGRAM_NAME.to_s)
  name.empty? ? "prompt_objects" : name
end

.resolve_environment(name_or_path) ⇒ String?

Resolve an environment name or path to an environment directory.

Parameters:

  • name_or_path (String)

Returns:

  • (String, nil)

    Path to the environment, or nil if not found



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/prompt_objects/cli/helpers.rb', line 19

def resolve_environment(name_or_path)
  # Check if it's already a path
  if File.directory?(name_or_path)
    manifest_path = File.join(name_or_path, "manifest.yml")
    return name_or_path if File.exist?(manifest_path)
  end

  # Check in standard locations
  locations = [
    File.join(Dir.home, ".prompt_objects", "environments", name_or_path),
    File.join(".", name_or_path)
  ]

  locations.each do |path|
    manifest_path = File.join(path, "manifest.yml")
    return path if File.exist?(manifest_path)
  end

  nil
end