Class: Sxn::Commands::Sessions

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/sxn/commands/sessions.rb

Overview

Manage development sessions

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV, local_options = {}, config = {}) ⇒ Sessions

Returns a new instance of Sessions.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sxn/commands/sessions.rb', line 13

def initialize(args = ARGV, local_options = {}, config = {})
  super
  @ui = Sxn::UI::Output.new
  @prompt = Sxn::UI::Prompt.new
  @table = Sxn::UI::Table.new
  @config_manager = Sxn::Core::ConfigManager.new
  @session_manager = Sxn::Core::SessionManager.new(@config_manager)
  @project_manager = Sxn::Core::ProjectManager.new(@config_manager)
  @worktree_manager = Sxn::Core::WorktreeManager.new(@config_manager, @session_manager)
  @template_manager = Sxn::Core::TemplateManager.new(@config_manager)
end

Instance Method Details

#activate(name = nil) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/sxn/commands/sessions.rb', line 281

def activate(name = nil)
  ensure_initialized!

  if name.nil?
    archived_sessions = @session_manager.list_sessions(status: "archived")
    if archived_sessions.empty?
      @ui.empty_state("No archived sessions to activate")
      return
    end

    choices = archived_sessions.map { |s| { name: s[:name], value: s[:name] } }
    name = @prompt.select("Select session to activate:", choices)
  end

  begin
    @session_manager.activate_session(name)
    @ui.success("Activated session '#{name}'")
  rescue Sxn::Error => e
    @ui.error(e.message)
    exit(e.exit_code)
  end
end

#add(name = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sxn/commands/sessions.rb', line 33

def add(name = nil)
  ensure_initialized!

  # Get session name interactively if not provided
  if name.nil?
    existing_sessions = @session_manager.list_sessions.map { |s| s[:name] }
    name = @prompt.session_name(existing_sessions: existing_sessions)
  end

  template_id = options[:template]

  # Validate template if specified
  if template_id
    begin
      @template_manager.validate_template(template_id)
    rescue Sxn::SessionTemplateNotFoundError, Sxn::SessionTemplateValidationError => e
      @ui.error(e.message)
      exit(1)
    end
  end

  # Get default branch - use provided option, or prompt interactively
  # Branch is required when using a template
  default_branch = options[:branch]
  if default_branch.nil?
    if template_id
      @ui.error("Branch is required when using a template. Use -b/--branch to specify.")
      exit(1)
    else
      default_branch = @prompt.default_branch(session_name: name)
    end
  end

  begin
    @ui.progress_start("Creating session '#{name}'")

    session = @session_manager.create_session(
      name,
      description: options[:description],
      linear_task: options[:linear_task],
      default_branch: default_branch,
      template_id: template_id
    )

    @ui.progress_done
    @ui.success("Created session '#{name}'")

    # Always activate the new session (this is the expected behavior)
    @session_manager.use_session(name)
    @ui.success("Switched to session '#{name}'")

    display_session_info(session)

    # Apply template if specified (with atomic rollback on failure)
    if template_id
      apply_template_to_session(name, template_id, default_branch)
    elsif !options[:skip_worktree]
      # Offer to add a worktree unless skipped or template was used
      offer_worktree_wizard(name)
    end
  rescue Sxn::Error => e
    @ui.progress_failed
    @ui.error(e.message)
    exit(e.exit_code)
  rescue StandardError => e
    @ui.progress_failed
    @ui.error("Unexpected error: #{e.message}")
    @ui.debug(e.backtrace.join("\n")) if ENV["SXN_DEBUG"]
    exit(1)
  end
end

#archive(name = nil) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/sxn/commands/sessions.rb', line 257

