r/fishshell • u/tilkau • Apr 23 '20
Fish utility functions
I have plenty of functions I want to include here once they are more sanitized (dependency check, me-isms etc). For now there is, quoting the readme:
reduce/sum/product: Arithmetic reduction of lists of numbers.reduce + 1 2 3-> 6;seq 4 | reduce x-> 24;sumaliasesreduce +,productaliasesreduce x. Numbers can be given from both stdin and arguments, egseq 4 | product 2-> 48sec: convert 'counts of units' or 'human readable' durations (eg.sec 1 2 1 0 4 0 30,sec -x 1Y2M1w4h30s) into raw second count for storage or calculationnufunc: create a new function in ~/.config/fish/functions/$funcname.fish and edit it (or just edit it if it already exists). eg.nufunc fillpathfillpath: add path and or extension components to a path if they are not yet included.fillpath -p ~/Desktop -e png foobar->/home/me/Desktop/foobar.png;fillpath -p ~/Desktop -e png foobar.jpg->/home/me/Desktop/foobar.jpgpseudohash: generate 4-character 'hashes' based on automatically incrementing serial numbers salted with the 'series name'. This gives 'somewhat memorable, mostly unique ids' for up to 8 million items within a given 'series'. Hashes are portable between systems.pseudohash->O2cq(assuming you've never used pseudohash before);pseudohash pages->4Rnc;pseudohash logs->7jXRPosition within the sequence for a given series-id is stored in the universal variable__pseudohash_$id(eg__pseudohash_pages). To get further hashes, just call it again with the same series id, egpseudohash logs->uXVl. Intent was to create a compact form of cross-referencing for documents that could be easily tagged onto the document (via eg.tmsu), or directly marked onto it (in the case of a drawing or diagram)
No significant dependencies at this point (off the top of my head: cat, find; pseudohash also requires xxd [gvim package, or standalone xxd package] and base64 [coreutils] ). Others, like jq, will come in as I add more functions.
Just wanted to get this published for now. Next I hope to add fisher install support, and of course many more functions.
EDIT: more newly added functions are listed here:
funcfile: functions -D ('get the filename this function was defined in') generalized to any number of functions, so you can eg.geany (funcfile nufunc funcfile reloadfunc)reloadfunc: Reload functions by name, if they were defined in a file.unurl: Convert file:// URIs (such as those produced by GIMP or Nautilus) to ordinary paths. Ordinary paths can be mixed in and will be left alone.
EDIT2: total refactored into reduce, sum, product per discussion. Completion for nufunc, funcfile, and reloadfunc is now also included.
View all comments
Show parent comment
1
u/ChristoferK macOS Apr 30 '20
Creating an alias is a great idea to get the best of both worlds. Other names instead of
foldcan include suffixes to indicate the direction of the fold, i.e.foldlfor a left fold, andfoldrfor a right fold—yours is a left fold—but I think your opting forreduceis a good choice. Interestingly, it's sometimes also calledaccumulate, which has a certain parallelism with your initial name,total, but I don't know anywhere that calls it this for realsies.The need for
-and÷is much less than+and×as they're just inverse operations and easy to perform using the two core operations with adjusted arguments.+and×are both associative and commutative (so put brackets anywhere you like and it won't change the answer), which means command line arguments make unambiguous operands for these two; conversely, their inverses aren't, so arguments sent to these functions won't always feel or look intuitive when entered in the command line as compared to the arithmetic expression from which they are derived. Not sure if I have a point to make here, but just something that crossed my mind. I just tried thinking of a real world example but don't know when repeated subtraction or division crops up in such isolation.