Class: GptHelpr::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/gpt_helpr/cli.rb

Class Method Summary collapse

Class Method Details

.copy_to_clipboard(output) ⇒ Object



90
91
92
93
# File 'lib/gpt_helpr/cli.rb', line 90

def self.copy_to_clipboard(output)
  escaped_output = Shellwords.escape(output)
  system("echo #{escaped_output} | pbcopy")
end

.generate_output(files, line_numbers = false) ⇒ Object



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

def self.generate_output(files, line_numbers = false)
  output = ''
  files.each do |file_info|
    file_path, re_text, lines_range = file_info

    content = process_file(file_path, lines_range, line_numbers)

    output += "\n==== file source `#{file_path} #{lines_range}`\n"
    output += "```\n"
    output += "#{content}\n"
    output += "```\n\n"
    output += "#{re_text}\n\n"
    output += "==== end of `#{file_path}`\n"
    output += "\n"
  end
  output
end

.interactive_mode(_line_numbers = false) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gpt_helpr/cli.rb', line 102

def self.interactive_mode(_line_numbers = false)
  files = []

  Readline.completion_append_character = nil
  Readline.completion_proc = ->(input) { Dir["#{input}*"].grep(/^#{Regexp.escape(input)}/) }

  loop do
    file_input = Readline.readline('File Path (optional :start:end): ', true)
    break if file_input.nil? || file_input.strip.empty?

    file_path, lines_range = file_input.split(':', 2)
    re_text = Readline.readline('Instructions: ', true)

    files << [file_path.strip, re_text.strip, lines_range]
  rescue Interrupt
    break
  end

  files
end

.process_file(file_path, lines_range = nil, line_numbers = false) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gpt_helpr/cli.rb', line 54

def self.process_file(file_path, lines_range = nil, line_numbers = false)
  content = File.read(file_path)

  if lines_range
    start_line, end_line = lines_range.split(':').map(&:to_i)
    content = content.lines[start_line - 1, end_line - start_line + 1].join
  end

  if line_numbers
    start_index = lines_range ? start_line : 1
    content = content.lines.each_with_index.map { |line, index| "#{start_index + index}: #{line}" }.join
  end

  content
rescue StandardError => e
  "Error reading file: #{e.message}"
end

.start(args = []) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gpt_helpr/cli.rb', line 8

def self.start(args = [])
  Dir.chdir(Dir.pwd)

  puts "== 🏴‍☠️ GptHelpr #{GptHelpr::VERSION} == Helping to dig your codebase and cook GPT-XX instructions [Location: #{Dir.pwd}]"
  line_numbers = args.include?('-ln') || args.include?('--line-numbers')

  if args.include?('-i') || args.include?('--interactive')
    files = interactive_mode(line_numbers)
  else

    if args.empty?
      puts 'Usage: gpt_helpr <file1> <instructions> / <file2> <instructions> [--f] [--file] [--i] [--interactive] [--ln] [--line-numbers]'
      exit 1
    end

    separator_indices = args.each_index.select { |i| args[i] == '/' }
    parts = []
    last_index = 0

    separator_indices.each do |index|
      parts << args[last_index...index]
      last_index = index + 1
    end
    parts << args[last_index..]

    files = parts.map do |part|
      file_path = part[0]
      re_text = part[1]
      lines_range = file_path.include?(':') ? file_path.split(':', 2).last : nil
      file_path = file_path.split(':').first

      [file_path, re_text, lines_range]
    end
  end

  final_output = generate_output(files, line_numbers)
  copy_to_clipboard(final_output)

  if args.include?('-f') || args.include?('--file')
    write_to_file(final_output)
  else
    puts final_output
    puts '(Output copied to clipboard)'
  end
end

.write_to_file(output) ⇒ Object



95
96
97
98
99
100
# File 'lib/gpt_helpr/cli.rb', line 95

def self.write_to_file(output)
  timestamp = Time.now.utc.iso8601
  filename = "gpt_#{timestamp}.md"
  File.write(filename, output)
  puts "(Output written to #{filename})"
end