def archive(name = nil)
  ensure_initialized!

  if name.nil?
    active_sessions = @session_manager.list_sessions(status: "active")
    if active_sessions.empty?
      @ui.empty_state("No active sessions to archive")
      return
    end

    choices = active_sessions.map { |s| { name: s[:name], value: s[:name] } }
    name = @prompt.select("Select session to archive:", choices)
  end

  begin
    @session_manager.archive_session(name)
    @ui.success("Archived session '#{name}'")
  rescue Sxn::Error => e
    @ui.error(e.message)
    exit(e.exit_code)
  end
end

#current(subcommand = nil) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/sxn/commands/sessions.rb', line 215

def current(subcommand = nil)
  ensure_initialized!

  # Handle 'sxn current enter' subcommand
  if subcommand == "enter"
    enter_session
    return
  elsif subcommand
    @ui.error("Unknown subcommand: #{subcommand}")
    @ui.info("Available: enter")
    exit(1)
  end

  begin
    session = @session_manager.current_session

    if session.nil?
      @ui.info("No active session")
      suggest_create_session
      return
    end

    # If --path flag, just output the path for shell integration
    if options[:path]
      puts session[:path]
      return
    end

    @ui.section("Current Session")
    display_session_info(session, verbose: options[:verbose])
  rescue Sxn::Error => e
    @ui.error(e.message)
    exit(e.exit_code)
  end
end

#enterObject



252
253
254
# File 'lib/sxn/commands/sessions.rb', line 252

def enter
  enter_session
end

#listObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/sxn/commands/sessions.rb', line 150

def list
  ensure_initialized!

  begin
    sessions = @session_manager.list_sessions(
      status: options[:status],
      limit: options[:limit]&.to_i || 50
    )

    @ui.section("Sessions")

    if sessions.empty?
      @ui.empty_state("No sessions found")
      suggest_create_session
    else
      @table.sessions(sessions)
      @ui.newline
      @ui.info("Total: #{sessions.size} sessions")

      current = @session_manager.current_session
      if current
        @ui.info("Current: #{current[:name]}")
      else
        @ui.recovery_suggestion("Use 'sxn use <session>' to activate a session")
      end
    end
  rescue Sxn::Error => e
    @ui.error(e.message)
    exit(e.exit_code)
  end
end

#remove(name = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/sxn/commands/sessions.rb', line 108

def remove(name = nil)
  ensure_initialized!

  # Interactive selection if name not provided
  if name.nil?
    sessions = @session_manager.list_sessions
    if sessions.empty?
      @ui.empty_state("No sessions found")
      return
    end

    choices = sessions.map { |s| { name: s[:name], value: s[:name] } }
    name = @prompt.select("Select session to remove:", choices)
  end

  # Skip confirmation if force flag is used
  if !options[:force] && !@prompt.confirm_deletion(name, "session")
    @ui.info("Cancelled")
    return
  end

  begin
    @ui.progress_start("Removing session '#{name}'")
    @session_manager.remove_session(name, force: options[:force])
    @ui.progress_done
    @ui.success("Removed session '#{name}'")
  rescue Sxn::SessionHasChangesError => e
    @ui.progress_failed
    @ui.error(e.message)
    @ui.recovery_suggestion("Use --force to remove anyway, or commit/stash changes first")
    exit(e.exit_code)
  rescue Sxn::Error => e
    @ui.progress_failed
    @ui.error(e.message)
    exit(e.exit_code)
  end
end

#use(name = nil) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/sxn/commands/sessions.rb', line 183

def use(name = nil)
  ensure_initialized!

  # Interactive selection if name not provided
  if name.nil?
    sessions = @session_manager.list_sessions(status: "active")
    if sessions.empty?
      @ui.empty_state("No active sessions found")
      suggest_create_session
      return
    end

    choices = sessions.map do |s|
      { name: "#{s[:name]} - #{s[:description] || "No description"}", value: s[:name] }
    end
    name = @prompt.select("Select session to activate:", choices)
  end

  begin
    session = @session_manager.use_session(name)
    @ui.success("Activated session '#{name}'")
    display_session_info(session)
  rescue Sxn::Error => e
    @ui.error(e.message)
    exit(e.exit_code)
  end
end