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.



12
13
14
15
16
17
18
19
# File 'lib/sxn/commands/sessions.rb', line 12

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)
end

Instance Method Details

#activate(name = nil) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/sxn/commands/sessions.rb', line 219

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



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
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sxn/commands/sessions.rb', line 26

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

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

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

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

    if options[:activate]
      @session_manager.use_session(name)
      @ui.success("Activated session '#{name}'")
    end

    display_session_info(session)
  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



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/sxn/commands/sessions.rb', line 195

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

#currentObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/sxn/commands/sessions.rb', line 174

def current
  ensure_initialized!

  begin
    session = @session_manager.current_session

    if session.nil?
      @ui.info("No active session")
      suggest_create_session
      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

#listObject



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
# File 'lib/sxn/commands/sessions.rb', line 110

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



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
104
# File 'lib/sxn/commands/sessions.rb', line 68

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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/sxn/commands/sessions.rb', line 143

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