summarylogtreecommitdiffstats
path: root/fzf2md
blob: 132d6ecea17dd82299b490e8a48d406e429712a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/bin/bash

# Dependencies check: Ensure `fzf`, `git`, and `bat` are installed.
if ! command -v fzf &>/dev/null; then
    echo "This script requires 'fzf'. Install it with your package manager."
    exit 1
fi

if ! command -v git &>/dev/null; then
    echo "This script requires 'git'. Install it with your package manager."
    exit 1
fi

if ! command -v bat &>/dev/null; then
    echo "This script requires 'bat'. Install it with your package manager."
    exit 1
fi

# Get files while respecting .gitignore if it exists.
if [ -f .gitignore ] || [ -d .git ]; then
    FILES=$(git ls-files --others --cached --exclude-standard)
else
    FILES=$(find . -type f -print)
fi

# Interactive file selection using fzf with bat for syntax-highlighted previews.
SELECTED_FILES=$(echo "$FILES" | fzf --multi --header="Select files (Tab to toggle, Enter to confirm):" \
    --preview="file --mime {} | grep -qE 'charset=binary' && echo 'Binary file: No preview available.' || bat --color=always --style=plain --line-range=:100 {}" \
    --preview-window=right:50% --bind space:toggle)

# If no files are selected, exit the script.
if [[ -z $SELECTED_FILES ]]; then
    echo "No files selected. Exiting."
    exit 1
fi

# Output formatting function.
print_file_content() {
    local file="$1"
    echo "**$file:**"
    echo
    if file --mime "$file" | grep -qE 'charset=binary'; then
        # Print a Markdown link for binary files.
        echo "[$file](./$file)"
    else
        # Print the file content, indented and wrapped in a code block.
        echo '```'
        sed 's/^/    /' "$file" # Indent each line of the file's content by 4 spaces.
        echo
        echo '```'
    fi
    echo
}

# Print output to terminal for each selected file.
while IFS= read -r file; do
    print_file_content "$file"
done <<<"$SELECTED_FILES"