Dotfiles update 2025-12-25 12:04
This commit is contained in:
616
dotfiles-refactor/zsh/functions/python-templates.zsh
Normal file
616
dotfiles-refactor/zsh/functions/python-templates.zsh
Normal file
@@ -0,0 +1,616 @@
|
||||
# ============================================================================
|
||||
# Python Project Template Functions
|
||||
# ============================================================================
|
||||
# Template-driven project scaffolding for Python applications.
|
||||
# Eliminates code duplication by using a common creation function.
|
||||
# ============================================================================
|
||||
|
||||
# Source bootstrap (handles all dependencies)
|
||||
source "${0:A:h}/../lib/bootstrap.zsh" 2>/dev/null || \
|
||||
source "$HOME/.dotfiles/zsh/lib/bootstrap.zsh" 2>/dev/null
|
||||
|
||||
# ============================================================================
|
||||
# Configuration
|
||||
# ============================================================================
|
||||
|
||||
typeset -g PY_PYTHON="${PY_PYTHON:-python3}"
|
||||
typeset -g PY_VENV="${PY_VENV:-venv}"
|
||||
typeset -g PY_GIT_INIT="${PY_GIT_INIT:-true}"
|
||||
|
||||
# ============================================================================
|
||||
# Internal Helper Functions
|
||||
# ============================================================================
|
||||
|
||||
# Validate project name and ensure directory doesn't exist
|
||||
_py_check_name() {
|
||||
[[ -z "$1" ]] && { df_print_warning "Project name required"; return 1; }
|
||||
[[ -d "$1" ]] && { df_print_warning "Directory '$1' already exists"; return 1; }
|
||||
return 0
|
||||
}
|
||||
|
||||
# Create and activate virtual environment
|
||||
_py_venv() {
|
||||
local project_dir="$1"
|
||||
df_print_step "Creating virtual environment"
|
||||
"$PY_PYTHON" -m venv "$project_dir/$PY_VENV"
|
||||
df_print_success "Created: $PY_VENV"
|
||||
}
|
||||
|
||||
# Install packages into project's venv
|
||||
_py_install() {
|
||||
local project_dir="$1"
|
||||
shift
|
||||
local packages=("$@")
|
||||
|
||||
[[ ${#packages[@]} -eq 0 ]] && return 0
|
||||
|
||||
df_print_step "Installing: ${packages[*]}"
|
||||
"$project_dir/$PY_VENV/bin/pip" install "${packages[@]}" -q
|
||||
}
|
||||
|
||||
# Create standard .gitignore for Python projects
|
||||
_py_gitignore() {
|
||||
local project_dir="$1"
|
||||
cat > "$project_dir/.gitignore" << 'EOF'
|
||||
# Python
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
*.so
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
|
||||
# Virtual environments
|
||||
venv/
|
||||
.venv/
|
||||
ENV/
|
||||
env/
|
||||
|
||||
# IDE
|
||||
.idea/
|
||||
.vscode/
|
||||
*.swp
|
||||
*.swo
|
||||
|
||||
# Testing
|
||||
.pytest_cache/
|
||||
.coverage
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
|
||||
# Type checking
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
|
||||
# Environment
|
||||
.env
|
||||
.env.*
|
||||
*.log
|
||||
|
||||
# Distribution
|
||||
*.manifest
|
||||
*.spec
|
||||
EOF
|
||||
df_print_success "Created .gitignore"
|
||||
}
|
||||
|
||||
# Initialize git repository
|
||||
_py_git() {
|
||||
local project_dir="$1"
|
||||
[[ "$PY_GIT_INIT" != "true" ]] && return 0
|
||||
|
||||
(
|
||||
cd "$project_dir"
|
||||
git init -q
|
||||
git add .
|
||||
git commit -q -m "Initial commit"
|
||||
)
|
||||
df_print_success "Git initialized"
|
||||
}
|
||||
|
||||
# Print next steps for user
|
||||
_py_next_steps() {
|
||||
local project_dir="$1"
|
||||
local extra_info="$2"
|
||||
|
||||
echo ""
|
||||
df_print_section "Next steps"
|
||||
df_print_indent "cd $project_dir"
|
||||
df_print_indent "source $PY_VENV/bin/activate"
|
||||
[[ -n "$extra_info" ]] && df_print_indent "$extra_info"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Template Definitions
|
||||
# ============================================================================
|
||||
# Each template function creates type-specific files and returns packages to install
|
||||
|
||||
_py_template_basic() {
|
||||
local name="$1"
|
||||
|
||||
# Create directory structure
|
||||
mkdir -p "$name"/{src,tests}
|
||||
touch "$name/src/__init__.py" "$name/tests/__init__.py"
|
||||
|
||||
# Create main.py
|
||||
cat > "$name/src/main.py" << 'EOF'
|
||||
#!/usr/bin/env python3
|
||||
"""Main entry point."""
|
||||
|
||||
|
||||
def main():
|
||||
"""Main function."""
|
||||
print("Hello, World!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
EOF
|
||||
|
||||
# Create requirements.txt
|
||||
cat > "$name/requirements.txt" << 'EOF'
|
||||
# Project dependencies
|
||||
# Add your dependencies here
|
||||
EOF
|
||||
|
||||
# Return packages to install (none for basic)
|
||||
echo ""
|
||||
}
|
||||
|
||||
_py_template_flask() {
|
||||
local name="$1"
|
||||
|
||||
# Create directory structure
|
||||
mkdir -p "$name"/{app/{templates,static/css},tests}
|
||||
|
||||
# Create app/__init__.py
|
||||
cat > "$name/app/__init__.py" << 'EOF'
|
||||
"""Flask application factory."""
|
||||
from flask import Flask
|
||||
|
||||
|
||||
def create_app(config_name=None):
|
||||
"""Create and configure the Flask application."""
|
||||
app = Flask(__name__)
|
||||
|
||||
# Load configuration
|
||||
app.config.from_mapping(
|
||||
SECRET_KEY='dev',
|
||||
DEBUG=True,
|
||||
)
|
||||
|
||||
# Register blueprints
|
||||
from app.routes import main
|
||||
app.register_blueprint(main)
|
||||
|
||||
return app
|
||||
EOF
|
||||
|
||||
# Create app/routes.py
|
||||
cat > "$name/app/routes.py" << 'EOF'
|
||||
"""Main application routes."""
|
||||
from flask import Blueprint, render_template
|
||||
|
||||
main = Blueprint('main', __name__)
|
||||
|
||||
|
||||
@main.route('/')
|
||||
def index():
|
||||
"""Home page."""
|
||||
return render_template('index.html')
|
||||
|
||||
|
||||
@main.route('/health')
|
||||
def health():
|
||||
"""Health check endpoint."""
|
||||
return {'status': 'ok'}
|
||||
EOF
|
||||
|
||||
# Create template
|
||||
cat > "$name/app/templates/index.html" << 'EOF'
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Flask App</title>
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Welcome to Flask</h1>
|
||||
<p>Your application is running!</p>
|
||||
</body>
|
||||
</html>
|
||||
EOF
|
||||
|
||||
# Create basic CSS
|
||||
cat > "$name/app/static/css/style.css" << 'EOF'
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
padding: 2rem;
|
||||
}
|
||||
EOF
|
||||
|
||||
# Create run script
|
||||
cat > "$name/app.py" << 'EOF'
|
||||
#!/usr/bin/env python3
|
||||
"""Application entry point."""
|
||||
from app import create_app
|
||||
|
||||
app = create_app()
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True, host='0.0.0.0', port=5000)
|
||||
EOF
|
||||
|
||||
# Create requirements.txt
|
||||
cat > "$name/requirements.txt" << 'EOF'
|
||||
Flask>=3.0.0
|
||||
python-dotenv>=1.0.0
|
||||
EOF
|
||||
|
||||
# Return packages to install
|
||||
echo "flask python-dotenv"
|
||||
}
|
||||
|
||||
_py_template_fastapi() {
|
||||
local name="$1"
|
||||
|
||||
# Create directory structure
|
||||
mkdir -p "$name"/{app/{routers,models},tests}
|
||||
touch "$name/app/__init__.py" "$name/app/routers/__init__.py" "$name/app/models/__init__.py"
|
||||
|
||||
# Create app/main.py
|
||||
cat > "$name/app/main.py" << 'EOF'
|
||||
"""FastAPI application."""
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
app = FastAPI(
|
||||
title="FastAPI App",
|
||||
description="A FastAPI application",
|
||||
version="0.1.0",
|
||||
)
|
||||
|
||||
# Configure CORS
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
"""Root endpoint."""
|
||||
return {"message": "Hello, World!"}
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
async def health():
|
||||
"""Health check endpoint."""
|
||||
return {"status": "ok"}
|
||||
EOF
|
||||
|
||||
# Create run script
|
||||
cat > "$name/run.py" << 'EOF'
|
||||
#!/usr/bin/env python3
|
||||
"""Development server entry point."""
|
||||
import uvicorn
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(
|
||||
"app.main:app",
|
||||
host="0.0.0.0",
|
||||
port=8000,
|
||||
reload=True,
|
||||
)
|
||||
EOF
|
||||
|
||||
# Create requirements.txt
|
||||
cat > "$name/requirements.txt" << 'EOF'
|
||||
fastapi>=0.109.0
|
||||
uvicorn[standard]>=0.27.0
|
||||
python-dotenv>=1.0.0
|
||||
EOF
|
||||
|
||||
# Return packages to install
|
||||
echo "fastapi uvicorn python-dotenv"
|
||||
}
|
||||
|
||||
_py_template_cli() {
|
||||
local name="$1"
|
||||
local pkg_name="${name//-/_}" # Replace hyphens with underscores for Python
|
||||
|
||||
# Create directory structure
|
||||
mkdir -p "$name"/{src/"$pkg_name",tests}
|
||||
|
||||
# Create package __init__.py
|
||||
cat > "$name/src/$pkg_name/__init__.py" << EOF
|
||||
"""${name} - A command-line tool."""
|
||||
__version__ = "0.1.0"
|
||||
EOF
|
||||
|
||||
# Create CLI entry point
|
||||
cat > "$name/src/$pkg_name/cli.py" << 'EOF'
|
||||
#!/usr/bin/env python3
|
||||
"""Command-line interface."""
|
||||
import click
|
||||
|
||||
|
||||
@click.group()
|
||||
@click.version_option()
|
||||
def cli():
|
||||
"""A command-line tool."""
|
||||
pass
|
||||
|
||||
|
||||
@cli.command()
|
||||
@click.argument('name', default='World')
|
||||
def greet(name):
|
||||
"""Greet someone by name."""
|
||||
click.echo(f"Hello, {name}!")
|
||||
|
||||
|
||||
@cli.command()
|
||||
@click.option('--verbose', '-v', is_flag=True, help='Enable verbose output')
|
||||
def info(verbose):
|
||||
"""Show application information."""
|
||||
click.echo("CLI Application v0.1.0")
|
||||
if verbose:
|
||||
click.echo("Built with Click")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
cli()
|
||||
EOF
|
||||
|
||||
# Create pyproject.toml for modern packaging
|
||||
cat > "$name/pyproject.toml" << EOF
|
||||
[build-system]
|
||||
requires = ["setuptools>=61.0"]
|
||||
build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "$name"
|
||||
version = "0.1.0"
|
||||
description = "A command-line tool"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.8"
|
||||
dependencies = [
|
||||
"click>=8.1.0",
|
||||
]
|
||||
|
||||
[project.scripts]
|
||||
$name = "${pkg_name}.cli:cli"
|
||||
|
||||
[project.optional-dependencies]
|
||||
dev = [
|
||||
"pytest>=7.0",
|
||||
"pytest-cov",
|
||||
]
|
||||
EOF
|
||||
|
||||
# Create requirements.txt (for compatibility)
|
||||
cat > "$name/requirements.txt" << 'EOF'
|
||||
click>=8.1.0
|
||||
EOF
|
||||
|
||||
# Create README
|
||||
cat > "$name/README.md" << EOF
|
||||
# ${name}
|
||||
|
||||
A command-line tool.
|
||||
|
||||
## Installation
|
||||
|
||||
\`\`\`bash
|
||||
pip install -e .
|
||||
\`\`\`
|
||||
|
||||
## Usage
|
||||
|
||||
\`\`\`bash
|
||||
${name} --help
|
||||
${name} greet World
|
||||
\`\`\`
|
||||
EOF
|
||||
|
||||
# Return packages to install
|
||||
echo "click"
|
||||
}
|
||||
|
||||
_py_template_data() {
|
||||
local name="$1"
|
||||
|
||||
# Create directory structure
|
||||
mkdir -p "$name"/{notebooks,data/{raw,processed},src,tests}
|
||||
touch "$name/src/__init__.py"
|
||||
|
||||
# Create main analysis script
|
||||
cat > "$name/src/analysis.py" << 'EOF'
|
||||
#!/usr/bin/env python3
|
||||
"""Data analysis module."""
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
|
||||
|
||||
def load_data(filepath):
|
||||
"""Load data from CSV file."""
|
||||
return pd.read_csv(filepath)
|
||||
|
||||
|
||||
def basic_stats(df):
|
||||
"""Calculate basic statistics."""
|
||||
return df.describe()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Data Science Project")
|
||||
print(f"NumPy version: {np.__version__}")
|
||||
print(f"Pandas version: {pd.__version__}")
|
||||
EOF
|
||||
|
||||
# Create sample notebook
|
||||
cat > "$name/notebooks/01_exploration.ipynb" << 'EOF'
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": ["# Data Exploration\n", "\n", "Initial data exploration notebook."]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": ["import pandas as pd\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "\n", "%matplotlib inline"]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"},
|
||||
"language_info": {"name": "python", "version": "3.10.0"}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
||||
EOF
|
||||
|
||||
# Create requirements.txt
|
||||
cat > "$name/requirements.txt" << 'EOF'
|
||||
pandas>=2.0.0
|
||||
numpy>=1.24.0
|
||||
matplotlib>=3.7.0
|
||||
seaborn>=0.12.0
|
||||
jupyter>=1.0.0
|
||||
scikit-learn>=1.3.0
|
||||
EOF
|
||||
|
||||
# Create .gitkeep files
|
||||
touch "$name/data/raw/.gitkeep" "$name/data/processed/.gitkeep"
|
||||
|
||||
# Return packages to install
|
||||
echo "pandas numpy matplotlib seaborn jupyter scikit-learn"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Main Project Creation Function
|
||||
# ============================================================================
|
||||
|
||||
_py_create_project() {
|
||||
local name="$1"
|
||||
local template="$2"
|
||||
local display_name="$3"
|
||||
local extra_info="$4"
|
||||
|
||||
# Validate
|
||||
_py_check_name "$name" || return 1
|
||||
|
||||
# Print header
|
||||
df_print_func_name "${display_name}: ${name}"
|
||||
|
||||
# Create base directory
|
||||
mkdir -p "$name"
|
||||
|
||||
# Run template-specific setup and capture packages
|
||||
local packages
|
||||
packages=$("_py_template_${template}" "$name")
|
||||
|
||||
# Common setup steps
|
||||
_py_venv "$name"
|
||||
|
||||
# Install template-specific packages
|
||||
if [[ -n "$packages" ]]; then
|
||||
_py_install "$name" $packages
|
||||
fi
|
||||
|
||||
# Finalization
|
||||
_py_gitignore "$name"
|
||||
_py_git "$name"
|
||||
|
||||
df_print_success "Created: $name"
|
||||
_py_next_steps "$name" "$extra_info"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Public API Functions
|
||||
# ============================================================================
|
||||
|
||||
py-new() {
|
||||
_py_create_project "$1" "basic" "Python Project"
|
||||
}
|
||||
|
||||
py-flask() {
|
||||
_py_create_project "$1" "flask" "Flask Project" "Run: python app.py"
|
||||
}
|
||||
|
||||
py-fastapi() {
|
||||
_py_create_project "$1" "fastapi" "FastAPI Project" "Run: python run.py | Docs: http://localhost:8000/docs"
|
||||
}
|
||||
|
||||
py-cli() {
|
||||
_py_create_project "$1" "cli" "CLI Project" "Install: pip install -e ."
|
||||
}
|
||||
|
||||
py-data() {
|
||||
_py_create_project "$1" "data" "Data Science Project" "Start Jupyter: jupyter notebook"
|
||||
}
|
||||
|
||||
# Quick venv activation helper
|
||||
venv() {
|
||||
local venv_dirs=("venv" ".venv" "env" ".env")
|
||||
for dir in "${venv_dirs[@]}"; do
|
||||
if [[ -f "$dir/bin/activate" ]]; then
|
||||
source "$dir/bin/activate"
|
||||
df_print_success "Activated: $dir"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
df_print_error "No virtual environment found"
|
||||
df_print_info "Create one with: python -m venv venv"
|
||||
return 1
|
||||
}
|
||||
|
||||
# List available templates
|
||||
py-templates() {
|
||||
df_print_func_name "Python Project Templates"
|
||||
echo ""
|
||||
df_print_indent "py-new <name> Basic Python project"
|
||||
df_print_indent "py-flask <name> Flask web application"
|
||||
df_print_indent "py-fastapi <name> FastAPI REST API"
|
||||
df_print_indent "py-cli <name> CLI tool with Click"
|
||||
df_print_indent "py-data <name> Data science project"
|
||||
echo ""
|
||||
df_print_info "Example: py-flask mywebapp"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Aliases
|
||||
# ============================================================================
|
||||
|
||||
alias pynew='py-new'
|
||||
alias pyflask='py-flask'
|
||||
alias pyfast='py-fastapi'
|
||||
alias pycli='py-cli'
|
||||
alias pydata='py-data'
|
||||
alias pytemplates='py-templates'
|
||||
135
dotfiles-refactor/zsh/lib/bootstrap.zsh
Normal file
135
dotfiles-refactor/zsh/lib/bootstrap.zsh
Normal file
@@ -0,0 +1,135 @@
|
||||
# ============================================================================
|
||||
# Dotfiles Bootstrap - Single Entry Point
|
||||
# ============================================================================
|
||||
# This is the ONE file to source in all scripts and functions.
|
||||
# It handles loading config, colors, and utils in the correct order with
|
||||
# proper fallbacks.
|
||||
#
|
||||
# Usage in zsh functions:
|
||||
# source "${0:A:h}/../lib/bootstrap.zsh"
|
||||
#
|
||||
# Usage in bash scripts:
|
||||
# source "${DOTFILES_HOME:-$HOME/.dotfiles}/zsh/lib/bootstrap.zsh"
|
||||
#
|
||||
# After sourcing, you have access to:
|
||||
# - All DF_* color variables
|
||||
# - All df_print_* functions
|
||||
# - All df_* utility functions
|
||||
# - All config variables from dotfiles.conf
|
||||
# ============================================================================
|
||||
|
||||
# Prevent double-sourcing (works in both bash and zsh)
|
||||
[[ -n "$_DF_BOOTSTRAP_LOADED" ]] && return 0
|
||||
|
||||
# ============================================================================
|
||||
# Determine Dotfiles Root
|
||||
# ============================================================================
|
||||
|
||||
_df_find_root() {
|
||||
# Check common locations in order of preference
|
||||
local locations=(
|
||||
"${DOTFILES_DIR}"
|
||||
"${DOTFILES_HOME}"
|
||||
"$HOME/.dotfiles"
|
||||
)
|
||||
|
||||
for loc in "${locations[@]}"; do
|
||||
[[ -n "$loc" && -d "$loc" && -f "$loc/dotfiles.conf" ]] && {
|
||||
echo "$loc"
|
||||
return 0
|
||||
}
|
||||
done
|
||||
|
||||
# Fallback: try to find from script location (zsh)
|
||||
if [[ -n "$ZSH_VERSION" ]]; then
|
||||
local script_dir="${0:A:h}"
|
||||
# Walk up looking for dotfiles.conf
|
||||
while [[ "$script_dir" != "/" ]]; do
|
||||
[[ -f "$script_dir/dotfiles.conf" ]] && { echo "$script_dir"; return 0; }
|
||||
script_dir="${script_dir:h}"
|
||||
done
|
||||
fi
|
||||
|
||||
# Last resort
|
||||
echo "$HOME/.dotfiles"
|
||||
}
|
||||
|
||||
# Set the root directory
|
||||
typeset -g _DF_ROOT="$(_df_find_root)"
|
||||
typeset -g DOTFILES_DIR="$_DF_ROOT"
|
||||
typeset -g DOTFILES_HOME="$_DF_ROOT"
|
||||
|
||||
# ============================================================================
|
||||
# Source Core Files (in correct order)
|
||||
# ============================================================================
|
||||
|
||||
# 1. Config first (sets DF_WIDTH, MOTD_STYLE, etc.)
|
||||
if [[ -f "$_DF_ROOT/zsh/lib/config.zsh" ]]; then
|
||||
source "$_DF_ROOT/zsh/lib/config.zsh"
|
||||
else
|
||||
# Minimal fallback config
|
||||
typeset -g DOTFILES_VERSION="${DOTFILES_VERSION:-1.0.0}"
|
||||
typeset -g DF_WIDTH="${DF_WIDTH:-66}"
|
||||
typeset -g MOTD_STYLE="${MOTD_STYLE:-compact}"
|
||||
typeset -g DOTFILES_BRANCH="${DOTFILES_BRANCH:-main}"
|
||||
fi
|
||||
|
||||
# 2. Colors second
|
||||
if [[ -f "$_DF_ROOT/zsh/lib/colors.zsh" ]]; then
|
||||
source "$_DF_ROOT/zsh/lib/colors.zsh"
|
||||
else
|
||||
# Minimal fallback colors
|
||||
typeset -g DF_RED=$'\033[0;31m'
|
||||
typeset -g DF_GREEN=$'\033[0;32m'
|
||||
typeset -g DF_YELLOW=$'\033[1;33m'
|
||||
typeset -g DF_BLUE=$'\033[0;34m'
|
||||
typeset -g DF_CYAN=$'\033[0;36m'
|
||||
typeset -g DF_NC=$'\033[0m'
|
||||
typeset -g DF_GREY=$'\033[38;5;242m'
|
||||
typeset -g DF_LIGHT_BLUE=$'\033[38;5;39m'
|
||||
typeset -g DF_LIGHT_GREEN=$'\033[38;5;82m'
|
||||
typeset -g DF_BOLD=$'\033[1m'
|
||||
typeset -g DF_DIM=$'\033[2m'
|
||||
fi
|
||||
|
||||
# 3. Utils last (depends on config and colors)
|
||||
if [[ -f "$_DF_ROOT/zsh/lib/utils.zsh" ]]; then
|
||||
source "$_DF_ROOT/zsh/lib/utils.zsh"
|
||||
fi
|
||||
|
||||
# ============================================================================
|
||||
# Ensure Critical Functions Exist
|
||||
# ============================================================================
|
||||
# If utils.zsh failed to load, provide minimal implementations
|
||||
|
||||
if ! declare -f df_print_header &>/dev/null; then
|
||||
df_print_header() {
|
||||
local name="${1:-script}"
|
||||
echo ""
|
||||
echo "=== ${name} ==="
|
||||
echo ""
|
||||
}
|
||||
fi
|
||||
|
||||
if ! declare -f df_print_func_name &>/dev/null; then
|
||||
df_print_func_name() {
|
||||
echo "--- ${1:-function} ---"
|
||||
}
|
||||
fi
|
||||
|
||||
if ! declare -f df_print_success &>/dev/null; then
|
||||
df_print_success() { echo "✓ $1"; }
|
||||
df_print_error() { echo "✗ $1" >&2; }
|
||||
df_print_warning() { echo "⚠ $1"; }
|
||||
df_print_info() { echo "ℹ $1"; }
|
||||
df_print_step() { echo "==> $1"; }
|
||||
fi
|
||||
|
||||
# ============================================================================
|
||||
# Mark as Loaded
|
||||
# ============================================================================
|
||||
|
||||
typeset -g _DF_BOOTSTRAP_LOADED=1
|
||||
|
||||
# Export for subshells (bash compatibility)
|
||||
export DOTFILES_DIR DOTFILES_HOME DOTFILES_VERSION DF_WIDTH
|
||||
86
dotfiles-refactor/zsh/lib/colors.zsh
Normal file
86
dotfiles-refactor/zsh/lib/colors.zsh
Normal file
@@ -0,0 +1,86 @@
|
||||
# ============================================================================
|
||||
# Shared Color Definitions for Dotfiles
|
||||
# ============================================================================
|
||||
# Source this file in scripts and functions to get consistent color support.
|
||||
#
|
||||
# Usage in zsh functions:
|
||||
# source "${0:A:h}/../lib/colors.zsh"
|
||||
#
|
||||
# Usage in bash scripts:
|
||||
# source "$HOME/.dotfiles/zsh/lib/colors.zsh"
|
||||
#
|
||||
# All variables are prefixed with DF_ (dotfiles) to avoid conflicts.
|
||||
# ============================================================================
|
||||
|
||||
# Prevent double-sourcing
|
||||
[[ -n "$_DF_COLORS_LOADED" ]] && return 0
|
||||
typeset -g _DF_COLORS_LOADED=1
|
||||
|
||||
# ============================================================================
|
||||
# Standard Colors (ANSI escape codes)
|
||||
# ============================================================================
|
||||
|
||||
typeset -g DF_RED=$'\033[0;31m'
|
||||
typeset -g DF_GREEN=$'\033[0;32m'
|
||||
typeset -g DF_YELLOW=$'\033[1;33m'
|
||||
typeset -g DF_BLUE=$'\033[0;34m'
|
||||
typeset -g DF_MAGENTA=$'\033[0;35m'
|
||||
typeset -g DF_CYAN=$'\033[0;36m'
|
||||
typeset -g DF_WHITE=$'\033[0;37m'
|
||||
|
||||
# Bold variants
|
||||
typeset -g DF_BOLD_RED=$'\033[1;31m'
|
||||
typeset -g DF_BOLD_GREEN=$'\033[1;32m'
|
||||
typeset -g DF_BOLD_YELLOW=$'\033[1;33m'
|
||||
typeset -g DF_BOLD_BLUE=$'\033[1;34m'
|
||||
typeset -g DF_BOLD_MAGENTA=$'\033[1;35m'
|
||||
typeset -g DF_BOLD_CYAN=$'\033[1;36m'
|
||||
typeset -g DF_BOLD_WHITE=$'\033[1;37m'
|
||||
|
||||
# Text styles
|
||||
typeset -g DF_BOLD=$'\033[1m'
|
||||
typeset -g DF_DIM=$'\033[2m'
|
||||
typeset -g DF_ITALIC=$'\033[3m'
|
||||
typeset -g DF_UNDERLINE=$'\033[4m'
|
||||
typeset -g DF_RESET=$'\033[0m'
|
||||
typeset -g DF_NC=$'\033[0m' # Alias for reset (No Color)
|
||||
|
||||
# ============================================================================
|
||||
# 256-Color Palette (used in theme and MOTD)
|
||||
# ============================================================================
|
||||
|
||||
typeset -g DF_GREY=$'\033[38;5;242m'
|
||||
typeset -g DF_LIGHT_GREY=$'\033[38;5;248m'
|
||||
typeset -g DF_DARK_GREY=$'\033[38;5;239m'
|
||||
typeset -g DF_ORANGE=$'\033[38;5;208m'
|
||||
typeset -g DF_LIGHT_ORANGE=$'\033[38;5;220m'
|
||||
typeset -g DF_PINK=$'\033[38;5;213m'
|
||||
typeset -g DF_PURPLE=$'\033[38;5;141m'
|
||||
typeset -g DF_LIGHT_BLUE=$'\033[38;5;39m'
|
||||
typeset -g DF_LIGHT_GREEN=$'\033[38;5;82m'
|
||||
typeset -g DF_BRIGHT_GREEN=$'\033[38;5;118m'
|
||||
typeset -g DF_TEAL=$'\033[38;5;51m'
|
||||
|
||||
# ============================================================================
|
||||
# Semantic Colors (for consistent UI)
|
||||
# ============================================================================
|
||||
|
||||
typeset -g DF_SUCCESS="$DF_GREEN"
|
||||
typeset -g DF_ERROR="$DF_RED"
|
||||
typeset -g DF_WARNING="$DF_YELLOW"
|
||||
typeset -g DF_INFO="$DF_CYAN"
|
||||
typeset -g DF_HINT="$DF_DIM"
|
||||
typeset -g DF_ACCENT="$DF_BLUE"
|
||||
typeset -g DF_MUTED="$DF_GREY"
|
||||
|
||||
# ============================================================================
|
||||
# Bash Compatibility
|
||||
# ============================================================================
|
||||
|
||||
# For bash scripts, export as regular variables too
|
||||
if [[ -n "$BASH_VERSION" ]]; then
|
||||
export DF_RED DF_GREEN DF_YELLOW DF_BLUE DF_MAGENTA DF_CYAN DF_WHITE
|
||||
export DF_BOLD DF_DIM DF_RESET DF_NC
|
||||
export DF_GREY DF_LIGHT_BLUE DF_LIGHT_GREEN
|
||||
export DF_SUCCESS DF_ERROR DF_WARNING DF_INFO
|
||||
fi
|
||||
154
dotfiles-refactor/zsh/lib/config.zsh
Normal file
154
dotfiles-refactor/zsh/lib/config.zsh
Normal file
@@ -0,0 +1,154 @@
|
||||
# ============================================================================
|
||||
# Dotfiles Configuration Loader
|
||||
# ============================================================================
|
||||
# This file loads dotfiles.conf and sets up all configuration variables.
|
||||
# It serves as the bridge between dotfiles.conf and the rest of the system.
|
||||
#
|
||||
# Source this file to get access to all configuration:
|
||||
# source "${0:A:h}/config.zsh"
|
||||
#
|
||||
# This file:
|
||||
# 1. Finds and sources dotfiles.conf
|
||||
# 2. Sets sensible defaults for any missing values
|
||||
# 3. Exports variables for use in subshells/scripts
|
||||
# ============================================================================
|
||||
|
||||
# Prevent double-sourcing
|
||||
[[ -n "$_DF_CONFIG_LOADED" ]] && return 0
|
||||
|
||||
# ============================================================================
|
||||
# Find and Source dotfiles.conf
|
||||
# ============================================================================
|
||||
|
||||
_df_find_config() {
|
||||
local locations=(
|
||||
"${DOTFILES_DIR}/dotfiles.conf"
|
||||
"${DOTFILES_HOME}/dotfiles.conf"
|
||||
"$HOME/.dotfiles/dotfiles.conf"
|
||||
"${0:A:h}/../../dotfiles.conf"
|
||||
)
|
||||
|
||||
for loc in "${locations[@]}"; do
|
||||
[[ -f "$loc" ]] && { echo "$loc"; return 0; }
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
_DF_CONFIG_FILE=$(_df_find_config)
|
||||
|
||||
if [[ -n "$_DF_CONFIG_FILE" && -f "$_DF_CONFIG_FILE" ]]; then
|
||||
source "$_DF_CONFIG_FILE"
|
||||
typeset -g _DF_CONFIG_LOADED=1
|
||||
else
|
||||
# Config file not found - set critical defaults
|
||||
typeset -g _DF_CONFIG_LOADED=1
|
||||
fi
|
||||
|
||||
# ============================================================================
|
||||
# Set Defaults for Any Missing Values
|
||||
# ============================================================================
|
||||
# These defaults ensure scripts work even if dotfiles.conf is incomplete
|
||||
|
||||
# Core Settings
|
||||
typeset -g DOTFILES_VERSION="${DOTFILES_VERSION:-1.0.0}"
|
||||
typeset -g DOTFILES_DIR="${DOTFILES_DIR:-$HOME/.dotfiles}"
|
||||
typeset -g DOTFILES_HOME="${DOTFILES_HOME:-$DOTFILES_DIR}" # Alias for compatibility
|
||||
typeset -g DOTFILES_BRANCH="${DOTFILES_BRANCH:-main}"
|
||||
typeset -g DOTFILES_BACKUP_PREFIX="${DOTFILES_BACKUP_PREFIX:-$HOME/.dotfiles_backup}"
|
||||
|
||||
# GitHub Settings
|
||||
typeset -g DOTFILES_GITHUB_USER="${DOTFILES_GITHUB_USER:-adlee-was-taken}"
|
||||
typeset -g DOTFILES_REPO_NAME="${DOTFILES_REPO_NAME:-dotfiles}"
|
||||
typeset -g DOTFILES_REPO_URL="${DOTFILES_REPO_URL:-https://github.com/${DOTFILES_GITHUB_USER}/${DOTFILES_REPO_NAME}.git}"
|
||||
typeset -g DOTFILES_RAW_URL="${DOTFILES_RAW_URL:-https://raw.githubusercontent.com/${DOTFILES_GITHUB_USER}/${DOTFILES_REPO_NAME}/${DOTFILES_BRANCH}}"
|
||||
|
||||
# Display Settings
|
||||
typeset -g DF_WIDTH="${DF_WIDTH:-66}"
|
||||
typeset -g ENABLE_MOTD="${ENABLE_MOTD:-true}"
|
||||
typeset -g MOTD_STYLE="${MOTD_STYLE:-compact}"
|
||||
typeset -g MOTD_SHOW_FAILED_SERVICES="${MOTD_SHOW_FAILED_SERVICES:-true}"
|
||||
typeset -g MOTD_SHOW_UPDATES="${MOTD_SHOW_UPDATES:-true}"
|
||||
|
||||
# Theme Settings
|
||||
typeset -g ZSH_THEME_NAME="${ZSH_THEME_NAME:-adlee}"
|
||||
typeset -g THEME_TIMER_THRESHOLD="${THEME_TIMER_THRESHOLD:-10}"
|
||||
typeset -g THEME_PATH_TRUNCATE_LENGTH="${THEME_PATH_TRUNCATE_LENGTH:-32}"
|
||||
|
||||
# Feature Toggles
|
||||
typeset -g ENABLE_SMART_SUGGESTIONS="${ENABLE_SMART_SUGGESTIONS:-true}"
|
||||
typeset -g ENABLE_COMMAND_PALETTE="${ENABLE_COMMAND_PALETTE:-true}"
|
||||
typeset -g ENABLE_SHELL_ANALYTICS="${ENABLE_SHELL_ANALYTICS:-false}"
|
||||
typeset -g ENABLE_VAULT="${ENABLE_VAULT:-true}"
|
||||
typeset -g DOTFILES_AUTO_SYNC_CHECK="${DOTFILES_AUTO_SYNC_CHECK:-true}"
|
||||
|
||||
# Btrfs Settings
|
||||
typeset -g BTRFS_DEFAULT_MOUNT="${BTRFS_DEFAULT_MOUNT:-/}"
|
||||
|
||||
# Snapper Settings
|
||||
typeset -g SNAPPER_CONFIG="${SNAPPER_CONFIG:-root}"
|
||||
typeset -g LIMINE_CONF="${LIMINE_CONF:-/boot/limine.conf}"
|
||||
|
||||
# Tmux Settings
|
||||
typeset -g TW_SESSION_PREFIX="${TW_SESSION_PREFIX:-work}"
|
||||
typeset -g TW_DEFAULT_TEMPLATE="${TW_DEFAULT_TEMPLATE:-dev}"
|
||||
|
||||
# Python Template Settings
|
||||
typeset -g PY_TEMPLATE_BASE_DIR="${PY_TEMPLATE_BASE_DIR:-$HOME/projects}"
|
||||
typeset -g PY_TEMPLATE_PYTHON="${PY_TEMPLATE_PYTHON:-python3}"
|
||||
typeset -g PY_TEMPLATE_VENV_NAME="${PY_TEMPLATE_VENV_NAME:-venv}"
|
||||
typeset -g PY_TEMPLATE_USE_POETRY="${PY_TEMPLATE_USE_POETRY:-false}"
|
||||
typeset -g PY_TEMPLATE_GIT_INIT="${PY_TEMPLATE_GIT_INIT:-true}"
|
||||
|
||||
# SSH Settings
|
||||
typeset -g SSH_AUTO_TMUX="${SSH_AUTO_TMUX:-true}"
|
||||
typeset -g SSH_TMUX_SESSION_PREFIX="${SSH_TMUX_SESSION_PREFIX:-ssh}"
|
||||
typeset -g SSH_SYNC_DOTFILES="${SSH_SYNC_DOTFILES:-ask}"
|
||||
|
||||
# Password Manager Settings
|
||||
typeset -g PW_CLIP_TIME="${PW_CLIP_TIME:-45}"
|
||||
|
||||
# Package Manager
|
||||
typeset -g AUR_HELPER="${AUR_HELPER:-auto}"
|
||||
|
||||
# Git Settings (with fallbacks to user identity)
|
||||
typeset -g GIT_USER_NAME="${GIT_USER_NAME:-$USER_FULLNAME}"
|
||||
typeset -g GIT_USER_EMAIL="${GIT_USER_EMAIL:-$USER_EMAIL}"
|
||||
typeset -g GIT_DEFAULT_BRANCH="${GIT_DEFAULT_BRANCH:-main}"
|
||||
|
||||
# ============================================================================
|
||||
# Export for Bash Scripts
|
||||
# ============================================================================
|
||||
# Bash scripts can't see typeset -g, so we export key variables
|
||||
|
||||
export DOTFILES_VERSION DOTFILES_DIR DOTFILES_HOME DOTFILES_BRANCH
|
||||
export DOTFILES_GITHUB_USER DOTFILES_REPO_NAME DOTFILES_REPO_URL DOTFILES_RAW_URL
|
||||
export DF_WIDTH MOTD_STYLE
|
||||
export ZSH_THEME_NAME
|
||||
|
||||
# ============================================================================
|
||||
# Helper Function: Get Config Value
|
||||
# ============================================================================
|
||||
# Usage: df_config "VARIABLE_NAME" "default_value"
|
||||
|
||||
df_config() {
|
||||
local var_name="$1"
|
||||
local default="$2"
|
||||
local value="${(P)var_name}"
|
||||
echo "${value:-$default}"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Helper Function: Show Config Summary
|
||||
# ============================================================================
|
||||
|
||||
df_show_config() {
|
||||
echo "Dotfiles Configuration"
|
||||
echo "======================"
|
||||
echo "Config File: ${_DF_CONFIG_FILE:-not found}"
|
||||
echo "Version: $DOTFILES_VERSION"
|
||||
echo "Directory: $DOTFILES_DIR"
|
||||
echo "Branch: $DOTFILES_BRANCH"
|
||||
echo "Display Width: $DF_WIDTH"
|
||||
echo "MOTD Style: $MOTD_STYLE"
|
||||
echo "Theme: $ZSH_THEME_NAME"
|
||||
}
|
||||
215
dotfiles-refactor/zsh/lib/utils.zsh
Normal file
215
dotfiles-refactor/zsh/lib/utils.zsh
Normal file
@@ -0,0 +1,215 @@
|
||||
# ============================================================================
|
||||
# Shared Utility Functions for Zsh Dotfiles
|
||||
# ============================================================================
|
||||
# Common helper functions used across multiple function files.
|
||||
#
|
||||
# This file is typically sourced via bootstrap.zsh, which handles loading
|
||||
# config.zsh and colors.zsh first.
|
||||
#
|
||||
# Direct usage (if needed):
|
||||
# source "${0:A:h}/../lib/utils.zsh"
|
||||
# ============================================================================
|
||||
|
||||
# Prevent double-sourcing
|
||||
[[ -n "$_DF_UTILS_LOADED" ]] && return 0
|
||||
typeset -g _DF_UTILS_LOADED=1
|
||||
|
||||
# ============================================================================
|
||||
# Source Dependencies (if not already loaded via bootstrap)
|
||||
# ============================================================================
|
||||
|
||||
_df_lib_dir="${0:A:h}"
|
||||
[[ ! -d "$_df_lib_dir" ]] && _df_lib_dir="$HOME/.dotfiles/zsh/lib"
|
||||
|
||||
# Source config if not already loaded
|
||||
[[ -z "$_DF_CONFIG_LOADED" ]] && {
|
||||
source "${_df_lib_dir}/config.zsh" 2>/dev/null || \
|
||||
source "$HOME/.dotfiles/zsh/lib/config.zsh" 2>/dev/null || {
|
||||
typeset -g DOTFILES_DIR="${DOTFILES_DIR:-$HOME/.dotfiles}"
|
||||
typeset -g DOTFILES_HOME="${DOTFILES_HOME:-$DOTFILES_DIR}"
|
||||
typeset -g DOTFILES_VERSION="${DOTFILES_VERSION:-unknown}"
|
||||
typeset -g DF_WIDTH="${DF_WIDTH:-66}"
|
||||
}
|
||||
}
|
||||
|
||||
# Source colors if not already loaded
|
||||
[[ -z "$_DF_COLORS_LOADED" ]] && {
|
||||
source "${_df_lib_dir}/colors.zsh" 2>/dev/null || \
|
||||
source "$HOME/.dotfiles/zsh/lib/colors.zsh" 2>/dev/null || {
|
||||
typeset -g DF_RED=$'\033[0;31m' DF_GREEN=$'\033[0;32m' DF_YELLOW=$'\033[1;33m'
|
||||
typeset -g DF_BLUE=$'\033[0;34m' DF_CYAN=$'\033[0;36m' DF_NC=$'\033[0m'
|
||||
typeset -g DF_GREY=$'\033[38;5;242m' DF_LIGHT_BLUE=$'\033[38;5;39m'
|
||||
typeset -g DF_LIGHT_GREEN=$'\033[38;5;82m' DF_BOLD=$'\033[1m' DF_DIM=$'\033[2m'
|
||||
}
|
||||
}
|
||||
|
||||
unset _df_lib_dir
|
||||
|
||||
# ============================================================================
|
||||
# Header Box Drawing (Centralized Implementation)
|
||||
# ============================================================================
|
||||
# These functions eliminate header duplication across all scripts.
|
||||
|
||||
# Build a horizontal line of specified character and width
|
||||
# Usage: _df_hline "═" 66
|
||||
_df_hline() {
|
||||
local char="${1:-═}"
|
||||
local width="${2:-$DF_WIDTH}"
|
||||
local line=""
|
||||
for ((i=0; i<width; i++)); do line+="$char"; done
|
||||
echo "$line"
|
||||
}
|
||||
|
||||
# Print a MOTD-style header box for scripts
|
||||
# Usage: df_print_header "script-name"
|
||||
df_print_header() {
|
||||
local script_name="${1:-script}"
|
||||
local user="${USER:-root}"
|
||||
local hostname="${HOST:-${HOSTNAME:-$(hostname -s 2>/dev/null)}}"
|
||||
local datetime=$(date '+%a %b %d %H:%M')
|
||||
local width="${DF_WIDTH:-66}"
|
||||
|
||||
# Build horizontal line
|
||||
local hline=$(_df_hline "═" "$width")
|
||||
local inner=$((width - 2))
|
||||
|
||||
# Header content
|
||||
local h_left="✦ ${user}@${hostname}"
|
||||
local h_center="${script_name}"
|
||||
local h_right="${datetime}"
|
||||
|
||||
# Calculate padding (distribute space evenly)
|
||||
local content_len=$((${#h_left} + ${#h_center} + ${#h_right}))
|
||||
local total_padding=$((inner - content_len))
|
||||
local left_pad=$((total_padding / 2))
|
||||
local right_pad=$((total_padding - left_pad))
|
||||
|
||||
# Build padding strings
|
||||
local left_spaces="" right_spaces=""
|
||||
for ((i=0; i<left_pad; i++)); do left_spaces+=" "; done
|
||||
for ((i=0; i<right_pad; i++)); do right_spaces+=" "; done
|
||||
|
||||
# Use red for root, light blue for normal users
|
||||
local user_color="${DF_LIGHT_BLUE}"
|
||||
[[ "${EUID:-$(id -u)}" -eq 0 ]] && user_color="${DF_RED}"
|
||||
|
||||
echo ""
|
||||
echo -e "${DF_GREY}╒${hline}╕${DF_NC}"
|
||||
echo -e "${DF_GREY}│${DF_NC} ${DF_BOLD}${user_color}${h_left}${DF_NC}${left_spaces}${DF_LIGHT_GREEN}${h_center}${right_spaces}${DF_NC}${DF_BOLD}${h_right}${DF_NC} ${DF_GREY}│${DF_NC}"
|
||||
echo -e "${DF_GREY}╘${hline}╛${DF_NC}"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Print a header box for functions (simpler, no user@host)
|
||||
# Usage: df_print_func_name "Function Name"
|
||||
df_print_func_name() {
|
||||
local func_name="${1:-func}"
|
||||
local datetime=$(date '+%a %b %d %H:%M')
|
||||
local width="${DF_WIDTH:-66}"
|
||||
|
||||
# Build horizontal line
|
||||
local hline=$(_df_hline "═" "$width")
|
||||
local inner=$((width - 2))
|
||||
|
||||
# Header content (function name on left, datetime on right)
|
||||
local h_left="${func_name}"
|
||||
local h_right="${datetime}"
|
||||
local h_pad=$((inner - ${#h_left} - ${#h_right}))
|
||||
|
||||
local h_spaces=""
|
||||
for ((i=0; i<h_pad; i++)); do h_spaces+=" "; done
|
||||
|
||||
echo -e "${DF_GREY}╒${hline}╕${DF_NC}"
|
||||
echo -e "${DF_GREY}│${DF_NC} ${DF_BOLD}${DF_LIGHT_BLUE}${h_left}${DF_NC}${h_spaces}${DF_BOLD}${h_right}${DF_NC} ${DF_GREY}│${DF_NC}"
|
||||
echo -e "${DF_GREY}╘${hline}╛${DF_NC}"
|
||||
}
|
||||
|
||||
# Print a simple section divider line
|
||||
# Usage: df_print_divider
|
||||
df_print_divider() {
|
||||
local width="${DF_WIDTH:-66}"
|
||||
local line=$(_df_hline "─" "$width")
|
||||
echo -e "${DF_CYAN}${line}${DF_NC}"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Output Formatting Functions
|
||||
# ============================================================================
|
||||
|
||||
df_print_step() { echo -e "${DF_BLUE}==>${DF_NC} $1"; }
|
||||
df_print_success() { echo -e "${DF_GREEN}✓${DF_NC} $1"; }
|
||||
df_print_error() { echo -e "${DF_RED}✗${DF_NC} $1" >&2; }
|
||||
df_print_warning() { echo -e "${DF_YELLOW}⚠${DF_NC} $1"; }
|
||||
df_print_info() { echo -e "${DF_CYAN}ℹ${DF_NC} $1"; }
|
||||
df_print_section() { echo -e "${DF_CYAN}$1:${DF_NC}"; }
|
||||
df_print_indent() { echo " $1"; }
|
||||
|
||||
# ============================================================================
|
||||
# Command Dependency Checking
|
||||
# ============================================================================
|
||||
|
||||
df_cmd_exists() { command -v "$1" &>/dev/null; }
|
||||
|
||||
df_require_cmd() {
|
||||
local cmd="$1"
|
||||
local package="${2:-$1}"
|
||||
|
||||
if ! command -v "$cmd" &>/dev/null; then
|
||||
df_print_error "$cmd not installed"
|
||||
echo "Install: sudo pacman -S $package"
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# User Confirmation
|
||||
# ============================================================================
|
||||
|
||||
df_confirm() {
|
||||
local prompt="$1"
|
||||
local response
|
||||
|
||||
if [[ -n "$ZSH_VERSION" ]]; then
|
||||
read -q "response?$prompt [y/N]: "
|
||||
echo
|
||||
[[ "$response" =~ ^[Yy]$ ]]
|
||||
else
|
||||
read -p "$prompt [y/N]: " response
|
||||
[[ "$response" =~ ^[Yy]$ ]]
|
||||
fi
|
||||
}
|
||||
|
||||
df_confirm_warning() {
|
||||
df_print_warning "$1"
|
||||
df_confirm "Continue?"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# File/Directory Helpers
|
||||
# ============================================================================
|
||||
|
||||
df_in_git_repo() { git rev-parse --git-dir &>/dev/null 2>&1; }
|
||||
df_git_root() { git rev-parse --show-toplevel 2>/dev/null; }
|
||||
df_ensure_dir() { [[ ! -d "$1" ]] && mkdir -p "$1"; }
|
||||
|
||||
df_ensure_file() {
|
||||
local file="$1" content="${2:-}"
|
||||
if [[ ! -f "$file" ]]; then
|
||||
df_ensure_dir "$(dirname "$file")"
|
||||
[[ -n "$content" ]] && echo "$content" > "$file" || touch "$file"
|
||||
fi
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Environment Checks
|
||||
# ============================================================================
|
||||
|
||||
df_in_tmux() { [[ -n "$TMUX" ]]; }
|
||||
df_is_btrfs() { [[ "$(df -T / 2>/dev/null | awk 'NR==2 {print $2}')" == "btrfs" ]]; }
|
||||
|
||||
# ============================================================================
|
||||
# FZF Helpers
|
||||
# ============================================================================
|
||||
|
||||
df_fzf_opts() { echo "--height=50% --layout=reverse --border=rounded"; }
|
||||
Reference in New Issue
Block a user