#!/bin/bash # This file contains helper functions for bash scripts. # Source it in your bash script to use the functions. #------------------------------------- # Docker-related functions # Attach to a running Docker container. attach(){ docker exec -it "$1" bash } # Enter development shell in a Docker container using a Python image. # Flags: --new/-n remove existing container first. python-dev() { local IMG="python-dev" local NAME="python-dev" while [[ $# -gt 0 ]]; do case "$1" in --help|-h) echo "Usage: python-dev [--new|-n]"; echo " --new, -n Remove existing container before starting"; return 0 ;; --new|-n) docker rm -f "$NAME" &>/dev/null; shift ;; *) echo "Unknown option: $1"; return 1 ;; esac done local built built="$(docker image inspect --format '{{.Created}}' "$IMG" 2>/dev/null)" [[ -n "$built" ]] && echo "Image built: $(date -d "$built" '+%Y-%m-%d %H:%M:%S')" local PROJECT_NAME PROJECT_NAME="$(basename "$(pwd)")" if docker container inspect "$NAME" &>/dev/null; then local STATE STATE="$(docker inspect -f '{{.State.Status}}' "$NAME")" if [[ "$STATE" == "running" ]]; then echo "Attaching to existing container '$NAME'" docker exec -it -e PROJECT_NAME="$PROJECT_NAME" "$NAME" bash else echo "Restarting stopped container '$NAME'" docker start -ai "$NAME" fi return $? fi echo "Creating new container '$NAME'" docker run -it \ --name="$NAME" \ --network=host \ -e PROJECT_NAME="$PROJECT_NAME" \ -v "$(pwd):/workspace" \ -v /workspace/.venv \ -v "$HOME/.ssh:/home/dev/.ssh:ro" \ -v "$HOME/.gitconfig:/home/dev/.gitconfig:ro" \ -w /workspace "$IMG" \ bash } #------------------------------------- # CAN and serial functions # Launch CAN viewer for the given interface (default: slcan0). canview() { if [ -z "$CAN_CHANNEL" ]; then echo "Warning: CAN_CHANNEL not set. Using default can0" CAN_CHANNEL=can0 fi if [ -z "$CAN_INTERFACE" ]; then echo "Warning: CAN_INTERFACE not set. Using default slcan0" CAN_INTERFACE=socketcan fi python3 -m can.viewer -c "$CAN_CHANNEL" -i "$CAN_INTERFACE" } # Connect to a serial device using Picocom (default: /dev/ttyACM0). repl(){ if [ -z "$AMPY_PORT" ]; then echo "Warning: AMPY_PORT not set. Using default /dev/ttyACM0" AMPY_PORT=/dev/ttyACM0 fi echo "Connecting to $AMPY_PORT" picocom -b 115200 $AMPY_PORT } #------------------------------------- # SSHFS and network functions # Mount a remote directory over SSHFS. Takes a remote path as an argument. function mount_ssh() { if [ -z "$1" ]; then echo "No argument provided." return 1 fi echo "Mounting $1" mkdir -p "$1" sshfs -o allow_other "$1:" "$1" } #------------------------------------- # Miscellaneous functions # Find a file and display its contents. find_and_cat() { if [[ -z $1 ]]; then echo "Usage: find_and_cat " return 1 fi find . -type f -name "$1" -print0 | while IFS= read -r -d '' file; do echo -e "File Location: $file\n" echo "File Contents:" cat "$file" echo -e "\n---\n" done } # record_window record_window() { local outfile="recording_$(date +%F_%H-%M-%S).mp4" echo "Click on the window you want to record..." local wininfo=$(xwininfo) local width=$(echo "$wininfo" | grep "Width:" | awk '{print $2}') local height=$(echo "$wininfo" | grep "Height:" | awk '{print $2}') local x=$(echo "$wininfo" | grep "Absolute upper-left X:" | awk '{print $4}') local y=$(echo "$wininfo" | grep "Absolute upper-left Y:" | awk '{print $4}') echo "Recording window at ${width}x${height}+${x},${y} to $outfile..." ffmpeg -video_size ${width}x${height} -framerate 30 -f x11grab -i :0.0+${x},${y} \ -vcodec libx264 -preset fast -pix_fmt yuv420p -movflags +faststart "$outfile" } fdcat() { # 1. Run fdfind with all passed arguments and pipe to fzf # Usage: fdcat -e py logging # Usage: fdcat -e js auth local file=$(fdfind "$@" | fzf --preview 'batcat --color=always --style=numbers {}') # 2. Copy and print if a selection was made if [ -n "$file" ]; then # Adjust 'xclip -sel clip' if you are on WSL (use clip.exe) or Mac (use pbcopy) echo -n "$file" | xclip -sel clip echo "✔ Copied: $file" else echo "Exit: No file selected." fi } #---- Aliases source ~/.aliases.sh