You are not logged in.
added all improvements suggested, here is the updated script
#!/bin/bash
shopt -s lastpipe
## make temporary file and copy all commands to it
f=$(mktemp /tmp/"${0##*/}".XXXXX)
cat $HOME/.config/shortcuts <(IFS=:; find $PATH -executable -printf "%f\n" | sort -r ) | awk '!NF || !x[$0]++' > "$f"
## choose command to execute using fzf (if run from terminal) or rofi (if run using window-manager shortcut)
if [[ $TERM = linux && -n $DISPLAY ]]; then
rofi -dmenu -font "Noto Sans Mono Medium 18" -p "RUN THIS" -input $f | read command
else
fzf --border --no-sort --reverse --height 10 --prompt "RUN THIS: " < $f | read command
fi
## cleanup at exit
clean_f() {
[[ -f "$f" ]] && rm "$f"
}
trap clean_f EXIT
## run the command
exec $command &
EDIT: I am happy to use cat for it's intended purpose (concatenate).
EDIT2 : removed "-type f" option from find command line, to allow symlinks like libreoffice to become available.
Last edited by Docbroke (2021-08-03 10:34:26)
Arch is home!
https://github.com/Docbroke
Offline
EDIT: I am happy to use cat for it's intended purpose (concatenate).
Still, you don't have to use 'cat'..;)
p="$HOME"/.config/shortcuts
IFS=:; find $PATH $p -executable -printf "%f\n" | sort -r | awk '!NF || !x[$0]++' > "$f"
Offline
I'm not clear on what that awk script does either. Isn't that just enforcing unique matches? Sort can do that with the -u flag.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
@Trilby, the awk script removes duplicates.
I didn't think about sort -u but that will work too.
Offline
Still, you don't have to use 'cat'..;)
p="$HOME"/.config/shortcuts IFS=:; find $PATH $p -executable -printf "%f\n" | sort -r | awk '!NF || !x[$0]++' > "$f"
You missed ! I am combining two files with cat. One is my personal shortcuts (in .confing/shortcuts) and two is commands from $PATH.
I'm not clear on what that awk script does either. Isn't that just enforcing unique matches? Sort can do that with the -u flag.
I am using sort with $PATH, but I don't want to sort after joining $PATH with my shortcuts (I need them on top). As a compromise I can have
cat $HOME/.config/shortcuts <(IFS=:; find $PATH -executable -printf "%f\n" | sort -ur ) > "$f"
Arch is home!
https://github.com/Docbroke
Offline
qinohe wrote:Still, you don't have to use 'cat'..;)
p="$HOME"/.config/shortcuts IFS=:; find $PATH $p -executable -printf "%f\n" | sort -r | awk '!NF || !x[$0]++' > "$f"
You missed ! I am combining two files with cat. One is my personal shortcuts (in .confing/shortcuts) and two is commands from $PATH.
Look again, 'p' is you shortcut file, 'find $PATH $p...> "$f"' will find both paths and store it in '$f' == one sorted list??
edit: forget what I said, you're right if 'shortcuts' is a file, I was thinking 'directory'...
Last edited by qinohe (2021-08-03 16:20:56)
Offline
Yes, I missed $p in your code, but it didn't make a sense as shortcuts is a file, with commands listed one per line. Why would I use cat with a directory ? Additionally there is no $p in updated script I posted above.
Arch is home!
https://github.com/Docbroke
Offline
Use a better shell
#!/bin/zsh
sort < somefile < somefile
More importantly though: why are you not using stest and a cache?
IFS=:
if stest -dqr -n "$cache" $PATH; then
stest -flx $PATH | sort -u > "$cache"
fi
Offline
That is part of dmenu_path code ? right!
I tried to use stest initially, but it needs dmenu to be installed ( as it is part of dmenu package). So I instead used find. I know it is not very logical but I don't use dmenu (as I prefer rofi), and I didn't like to keep full package installed for part of it's code.
Someday I may move to dash or ash from bash, but zsh is in opposite direction. For me leaner is better.
Last edited by Docbroke (2021-08-03 19:28:36)
Arch is home!
https://github.com/Docbroke
Offline
Yup, comes in handy regardless.
You could map it to find and s till use a cache depending on the age of the elements in $PATH (possibly also compare $PATH to the last invocation)
Offline
Yup, comes in handy regardless.
If you really don't like dmenu installed there is stest in AUR.
I am with seth here and agree it's useful.
BTW. glanced over that code again, and, the cat can be removed by a bashism, I just think that cat is ugly, sorry ;-(
p=$(<"$HOME"/.config/shortcuts)
IFS=:; find $PATH $p -executable -printf "%f\n" | sort -ru
Offline
I assume .config/shortcuts contains a list of the completions he wants (shell aliases or functions?), not some paths.
So you must pipe the contents of that file and the result of the find into sort.
Offline
I assume .config/shortcuts contains a list of the completions he wants (shell aliases or functions?), not some paths.
So you must pipe the contents of that file and the result of the find into sort.
Yes, I understand, than there would still be a workaround needed for that alias will not be found.
I think the tool is okay but a lot of apps in the list will not work so you kinda need to know what will work and what wont, using a terminal or rofi etc...
Offline
I assume .config/shortcuts contains a list of the completions he wants (shell aliases or functions?), not some paths.
Yes indeed. Here are some selected lines from shortcuts. @qinohe, this allows me to run commands that otherwise would have failed when run using rofi.
w3search
terminator --geometry=800x400 -x openfile
terminator --geometry=900x1000 -x vifm
terminator --geometry=700x400 -x pasd
terminator -x pase
terminator --geometry=300x350 -x otp
terminator --geometry=700x1000 -x nnn -Rex
terminator --geometry=900x550 -x calrem
terminator --geometry=900x1000 -x htop
link-handle
vncviewer -Fullscreen -LowColorLevel 0 piserver
weaver //target/0 //profile/gmail mail.google.com
and here are some of the scripts used
┌─[sharad] (07:30 AM)-(Wed Aug 04) [~]
└───▶ cat bin/pasd
#!/bin/sh
cd $HOME/dotfiles/config/.config/password-store
#gpg -d $(fzf)
file=$(find . -type f | fzf --border --reverse --height 10 --prompt "View Password: ")
pass ${file%.gpg} && sleep 60
┌─[sharad] (07:30 AM)-(Wed Aug 04) [~]
└───▶ cat bin/openfile
#!/bin/sh
exec opener "$(find . -type f | fzf --border --reverse --height 10 --prompt "Open File : ")"
┌─[sharad] (07:30 AM)-(Wed Aug 04) [~]
└───▶ cat bin/calrem
#!/bin/sh
rem -cu+2
sleep 60
opener is mime-type based file opener written in bash, here is the sample code, actual file is longer as it contains many mimetype associations as well as some file suffix based associations. I am still in the process of improving it's code.
└───▶ cat bin/opener
#!/usr/bin/env bash
export TERM=terminator
[[ -z $1 || ! -e $1 ]] &&
exit 1
#mime_type="$(mimetype -biL "$1")"
mime_type="$(file -b --mime-type "$1")"
case "$mime_type" in
text/html|application/x-mimearchive)
weaver "$@" || vimb "$@" || chromium "$@" || firefox "$@" || $TERM -x w3m "$@"
;;
text/*|*/xml|application/x-httpd-php3|application/x-httpd-php4|application/x-httpd-php5|application/x-shellscript)
$TERM -x "${VISUAL:-${EDITOR:-vis}}" "$@"
;;
esac
Last edited by Docbroke (2021-08-04 02:11:59)
Arch is home!
https://github.com/Docbroke
Offline
Offline
xdg-open has limitations ( or so I believe). It needs desktop files to open apps, and it works with mimetypes only. With my code I am free to use command names without creating desktop files and I can use regular expressions. For example .rar and .cbr are both "application/x-rar", but I use unarchiver for .rar and mupdf/foliate for .cbr/.cbz
Arch is home!
https://github.com/Docbroke
Offline
@qinohe, this allows me to run commands that otherwise would have failed when run using rofi.
...
Makes sense, thanks, for explaining, now I see the why;)
Offline
I have posted file opener script on github, that works similar to xdg-open (hinted above), https://github.com/Docbroke/opener
Arch is home!
https://github.com/Docbroke
Offline
which is a partial implementation of a pipe based relational database system
so, to find all people named "John Doe", whow's birthday falls on a holiday; you'd do something like this.
cat input.tsv | rows Name eq "John Doe" | columns Name Birthday Address | jointbl col.Birthday=col.Holidays main_table
well. the joins are the next thing i'm implementing. and there are some utility functions (like adding headers automatically and/or interactively) that I want to do.
Offline
cat input.tsv | rows Name eq "John Doe" | columns Name Birthday Address | jointbl col.Birthday=col.Holidays main_table
Useless use of cat?!
Or: Why doesn't rows take a file as argument?
Inofficial first vice president of the Rust Evangelism Strike Force
Offline
Why not juse use sqlite? What benefit does the pipes system give over the following? You most likely have sqlite already installed.
sqlite3 <<EOF
.headers on
.mode tabs
.import input.tsv people
select Name, Birthday, Address from people
join main_table on people.Birthday = main_table.Holidays
where Name = 'John Doe'
EOF
(Note, the ".headers on" and ".mode tabs" can be specified in your config file so they'd not even be needed if that's how you wanted to use it).
Last edited by Trilby (2021-08-09 13:22:40)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
I've had a look at the gitlab page and the oldest commit I could find wasn't even a week old. Looks like an early WIP and I'd give it a lot of slack regarding file inputs and cat abuse. I don't see a benefit over sqlite either, but I wouldn't whip out go to replace awk/grep/sed/cut either.
Offline
Transpose a data table in pure sed. Pass a file name, and optionally a delimeter character (as argv[2]) which defaults to tab:
#!/bin/sh
s=${2:-\\t}
sed -n -f /dev/stdin <<EOF "$1"
{ H; }
\$ {
g; s/^\n//
:loop
h; s/$s[^\n]*/$s/g; s/\n//g; s/$s\$//; p
g; s/[^$s]*$s\([^\n]*\)\n*/\1\n/g
/$s/ b loop
s/\n/$s/g; s/$s\$//; p
}
EOF
(developed here)
Last edited by Trilby (2021-08-11 03:32:32)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
xdg-open has limitations ( or so I believe). It needs desktop files to open apps, and it works with mimetypes only. With my code I am free to use command names without creating desktop files and I can use regular expressions. For example .rar and .cbr are both "application/x-rar", but I use unarchiver for .rar and mupdf/foliate for .cbr/.cbz
xdg-open is so horrible. The insistence on stupid desktop files to run simple commands, and inability to use globs or regexes for mime types - for example it makes sense to assign "video/*" to VLC (which could potentially be done inside mimeapps.list using globs), but xdg-open does not enable this but instead opts to map every single video mime type to a desktop file.
I did something similar to your script in Node to replace xdg-open: https://github.com/karabaja4/mimejs
Last edited by karabaja4 (2021-08-11 13:57:24)
Offline
There's a high variety of xdg-open "replacements", most of which offer incompatible behavior. See the table here: https://wiki.archlinux.org/title/Defaul … ce_openers
Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby
Offline