Skip to content

Instantly share code, notes, and snippets.

@Zxce3
Created January 7, 2025 17:21
Show Gist options
  • Save Zxce3/72749a80f019c2db61771194ca22864e to your computer and use it in GitHub Desktop.
Save Zxce3/72749a80f019c2db61771194ca22864e to your computer and use it in GitHub Desktop.
A script to manage Oh My Zsh themes, including listing, changing, previewing, and backing up themes.
#!/bin/bash
# Oh My Zsh Theme Manager
# Description: A script to manage Oh My Zsh themes, including listing, changing, previewing, and backing up themes.
# Author: Memet Zx
# Github: https://github.com/zxce3
# Purpose: To provide an easy way to manage Oh My Zsh themes from the command line.
# Usage: ./chzsh.sh [option]
# Options:
# -l, --list List all available themes
# -r, --random Apply a random theme
# -p, --preview Preview a theme without applying it
# -b, --backup Backup current theme configuration
# -f, --font Install Powerline or Nerd Font
# -h, --help Show this help message
# No option will start interactive theme selection
# Script information
SCRIPT_VERSION="1.0.0"
SCRIPT_DATE="$(date +%Y-%m-%d)"
CURRENT_USER="$USER"
# Path to the Oh My Zsh themes directory
THEMES_DIR="$HOME/.oh-my-zsh/themes"
# Path to the .zshrc file
ZSHRC="$HOME/.zshrc"
# Path to backup directory
BACKUP_DIR="$HOME/.zsh_theme_backups"
# Colors for better visualization
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to show script usage
show_help() {
echo -e "${BLUE}Oh My Zsh Theme Manager v${SCRIPT_VERSION}${NC}"
echo -e "Current user: ${GREEN}${CURRENT_USER}${NC}"
echo -e "Current time: ${GREEN}$(date -u '+%Y-%m-%d %H:%M:%S UTC')${NC}"
echo -e "\n${BLUE}Usage:${NC}"
echo " $0 [option]"
echo -e "\n${BLUE}Options:${NC}"
echo " -l, --list List all available themes"
echo " -r, --random Apply a random theme"
echo " -p, --preview Preview a theme without applying it"
echo " -b, --backup Backup current theme configuration"
echo " -f, --font Install Powerline or Nerd Font"
echo " -h, --help Show this help message"
echo " No option will start interactive theme selection"
}
# Function to list available themes with numbers
list_themes() {
echo -e "${BLUE}Available themes:${NC}"
i=1
while IFS= read -r theme; do
theme_name=$(basename "$theme" .zsh-theme)
if [ "$((i % 3))" == "1" ]; then
printf "\n%3d) %-25s" $i "$theme_name"
else
printf "%3d) %-25s" $i "$theme_name"
fi
((i++))
done < <(ls "$THEMES_DIR"/*.zsh-theme)
echo -e "\n"
}
# Function to get current theme
get_current_theme() {
current_theme=$(grep "^ZSH_THEME=" "$ZSHRC" | cut -d'"' -f2)
echo "$current_theme"
}
# Function to backup current configuration
backup_config() {
mkdir -p "$BACKUP_DIR"
backup_file="$BACKUP_DIR/zshrc_$(date +%Y%m%d_%H%M%S).backup"
cp "$ZSHRC" "$backup_file"
echo -e "${GREEN}Configuration backed up to: $backup_file${NC}"
}
# Function to update .zshrc with selected theme
change_theme() {
local selected_theme="$1"
local current_theme=$(get_current_theme)
# Backup before changing
backup_config
if [ "$current_theme" = "$selected_theme" ]; then
echo -e "${YELLOW}Theme '$selected_theme' is already active.${NC}"
return
fi
if grep -q "^ZSH_THEME=" "$ZSHRC"; then
sed -i.bak "s/^ZSH_THEME=.*/ZSH_THEME=\"$selected_theme\"/" "$ZSHRC"
else
echo "ZSH_THEME=\"$selected_theme\"" >> "$ZSHRC"
fi
echo -e "${GREEN}Theme changed from '$current_theme' to '$selected_theme'.${NC}"
echo -e "${YELLOW}Restarting your terminal to apply the changes...${NC}"
exec zsh
}
# Function to apply a random theme
apply_random_theme() {
local themes=($(ls "$THEMES_DIR"/*.zsh-theme | xargs -n 1 basename | sed 's/\.zsh-theme//'))
local random_theme=${themes[$RANDOM % ${#themes[@]}]}
change_theme "$random_theme"
}
# Function to preview theme (simulated)
preview_theme() {
local theme="$1"
echo -e "${BLUE}Preview of theme '$theme':${NC}"
echo -e "${YELLOW}Note: This is a simulated preview. Actual appearance may vary.${NC}"
# Display some sample prompt styles
echo -e "\nSample prompts:"
echo -e "$ ${CURRENT_USER}@hostname ~/directory"
echo -e "➜ ~/projects git:(main) ✗"
echo -e "→ ~/documents [master|●1]"
}
# Function to install Powerline or Nerd Font
install_font() {
echo -e "${BLUE}Installing Powerline or Nerd Font...${NC}"
echo -e "${YELLOW}Please choose a font to install:${NC}"
echo "1) Powerline Font"
echo "2) Nerd Font"
echo -n "Enter your choice (1 or 2): "
read font_choice
case "$font_choice" in
1)
git clone https://github.com/powerline/fonts.git --depth=1
cd fonts
./install.sh
cd ..
rm -rf fonts
echo -e "${GREEN}Powerline Font installed successfully.${NC}"
;;
2)
git clone https://github.com/ryanoasis/nerd-fonts.git --depth=1
cd nerd-fonts
./install.sh
cd ..
rm -rf nerd-fonts
echo -e "${GREEN}Nerd Font installed successfully.${NC}"
;;
*)
echo -e "${RED}Invalid choice. No font installed.${NC}"
;;
esac
}
# Check if Oh My Zsh themes directory exists
if [ ! -d "$THEMES_DIR" ]; then
echo -e "${RED}Error: Oh My Zsh themes directory not found at $THEMES_DIR${NC}"
exit 1
fi
# Parse command line arguments
case "$1" in
-l|--list)
list_themes
echo -n "Enter theme number or name to select (or type 'cancel' or 'q' to exit): "
read theme_input
if [ "$theme_input" = "cancel" ] || [ "$theme_input" = "q" ]; then
echo -e "${YELLOW}Theme selection canceled.${NC}"
exit 0
fi
# Convert number input to theme name if applicable
if [[ "$theme_input" =~ ^[0-9]+$ ]]; then
theme=$(ls "$THEMES_DIR"/*.zsh-theme | sed 's/\.zsh-theme//' | sed -n "${theme_input}p" | xargs basename)
else
theme="$theme_input"
fi
if [ -f "$THEMES_DIR/$theme.zsh-theme" ]; then
change_theme "$theme"
else
echo -e "${RED}Theme '$theme' not found. Please make sure you entered the correct theme name.${NC}"
exit 1
fi
exit 0
;;
-r|--random)
apply_random_theme
exit 0
;;
-p|--preview)
list_themes
echo -n "Enter theme number or name to preview: "
read theme_input
if [[ "$theme_input" =~ ^[0-9]+$ ]]; then
theme_name=$(ls "$THEMES_DIR"/*.zsh-theme | sed 's/\.zsh-theme//' | sed -n "${theme_input}p" | xargs basename)
else
theme_name="$theme_input"
fi
preview_theme "$theme_name"
exit 0
;;
-b|--backup)
backup_config
exit 0
;;
-f|--font)
install_font
exit 0
;;
-h|--help)
show_help
exit 0
;;
esac
# Interactive theme selection
echo -e "${BLUE}Current theme:${NC} $(get_current_theme)"
echo -e "${YELLOW}Select a theme from the list below (or type 'cancel' or 'q' to exit):${NC}"
list_themes
echo -n "Enter theme number or name: "
read theme_input
if [ "$theme_input" = "cancel" ] || [ "$theme_input" = "q" ]; then
echo -e "${YELLOW}Theme selection canceled.${NC}"
exit 0
fi
# Convert number input to theme name if applicable
if [[ "$theme_input" =~ ^[0-9]+$ ]]; then
theme=$(ls "$THEMES_DIR"/*.zsh-theme | sed 's/\.zsh-theme//' | sed -n "${theme_input}p" | xargs basename)
else
theme="$theme_input"
fi
if [ -f "$THEMES_DIR/$theme.zsh-theme" ]; then
change_theme "$theme"
else
echo -e "${RED}Theme '$theme' not found. Please make sure you entered the correct theme name.${NC}"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment