diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-11-29 22:12:45 +0100 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-11-29 22:12:45 +0100 |
commit | fefcbc3a750981e8df25ee84441a214642d2195c (patch) | |
tree | 916e69d9c8e12811578c67782fb9cd90099e425b /.local/bin/linkview | |
parent | update (diff) | |
download | dotfiles-bsd-fefcbc3a750981e8df25ee84441a214642d2195c.tar.gz dotfiles-bsd-fefcbc3a750981e8df25ee84441a214642d2195c.tar.bz2 dotfiles-bsd-fefcbc3a750981e8df25ee84441a214642d2195c.tar.xz dotfiles-bsd-fefcbc3a750981e8df25ee84441a214642d2195c.tar.zst dotfiles-bsd-fefcbc3a750981e8df25ee84441a214642d2195c.zip |
update
Diffstat (limited to '')
-rwxr-xr-x | .local/bin/linkview | 176 |
1 files changed, 56 insertions, 120 deletions
diff --git a/.local/bin/linkview b/.local/bin/linkview index f4e6145..3fb79e6 100755 --- a/.local/bin/linkview +++ b/.local/bin/linkview @@ -7,11 +7,11 @@ use File::HomeDir qw(home); use File::Basename qw(basename); use Cwd qw(cwd); use Term::ReadKey; -use Term::ANSIColor qw(colored); use Capture::Tiny qw(capture); use POSIX qw(setsid); use constant { + TERMINAL_PATH => '/usr/local/bin/st', MPVIEW_PATH => '/home/jozan/.local/bin/mpview', MPV_PATH => '/usr/local/bin/mpv', YTDL_PATH => '/usr/local/bin/youtube-dl', @@ -22,6 +22,15 @@ use constant { FIREFOX_PATH => '/usr/local/bin/firefox', NOTIFYSEND_PATH => '/usr/local/bin/notify-send' }; +use constant PROG_LIST => "" . +"mpv" . "\n" . +"mpv-term" . "\n" . +"youtube-dl" . "\n" . +"fetch" . "\n" . +"sxiv" . "\n" . +"zathura" . "\n" . +"w3m" . "\n" . +"firefox" . "\n"; sub open_link { @@ -33,13 +42,22 @@ sub open_link my @wc; my $i; - if ($a == 1) { - exec( - MPVIEW_PATH, - $url - ); + if ($a eq "mpv") { + if (system(MPVIEW_PATH, $url) != 0) { + system( + NOTIFYSEND_PATH, + '-u', 'critical', + '-t', '10000', + 'playback failed', + ' Failed to open <b>' . $url . '</b>' + ); + } + exit 0; + } + elsif ($a eq "mpv-term") { + exec(TERMINAL_PATH, '-e', MPV_PATH, '--audio-channels=stereo', $url); } - elsif ($a == 2 || $a == 3) { + elsif ($a eq "fetch" || $a eq "youtube-dl") { $pid = fork(); if (not $pid) { setsid(); @@ -74,57 +92,45 @@ sub open_link $file_name =~ s/^[0-9]+/\[\.\.\.\]/; system( NOTIFYSEND_PATH, - '-u', - 'normal', - '-t', - '10000', + '-u', 'normal', + '-t', '10000', 'download started', ' <b>'. $file_name . '</b> started downloading' - ); + ); $ret = -1; - if ($a == 2) { + if ($a eq "fetch") { (undef, undef, $ret) = capture { - system( - YTDL_PATH, - $url - ); + system(YTDL_PATH, $url); }; } else { (undef, undef, $ret) = capture { - system( - FETCH_PATH, - $url - ); + system(FETCH_PATH, $url); }; } if ($ret == 0) { system( NOTIFYSEND_PATH, - '-u', - 'normal', - '-t', - '10000', + '-u', 'normal', + '-t', '10000', 'download complete', - ' <b>' . $file_name . '</b> downloaded successfully to ' - . '<b>' . $pwd . '</b>' - ); + ' <b>' . $file_name . '</b> downloaded successfully to ' . + '<b>' . $pwd . '</b>' + ); } else { system( NOTIFYSEND_PATH, - '-u', - 'critical', - '-t', - '10000', + '-u', 'critical', + '-t', '10000', 'download failed', ' failed to download <b>' . $file_name . '</b>' - ); + ); } exit 0; } } - elsif ($a == 4 || $a == 5) { + elsif ($a eq "sxiv" || $a eq "zathura") { $pid = fork(); if (not $pid) { setsid(); @@ -133,109 +139,47 @@ sub open_link $file_name =~ s/.+\///g; system( NOTIFYSEND_PATH, - '-u', - 'low', - '-t', - '5000', + '-u', 'low', + '-t', '5000', 'download started', ' fetching <b>'. $file_name . '</b> to <b>/tmp</b>' - ); + ); system( FETCH_PATH, '-q', $url ); - if ($a == 4) { - exec( - SXIV_PATH, - '-b', - '-a', - $file_name - ); + if ($a == "sxiv") { + exec(SXIV_PATH, '-b', '-a', $file_name); } else { - exec( - ZATHURA_PATH, - $file_name - ); + exec(ZATHURA_PATH, $file_name); } } } - elsif ($a == 6) { - system('clear'); - exec( - MPV_PATH, - '--really-quiet', - '--audio-channels=stereo', - '--loop', - $url - ); + elsif ($a eq "w3m") { + exec(TERMINAL_PATH, '-e', W3M_PATH, $url); } - elsif ($a == 7) { - exec( - W3M_PATH, - $url - ); - } - elsif ($a == 8) { + elsif ($a eq "firefox") { $pid = fork(); if (not $pid) { setsid(); capture { - exec( - FIREFOX_PATH, - '--kiosk', - $url); + exec(FIREFOX_PATH, '--kiosk', $url); }; } } - elsif ($a == 9) { - system('clear'); - exec( - MPV_PATH, - '--audio-channels=stereo', - $url - ); - } return; } -sub prompt +sub dmenu_prompt { + my ($url) = @_; my $answer; + my $list = PROG_LIST; - $answer = -1; - print - "Open with?\n" . - "1) mpv (default)\n" . - "2) youtube-dl\n" . - "3) fetch\n" . - "4) sxiv\n" . - "5) zathura\n" . - "6) mpv-loop\n" . - "7) w3m\n" . - "8) firefox\n" . - "9) mpv-verbose\n" . - "0) quit\n" . - "\n" . - "> "; - open(TTY, "</dev/tty"); - ReadMode "raw"; - $answer = ReadKey 0, *TTY; - ReadMode "normal"; - close(TTY); - print "\n\n"; - if ($answer eq "\n") { - return 1; - } - if ( - !looks_like_number($answer) || - $answer < 0 || - $answer > 9 - ) { - print "linkview: bad answer\n"; - return -1; - } + $answer = `printf "$list\nURL: $url\n" | dmenu -i -l 10`; + chomp $answer; return $answer; } @@ -247,15 +191,7 @@ sub main print STDERR "linkview: no URL\n"; exit 1; } - system('tput', 'clear'); - print 'URL: ' . colored($ARGV[0], 'red') . "\n\n"; - $answer = -1; - while ($answer == -1) { - $answer = prompt(); - } - if ($answer == 0) { - exit 0; - } + $answer = dmenu_prompt($ARGV[0]); open_link($answer, $ARGV[0]); return; } |