Skip to content

Instantly share code, notes, and snippets.

@kuntau
Forked from wolandark/execute_all.sh
Created August 1, 2024 09:55
Show Gist options
  • Save kuntau/1b736b3cb92fddca338893886d4ff45a to your computer and use it in GitHub Desktop.
Save kuntau/1b736b3cb92fddca338893886d4ff45a to your computer and use it in GitHub Desktop.

Revisions

  1. @wolandark wolandark revised this gist Aug 1, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion execute_all.sh
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ mime=$(file --mime-type -b "$1" 2>/dev/null)
    case "$mime" in
    text/*)
    # Use vim for text files
    vim "$1" >/dev/null 2>&1
    vim "$1"
    ;;
    *)
    # Let xdg-open handle the rest
  2. @wolandark wolandark revised this gist Aug 1, 2024. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions export FZF_CTRL_T_OPTS
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # to be placed in ~/.bashrc

    export FZF_CTRL_T_OPTS="
    --preview '~/preview_all.sh {}'
    --height '100%'
    --bind 'ctrl-/:change-preview-window(down|hidden|)'
    --bind 'enter:execute(~/execute_all.sh {+})'"
  3. @wolandark wolandark revised this gist Aug 1, 2024. No changes.
  4. @wolandark wolandark revised this gist Aug 1, 2024. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions execute_all.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    #!/bin/bash

    # MIME type of the file
    mime=$(file --mime-type -b "$1" 2>/dev/null)

    # Handle different MIME types
    case "$mime" in
    text/*)
    # Use vim for text files
    vim "$1" >/dev/null 2>&1
    ;;
    *)
    # Let xdg-open handle the rest
    xdg-open "$1" >/dev/null 2>&1
    ;;
    esac

    exit 0
  5. @wolandark wolandark created this gist Aug 1, 2024.
    48 changes: 48 additions & 0 deletions preview_all.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    #!/bin/bash

    # MIME type of the file
    mime=$(file --mime-type -b "$1" 2>/dev/null)

    # Cleanup function
    cleanup() {
    [[ -n "$thumb_cache" ]] && rm -f "$thumb_cache"
    }
    trap cleanup EXIT

    # Handle different MIME types
    case "$mime" in
    text/*)
    # Use bat for text files
    bat --color=always --style=numbers "$1" 2>/dev/null
    ;;
    inode/directory)
    tree "$1" 2>/dev/null
    ;;
    image/jpeg | image/png)
    # Use magick to display images in the terminal using sixel
    magick "$1" -geometry 800x480 sixel:- 2>/dev/null
    ;;
    application/pdf)
    # Use pdftotext for PDF files
    pdftotext "$1" - | head -n 1000 2>/dev/null
    ;;
    application/epub+zip)
    # Use epub2txt for EPUB files
    epub2txt "$1" | head -n 1000 2>/dev/null
    ;;
    video/*)
    # Generate a thumbnail for video files
    thumb_cache=$(mktemp "${TMPDIR:-/tmp}/thumb_cache.XXXXX.png")
    ffmpegthumbnailer -i "$1" -o "$thumb_cache" -s 0 2>/dev/null
    # Display the thumbnail
    img2sixel < "$thumb_cache" 2>/dev/null
    ;;
    application/x-tar | application/gzip | application/x-compressed-tar | application/x-bzip2 | application/x-xz | application/zip | application/x-7z-compressed | application/x-rar-compressed)
    ~/.config/lf/pistol-static-linux-x86_64 "$1" 2>/dev/null
    ;;
    *)
    # Default: show file details
    echo "File type: $mime"
    echo "No preview available for this file type."
    ;;
    esac