Module: Wralph::Interfaces::Shell

Defined in:
lib/wralph/interfaces/shell.rb

Class Method Summary collapse

Class Method Details

.ask_user_to_continue(message = 'Continue? (y/N) ') ⇒ Object



53
54
55
56
57
# File 'lib/wralph/interfaces/shell.rb', line 53

def self.ask_user_to_continue(message = 'Continue? (y/N) ')
  print message
  response = $stdin.gets.chomp
  exit 1 unless response =~ /^[Yy]$/
end

.command_exists?(cmd) ⇒ Boolean

Check if a command exists

Returns:

  • (Boolean)


11
12
13
# File 'lib/wralph/interfaces/shell.rb', line 11

def self.command_exists?(cmd)
  system("which #{cmd} > /dev/null 2>&1")
end

.get_worktreesObject



23
24
25
26
# File 'lib/wralph/interfaces/shell.rb', line 23

def self.get_worktrees
  json_output, = run_command("wt list --format=json")
  JSON.parse(json_output.force_encoding('UTF-8'))
end

.run_command(cmd, raise_on_error: false) ⇒ Object

Run a command and return stdout, stderr, and status



16
17
18
19
20
21
# File 'lib/wralph/interfaces/shell.rb', line 16

def self.run_command(cmd, raise_on_error: false)
  stdout, stderr, status = Open3.capture3(cmd)
  raise "Command failed: #{cmd}\n#{stderr}" if raise_on_error && !status.success?

  [stdout.chomp, stderr.chomp, status.success?]
end

.switch_into_worktree(branch_name, create_if_not_exists: true) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/wralph/interfaces/shell.rb', line 28

def self.switch_into_worktree(branch_name, create_if_not_exists: true)
  # Check if any entry matches the branch name
  worktree = get_worktrees.find { |wt| wt['branch'] == branch_name }
  if worktree
    success = system("wt switch #{worktree['branch']}")
    unless success
      Print.error "Failed to switch to branch #{branch_name}"
      exit 1
    end
  elsif create_if_not_exists
    success = system("wt switch --create #{branch_name}")
    unless success
      Print.error "Failed to switch to branch #{branch_name}"
      exit 1
    end
    worktree = get_worktrees.find { |wt| wt['branch'] == branch_name }
  else
    Print.error "Worktree for branch #{branch_name} not found. Use --create to create it."
    exit 1
  end

  # Change the directory of the CURRENT Ruby process to the new worktree
  Dir.chdir(worktree['path'])
end