diff options
Diffstat (limited to 'docs/index.xml')
-rw-r--r-- | docs/index.xml | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/docs/index.xml b/docs/index.xml index 9245aba..f8b2aeb 100644 --- a/docs/index.xml +++ b/docs/index.xml | |||
@@ -12,6 +12,204 @@ | |||
12 | <language>en-us</language> | 12 | <language>en-us</language> |
13 | <copyright>Creative Commons BY-NC-SA 4.0</copyright> | 13 | <copyright>Creative Commons BY-NC-SA 4.0</copyright> |
14 | <item> | 14 | <item> |
15 | <title>OSC-52</title> | ||
16 | <description><p>I use <code>ssh</code> a lot. Copying text from the remote machine to | ||
17 | the host machine always sucked. But OSC-52 makes that easy.</p> | ||
18 | <p>OSC-52 is an ANSI escape sequence to write text to the terminal | ||
19 | emulator. The terminal emulator, if it understands what is going on, | ||
20 | will in turn write this text to the system clipboard.</p> | ||
21 | <p>What this means is some <code>printf</code> magic can send text to | ||
22 | your clipboard. I store this one-liner in a script called | ||
23 | <code>oclip</code>:</p> | ||
24 | <div class="sourceCode" id="cb1"><pre | ||
25 | class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="bu">printf</span> <span class="st">&quot;\033]52;c;%s\007&quot;</span> <span class="st">&quot;</span><span class="va">$(</span><span class="fu">base64</span> <span class="op">&lt;&amp;</span><span class="dv">0</span><span class="va">)</span><span class="st">&quot;</span></span></code></pre></div> | ||
26 | <p>and I run it with:</p> | ||
27 | <div class="sourceCode" id="cb2"><pre | ||
28 | class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ex">remote</span> $ cat some_file.txt <span class="kw">|</span> <span class="ex">oclip</span></span> | ||
29 | <span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a></span> | ||
30 | <span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="co"># some_file.txt&#39;s contents are now the host&#39;s clipboard</span></span></code></pre></div> | ||
31 | <h3 id="the-catch">The catch</h3> | ||
32 | <p>Your terminal emulator must support OSC-52, <code>alacritty</code> | ||
33 | and <code>termux</code> seem to support this out of the box. In | ||
34 | <code>st</code>, OSC-52 works with this change to | ||
35 | <code>config.h</code>:</p> | ||
36 | <pre><code>int allowwindowops = 1;</code></pre> | ||
37 | <p>If you are using <code>tmux</code>, you need to flip this switch | ||
38 | on:</p> | ||
39 | <pre><code>set -s set-clipboard on</code></pre> | ||
40 | <p>If you are inside <code>nvim</code>, it may work as expected as long | ||
41 | as <code>$SSH_TTY</code> is set. I sometimes physically start a session, | ||
42 | and <code>ssh</code> into the same session later from another machine, | ||
43 | and <code>$SSH_TTY</code> remains unset, so I force OSC-52 in | ||
44 | <code>nvim</code> at all times (see <a | ||
45 | href="https://neovim.io/doc/user/provider.html#clipboard-osc52">nvimdoc</a>):</p> | ||
46 | <div class="sourceCode" id="cb5"><pre | ||
47 | class="sourceCode lua"><code class="sourceCode lua"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="va">vim</span><span class="op">.</span><span class="va">g</span><span class="op">.</span><span class="va">clipboard</span> <span class="op">=</span> <span class="op">{</span></span> | ||
48 | <span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="va">name</span> <span class="op">=</span> <span class="st">&#39;OSC 52&#39;</span><span class="op">,</span></span> | ||
49 | <span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="va">copy</span> <span class="op">=</span> <span class="op">{</span></span> | ||
50 | <span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> <span class="op">[</span><span class="st">&#39;+&#39;</span><span class="op">]</span> <span class="op">=</span> <span class="fu">require</span><span class="op">(</span><span class="st">&#39;vim.ui.clipboard.osc52&#39;</span><span class="op">).</span>copy<span class="op">(</span><span class="st">&#39;+&#39;</span><span class="op">),</span></span> | ||
51 | <span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> <span class="op">[</span><span class="st">&#39;*&#39;</span><span class="op">]</span> <span class="op">=</span> <span class="fu">require</span><span class="op">(</span><span class="st">&#39;vim.ui.clipboard.osc52&#39;</span><span class="op">).</span>copy<span class="op">(</span><span class="st">&#39;*&#39;</span><span class="op">),</span></span> | ||
52 | <span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="op">},</span></span> | ||
53 | <span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> <span class="va">paste</span> <span class="op">=</span> <span class="op">{</span></span> | ||
54 | <span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> <span class="op">[</span><span class="st">&#39;+&#39;</span><span class="op">]</span> <span class="op">=</span> <span class="fu">require</span><span class="op">(</span><span class="st">&#39;vim.ui.clipboard.osc52&#39;</span><span class="op">).</span>paste<span class="op">(</span><span class="st">&#39;+&#39;</span><span class="op">),</span></span> | ||
55 | <span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="op">[</span><span class="st">&#39;*&#39;</span><span class="op">]</span> <span class="op">=</span> <span class="fu">require</span><span class="op">(</span><span class="st">&#39;vim.ui.clipboard.osc52&#39;</span><span class="op">).</span>paste<span class="op">(</span><span class="st">&#39;*&#39;</span><span class="op">),</span></span> | ||
56 | <span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="op">},</span></span> | ||
57 | <span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a><span class="op">}</span></span></code></pre></div> | ||
58 | <p>If you are inside <code>nvim</code> inside <code>tmux</code> inside | ||
59 | an <code>ssh</code> session inside <code>st</code>, you neeed all of the | ||
60 | above tweaks. <code>nvim</code> will pass the contents around to | ||
61 | <code>tmux</code>, which in turn will pass the contents to | ||
62 | <code>st</code>, which should pass it to your system clipboard.</p></description> | ||
63 | <link>https://peppe.rs/posts/OSC-52/</link> | ||
64 | <pubDate>Wed, 27 Nov 2024 22:56:00 +0000</pubDate> | ||
65 | <guid>https://peppe.rs/posts/OSC-52/</guid> | ||
66 | </item> | ||
67 | <item> | ||
68 | <title>Introducing Tablespoon</title> | ||
69 | <description><p><a href="https://git.peppe.rs/languages/tbsp">tbsp</a> (tree-based | ||
70 | source-processing language) is an awk-like language that operates on | ||
71 | tree-sitter syntax trees. To motivate the need for such a program, we | ||
72 | could begin by writing a markdown-to-html converter using | ||
73 | <code>tbsp</code> and <a | ||
74 | href="https://github.com/tree-sitter-grammars/tree-sitter-markdown">tree-sitter-md</a>. | ||
75 | We need some markdown to begin with:</p> | ||
76 | <pre><code># 1 heading | ||
77 | |||
78 | content of first paragraph | ||
79 | |||
80 | ## 1.1 heading | ||
81 | |||
82 | content of nested paragraph</code></pre> | ||
83 | <p>For future reference, this markdown is parsed like so by | ||
84 | tree-sitter-md (visualization generated by <a | ||
85 | href="https://git.peppe.rs/cli/tree-viz">tree-viz</a>):</p> | ||
86 | <pre><code>document | ||
87 | | section | ||
88 | | | atx_heading | ||
89 | | | | atx_h1_marker &quot;#&quot; | ||
90 | | | | heading_content inline &quot;1 heading&quot; | ||
91 | | | paragraph | ||
92 | | | | inline &quot;content of first paragraph&quot; | ||
93 | | | section | ||
94 | | | | atx_heading | ||
95 | | | | | atx_h2_marker &quot;##&quot; | ||
96 | | | | | heading_content inline &quot;1.1 heading&quot; | ||
97 | | | | paragraph | ||
98 | | | | | inline &quot;content of nested paragraph&quot;</code></pre> | ||
99 | <p>Onto the converter itself. Every <code>tbsp</code> program is written | ||
100 | as a collection of stanzas. Typically, we start with a stanza like | ||
101 | so:</p> | ||
102 | <pre><code>BEGIN { | ||
103 | int depth = 0; | ||
104 | |||
105 | print(&quot;&lt;html&gt;\n&quot;); | ||
106 | print(&quot;&lt;body&gt;\n&quot;); | ||
107 | }</code></pre> | ||
108 | <p>The stanza begins with a “pattern”, in this case, <code>BEGIN</code>, | ||
109 | and is followed a block of code. This block specifically, is executed | ||
110 | right at the beginning, before traversing the parse tree. In this | ||
111 | stanza, we set a “depth” variable to keep track of nesting of markdown | ||
112 | headers, and begin our html document by printing the | ||
113 | <code>&lt;html&gt;</code> and <code>&lt;body&gt;</code> tags.</p> | ||
114 | <p>We can follow this stanza with an <code>END</code> stanza, that is | ||
115 | executed after the traversal:</p> | ||
116 | <pre><code>END { | ||
117 | print(&quot;&lt;/body&gt;\n&quot;); | ||
118 | print(&quot;&lt;/html&gt;\n&quot;); | ||
119 | }</code></pre> | ||
120 | <p>In this stanza, we close off the tags we opened at the start of the | ||
121 | document. We can move onto the interesting bits of the conversion | ||
122 | now:</p> | ||
123 | <pre><code>enter section { | ||
124 | depth += 1; | ||
125 | } | ||
126 | leave section { | ||
127 | depth -= 1; | ||
128 | }</code></pre> | ||
129 | <p>The above stanzas begin with <code>enter</code> and | ||
130 | <code>leave</code> clauses, followed by the name of a tree-sitter node | ||
131 | kind: <code>section</code>. The <code>section</code> identifier is | ||
132 | visible in the tree-visualization above, it encompasses a | ||
133 | markdown-section, and is created for every markdown header. To | ||
134 | understand how <code>tbsp</code> executes above stanzas:</p> | ||
135 | <pre><code>document ... depth = 0 | ||
136 | | section &lt;-------- enter section (1) ... depth = 1 | ||
137 | | | atx_heading | ||
138 | | | | inline | ||
139 | | | paragraph | ||
140 | | | | inline | ||
141 | | | section &lt;----- enter section (2) ... depth = 2 | ||
142 | | | | atx_heading | ||
143 | | | | | inline | ||
144 | | | | paragraph | ||
145 | | | | | inline | ||
146 | | | | &lt;----------- leave section (2) ... depth = 1 | ||
147 | | | &lt;-------------- leave section (1) ... depth = 0 </code></pre> | ||
148 | <p>The following stanzas should be self-explanatory now:</p> | ||
149 | <pre><code>enter atx_heading { | ||
150 | print(&quot;&lt;h&quot;); | ||
151 | print(depth); | ||
152 | print(&quot;&gt;&quot;); | ||
153 | } | ||
154 | leave atx_heading { | ||
155 | print(&quot;&lt;/h&quot;); | ||
156 | print(depth); | ||
157 | print(&quot;&gt;\n&quot;); | ||
158 | } | ||
159 | |||
160 | enter inline { | ||
161 | print(text(node)); | ||
162 | }</code></pre> | ||
163 | <p>But an explanation is included nonetheless:</p> | ||
164 | <pre><code>document ... depth = 0 | ||
165 | | section &lt;-------- enter section (1) ... depth = 1 | ||
166 | | | atx_heading &lt;- enter atx_heading ... print &quot;&lt;h1&gt;&quot; | ||
167 | | | | inline &lt;--- enter inline ... print .. | ||
168 | | | | &lt;----------- leave atx_heading ... print &quot;&lt;/h1&gt;&quot; | ||
169 | | | paragraph | ||
170 | | | | inline &lt;--- enter inline ... print .. | ||
171 | | | section &lt;----- enter section (2) ... depth = 2 | ||
172 | | | | atx_heading enter atx_heading ... print &quot;&lt;h2&gt;&quot; | ||
173 | | | | | inline &lt;- enter inline ... print .. | ||
174 | | | | | &lt;-------- leave atx_heading ... print &quot;&lt;/h2&gt;&quot; | ||
175 | | | | paragraph | ||
176 | | | | | inline &lt;- enter inline ... print .. | ||
177 | | | | &lt;----------- leave section (2) ... depth = 1 | ||
178 | | | &lt;-------------- leave section (1) ... depth = 0 </code></pre> | ||
179 | <p>The <a | ||
180 | href="https://git.peppe.rs/languages/tbsp/tree/examples">examples</a> | ||
181 | directory contains a complete markdown-to-html converter, along with a | ||
182 | few other motivating examples.</p> | ||
183 | <h3 id="usage">Usage</h3> | ||
184 | <p>The <code>tbsp</code> evaluator is written in rust, use cargo to | ||
185 | build and run:</p> | ||
186 | <pre><code>cargo build --release | ||
187 | ./target/release/tbsp --help</code></pre> | ||
188 | <p><code>tbsp</code> requires three inputs:</p> | ||
189 | <ul> | ||
190 | <li>a <code>tbsp</code> program, referred to as “program file”</li> | ||
191 | <li>a language</li> | ||
192 | <li>an input file or some input text at stdin</li> | ||
193 | </ul> | ||
194 | <p>You can run the interpreter like so (this program prints an overview | ||
195 | of a rust file):</p> | ||
196 | <pre><code>$ ./target/release/tbsp \ | ||
197 | -f./examples/code-overview/overview.tbsp \ | ||
198 | -l rust \ | ||
199 | src/main.rs | ||
200 | module | ||
201 | └╴struct Cli | ||
202 | └╴trait Cli | ||
203 | └╴fn program | ||
204 | └╴fn language | ||
205 | └╴fn file | ||
206 | └╴fn try_consume_stdin | ||
207 | └╴fn main</code></pre></description> | ||
208 | <link>https://peppe.rs/posts/introducing_tablespoon/</link> | ||
209 | <pubDate>Thu, 01 Aug 2024 19:18:00 +0000</pubDate> | ||
210 | <guid>https://peppe.rs/posts/introducing_tablespoon/</guid> | ||
211 | </item> | ||
212 | <item> | ||
15 | <title>Snip Snap</title> | 213 | <title>Snip Snap</title> |
16 | <description><p>I regularly switch between exactly two things while working, a | 214 | <description><p>I regularly switch between exactly two things while working, a |
17 | “current” and an “alternate” item; a lot of tools I use seem to support | 215 | “current” and an “alternate” item; a lot of tools I use seem to support |