User:Riviera/Go fish: Difference between revisions
(Created page with "<blockquote> In the shell I find a marvelous mess of constellations, nebulae, interstellar gaps, awesome gullies, that provokes in me an indescribable sense of vertigo, as if I am hanging from earth upside down on the brink of infinite space, with terrestrial gravity still holding me by the heels but about to release me any moment. ---- Nancy Mauro-Flude, 2008 </blockquote> =A Two-Part Syntax Highlighter= Below are two fish scripts. The first function, <tt>highlight</...") |
No edit summary |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
<includeonly>==Go Fish==</includeonly> | |||
<blockquote> | <blockquote> | ||
Line 6: | Line 7: | ||
</blockquote> | </blockquote> | ||
The shell is a computer programme which launches other programmes. This efficient, textual method of interacting with a computer raises practical and ethical questions. On a practical level the question arises as to which shell to choose from. <tt>bash</tt> is the default login shell on Linux distributions such as Debian and Rasbian. However, it is possible to choose a different shell. This choice leads to a discussion of the ethical issue of accessibility. A feature of this discussion involves unpacking the relationship between norms and defaults. In this wiki page I outline some implications of switching to a shell other than <tt>bash</tt> by way of several examples. I describe why I have chosen to use one shell, rather than another shell and argue in favour of <tt>fish</tt> as a default shell. Overall I argue that shell scripting (with <tt>fish</tt> or bash) could be an effective way in which to activate Radio Worm's sonic archive. | |||
<noinclude>=<tt>fish</tt> in practice =</noinclude> | |||
<includeonly>===<tt>fish</tt> in practice ===</includeonly> | |||
<tt>fish</tt> (https://fishshell.com/) has many features that other shells do not have. This includes enhanced autocompletion, a different way of scripting, improved syntax highlighting and idiosyncratic configuration. Three quarters of these features make it easier to get started with using the command line to interface with the computer. In implementing a different scripting language to <tt>bash</tt>, <tt>fish</tt> scripts pose a hurdle relating to interoperability. One must have access to a working installation of the software to run the script and often this is not the case by default. | |||
<tt>fish</tt> departs from it's normative counterpart <tt>bash</tt> in both helpful and obscure ways. Firstly, there is no file such as <tt>.fishrc</tt>. Instead <tt>fish</tt> keeps configuration data in various user-specific and site-wide configuration files. Extending the functional capabilities of the shell involves adding scripts under <tt>~/.config/fish/functions/</tt>. Command expansion is written differently. For more examples, see [https://fishshell.com/docs/current/fish_for_bash_users.html fish for bash users]. | |||
== <tt>higlight.fish</tt> == | <noinclude>==Going Fishing: A Syntax Highlighter ==</noinclude> | ||
<includeonly>====Going Fishing: A Syntax Highlighter====</includeonly> | |||
Below are two fish scripts, the second works in tandem with the first. The first function, <tt>highlight</tt>, utilises the <tt>sed</tt> command to edit HTML snippets generated by Emacs' Org Mode's HTML export backend. The second script works by calling the <tt>highlight</tt> function several times on the same file. Really this should be a single script, but I cannot figure out how to execute <tt>sed</tt> this many times in a row... | |||
<noinclude>===<tt>higlight.fish</tt>===</noinclude> | |||
<includeonly>=====<tt>higlight.fish</tt>=====</includeonly> | |||
<div style="font-family: Monospace; background-color: #dbe6f0;"><span style="color:teal">#</span><span style="color:teal">! /usr/bin/fish</span> | <div style="font-family: Monospace; background-color: #dbe6f0;"><span style="color:teal">#</span><span style="color:teal">! /usr/bin/fish</span> | ||
<span style="color:green">function</span> <span style="color:yellow">highlight</span> <span style="org-negation-char">-d</span> <span style="color:olive">"Highlight HTML code"</span> <span style="org-negation-char">-a</span> file | <span style="color:green">function</span> <span style="color:yellow">highlight</span> <span style="org-negation-char">-d</span> <span style="color:olive">"Highlight HTML code"</span> <span style="org-negation-char">-a</span> file | ||
Line 16: | Line 27: | ||
<span style="color:purple">sed</span> -i <span style="color:olive">'s/<pre/<div/'</span> <span style="color:olive">$</span><span style="color:orange">argv</span>; | <span style="color:purple">sed</span> -i <span style="color:olive">'s/<pre/<div/'</span> <span style="color:olive">$</span><span style="color:orange">argv</span>; | ||
<span style="color:purple">sed</span> -i <span style="color:olive">'s/<\/pre>/<\/div>/'</span> <span style="color:olive">$</span><span style="color:orange">argv</span>; | <span style="color:purple">sed</span> -i <span style="color:olive">'s/<\/pre>/<\/div>/'</span> <span style="color:olive">$</span><span style="color:orange">argv</span>; | ||
<span style="color:purple">sed</span> -i <span style="color:olive">'s/ | <span style="color:purple">sed</span> -i <span style="color:olive">'s/class=/style=/'</span> <span style="color:olive">$</span><span style="color:orange">argv</span>; | ||
<span style="color:purple">sed</span> -i -E <span style="color:olive">'s/"src src-<nowiki>[[:alpha:]]</nowiki>*"/"font-family: Monospace; background-color: #dbe6f0;"/'</span> <span style="color:olive">$</span><span style="color:orange">argv</span>; | <span style="color:purple">sed</span> -i -E <span style="color:olive">'s/"src src-<nowiki>[[:alpha:]]</nowiki>*"/"font-family: Monospace; background-color: #dbe6f0;"/'</span> <span style="color:olive">$</span><span style="color:orange">argv</span>; | ||
<span style="color:purple">sed</span> -i <span style="color:olive">'s/"color:teal"/"color:teal"/'</span> <span style="color:olive">$</span><span style="color:orange">argv</span>; | <span style="color:purple">sed</span> -i <span style="color:olive">'s/"color:teal"/"color:teal"/'</span> <span style="color:olive">$</span><span style="color:orange">argv</span>; | ||
Line 28: | Line 39: | ||
<span style="color:green">end</span> | <span style="color:green">end</span> | ||
</div> | </div> | ||
The <tt>s</tt> command in <tt>sed</tt> language is a shorthand for '''substitute'''. <tt>s</tt> commands are of the form | The <tt>s</tt> command in <tt>sed</tt> language is a shorthand for '''substitute'''. <tt>s</tt> commands are of the form | ||
<pre> | <pre> | ||
Line 34: | Line 44: | ||
</pre> | </pre> | ||
With the <tt>s</tt> command, <tt>sed</tt> queries a text file for the presence of a particular string, or regular expression, and replaces what is found with user defined text. In the above example, classes of HTML elements are restyled with colour to highlight different parts of the script. | With the <tt>s</tt> command, <tt>sed</tt> queries a text file for the presence of a particular string, or regular expression, and replaces what is found with user defined text. In the above example, classes of HTML elements are restyled with colour to highlight different parts of the script. Represented as a table, the changes are: | ||
{| class="wikitable" style="font-family: Monospace" | |||
|- | |||
|'''find''' | |||
|'''replace''' | |||
|- | |||
|<tt><pre</tt> | |||
|<tt><div</tt> | |||
|- | |||
|<nowiki></pre></nowiki> | |||
|<nowiki></div></nowiki> | |||
== <tt>higlight_all.fish</tt> == | |- | ||
|class= | |||
|style= | |||
|- | |||
|"src src-<nowiki>[[:alpha:]]</nowiki>*" | |||
|"font-family: Monospace; background-color: #dbe6f0" | |||
|- | |||
|"org-comment-delimiter" | |||
|"color:teal" | |||
|- | |||
|"org-comment" | |||
|"color:teal" | |||
|- | |||
| "org-keyword" | |||
|"color:green" | |||
|- | |||
|"org-function-name" | |||
|"color:yellow" | |||
|- | |||
|"org-variable-name" | |||
|"color:orange" | |||
|- | |||
|"org-string" | |||
|"color:olive" | |||
|- | |||
|"org-constant" | |||
|"color:gray" | |||
|- | |||
|"org-builtin" | |||
|"color:purple" | |||
|} | |||
<noinclude>===<tt>higlight_all.fish</tt>===</noinclude> | |||
<includeonly>=====<tt>higlight_all.fish</tt>=====</includeonly> | |||
<div style="font-family: Monospace; background-color: #dbe6f0;"><span style="color:teal">#</span><span style="color:teal">! /usr/bin/fish</span> | <div style="font-family: Monospace; background-color: #dbe6f0;"><span style="color:teal">#</span><span style="color:teal">! /usr/bin/fish</span> | ||
<span style="color:green">function</span> <span style="color:yellow">highlight_all</span> <span style="org-negation-char">-d</span> <span style="color:olive">"Call the highlight command several times"</span> <span style="org-negation-char">-a</span> file | <span style="color:green">function</span> <span style="color:yellow">highlight_all</span> <span style="org-negation-char">-d</span> <span style="color:olive">"Call the highlight command several times"</span> <span style="org-negation-char">-a</span> file | ||
Line 46: | Line 114: | ||
<span style="color:green">end</span> | <span style="color:green">end</span> | ||
</div> | </div> | ||
This command calls <tt>highlight</tt> multiple times to ensure the script runs to completion. I could improve this such that only one script is required. |
Latest revision as of 16:03, 22 December 2023
In the shell I find a marvelous mess of constellations, nebulae, interstellar gaps, awesome gullies, that provokes in me an indescribable sense of vertigo, as if I am hanging from earth upside down on the brink of infinite space, with terrestrial gravity still holding me by the heels but about to release me any moment.
Nancy Mauro-Flude, 2008
The shell is a computer programme which launches other programmes. This efficient, textual method of interacting with a computer raises practical and ethical questions. On a practical level the question arises as to which shell to choose from. bash is the default login shell on Linux distributions such as Debian and Rasbian. However, it is possible to choose a different shell. This choice leads to a discussion of the ethical issue of accessibility. A feature of this discussion involves unpacking the relationship between norms and defaults. In this wiki page I outline some implications of switching to a shell other than bash by way of several examples. I describe why I have chosen to use one shell, rather than another shell and argue in favour of fish as a default shell. Overall I argue that shell scripting (with fish or bash) could be an effective way in which to activate Radio Worm's sonic archive.
fish in practice
fish (https://fishshell.com/) has many features that other shells do not have. This includes enhanced autocompletion, a different way of scripting, improved syntax highlighting and idiosyncratic configuration. Three quarters of these features make it easier to get started with using the command line to interface with the computer. In implementing a different scripting language to bash, fish scripts pose a hurdle relating to interoperability. One must have access to a working installation of the software to run the script and often this is not the case by default.
fish departs from it's normative counterpart bash in both helpful and obscure ways. Firstly, there is no file such as .fishrc. Instead fish keeps configuration data in various user-specific and site-wide configuration files. Extending the functional capabilities of the shell involves adding scripts under ~/.config/fish/functions/. Command expansion is written differently. For more examples, see fish for bash users.
Going Fishing: A Syntax Highlighter
Below are two fish scripts, the second works in tandem with the first. The first function, highlight, utilises the sed command to edit HTML snippets generated by Emacs' Org Mode's HTML export backend. The second script works by calling the highlight function several times on the same file. Really this should be a single script, but I cannot figure out how to execute sed this many times in a row...
higlight.fish
function highlight -d "Highlight HTML code" -a file
argparse --name='highlight' 'h/help' -- $argv; sed -i 's/<pre/<div/' $argv; sed -i 's/<\/pre>/<\/div>/' $argv; sed -i 's/class=/style=/' $argv; sed -i -E 's/"src src-[[:alpha:]]*"/"font-family: Monospace; background-color: #dbe6f0;"/' $argv; sed -i 's/"color:teal"/"color:teal"/' $argv; sed -i 's/"color:teal"/"color:teal"/' $argv; sed -i 's/"color:green"/"color:green"/' $argv; sed -i 's/"color:yellow"/"color:yellow"/' $argv; sed -i 's/"color:orange"/"color:orange"/' $argv; sed -i 's/"color:olive"/"color:olive"/' $argv; sed -i 's/"color:gray"/"color:gray"/' $argv; sed -i 's/"color:purple"/"color:purple"/' $argv;
end
The s command in sed language is a shorthand for substitute. s commands are of the form
's/find/replace/'
With the s command, sed queries a text file for the presence of a particular string, or regular expression, and replaces what is found with user defined text. In the above example, classes of HTML elements are restyled with colour to highlight different parts of the script. Represented as a table, the changes are:
find | replace |
<pre | <div |
</pre> | </div> |
class= | style= |
"src src-[[:alpha:]]*" | "font-family: Monospace; background-color: #dbe6f0" |
"org-comment-delimiter" | "color:teal" |
"org-comment" | "color:teal" |
"org-keyword" | "color:green" |
"org-function-name" | "color:yellow" |
"org-variable-name" | "color:orange" |
"org-string" | "color:olive" |
"org-constant" | "color:gray" |
"org-builtin" | "color:purple" |
higlight_all.fish
function highlight_all -d "Call the highlight command several times" -a file
set -l options (fish_opt -s h -l help) argparse --name='highlight_all' 'h/help' -- $argv; for n in (seq 1 5); highlight $argv; end
end
This command calls highlight multiple times to ensure the script runs to completion. I could improve this such that only one script is required.