Class: Sxn::UI::Prompt

Inherits:
Object
  • Object
show all
Defined in:
lib/sxn/ui/prompt.rb

Overview

Interactive prompts with validation

Instance Method Summary collapse

Constructor Details

#initializePrompt

Returns a new instance of Prompt.



9
10
11
# File 'lib/sxn/ui/prompt.rb', line 9

def initialize
  @prompt = TTY::Prompt.new(interrupt: :exit)
end

Instance Method Details

#ask(message, options = {}) ⇒ Object



13
14
15
# File 'lib/sxn/ui/prompt.rb', line 13

def ask(message, options = {}, &)
  @prompt.ask(message, **options, &)
end

#ask_yes_no(message, default: false) ⇒ Object



17
18
19
# File 'lib/sxn/ui/prompt.rb', line 17

def ask_yes_no(message, default: false)
  @prompt.yes?(message, default: default)
end

#branch_name(message = "Enter branch name:", default: nil) ⇒ Object



64
65
66
67
68
69
# File 'lib/sxn/ui/prompt.rb', line 64

def branch_name(message = "Enter branch name:", default: nil)
  ask(message, default: default) do |q|
    q.validate(%r{\A[a-zA-Z0-9_/-]+\z}, "Branch name must be a valid git branch name")
    q.modify :strip
  end
end

#confirm_deletion(item_name, item_type = "item") ⇒ Object



71
72
73
74
# File 'lib/sxn/ui/prompt.rb', line 71

def confirm_deletion(item_name, item_type = "item")
  ask_yes_no("Are you sure you want to delete #{item_type} '#{item_name}'? This action cannot be undone.",
             default: false)
end

#folder_name(message = "Enter sessions folder name:", default: nil) ⇒ Object



29
30
31
32
33
34
# File 'lib/sxn/ui/prompt.rb', line 29

def folder_name(message = "Enter sessions folder name:", default: nil)
  ask(message, default: default) do |q|
    q.validate(/\A[a-zA-Z0-9_-]+\z/, "Folder name must contain only letters, numbers, hyphens, and underscores")
    q.modify :strip
  end
end

#multi_select(message, choices, options = {}) ⇒ Object



25
26
27
# File 'lib/sxn/ui/prompt.rb', line 25

def multi_select(message, choices, options = {})
  @prompt.multi_select(message, choices, **options)
end

#project_detection_confirm(detected_projects) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sxn/ui/prompt.rb', line 102

def project_detection_confirm(detected_projects)
  return false if detected_projects.empty?

  puts ""
  puts "Detected projects in current directory:"
  detected_projects.each do |project|
    puts "  #{project[:name]} (#{project[:type]}) - #{project[:path]}"
  end
  puts ""

  ask_yes_no("Would you like to register these projects automatically?", default: true)
end

#project_name(message = "Enter project name:") ⇒ Object



46
47
48
49
50
51
# File 'lib/sxn/ui/prompt.rb', line 46

def project_name(message = "Enter project name:")
  ask(message) do |q|
    q.validate(/\A[a-zA-Z0-9_-]+\z/, "Project name must contain only letters, numbers, hyphens, and underscores")
    q.modify :strip
  end
end

#project_path(message = "Enter project path:") ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/sxn/ui/prompt.rb', line 53

def project_path(message = "Enter project path:")
  ask(message) do |q|
    q.validate(lambda { |path|
      expanded = File.expand_path(path)
      File.directory?(expanded) && File.readable?(expanded)
    }, "Path must be a readable directory")
    q.modify :strip
    q.convert ->(path) { File.expand_path(path) }
  end
end

#rule_typeObject



76
77
78
79
80
81
82
# File 'lib/sxn/ui/prompt.rb', line 76

def rule_type
  select("Select rule type:", [
           { name: "Copy Files", value: "copy_files" },
           { name: "Setup Commands", value: "setup_commands" },
           { name: "Template", value: "template" }
         ])
end

#select(message, choices, options = {}) ⇒ Object



21
22
23
# File 'lib/sxn/ui/prompt.rb', line 21

def select(message, choices, options = {})
  @prompt.select(message, choices, **options)
end

#session_name(message = "Enter session name:", existing_sessions: []) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/sxn/ui/prompt.rb', line 36

def session_name(message = "Enter session name:", existing_sessions: [])
  ask(message) do |q|
    q.validate(/\A[a-zA-Z0-9_-]+\z/, "Session name must contain only letters, numbers, hyphens, and underscores")
    q.validate(lambda { |name|
      !existing_sessions.include?(name)
    }, "Session name already exists")
    q.modify :strip
  end
end

#sessions_folder_setupObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sxn/ui/prompt.rb', line 84

def sessions_folder_setup
  puts "Setting up sessions folder..."
  puts "This will create a folder where all your development sessions will be stored."
  puts ""

  default_folder = "#{File.basename(Dir.pwd)}-sessions"
  folder = folder_name("Sessions folder name:", default: default_folder)

  current_dir = ask_yes_no("Create sessions folder in current directory?", default: true)

  unless current_dir
    base_path = project_path("Base path for sessions folder:")
    folder = File.join(base_path, folder)
  end

  folder
end