Skip to content

Instantly share code, notes, and snippets.

@imneonizer
Last active September 6, 2025 10:07
Show Gist options
  • Select an option

  • Save imneonizer/fc8d683ed3e0c02cd52370a81c28ca0e to your computer and use it in GitHub Desktop.

Select an option

Save imneonizer/fc8d683ed3e0c02cd52370a81c28ca0e to your computer and use it in GitHub Desktop.
docker ps output formatting for clean preview
#!/bin/bash
# Function to print colored docker ps table
print_colored_docker_ps() {
# List of readable color codes
local colors=(31 32 33 34 35 36 91 92 93 94 95 96)
declare -A project_colors
# Print table header
printf "%-12s\t%-30s\t%-40s\t%s\n" "CONTAINER ID" "NAMES" "IMAGE" "STATUS"
# Read docker ps output into an array to process after color assignment
mapfile -t docker_lines < <(docker ps --format '{{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Status}}')
# Collect all unique projects
declare -A all_projects
for line in "${docker_lines[@]}"; do
IFS=$'\t' read -r id name image status <<< "$line"
project="${name%%_*}"
if [[ -z "$project" ]]; then
project="$name"
fi
all_projects["$project"]=1
done
# Assign a unique color to each project (cycling if more projects than colors)
local i=0
for project in "${!all_projects[@]}"; do
color=${colors[$((i % ${#colors[@]}))]}
project_colors["$project"]=$color
((i++))
done
# Print each line with the assigned project color
for line in "${docker_lines[@]}"; do
IFS=$'\t' read -r id name image status <<< "$line"
project="${name%%_*}"
if [[ -z "$project" ]]; then
project="$name"
fi
color=${project_colors[$project]}
printf "\e[1;${color}m%-12s\t%-30s\t%-40s\t%s\e[0m\n" "$id" "$name" "$image" "$status"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment