Skip to content

Commit 5075daf

Browse files
committed
feat(docker): add ps script
1 parent 49192b2 commit 5075daf

File tree

1 file changed

+68
-0
lines changed
  • scripts/docker

1 file changed

+68
-0
lines changed

Diff for: scripts/docker/ps

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
source "$DOTFILES_PATH/scripts/core/_main.sh"
6+
7+
##? List all containers
8+
##?
9+
##? Usage:
10+
##? ps
11+
docs::parse "$@"
12+
13+
RESET="\e[0m"
14+
BOLD="\e[1m"
15+
CYAN="\e[36m"
16+
YELLOW="\e[33m"
17+
GREEN="\e[32m"
18+
MAGENTA="\e[35m"
19+
BLUE="\e[34m"
20+
UNDERLINE="\e[4m"
21+
22+
declare -a headers=("Name" "Image" "Ports" "Status" "Created")
23+
declare -a colors=($CYAN $YELLOW $GREEN $MAGENTA $BLUE)
24+
25+
get_column_widths() {
26+
local data="$1"
27+
local -n widths=$2
28+
29+
for i in {0..4}; do
30+
widths[$i]=$(echo "$data" | awk -F'|' -v col=$((i + 1)) '{print length($col)}' | sort -nr | head -n1)
31+
widths[$i]=$((widths[$i] > ${#headers[$i]} ? widths[$i] : ${#headers[$i]}))
32+
done
33+
}
34+
35+
print_headers() {
36+
local -n widths=$1
37+
for i in {0..4}; do
38+
if [ "$i" -lt 4 ]; then
39+
printf "${UNDERLINE}${BOLD}${colors[$i]}%-${widths[$i]}s ${RESET}" "${headers[$i]}"
40+
else
41+
printf "${UNDERLINE}${BOLD}${colors[$i]}%-${widths[$i]}s${RESET}" "${headers[$i]}"
42+
fi
43+
done
44+
printf "\n"
45+
}
46+
47+
print_data_rows() {
48+
local data="$1"
49+
local -n widths=$2
50+
echo "$data" | while IFS='|' read -r "${headers[@]}"; do
51+
for i in {0..4}; do
52+
if [ "$i" -lt 4 ]; then
53+
printf "${colors[$i]}%-${widths[$i]}s ${RESET}" "${!headers[$i]}"
54+
else
55+
printf "${colors[$i]}%-${widths[$i]}s${RESET}" "${!headers[$i]}"
56+
fi
57+
done
58+
printf "\n"
59+
done
60+
}
61+
62+
docker_data=$(docker ps -a --format "{{.Names}}|{{.Image}}|{{.Ports}}|{{.Status}}|{{.CreatedAt}}" | sort)
63+
64+
declare -a column_widths
65+
get_column_widths "$docker_data" column_widths
66+
67+
print_headers column_widths
68+
print_data_rows "$docker_data" column_widths

0 commit comments

Comments
 (0)