Skip to content

Commit bcfeb91

Browse files
committed
feat: add --status option to show running balatro instances
1 parent b48bd70 commit bcfeb91

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

balatro.sh

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ HEADLESS=false
88
FAST=false
99
FORCE_KILL=true
1010
KILL_ONLY=false
11+
STATUS_ONLY=false
1112

1213
# Platform detection
1314
case "$OSTYPE" in
@@ -29,21 +30,24 @@ show_usage() {
2930
cat <<EOF
3031
Usage: $0 -p PORT [OPTIONS]
3132
$0 --kill
33+
$0 --status
3234
33-
Required (unless --kill):
35+
Required (unless --kill or --status):
3436
-p, --port PORT Specify port for Balatro instance (can be used multiple times)
3537
3638
Options:
3739
--headless Enable headless mode (sets BALATROBOT_HEADLESS=1)
3840
--fast Enable fast mode (sets BALATROBOT_FAST=1)
3941
--kill Kill all running Balatro instances and exit
42+
--status Show information about running Balatro instances
4043
-h, --help Show this help message
4144
4245
Examples:
4346
$0 -p 12347 # Start single instance on port 12347
4447
$0 -p 12346 -p 12347 # Start two instances on ports 12346 and 12347
4548
$0 --headless --fast -p 12346 # Start with headless and fast mode
4649
$0 --kill # Kill all running Balatro instances
50+
$0 --status # Show running instances
4751
4852
EOF
4953
}
@@ -76,6 +80,10 @@ parse_arguments() {
7680
KILL_ONLY=true
7781
shift
7882
;;
83+
--status)
84+
STATUS_ONLY=true
85+
shift
86+
;;
7987
-h | --help)
8088
show_usage
8189
exit 0
@@ -96,6 +104,13 @@ parse_arguments() {
96104
show_usage
97105
exit 1
98106
fi
107+
elif [[ "$STATUS_ONLY" == "true" ]]; then
108+
# In status mode, no ports are required
109+
if [[ ${#PORTS[@]} -gt 0 ]]; then
110+
echo "Error: --status cannot be used with port specifications" >&2
111+
show_usage
112+
exit 1
113+
fi
99114
else
100115
# In normal mode, at least one port must be specified
101116
if [[ ${#PORTS[@]} -eq 0 ]]; then
@@ -285,6 +300,58 @@ print_instance_info() {
285300
done
286301
}
287302

303+
# Show status of running Balatro instances
304+
show_status() {
305+
# Build platform-specific grep pattern
306+
local grep_pattern=""
307+
for i in "${!PROCESS_PATTERNS[@]}"; do
308+
if [[ $i -eq 0 ]]; then
309+
grep_pattern="${PROCESS_PATTERNS[$i]}"
310+
else
311+
grep_pattern="$grep_pattern|${PROCESS_PATTERNS[$i]}"
312+
fi
313+
done
314+
315+
# Find running Balatro processes
316+
local running_processes=()
317+
while IFS= read -r line; do
318+
running_processes+=("$line")
319+
done < <(ps aux | grep -E "($grep_pattern)" | grep -v grep | awk '{print $2}')
320+
321+
if [[ ${#running_processes[@]} -eq 0 ]]; then
322+
echo "No Balatro instances are currently running"
323+
return 0
324+
fi
325+
326+
# For each running process, find its listening port
327+
for pid in "${running_processes[@]}"; do
328+
local port=""
329+
local log_file=""
330+
331+
# Use lsof to find listening ports for this PID
332+
if command -v lsof >/dev/null 2>&1; then
333+
# Look for TCP listening ports (any port >=1024, matching script validation)
334+
local ports_output
335+
ports_output=$(lsof -Pan -p "$pid" -i TCP 2>/dev/null | grep LISTEN | awk '{print $9}' | cut -d: -f2)
336+
337+
# Find the first valid port for this Balatro instance
338+
while IFS= read -r found_port; do
339+
if [[ "$found_port" =~ ^[0-9]+$ ]] && [[ "$found_port" -ge 1024 ]] && [[ "$found_port" -le 65535 ]]; then
340+
port="$found_port"
341+
log_file="logs/balatro_${port}.log"
342+
break
343+
fi
344+
done <<<"$ports_output"
345+
fi
346+
347+
# Only display processes that have a listening port (actual Balatro instances)
348+
if [[ -n "$port" ]]; then
349+
echo "• Port $port, PID $pid, Log: $log_file"
350+
fi
351+
# Skip processes without listening ports - they're not actual Balatro instances
352+
done
353+
}
354+
288355
# Cleanup function for signal handling
289356
cleanup() {
290357
echo ""
@@ -317,6 +384,12 @@ main() {
317384
exit 0
318385
fi
319386

387+
# Handle status-only mode
388+
if [[ "$STATUS_ONLY" == "true" ]]; then
389+
show_status
390+
exit 0
391+
fi
392+
320393
# Create logs directory
321394
if ! create_logs_directory; then
322395
exit 1

0 commit comments

Comments
 (0)