Skip to content

Instantly share code, notes, and snippets.

View yanli0303's full-sized avatar

Yan Li yanli0303

View GitHub Profile
@yanli0303
yanli0303 / README.md
Last active September 12, 2025 20:41
Add shell-script as the Git pre-commit hook

Shell script as Git pre-commit hook

This pre-commit.sh file could serve as a Git pre-commit hook and perform tasks against staged changes on git commit.

Backup stash

  1. Detects staged and unstaged changes:

    • This script checks which files are staged and which files contain additional, unstaged changes.
  2. Stashes unstaged changes:

@yanli0303
yanli0303 / index.html
Last active September 12, 2025 12:33
Open & Close Transitions with <details>
<style type="text/css">
details {
inline-size: 50ch;
@media (prefers-reduced-motion: no-preference) {
interpolate-size: allow-keywords;
}
&::details-content {
opacity: 0;
@yanli0303
yanli0303 / rename_photos.py
Last active December 20, 2025 17:40
Rename pixel photo/video files to YYYY-MM-DD_HH-mm-SS_sss.jpg
import os
import sys
from datetime import datetime, timedelta, timezone
def is_eastern_dst(dt: datetime) -> bool:
"""
Check if a datetime is during Daylight Saving Time in US Eastern timezone.
DST starts on the second Sunday in March at 2:00 AM.
@yanli0303
yanli0303 / flat_dir.py
Created August 17, 2025 01:42
Directory Flattening Script, it flattens a directory by moving all files from subdirectories to the root directory
#!/usr/bin/env python3
"""
Directory Flattening Script
This script accepts a path to a directory and flattens it by moving all files
from subdirectories to the root directory. When file name conflicts occur,
it keeps all files by adding numbered suffixes like (1), (2), etc.
Usage:
python flat_dir.py <directory_path>
@yanli0303
yanli0303 / cleanup_images.py
Last active August 17, 2025 02:02
Image cleanup script that moves .jpg and .png files that don't match their directory pattern to a backup location.
#!/usr/bin/env python3
"""
Image cleanup script that moves .jpg and .png files that don't match their directory pattern to a backup location.
The pattern is derived from the directory name by removing all spaces, dots, hyphens, #, and @.
Files are matched by removing same characters from their names as well.
Instead of deleting files, they are moved to a backup directory with the same structure.
"""
import argparse
@yanli0303
yanli0303 / delete_duplicate_images.py
Created August 15, 2025 12:24
Delete image files whom name ends with `(number)`
#!/usr/bin/env python3
"""
Script to delete duplicate image files that end with " (number)" pattern.
This typically happens when files are duplicated by the OS or applications.
Usage: python3 delete_duplicate_images.py <directory_path>
"""
import argparse
import os
@yanli0303
yanli0303 / rename_subdirectories_by_regex.py
Created August 15, 2025 12:16
Rename subdirectories by regular expression
#!/usr/bin/env python3
"""
Script to rename directories by removing trailing " (\\d\\dP)" pattern.
Usage:
python rename_directories.py /path/to/directory
Example:
python rename_directories.py ~/Desktop/videos
@yanli0303
yanli0303 / dark-mode-invert-color.css
Created January 11, 2025 15:40
dark-mode-invert-color.css
@media (prefers-color-scheme: dark) {
pre.src > *,
pre.example > * {
filter: invert(20%) brightness(200%);
}
}
html:has(meta[name="color-scheme"][content="dark"]) pre.src > *,
html:has(meta[name="color-scheme"][content="dark"]) pre.example > * {
filter: invert(20%) brightness(200%);
@yanli0303
yanli0303 / http-cache-headers.md
Created July 31, 2024 17:32
HTTP cache headers

HTTP cache headers

  • Immutable Files: Cache-Control: max-age=31536000
  • Mutable Files: Cache-Control: no-cache; etag; Last-Modified;
  • Never cache: Cache-Control: no-store;
@yanli0303
yanli0303 / html-popover-property.html
Created July 31, 2024 17:31
HTML popover property
<!DOCTYPE html>
<html>
<head>
<title>HTML popover property</title>
</head>
<body>
<button id="button" popovertarget="select">请选择</button>
<menu id="select" popover>
<div><input type="radio" name="type" value="">请选择</div>