aboutsummaryrefslogblamecommitdiff
path: root/bin/xgo.sh
blob: c59409a58417215f0538fc48d78f47da23469780 (plain) (tree)
1
2
3
4
5
6
7
8
9



                                                   
                                                 
          


                                
            
 
                                                               
                
 

                                                                              
                                                         

                                        
              
 
                                                       
                            
                                       

                                                         

              
                                                                          

                                                                            

                              

                                                          
              
 

                                                                           

                                           
                                                           
              

        


                            
                          
               
 
                                                                               
                                          
                         


                                               

                                         
              
 
                                        
                         
                                                           
              
 



                                                                           
                 
                                       
              

                                               
                    
                                       
                                                                              
              
 




                                            
 
        
# Test and open a clipboard URL with an apt program

# Check arguments
if [ "$#" -eq 0 ] ; then
    printf >&2 'xgo: At least one URL required\n'
    exit 2
fi

# Iterate over the URL arguments
for url do (

    # Look for patterns in the URL that suggest transformations
    case $url in

        # If this is a GitHub or GitLab link, swap "blob" for "raw" to get the
        # actual file
        (*://github.com/*/blob/*|*://gitlab.com/*/blob/*)
            url=$(printf '%s\n' "$url" |
                sed 's_/blob/_/raw/_')
            ;;

        # Dig out the plain text for pastebin.com links
        (*://pastebin.com/*)
            # shellcheck disable=SC2016
            url=$(printf '%s\n' "$url" |
                sed 's_/[A-Za-z0-9][A-Za-z0-9]*$_/raw&_')
            ;;

        # If this is a not-direct imgur link and not to an album, swap URL
        # elements to get to the actual file (it may not actually be a JPEG;
        # the MIME type will tell us)
        (*://imgur.com/a/*) ;;
        (*://imgur.com/*)
            url=$(printf '%s\n' "$url" |
                sed 's_imgur\.com_i.imgur.com_;s/$/.jpg/')
            ;;

        # If this is a YouTube video without a given start time, load it in
        # mpv(1)
        (*[/.]youtube.com/watch*[?\&]t=) ;;
        (*[/.]youtube.com/watch*)
            exec mpv --force-window --no-terminal -- "$url"
            ;;
    esac

    # Get the MIME type data
    mt=$(urlmt "$url")

    # Switch on media type
    case $mt in

        # Open PDFs in xpdf(1); download them first as xpdf(1) does not seem to
        # have a way to handle stdin files
        (application/pdf)
            (
                cd -- "$HOME"/Downloads || exit
                curl -O -- "$url" || exit
                exec xpdf -- "${url##*/}"
            )
            ;;

        # Open audio and video in mpv(1)
        (audio/*|video/*)
            exec mpv --force-window --no-terminal -- "$url"
            ;;

        # Open GIFs with a looping mpv(1), and all other images with feh(1)
        (image/gif)
            exec mpv --force-window --loop=inf --no-terminal -- "$url"
            ;;
        (image/*)
            exec curl -- "$url" | feh -
            ;;

        # Open plain text in a terminal view(1)
        (text/plain)
            # shellcheck disable=SC2016
            exec x-terminal-emulator -e sh -c 'curl -- "$1" | view -' _ "$url"
            ;;

        # Otherwise, just pass it to br(1df)
        (*)
            exec br "$url"
            ;;
    esac

) & done