Class: N2B::CLI

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

Constant Summary

Constants inherited from Base

Base::CONFIG_FILE, Base::HISTORY_FILE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#get_config, #load_config

Constructor Details

#initialize(args) ⇒ CLI

Returns a new instance of CLI.



7
8
9
10
# File 'lib/n2b/cli.rb', line 7

def initialize(args)
  @args = args
  @options = parse_options
end

Class Method Details

.run(args) ⇒ Object



3
4
5
# File 'lib/n2b/cli.rb', line 3

def self.run(args)
  new(args).execute
end

Instance Method Details

#executeObject



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
# File 'lib/n2b/cli.rb', line 12

def execute
  config = get_config(reconfigure: @options[:config])
  input_text = @args.join(' ')
  if input_text.empty?
    puts "Enter your natural language command:"
    input_text = $stdin.gets.chomp
  end

  bash_commands = call_llm(input_text, config)

  puts "\nTranslated #{get_user_shell} Commands:"
  puts "------------------------"
  puts bash_commands['commands']
  puts "------------------------"
  if bash_commands['explanation']
    puts "Explanation:" 
    puts bash_commands['explanation']
    puts "------------------------"
  end 

  if @options[:execute]
    puts "Press Enter to execute these commands, or Ctrl+C to cancel."
    $stdin.gets
    system(bash_commands['commands'].join("\n"))
  else
    add_to_shell_history(bash_commands['commands'].join("\n")) if config['append_to_shell_history']
  end
end