Class: ClaudeSwarm::Commands::Ps

Inherits:
Object
  • Object
show all
Defined in:
lib/claude_swarm/commands/ps.rb

Instance Method Summary collapse

Instance Method Details

#executeObject



6
7
8
9
10
11
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
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
# File 'lib/claude_swarm/commands/ps.rb', line 6

def execute
  run_dir = ClaudeSwarm.joined_run_dir
  unless Dir.exist?(run_dir)
    puts "No active sessions"
    return
  end

  # Read all symlinks in run directory and process them
  sessions = Dir.glob("#{run_dir}/*").filter_map do |symlink|
    process_symlink(symlink)
  end

  if sessions.empty?
    puts "No active sessions"
    return
  end

  # Check if any session is missing main instance costs
  any_missing_main = sessions.any? { |s| !s[:main_has_cost] }

  # Column widths
  col_session = 15
  col_swarm = 25
  col_cost = 12
  col_uptime = 10

  # Display header with proper spacing
  header = "#{
    "SESSION_ID".ljust(col_session)
  }  #{
    "SWARM_NAME".ljust(col_swarm)
  }  #{
    "TOTAL_COST".ljust(col_cost)
  }  #{
    "UPTIME".ljust(col_uptime)
  }  DIRECTORY"

  # Only show warning if any session is missing main instance costs
  if any_missing_main
    puts "\n⚠️  \e[3mTotal cost does not include the cost of the main instance for some sessions\e[0m\n\n"
  else
    puts
  end

  puts header
  puts "-" * header.length

  # Display sessions sorted by start time (newest first)
  sessions.sort_by { |s| s[:start_time] }.reverse.each do |session|
    cost_str = format("$%.4f", session[:cost])
    # Add asterisk if this session is missing main instance cost
    cost_str += "*" unless session[:main_has_cost]

    puts "#{
      session[:id].ljust(col_session)
    }  #{
      truncate(session[:name], col_swarm).ljust(col_swarm)
    }  #{
      cost_str.ljust(col_cost)
    }  #{
      session[:uptime].ljust(col_uptime)
    }  #{session[:directory]}"
  end
end