Choly's Blog https://choly.ca/ Recent content on Choly's Blog Hugo en-us Tue, 02 Sep 2025 08:40:07 -0400 Go: Defer Blocks (idea) https://choly.ca/post/go-defer-blocks/ Tue, 02 Sep 2025 08:40:07 -0400 https://choly.ca/post/go-defer-blocks/ <p>This is an idea that I&rsquo;ve had for a while. It&rsquo;s an extension of <code>defer</code> that behaves similar to <a href="https://go.googlesource.com/proposal/+/master/design/go2draft-error-handling-overview.md">handle</a>. I&rsquo;m not sure if it&rsquo;s original, but I thought it was worth writing down.</p> <h3 id="syntax">Syntax</h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">defer</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#8f5902;font-style:italic">// Code runs at function exit</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">defer</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">_</span> <span style="color:#204a87;font-weight:bold">string</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> <span style="color:#204a87;font-weight:bold">error</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#4e9a06">&#34;&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">fmt</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Errorf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;wrapped: %w&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span> </span></span></code></pre></div><h3 id="semantics">Semantics</h3> <ul> <li>A <code>return</code> inside a defer block overrides earlier return values.</li> <li>Optionally binds return values for inspection/modification.</li> </ul> <h3 id="example">Example</h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">func</span> <span style="color:#000">CopyFile</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">dst</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">src</span> <span style="color:#204a87;font-weight:bold">string</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">error</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">defer</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">err</span> <span style="color:#204a87;font-weight:bold">error</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">fmt</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Errorf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;failed to copy %s to %w: %w&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">src</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">dst</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> <span style="color:#000">in</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">os</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Open</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">src</span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">err</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">defer</span> <span style="color:#000">in</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Close</span><span style="color:#000;font-weight:bold">()</span> </span></span><span style="display:flex;"><span> <span style="color:#000">out</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">try</span> <span style="color:#000">os</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Create</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">dst</span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">err</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">defer</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">err</span> <span style="color:#204a87;font-weight:bold">error</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#000">_</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">os</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Remove</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">dst</span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">_</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">io</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Copy</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">out</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">in</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">err</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">out</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Close</span><span style="color:#000;font-weight:bold">()</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span> </span></span></code></pre></div> Go: Blank Lines and Error Handling Syntax https://choly.ca/post/go-blank-lines-error-handling/ Tue, 02 Sep 2025 08:17:34 -0400 https://choly.ca/post/go-blank-lines-error-handling/ <p>When writing Go, I generally don&rsquo;t include a lot of blank lines. Here&rsquo;s an example (ignore the lack of error wrapping/annotation):</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">func</span> <span style="color:#000">zipmod</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">dir</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">modpath</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">version</span> <span style="color:#204a87;font-weight:bold">string</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">([]</span><span style="color:#204a87;font-weight:bold">byte</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">error</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">var</span> <span style="color:#000">buf</span> <span style="color:#000">bytes</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Buffer</span> </span></span><span style="display:flex;"><span> <span style="color:#000">zw</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">zip</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">NewWriter</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">buf</span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">filepath</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">WalkDir</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">dir</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">func</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">path</span> <span style="color:#204a87;font-weight:bold">string</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">d</span> <span style="color:#000">fs</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">DirEntry</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> <span style="color:#204a87;font-weight:bold">error</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">error</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">err</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">d</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">IsDir</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87;font-weight:bold">nil</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> <span style="color:#000">relpath</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">filepath</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Rel</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">dir</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">path</span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">err</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> <span style="color:#8f5902;font-style:italic">// Create zip entry with module@version prefix</span> </span></span><span style="display:flex;"><span> <span style="color:#000">dst</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">zw</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Create</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">modpath</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#4e9a06">&#34;@&#34;</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">version</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#4e9a06">&#34;/&#34;</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">filepath</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">ToSlash</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">relpath</span><span style="color:#000;font-weight:bold">))</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">err</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> <span style="color:#000">src</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">os</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Open</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">path</span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">err</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">defer</span> <span style="color:#000">src</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Close</span><span style="color:#000;font-weight:bold">()</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">_</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">io</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Copy</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">dst</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">src</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">err</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">src</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Close</span><span style="color:#000;font-weight:bold">()</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">})</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87;font-weight:bold">nil</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">zw</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Close</span><span style="color:#000;font-weight:bold">();</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#204a87;font-weight:bold">nil</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">buf</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Bytes</span><span style="color:#000;font-weight:bold">(),</span> <span style="color:#204a87;font-weight:bold">nil</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span> </span></span></code></pre></div><p>The <code>if err != nil</code> blocks already serve the purpose of visually splitting up the operations, so adding additional whitespace feels redundant. Now let&rsquo;s imagine Go had a <code>try</code> keyword for error checking.</p> Agent Recursion https://choly.ca/post/agent-recursion/ Sun, 18 May 2025 16:31:05 -0400 https://choly.ca/post/agent-recursion/ <p>When AI agents start using tools, the context window fills up fast. Each <code>run_command</code> or <code>read_file</code> tool call dumps a lot of text that quickly loses relevance in subsequent steps.</p> <p>I&rsquo;ve been experimenting with the idea of &ldquo;recursive agents&rdquo; to mitigate this issue. The idea is simple: <strong>An agent can kick off new instances of itself to handle specific subtasks</strong>.</p> <p>The key part is that, when a child agent finishes, it doesn’t send its entire operational log or full tool output back to the parent; instead, it returns only a brief summary of the results.</p> Refactoring as Discovery https://choly.ca/post/refactor-search/ Fri, 02 May 2025 09:25:37 -0400 https://choly.ca/post/refactor-search/ <p>Refactoring usually falls into two modes:</p> <ul> <li><strong>Small, local changes</strong> that make later improvements possible</li> <li><strong>Large, structural changes</strong> that improve the overall design</li> </ul> <p>Before the big changes can happen, you often need to do a lot of small, mechanical edits. These help clean things up, but they also serve as a way to re-learn the codebase. What developers often won’t admit is that they usually <em>don’t know</em> what the big change is going to be when they start. They just know the code isn’t right.</p> Semgrep: AutoFixes using LLMs https://choly.ca/post/semgrep-autofix-llm/ Sun, 24 Mar 2024 12:00:00 -0500 https://choly.ca/post/semgrep-autofix-llm/ <h3 id="semgrep">Semgrep:</h3> <p>Semgrep is an incredible tool that allows you to search code by matching against the Abstract Syntax Tree (AST). For instance, if you want to find all method calls named <code>get_foo</code>, you can write a pattern like this:</p> <pre tabindex="0"><code>$A.get_foo(...) </code></pre><p>Test your own patterns using the playground: <a href="https://semgrep.dev/playground/new">https://semgrep.dev/playground/new</a></p> <p>While there are other tools like this, semgrep is currently the most capable:</p> <ul> <li><a href="https://ast-grep.github.io/">https://ast-grep.github.io/</a></li> <li><a href="https://codeql.github.com/">https://codeql.github.com/</a></li> <li><a href="https://comby.dev/">https://comby.dev/</a></li> </ul> <h3 id="autofix">AutoFix:</h3> <p>Semgrep not only searches using patterns but also supports rewriting the matches. Here&rsquo;s a simple rule definition from their documentation:</p> Git: programmatic staging https://choly.ca/post/git-programmatic-staging/ Fri, 01 Mar 2024 11:57:11 -0400 https://choly.ca/post/git-programmatic-staging/ <p>In the past year, I’ve been using a lot of tools to automatically rewrite/refactor code. These include <a href="https://semgrep.dev/">semgrep</a>, <a href="https://ast-grep.github.io/">ast-grep</a>, LLMs, and one-off scripts. After running these tools on a large code-base, you usually end up with lots of additional unintended changes. These range from formatting/whitespace to unrequested modifications by LLMs.</p> <p>The subsequent &ldquo;cleanup&rdquo; step is a very manual and tedious process. I’m essentially running <code>git add -p</code> and staging hunks one at a time. At times it feels like this step offsets the productivity gain from the rewrite tool itself.</p> 3D Printing https://choly.ca/post/3d-printing/ Sat, 09 Dec 2023 09:11:00 -0500 https://choly.ca/post/3d-printing/ <p>About a year ago, I purchased a BambuLab P1P 3D printer for about $1000 CAD (after taxes and shipping). This is something I’ve wanted to do on-and-off for the past 10 years and I finally decided to pull the trigger.</p> <p>For my first few projects, I decided to print existing STL models I found on the internet instead of designing my own. This was a good idea at first, but eventually led me down the (very expensive) rabbit hole of 3D printed RC (Radio Control) vehicles. This was not ideal because it resulted in learning (and paying for) two separate hobbies at the same time: 3D printing + RC. The cost and complexity of the project was off-putting and the printer sat idle for a few months after that.</p> AWS X-Ray: Force Sample https://choly.ca/post/x-ray-force-sample/ Tue, 17 Jan 2023 12:44:05 -0500 https://choly.ca/post/x-ray-force-sample/ <h3 id="background">Background</h3> <p>At my current workplace, we use X-Ray configured with a sample rate of 0.01. This means that a random 1% of requests will be traced. The low rate is great at keeping costs down, but it&rsquo;s not useful for debugging specific failed requests. Fortunately, you can force X-Ray to sample your request by generating a trace id, and setting the <code>X-Amzn-Trace-Id</code> header.</p> <h3 id="generating-trace-ids">Generating trace IDs</h3> <p>The id format is documented <a href="https://docs.aws.amazon.com/jjxray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids">here</a> along with a Python code sample.</p> Debugging a flaky Go test with Mozilla rr https://choly.ca/post/debugging-go-with-rr/ Mon, 22 Feb 2021 12:48:39 -0500 https://choly.ca/post/debugging-go-with-rr/ <blockquote> <p>This is how you debug a test that only fails once every 1000 times.</p></blockquote> <h2 id="the-test">The Test</h2> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">package</span> <span style="color:#000">my</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000;font-weight:bold">(</span> </span></span><span style="display:flex;"><span> <span style="color:#4e9a06">&#34;math/rand&#34;</span> </span></span><span style="display:flex;"><span> <span style="color:#4e9a06">&#34;testing&#34;</span> </span></span><span style="display:flex;"><span> <span style="color:#4e9a06">&#34;time&#34;</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">func</span> <span style="color:#000">init</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#000">rand</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Seed</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">time</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Now</span><span style="color:#000;font-weight:bold">().</span><span style="color:#000">UnixNano</span><span style="color:#000;font-weight:bold">())</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">func</span> <span style="color:#000">TestRandFail</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">t</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">testing</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">T</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">n</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">rand</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Intn</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">1000</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#000">n</span> <span style="color:#ce5c00;font-weight:bold">==</span> <span style="color:#0000cf;font-weight:bold">50</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#000">t</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Fatalf</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;finally got %d&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">n</span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span> </span></span></code></pre></div><ul> <li>This is obviously a pedagogical example.</li> </ul> <h2 id="get-the-newest-version-of-rr">Get the newest version of <a href="https://github.com/rr-debugger/rr">rr</a></h2> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>git clone https://github.com/rr-debugger/rr.git </span></span><span style="display:flex;"><span><span style="color:#204a87">cd</span> rr </span></span><span style="display:flex;"><span>git checkout 5.4.0 <span style="color:#8f5902;font-style:italic"># change this to the latest release (DO NOT BUILD HEAD)</span> </span></span><span style="display:flex;"><span>mkdir build </span></span><span style="display:flex;"><span><span style="color:#204a87">cd</span> build </span></span><span style="display:flex;"><span>cmake .. </span></span><span style="display:flex;"><span>make -j8 </span></span><span style="display:flex;"><span>sudo make install </span></span></code></pre></div><ul> <li><strong>Warning</strong>: <code>rr</code> <a href="https://github.com/rr-debugger/rr/wiki/Building-And-Installing#virtual-machine-guests">does not work in VirtualBox</a>.</li> <li><a href="https://github.com/rr-debugger/rr/wiki/Building-And-Installing">https://github.com/rr-debugger/rr/wiki/Building-And-Installing</a></li> </ul> <h2 id="compile-your-failing-test-to-a-binary">Compile your failing test to a binary</h2> <pre tabindex="0"><code>go test -gcflags &#39;all=-N -l&#39; -c </code></pre><ul> <li>The <code>-gcflags 'all=-N -l'</code> disables optimizations and inlining.</li> </ul> <h2 id="install-the-following-rrloop-script">Install the following <code>rrloop</code> script.</h2> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">#!/bin/sh </span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">while</span> : </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">do</span> </span></span><span style="display:flex;"><span> rr <span style="color:#000">$@</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#ce5c00;font-weight:bold">[</span> <span style="color:#000">$?</span> -ne <span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#ce5c00;font-weight:bold">]</span><span style="color:#000;font-weight:bold">;</span> <span style="color:#204a87;font-weight:bold">then</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87">echo</span> <span style="color:#4e9a06">&#34;encountered non-zero exit code: </span><span style="color:#000">$?</span><span style="color:#4e9a06">&#34;</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span> exit<span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">fi</span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">done</span> </span></span></code></pre></div><ul> <li><code>rrloop</code> is a wrapper around <code>rr</code> which keeps looping until it sees a non-zero exit code.</li> <li>This works because <code>rr</code> exits using the recorded process&rsquo; exit code.</li> </ul> <h2 id="record-the-test-execution-in-a-loop-until-it-fails">Record the test execution in a loop until it fails</h2> <pre tabindex="0"><code>echo -1 | sudo tee -a /proc/sys/kernel/perf_event_paranoid echo 0 | sudo tee -a /proc/sys/kernel/kptr_restrict rrloop record ./my.test </code></pre><ul> <li>If you have multiple tests, you can pass <code>-test.run=TestRandFail</code> to only run a specific one.</li> <li>The <code>--chaos</code> flag often <a href="https://robert.ocallahan.org/2016/02/introducing-rr-chaos-mode.html">increases the chances of the failure</a>.</li> </ul> <p>Example Output:</p> Merging Multiple Git Repositories Into A MonoRepo https://choly.ca/post/git-merge-to-monorepo/ Mon, 13 Aug 2018 12:04:56 -0400 https://choly.ca/post/git-merge-to-monorepo/ <p><strong>Note:</strong> Replace <code>thing</code> with your own repo name in the examples.</p> <h3 id="1-create-a-repository-which-will-store-all-your-code">1. Create a repository which will store all your code</h3> <pre tabindex="0"><code>mkdir monorepo &amp;&amp; cd monorepo git init . echo &#34;# MonoRepo&#34; &gt; README.md git add . git commit -m &#34;first commit&#34; </code></pre><h3 id="2-clone-one-of-the-existing-repositories-to-a-temporary-location">2. Clone one of the existing repositories to a temporary location</h3> <p>Example Remote: <code>ssh://[email protected]/thing.git</code></p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>mkdir /tmp/thing </span></span><span style="display:flex;"><span>git clone ssh://[email protected]/thing.git /tmp/thing </span></span><span style="display:flex;"><span><span style="color:#204a87">cd</span> /tmp/thing </span></span></code></pre></div><h3 id="3-use-git-filter-branch-to-rewrite-the-history-into-a-sub-directory">3. Use <a href="https://git-scm.com/docs/git-filter-branch">git filter-branch</a> to rewrite the history into a sub-directory.</h3> <p><strong>Note:</strong> this step can take a very long time</p> Go: Composable http.Handler https://choly.ca/post/go-experiments-with-handler/ Mon, 19 Feb 2018 12:17:21 -0400 https://choly.ca/post/go-experiments-with-handler/ <p>When using <code>net/http</code>, handling errors is kinda annoying.</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#000">http</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">HandleFunc</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;/foo&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">func</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">w</span> <span style="color:#000">http</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">ResponseWriter</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">r</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">http</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Request</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#000">thing</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">storage</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;thing&#34;</span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#000">http</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Error</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">w</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Error</span><span style="color:#000;font-weight:bold">(),</span> <span style="color:#0000cf;font-weight:bold">500</span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> <span style="color:#000">_</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">json</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">NewEncoder</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">w</span><span style="color:#000;font-weight:bold">).</span><span style="color:#000">Encode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">thing</span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">})</span> </span></span></code></pre></div><p>Ideally, I could just return an error from the handler. Let&rsquo;s create a type to let that happen.</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">type</span> <span style="color:#000">MyHandlerFunc</span> <span style="color:#204a87;font-weight:bold">func</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">w</span> <span style="color:#000">http</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">ResponseWriter</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">r</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">http</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Request</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">error</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">func</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">f</span> <span style="color:#000">MyHandlerFunc</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">ServeHTTP</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">w</span> <span style="color:#000">http</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">ResponseWriter</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">r</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">http</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Request</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">f</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">w</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">r</span><span style="color:#000;font-weight:bold">);</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#000">http</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Error</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">w</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Error</span><span style="color:#000;font-weight:bold">(),</span> <span style="color:#0000cf;font-weight:bold">500</span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span> </span></span></code></pre></div><p>Here&rsquo;s the previous code refactored to use the new handler.</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#000">http</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Handle</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;/foo&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">MyHandlerFunc</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">func</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">w</span> <span style="color:#000">http</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">ResponseWriter</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">r</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">http</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Request</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">error</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#000">thing</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">:=</span> <span style="color:#000">storage</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;thing&#34;</span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000">err</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#204a87;font-weight:bold">nil</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">err</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">json</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">NewEncoder</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">w</span><span style="color:#000;font-weight:bold">).</span><span style="color:#000">Encode</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">thing</span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}))</span> </span></span></code></pre></div><p>Better, but what if we want to control the error status code? There could be a special <code>error</code> type that <code>MyHandlerFunc</code> checks for.</p> Angular Events https://choly.ca/post/angular-events/ Wed, 23 Mar 2016 10:06:22 -0400 https://choly.ca/post/angular-events/ <p>I&rsquo;ve been trying to find an elegant way of dealing with events in AngularJS recently. If you&rsquo;re not farmiliar with Angular, that&rsquo;s ok, this is a pretty common pattern.</p> <p>Here I have a controller that registers an event listener:</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-js" data-lang="js"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">function</span> <span style="color:#000">MyController</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">$rootScope</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#000">$rootScope</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">$on</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#39;event1&#39;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000;font-weight:bold">()</span> <span style="color:#000;font-weight:bold">=&gt;</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#000">console</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">log</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#39;event 1 occured&#39;</span><span style="color:#000;font-weight:bold">);</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">});</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span> </span></span></code></pre></div><p>There&rsquo;s an issue with this code. It doesn&rsquo;t unbind the listener when the controller (or its scope) is destroyed. Let&rsquo;s take care of this.</p> TypeScript: Working with JSON https://choly.ca/post/typescript-json/ Sat, 19 Mar 2016 15:41:44 -0400 https://choly.ca/post/typescript-json/ <p><strong>EDITS:</strong></p> <ul> <li>Calling <code>toString</code> on <code>Date</code> is for illustrative purposes.</li> <li>There&rsquo;s a full commented example at the end.</li> <li>Use <code>toJSON</code> method as suggested by <a href="http://choly.ca/post/typescript-json/#comment-2579762437">Schipperz</a>.</li> <li>Add <code>reviver</code> method as suggested by <a href="http://choly.ca/post/typescript-json/#comment-2579491209">Anders Ringqvist</a>.</li> </ul> <hr> <p>So you have a <code>User</code> type in your code.</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-ts" data-lang="ts"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">interface</span> <span style="color:#000">User</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#000">name</span>: <span style="color:#204a87;font-weight:bold">string</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span> <span style="color:#000">age</span>: <span style="color:#204a87;font-weight:bold">number</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span> <span style="color:#000">created</span>: <span style="color:#204a87;font-weight:bold">Date</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span> </span></span></code></pre></div><p>At some point you&rsquo;re going to want to encode this as JSON. This works as you&rsquo;d expect.</p> <pre tabindex="0"><code> &gt; JSON.stringify({ name: &#34;bob&#34;, age: 34, created: new Date() }); &#39;{&#34;name&#34;:&#34;bob&#34;,&#34;age&#34;:34,&#34;created&#34;:&#34;2016-03-19T18:15:12.710Z&#34;}&#39; </code></pre><p>The problem is that the <code>created</code> field is no longer a <code>Date</code> when you parse it back.</p> Custom JSON Marshalling in Go https://choly.ca/post/go-json-marshalling/ Fri, 10 Apr 2015 12:54:44 -0400 https://choly.ca/post/go-json-marshalling/ <p>Go&rsquo;s <code>encoding/json</code> package makes it really easy to marshal <code>struct</code>s to JSON data.</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">package</span> <span style="color:#000">main</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000;font-weight:bold">(</span> </span></span><span style="display:flex;"><span> <span style="color:#4e9a06">&#34;encoding/json&#34;</span> </span></span><span style="display:flex;"><span> <span style="color:#4e9a06">&#34;os&#34;</span> </span></span><span style="display:flex;"><span> <span style="color:#4e9a06">&#34;time&#34;</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">type</span> <span style="color:#000">MyUser</span> <span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#000">ID</span> <span style="color:#204a87;font-weight:bold">int64</span> <span style="color:#4e9a06">`json:&#34;id&#34;`</span> </span></span><span style="display:flex;"><span> <span style="color:#000">Name</span> <span style="color:#204a87;font-weight:bold">string</span> <span style="color:#4e9a06">`json:&#34;name&#34;`</span> </span></span><span style="display:flex;"><span> <span style="color:#000">LastSeen</span> <span style="color:#000">time</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Time</span> <span style="color:#4e9a06">`json:&#34;lastSeen&#34;`</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">func</span> <span style="color:#000">main</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#000">_</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">json</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">NewEncoder</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">os</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Stdout</span><span style="color:#000;font-weight:bold">).</span><span style="color:#000">Encode</span><span style="color:#000;font-weight:bold">(</span> </span></span><span style="display:flex;"><span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">MyUser</span><span style="color:#000;font-weight:bold">{</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;Ken&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">time</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Now</span><span style="color:#000;font-weight:bold">()},</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span> </span></span></code></pre></div><p>Output:</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">{</span><span style="color:#204a87;font-weight:bold">&#34;id&#34;</span><span style="color:#000;font-weight:bold">:</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">,</span><span style="color:#204a87;font-weight:bold">&#34;name&#34;</span><span style="color:#000;font-weight:bold">:</span><span style="color:#4e9a06">&#34;Ken&#34;</span><span style="color:#000;font-weight:bold">,</span><span style="color:#204a87;font-weight:bold">&#34;lastSeen&#34;</span><span style="color:#000;font-weight:bold">:</span><span style="color:#4e9a06">&#34;2009-11-10T23:00:00Z&#34;</span><span style="color:#000;font-weight:bold">}</span> </span></span></code></pre></div><p>But what if we want to change how one of the field values are displayed? For example, say I wanted <code>LastSeen</code> to be a unix timestamp.</p> <p>The simple solution is to introduce another auxiliary <code>struct</code> and populate it with the correctly formatted values in the <code>MarshalJSON</code> method.</p> TypeScript completion in Vim https://choly.ca/post/typescript-vim-completion/ Tue, 31 Mar 2015 10:17:00 -0400 https://choly.ca/post/typescript-vim-completion/ <p>One of the main advantages of using static types is that you get much better support from your tools. I recently got <a href="http://www.typescriptlang.org/">TypeScript</a> auto-completion working in vim and I&rsquo;m documenting how to do it here.</p> <h3 id="demo">Demo:</h3> <p><img src="https://choly.ca/images/typescript-vim-completion.gif" alt="demo"></p> <h3 id="1-install-tss">1. Install TSS</h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>git clone https://github.com/clausreinke/typescript-tools.git </span></span><span style="display:flex;"><span><span style="color:#204a87">cd</span> typescript-tools </span></span><span style="display:flex;"><span>git checkout testing_ts1.4 </span></span><span style="display:flex;"><span>sudo npm install -g </span></span></code></pre></div><h3 id="2-install-vim-plugin">2. Install Vim Plugin</h3> <p>I&rsquo;m using <a href="https://github.com/gmarik/Vundle.vim">Vundle</a> to manage my plugins.</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-vim" data-lang="vim"><span style="display:flex;"><span><span style="color:#000">Bundle</span> <span style="color:#4e9a06">&#34;icholy/typescript-tools.git&#34;</span> </span></span><span style="display:flex;"><span><span style="color:#000">au</span> <span style="color:#000">BufRead</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">BufNewFile</span> *.<span style="color:#000">ts</span> <span style="color:#000">setlocal</span> <span style="color:#204a87;font-weight:bold">filetype</span><span style="color:#000;font-weight:bold">=</span><span style="color:#000">typescript</span> </span></span></code></pre></div><h3 id="3-install-tsd">3. Install TSD</h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>sudo npm install tsd@next -g </span></span></code></pre></div><h3 id="4-create-project">4. Create Project</h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>mkdir project </span></span><span style="display:flex;"><span><span style="color:#204a87">cd</span> project </span></span><span style="display:flex;"><span>tsd init </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span>tsd install jquery --save </span></span><span style="display:flex;"><span>tsd install angularjs --save </span></span></code></pre></div><h3 id="5-create-tsconfigjson">5. Create <code>tsconfig.json</code></h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-json" data-lang="json"><span style="display:flex;"><span><span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&#34;compilerOptions&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&#34;target&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#4e9a06">&#34;es5&#34;</span><span style="color:#000;font-weight:bold">,</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&#34;noImplicitAny&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">false</span><span style="color:#000;font-weight:bold">,</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">},</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">&#34;files&#34;</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">[</span> </span></span><span style="display:flex;"><span> <span style="color:#4e9a06">&#34;typings/tsd.d.ts&#34;</span><span style="color:#000;font-weight:bold">,</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span> <span style="color:#4e9a06">&#34;all.ts&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;your.ts&#34;</span><span style="color:#000;font-weight:bold">,</span> </span></span><span style="display:flex;"><span> <span style="color:#4e9a06">&#34;other.ts&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#4e9a06">&#34;files.ts&#34;</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">]</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span> </span></span></code></pre></div><h3 id="6-start-tss-in-vim">6. Start TSS in vim</h3> <p>Make sure you&rsquo;re cwd is somewhere in the directory containing <code>tsconfig.json</code></p> interactive filtering with less https://choly.ca/post/interactive-filtering-with-less/ Mon, 31 Mar 2014 12:56:53 -0400 https://choly.ca/post/interactive-filtering-with-less/ <p>I discovered a cool little feature in <a href="http://linux.die.net/man/1/less">less</a> (not less.css) today. You can interactively filter the data.</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-man" data-lang="man"><span style="display:flex;"><span>&amp;pattern </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span> Display only lines which match the pattern; lines which do not match the pattern are not displayed. If pattern is empty (if you type &amp; immediately followed by ENTER), any filtering is turned off, and all lines are displayed. While filtering is in effect, an ampersand is displayed at the beginning of the prompt, as a reminder that some lines in the file may be hidden. </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span> Certain characters are special as in the / command: </span></span></code></pre></div><p><strong>Activate:</strong> <code>&amp;pattern</code> hit enter<br> <strong>Disable:</strong> <code>&amp;</code> hit enter</p> C++: Make Repl https://choly.ca/post/c-plus-plus-make-repl/ Mon, 31 Dec 2012 11:26:44 -0400 https://choly.ca/post/c-plus-plus-make-repl/ <p>One of the things I really like about dynamic languages like javascript &amp; python is the repl. After you’ve gotten used to that type of exploratory programming, it’s hard to go back to the edit/compile/run cycle.</p> <p>Luckily that has finally changed with <a href="http://root.cern.ch/drupal/content/cling">cling</a>. It’s an interactive C++ environment that behaves pretty much like a repl. In my recent projects I’ve been adding a new make rule: repl which lets me interactively play with the code I’m working on and it has drastically improved my productivity.</p> C++: Inline Functions https://choly.ca/post/c-plus-plus-inline-functions/ Mon, 31 Dec 2012 11:21:15 -0400 https://choly.ca/post/c-plus-plus-inline-functions/ <p>Even though overuse of getter and setter functions can be frowned upon, they can help a lot if you’re looking to provide a intuitive api. However the overhead the additional function call introduces is undesirable. Thankfully, there’s the <code>inline</code> keyword. It tells the compiler to replace each invocation of the function with the body of the function.</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">Foo</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">m_number</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#0000cf;font-weight:bold">123</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">inline</span> <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">number</span> <span style="color:#000;font-weight:bold">()</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">m_number</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">};</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">main</span> <span style="color:#000;font-weight:bold">()</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#000">Foo</span> <span style="color:#000">foo</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span> <span style="color:#8f5902;font-style:italic">// used like a regular function </span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">cout</span> <span style="color:#ce5c00;font-weight:bold">&lt;&lt;</span> <span style="color:#000">foo</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">number</span><span style="color:#000;font-weight:bold">()</span> <span style="color:#ce5c00;font-weight:bold">&lt;&lt;</span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">endl</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span> <span style="color:#8f5902;font-style:italic">// compiled to almost identical assembly as this </span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">cout</span> <span style="color:#ce5c00;font-weight:bold">&lt;&lt;</span> <span style="color:#000">foo</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">m_number</span> <span style="color:#ce5c00;font-weight:bold">&lt;&lt;</span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">endl</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span> </span></span></code></pre></div><p>However the <code>inline</code> keyword isn’t a guarantee that the compiler will do this. It’s more of a hint to the compiler. So keep in mind that the compiler is free to ignore the fact that a function is declared <code>inline</code> and it can inline functions that haven’t been delcared as such. But in examples similar to the one above, you can assume it will behave as expected.</p> Vim Marks https://choly.ca/post/vim-marks/ Fri, 07 Dec 2012 11:29:54 -0400 https://choly.ca/post/vim-marks/ <p>Marks are a feature that I’ve never really used enough. Hopefully writing about them will change that for the better.</p> <h3 id="make-a-basic-file-local-mark-called-a">Make a basic, file local, mark called <code>a</code></h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-vim" data-lang="vim"><span style="display:flex;"><span><span style="color:#000">ma</span> </span></span></code></pre></div><h3 id="jump-back-to-that-mark">Jump back to that mark</h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-vim" data-lang="vim"><span style="display:flex;"><span>&#39;<span style="color:#000">a</span> </span></span></code></pre></div><p>Now I try to be pragmatic. So use cases are what motivate me to learn new thing. I think that marks are a good replacement for a lot of the things I use visual line <code>V</code> mode for now.</p> C++ Extending Classes via the Stream Operator https://choly.ca/post/c-plus-plus-extending-classes-via-the-stream-operator/ Mon, 03 Dec 2012 11:49:42 -0400 https://choly.ca/post/c-plus-plus-extending-classes-via-the-stream-operator/ <h3 id="vision">Vision</h3> <p>Looking for a way to create a class which behaved like one of the <code>std::ostream</code> classes.</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span><span style="color:#000">MyClass</span> <span style="color:#000">obj</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#000">obj</span> <span style="color:#ce5c00;font-weight:bold">&lt;&lt;</span> <span style="color:#4e9a06">&#34;foo&#34;</span> <span style="color:#ce5c00;font-weight:bold">&lt;&lt;</span> <span style="color:#0000cf;font-weight:bold">123</span> <span style="color:#ce5c00;font-weight:bold">&lt;&lt;</span> <span style="color:#000">some_string</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">c_str</span><span style="color:#000;font-weight:bold">();</span> </span></span></code></pre></div><h3 id="problem">Problem</h3> <p>Implementing all those <code>operator&lt;&lt;</code> overloads would be redundant because something like <code>std::stringstream</code> already does it. However inheriting from <code>std::stringstream</code> is more complicated than it should be.</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">MyClass</span> <span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">stringstream</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#8f5902;font-style:italic">/* not that simple ... */</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">};</span> </span></span></code></pre></div><h3 id="solution">Solution</h3> <p>You can use a simple template to achive the desired effect.</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">MyClass</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">stringstream</span> <span style="color:#000">m_ss</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">template</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">T</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> </span></span><span style="display:flex;"><span> <span style="color:#000">MyClass</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#204a87;font-weight:bold">operator</span><span style="color:#ce5c00;font-weight:bold">&lt;&lt;</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">T</span> <span style="color:#204a87;font-weight:bold">const</span><span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000">rhs</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#000">m_ss</span> <span style="color:#ce5c00;font-weight:bold">&lt;&lt;</span> <span style="color:#000">rhs</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#204a87;font-weight:bold">this</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">}</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">};</span> </span></span></code></pre></div><p>This comes with the benefit being able to ‘hook’ into each invocation.</p> C++ Log4cxx vs Glog vs Boost.log vs Wrapper https://choly.ca/post/c-plus-plus-logging-log4cxx-vs-glog-vs-wrapper/ Mon, 03 Dec 2012 11:44:06 -0400 https://choly.ca/post/c-plus-plus-logging-log4cxx-vs-glog-vs-wrapper/ <p>It seems that logging in C++ isn’t a much discused topic when compared to a language like java. In a recent C++ project, I needed to add real logging support. Up till this point, the following was good enough (don’t judge).</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-cpp" data-lang="cpp"><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">#ifdef DEBUG </span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">cerr</span> <span style="color:#ce5c00;font-weight:bold">&lt;&lt;</span> <span style="color:#4e9a06">&#34;some error&#34;</span> <span style="color:#ce5c00;font-weight:bold">&lt;&lt;</span> <span style="color:#000">std</span><span style="color:#ce5c00;font-weight:bold">::</span><span style="color:#000">endl</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">#endif </span></span></span></code></pre></div><p>I started googling and the following to be the most popular and mature.</p> <h3 id="glog">glog</h3> <p><a href="http://code.google.com/p/google-glog/">homepage</a></p> <p>glog was my first choice because it’s the simplest one to set up and it has hardly any dependencies. The interface is also nice to use.</p> Libpq: PQexec Timeout https://choly.ca/post/libpq-pqexec-timeout/ Mon, 03 Dec 2012 11:40:51 -0400 https://choly.ca/post/libpq-pqexec-timeout/ <h3 id="1-establish-the-connection">1. Establish the connection</h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#000">PGconn</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">pg_conn</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">PQconnect</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;info&#34;</span><span style="color:#000;font-weight:bold">);</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// error check </span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">PQstatus</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">pg_conn</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#ce5c00;font-weight:bold">!=</span> <span style="color:#000">CONNECTION_OK</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">throw</span> <span style="color:#4e9a06">&#34;invalid connection&#34;</span><span style="color:#000;font-weight:bold">;</span> </span></span></code></pre></div><h3 id="2-grab-the-socket-file-descriptor">2. Grab the socket file descriptor</h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">socket_fd</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">PQsocket</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">pg_conn</span><span style="color:#000;font-weight:bold">);</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// error check </span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">socket_fd</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">throw</span> <span style="color:#4e9a06">&#34;invalid socket&#34;</span><span style="color:#000;font-weight:bold">;</span> </span></span></code></pre></div><h3 id="3-set-the-timeout">3. Set the timeout</h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// 5 second timeout </span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">struct</span> <span style="color:#000">timeval</span> <span style="color:#000">timeout</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000;font-weight:bold">{</span> <span style="color:#0000cf;font-weight:bold">5</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#000;font-weight:bold">};</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// recv timeout </span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">setopt_result_1</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">setsockopt</span><span style="color:#000;font-weight:bold">(</span> </span></span><span style="display:flex;"><span> <span style="color:#000">socket_fd</span><span style="color:#000;font-weight:bold">,</span> </span></span><span style="display:flex;"><span> <span style="color:#000">SOL_SOCKET</span><span style="color:#000;font-weight:bold">,</span> </span></span><span style="display:flex;"><span> <span style="color:#000">SO_RCVTIMEO</span><span style="color:#000;font-weight:bold">,</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">char</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">timeout</span><span style="color:#000;font-weight:bold">,</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">timeout</span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">);</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// send timeout </span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">setopt_result_2</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">setsockopt</span><span style="color:#000;font-weight:bold">(</span> </span></span><span style="display:flex;"><span> <span style="color:#000">socket_fd</span><span style="color:#000;font-weight:bold">,</span> </span></span><span style="display:flex;"><span> <span style="color:#000">SOL_SOCKET</span><span style="color:#000;font-weight:bold">,</span> </span></span><span style="display:flex;"><span> <span style="color:#000">SO_SNDTIMEO</span><span style="color:#000;font-weight:bold">,</span> </span></span><span style="display:flex;"><span> <span style="color:#000;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">char</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000;font-weight:bold">)</span><span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#000">timeout</span><span style="color:#000;font-weight:bold">,</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">sizeof</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">timeout</span><span style="color:#000;font-weight:bold">)</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">);</span> </span></span><span style="display:flex;"><span> </span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic">// error check </span></span></span><span style="display:flex;"><span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">setopt_result_1</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#0000cf;font-weight:bold">0</span> <span style="color:#ce5c00;font-weight:bold">||</span> <span style="color:#000">setopt_result_2</span> <span style="color:#ce5c00;font-weight:bold">&lt;</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">throw</span> <span style="color:#4e9a06">&#34;failed to set timeout&#34;</span><span style="color:#000;font-weight:bold">;</span> </span></span></code></pre></div> SWAPM: Code generation made easy. https://choly.ca/post/swapm-code-generation-made-easy/ Thu, 18 Oct 2012 12:02:06 -0400 https://choly.ca/post/swapm-code-generation-made-easy/ <p>I finally got around to reading the Pragmatic Programmer book. One thing that really interested me was the section on Code Generation. So in a recent C++ project I was interfacing with postgres and there was a LOT of code repetition. The sql query, class members, getters/setters, response parsing logic. They all contained the same information. Perfect I thought, here was the ideal chance to give code generation a shot. My first incarnation was a very ugly perl script (aren&rsquo;t they all .. ?) which used mustache as the templating engine. It worked, But I had to copy and paste the generated code into my source every time it changed which was a pain.</p> A week with Vim https://choly.ca/post/week-with-vim/ Tue, 16 Oct 2012 12:17:21 -0400 https://choly.ca/post/week-with-vim/ <p>During the past week I&rsquo;ve been learning to use Vim (gVim). Day 1 and 2 weren&rsquo;t fun to say the least. But now I&rsquo;m completely hooked. I&rsquo;m the type of person who will sit there for hours customizing my development environment until I think it&rsquo;s perfect. I&rsquo;ve been playing with almost every cool plugin i can find (and wasting a lot of time in the process).</p> <p><img src="https://choly.ca/images/vim-cheatsheet.png" alt="vim cheatsheet"></p> <p>So Vim is, without a doubt, the best text editor I&rsquo;ve ever used. However, that&rsquo;s all it is. It&rsquo;s just a text editor and I know a lot people don&rsquo;t agree with me on this one, but IDE&rsquo;s do help. They just make everything better. Therefore the ultimate combination would be an ide with vim as the text editor.</p> Ember.js with Brunch https://choly.ca/post/emberjs-with-brunch/ Thu, 07 Jun 2012 12:04:56 -0400 https://choly.ca/post/emberjs-with-brunch/ <p>I&rsquo;ve recently discovered the brilliant <a href="http://emberjs.com/">Ember.js</a> library and the first major issue I ran into was how to organize/modularize this thing!? At first I just opted into <a href="http://requirejs.org/">RequireJs</a> because that&rsquo;s what I know but I started hitting walls fast. I decided to try out the <a href="http://brunch.io/">Brunch</a> build system since I had heard good things about it before and this was a great opportunity to learn how to use it. Brunch uses skeletons which are essentially project templates to get rid of the boilerplate. Google search &ldquo;ember brunch&rdquo; and I found <a href="https://github.com/charlesjolley/ember-brunch">charlesjolley/ember-brunch</a> perfect!</p> CSS Compass Gradient Generator https://choly.ca/post/css-compass-gradient-generator/ Thu, 27 Oct 2011 12:10:52 -0400 https://choly.ca/post/css-compass-gradient-generator/ <p>This is a css gradient generator that i&rsquo;ve been using for a while:</p> <ul> <li><a href="http://www.colorzilla.com/gradient-editor/">http://www.colorzilla.com/gradient-editor/</a></li> </ul> <p><strong>CSS Output</strong></p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-css" data-lang="css"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">background</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#000;font-weight:bold">#</span><span style="color:#000">1e5799</span><span style="color:#ce5c00;font-weight:bold">;</span> <span style="color:#8f5902;font-style:italic">/* Old browsers */</span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">background</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">-moz-linear-gradient</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">top</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000;font-weight:bold">#</span><span style="color:#000">1e5799</span> <span style="color:#204a87;font-weight:bold">0</span><span style="color:#ce5c00;font-weight:bold">%,</span> <span style="color:#000;font-weight:bold">#</span><span style="color:#000">2989d8</span> <span style="color:#204a87;font-weight:bold">50</span><span style="color:#ce5c00;font-weight:bold">%,</span> <span style="color:#000;font-weight:bold">#</span><span style="color:#000">207cca</span> <span style="color:#204a87;font-weight:bold">51</span><span style="color:#ce5c00;font-weight:bold">%,</span> <span style="color:#000;font-weight:bold">#</span><span style="color:#000">7db9e8</span> <span style="color:#204a87;font-weight:bold">100</span><span style="color:#ce5c00;font-weight:bold">%);</span> <span style="color:#8f5902;font-style:italic">/* FF3.6+ */</span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">background</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">-webkit-gradient</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">linear</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">left</span> <span style="color:#204a87;font-weight:bold">top</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">left</span> <span style="color:#204a87;font-weight:bold">bottom</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">color-stop</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">0</span><span style="color:#ce5c00;font-weight:bold">%,</span><span style="color:#000;font-weight:bold">#</span><span style="color:#000">1e5799</span><span style="color:#ce5c00;font-weight:bold">),</span> <span style="color:#204a87;font-weight:bold">color-stop</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">50</span><span style="color:#ce5c00;font-weight:bold">%,</span><span style="color:#000;font-weight:bold">#</span><span style="color:#000">2989d8</span><span style="color:#ce5c00;font-weight:bold">),</span> <span style="color:#204a87;font-weight:bold">color-stop</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">51</span><span style="color:#ce5c00;font-weight:bold">%,</span><span style="color:#000;font-weight:bold">#</span><span style="color:#000">207cca</span><span style="color:#ce5c00;font-weight:bold">),</span> <span style="color:#204a87;font-weight:bold">color-stop</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">100</span><span style="color:#ce5c00;font-weight:bold">%,</span><span style="color:#000;font-weight:bold">#</span><span style="color:#000">7db9e8</span><span style="color:#ce5c00;font-weight:bold">));</span> <span style="color:#8f5902;font-style:italic">/* Chrome,Safari4+ */</span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">background</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">-webkit-linear-gradient</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">top</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000;font-weight:bold">#</span><span style="color:#000">1e5799</span> <span style="color:#204a87;font-weight:bold">0</span><span style="color:#ce5c00;font-weight:bold">%,</span><span style="color:#000;font-weight:bold">#</span><span style="color:#000">2989d8</span> <span style="color:#204a87;font-weight:bold">50</span><span style="color:#ce5c00;font-weight:bold">%,</span><span style="color:#000;font-weight:bold">#</span><span style="color:#000">207cca</span> <span style="color:#204a87;font-weight:bold">51</span><span style="color:#ce5c00;font-weight:bold">%,</span><span style="color:#000;font-weight:bold">#</span><span style="color:#000">7db9e8</span> <span style="color:#204a87;font-weight:bold">100</span><span style="color:#ce5c00;font-weight:bold">%);</span> <span style="color:#8f5902;font-style:italic">/* Chrome10+,Safari5.1+ */</span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">background</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">-o-linear-gradient</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">top</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000;font-weight:bold">#</span><span style="color:#000">1e5799</span> <span style="color:#204a87;font-weight:bold">0</span><span style="color:#ce5c00;font-weight:bold">%,</span><span style="color:#000;font-weight:bold">#</span><span style="color:#000">2989d8</span> <span style="color:#204a87;font-weight:bold">50</span><span style="color:#ce5c00;font-weight:bold">%,</span><span style="color:#000;font-weight:bold">#</span><span style="color:#000">207cca</span> <span style="color:#204a87;font-weight:bold">51</span><span style="color:#ce5c00;font-weight:bold">%,</span><span style="color:#000;font-weight:bold">#</span><span style="color:#000">7db9e8</span> <span style="color:#204a87;font-weight:bold">100</span><span style="color:#ce5c00;font-weight:bold">%);</span> <span style="color:#8f5902;font-style:italic">/* Opera 11.10+ */</span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">background</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">-ms-linear-gradient</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">top</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000;font-weight:bold">#</span><span style="color:#000">1e5799</span> <span style="color:#204a87;font-weight:bold">0</span><span style="color:#ce5c00;font-weight:bold">%,</span><span style="color:#000;font-weight:bold">#</span><span style="color:#000">2989d8</span> <span style="color:#204a87;font-weight:bold">50</span><span style="color:#ce5c00;font-weight:bold">%,</span><span style="color:#000;font-weight:bold">#</span><span style="color:#000">207cca</span> <span style="color:#204a87;font-weight:bold">51</span><span style="color:#ce5c00;font-weight:bold">%,</span><span style="color:#000;font-weight:bold">#</span><span style="color:#000">7db9e8</span> <span style="color:#204a87;font-weight:bold">100</span><span style="color:#ce5c00;font-weight:bold">%);</span> <span style="color:#8f5902;font-style:italic">/* IE10+ */</span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">background</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">linear-gradient</span><span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">top</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#000;font-weight:bold">#</span><span style="color:#000">1e5799</span> <span style="color:#204a87;font-weight:bold">0</span><span style="color:#ce5c00;font-weight:bold">%,</span><span style="color:#000;font-weight:bold">#</span><span style="color:#000">2989d8</span> <span style="color:#204a87;font-weight:bold">50</span><span style="color:#ce5c00;font-weight:bold">%,</span><span style="color:#000;font-weight:bold">#</span><span style="color:#000">207cca</span> <span style="color:#204a87;font-weight:bold">51</span><span style="color:#ce5c00;font-weight:bold">%,</span><span style="color:#000;font-weight:bold">#</span><span style="color:#000">7db9e8</span> <span style="color:#204a87;font-weight:bold">100</span><span style="color:#ce5c00;font-weight:bold">%);</span> <span style="color:#8f5902;font-style:italic">/* W3C */</span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">filter</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">progid</span><span style="color:#000;font-weight:bold">:</span><span style="color:#5c35cc;font-weight:bold">DXImageTransform</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Microsoft</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">gradient</span><span style="color:#ce5c00;font-weight:bold">(</span> <span style="color:#204a87;font-weight:bold">startColorstr</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#4e9a06">&#39;#1e5799&#39;</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#204a87;font-weight:bold">endColorstr</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#4e9a06">&#39;#7db9e8&#39;</span><span style="color:#ce5c00;font-weight:bold">,</span><span style="color:#204a87;font-weight:bold">GradientType</span><span style="color:#ce5c00;font-weight:bold">=</span><span style="color:#204a87;font-weight:bold">0</span> <span style="color:#ce5c00;font-weight:bold">);</span> <span style="color:#8f5902;font-style:italic">/* IE6-9 */</span> </span></span></code></pre></div><p>However I just noticed the <strong>switch to scss</strong> option!</p> <p><strong>SCSS Ouput</strong></p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-scss" data-lang="scss"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">@include</span><span style="color:#5c35cc;font-weight:bold"> filter-gradient</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">#1e5799</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">#7db9e8</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#ce5c00">horizontal</span><span style="color:#000;font-weight:bold">);</span> </span></span><span style="display:flex;"><span><span style="color:#000">$experimental-support-for-svg</span><span style="color:#ce5c00;font-weight:bold">:</span> <span style="color:#000">true</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">@include</span><span style="color:#5c35cc;font-weight:bold"> background-image</span><span style="color:#000;font-weight:bold">(</span> </span></span><span style="display:flex;"><span> <span style="color:#000">linear-gradient</span><span style="color:#000;font-weight:bold">(</span><span style="color:#ce5c00">left</span><span style="color:#ce5c00;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">#1e5799</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#204a87;font-weight:bold">%</span><span style="color:#ce5c00;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">#2989d8</span> <span style="color:#0000cf;font-weight:bold">50</span><span style="color:#204a87;font-weight:bold">%</span><span style="color:#ce5c00;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">#207cca</span> <span style="color:#0000cf;font-weight:bold">51</span><span style="color:#204a87;font-weight:bold">%</span><span style="color:#ce5c00;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">#7db9e8</span> <span style="color:#0000cf;font-weight:bold">100</span><span style="color:#204a87;font-weight:bold">%</span><span style="color:#000;font-weight:bold">));</span> </span></span></code></pre></div><p>This makes implementing complex cross-browser css gradients painless.</p> VMware Workstation Ubuntu problems https://choly.ca/post/vmware-workstation-ubuntu-problems/ Sun, 16 Oct 2011 12:14:52 -0400 https://choly.ca/post/vmware-workstation-ubuntu-problems/ <p>I just tried starting up vmware workstation and was greeted with a message saying it needed to compile some modules and then went on to fail this step no matter what. This is an issue I&rsquo;ve encountered before on Ubuntu 11.04 and now on 11.10.</p> <p>This is a bug with all v7.x of workstation and can be fixed with a simple patch I found today at <a href="http://weltall.heliohost.org/wordpress/2011/05/14/running-vmware-workstation-player-on-linux-2-6-39-updated/">http://weltall.heliohost.org/wordpress/2011/05/14/running-vmware-workstation-player-on-linux-2-6-39-updated/</a></p> <p>How to use it:</p> QML is Awesome https://choly.ca/post/qml-is-awesome/ Fri, 30 Sep 2011 12:20:44 -0400 https://choly.ca/post/qml-is-awesome/ <p>QML is Nokia&rsquo;s recent addition to its well known Qt framework and comes part of the Qt Quick Suite</p> <p>The way I describe it to people is:</p> <blockquote> <p>it&rsquo;s like html and css combined with the power of Qt in a extremely simple syntax.</p></blockquote> <div class="embed video-player"> <iframe class="youtube-player" type="text/html" width="640" height="385" src="http://www.youtube.com/embed/TN4RrBIft6A" allowfullscreen frameborder="0"> </iframe> </div> <h3 id="why">Why?</h3> <p>I have used Swing, WinForms, and GTK in the past and never really liked anything to do with GUI work. QML is the first time I&rsquo;m actually enjoying designing user interfaces and this results in a better end result. It feels a lot more like web development except without browser specific issues.</p> Qt Creator + Boost on Ubuntu 11.04 https://choly.ca/post/qt-creator-boost-on-ubuntu-1104/ Mon, 26 Sep 2011 12:23:39 -0400 https://choly.ca/post/qt-creator-boost-on-ubuntu-1104/ <h3 id="1-make-a-home-for-boost">1. make a home for boost</h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>sudo mkdir -p /code/include </span></span><span style="display:flex;"><span>sudo chown -R YOUR_USER_NAME /code </span></span><span style="display:flex;"><span><span style="color:#204a87">cd</span> /code/include </span></span></code></pre></div><h3 id="2-download-boost">2. download boost</h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>sudo apt-get install subversion </span></span><span style="display:flex;"><span>svn co http://svn.boost.org/svn/boost/trunk boost </span></span><span style="display:flex;"><span><span style="color:#204a87">cd</span> boost </span></span></code></pre></div><h3 id="3-compile-boost">3. compile boost</h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>sudo ./bootstrap.sh </span></span><span style="display:flex;"><span>sudo ./b2 </span></span></code></pre></div><p><strong>note:</strong> this will take a while, go get some coffee.</p> <h3 id="4-include-in-qt-project">4. Include in qt project</h3> <p>Add the following to your <code>.pro</code> file.</p> <pre tabindex="0"><code>INCLUDEPATH += /code/include/ LIBS += -L/code/include/boost/bin.v2/libs/ -lboost_system -lboost_filesystem -lboost_asio </code></pre><p>In this example i&rsquo;m linking <code>boost::filesystem</code> and <code>boost::asio.</code> <code>boost::system</code> is required by other boost libraries but if you can compile without it, then trash it.</p> Compile CompassApp on Ubuntu 11.04 https://choly.ca/post/compile-compassapp-on-ubuntu-1104/ Tue, 20 Sep 2011 12:27:52 -0400 https://choly.ca/post/compile-compassapp-on-ubuntu-1104/ <div class="embed video-player"> <iframe class="youtube-player" type="text/html" width="640" height="385" src="http://www.youtube.com/embed/pFCYhy9N6co" allowfullscreen frameborder="0"> </iframe> </div> <h3 id="1-install-rvm">1. Install RVM</h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>bash &lt; &lt;<span style="color:#ce5c00;font-weight:bold">(</span>curl -s https://rvm.beginrescueend.com/install/rvm<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span><span style="color:#204a87">echo</span> <span style="color:#4e9a06">&#39;if [[ -s &#34;$HOME/.rvm/scripts/rvm&#34; ]] ; then source &#34;$HOME/.rvm/scripts/rvm&#34; ; fi&#39;</span> &gt; ~/.bashrc </span></span><span style="display:flex;"><span>rvm install 1.9.2 </span></span></code></pre></div><h3 id="2-install-jruby">2. Install jRuby</h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>rvm install jruby </span></span><span style="display:flex;"><span><span style="color:#204a87">cd</span> ~/.rvm/bin/jruby-1.6.4 -S gem install rawr </span></span></code></pre></div><h3 id="3-get-and-compile-compassapp">3. Get and Compile CompassApp</h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span>git clone https://github.com/handlino/CompassApp.git </span></span><span style="display:flex;"><span><span style="color:#204a87">cd</span> CompassApp </span></span><span style="display:flex;"><span>bin/build-all.sh </span></span></code></pre></div><h3 id="4-run-compassapp">4. Run CompassApp</h3> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-sh" data-lang="sh"><span style="display:flex;"><span><span style="color:#204a87">cd</span> package/compass.app/ </span></span><span style="display:flex;"><span>./run.sh </span></span></code></pre></div> Cloud9 IDE https://choly.ca/post/cloud9-ide/ Wed, 31 Aug 2011 12:31:21 -0400 https://choly.ca/post/cloud9-ide/ <p>I&rsquo;ve always wanted to like web based IDE&rsquo;s. However, there was one thing that always prevented it: they&rsquo;ve always been terrible.</p> <p>Until now that is. <a href="http://cloud9ide.com/">http://cloud9ide.com/</a> Cloud9 is epic. It&rsquo;s built on node.js and has support for coffeescript and sass syntax highlighting and real time error checking. I can&rsquo;t even find an desktop ide to do that right!</p> <p>It gets better though. It&rsquo;s 100% open source so you can install it on locally. I kinda used this as a general guide but didn&rsquo;t need most of it</p> Sass Compass Blueprint https://choly.ca/post/sass-compass-blueprint/ Thu, 11 Aug 2011 12:34:59 -0400 https://choly.ca/post/sass-compass-blueprint/ <p>So I just realized that I hadn&rsquo;t actually written anything about compass. Now I feel a little dumb about the title of the Formalize post but w.e shit happens. Anyway I&rsquo;ll be talking about css in this post. I started using these a while back so I don&rsquo;t really know why I haven&rsquo;t posted anything about it. Better late than never.</p> <p>Let&rsquo;s start with SASS. Syntactically Awesome Style Sheet</p> <p>well actually I&rsquo;ll be talking about SCSS which is sass syntax which is more like css. Well actually it&rsquo;s exactly like regular css except you can add way more stuff. but regular css is still valid scss. I&rsquo;m not going to try to explain so here&rsquo;s an example.</p> CoffeeScript https://choly.ca/post/coffeescript/ Sun, 07 Aug 2011 12:37:05 -0400 https://choly.ca/post/coffeescript/ <p>I just spent the last 5 hours learning CoffeeScript and I feel like I have pretty much everything down. My brain is kinda dead right now, but at the same time I&rsquo;m pretty excited to actually try it in a real project. In case you don&rsquo;t know CoffeeScript is a python-esque language which &lsquo;compiles&rsquo; into javascript. classes, list comprehension inheritance, ranges, semantic code etc&hellip;. dream come true. <a href="http://jashkenas.github.com/coffee-script/">http://jashkenas.github.com/coffee-script/</a></p> <p>One thing I was worried about was being able to use 3rd party libraries with it. It&rsquo;s actually not different at all&hellip; Once you figure it all out, you realize that it&rsquo;s still the exact same js you&rsquo;re working with and you can do just as much. It&rsquo;s just a lot less shitty. Yes, the learning curve is balls, but it&rsquo;s definitely worth it (i hope).</p> Formalize [More Compass] https://choly.ca/post/formalize-compass/ Fri, 05 Aug 2011 12:39:12 -0400 https://choly.ca/post/formalize-compass/ <p>I think forms and all things related ( inputs, buttons, etc&hellip; ) are probably one of the more annoying things when building a website. They&rsquo;re just not consistent and it takes a lot of effort to make them look decent. I spent some time looking for a tool to help me with this and I ended up with formalize which comes as a compass plugin and integrates with any web framework you&rsquo;re using. <a href="http://formalize.me/">http://formalize.me/</a> It actually didn&rsquo;t jump out at me the first time around when I checked out the site. It didn&rsquo;t &lsquo;dazzel&rsquo; me so I kept looking, but what I realized was that it shouldn&rsquo;t stand out. It&rsquo;s a form&hellip; You don&rsquo;t want it to be shiny with crazy animations. So I went back and used it for the project I&rsquo;m currently working on. It was perfect&hellip; enough said.</p> Reloader - multi browser live web preview https://choly.ca/post/reloader/ Wed, 20 Jul 2011 12:40:37 -0400 https://choly.ca/post/reloader/ <p>I recently started developing on linux and unfortunately stylizer 5 does not support linux. So I&rsquo;m back to using kate. However, one thing that I really missed right away was the instant preview feature. Having to go and refresh multiple browsers every time you change a line of code blows. I searched around for a bit and found a few tools but none of them were any good. I needed something that would work in multiple browsers at the same time and I couldn&rsquo;t find anything to my liking so I wrote my own.</p> JPProxy - tiny jsonp proxy https://choly.ca/post/jpproxy/ Fri, 15 Jul 2011 12:43:11 -0400 https://choly.ca/post/jpproxy/ <p>JPProxy is a very simple yet powerful JSONP script. It allows you to make ajax like requests to any page on a server that has the jpproxy.php script on it. I tried really hard to make it as simple and generic as possible so the source is tiny.</p> <h3 id="1-client">1. Client</h3> <p>A script tag is injected into the DOM and all the values are added to the url as GET parameters.</p> Skybound Stylizer 5 - CSS Editor https://choly.ca/post/skybound-stylizer-5-css-editor/ Thu, 14 Jul 2011 12:55:01 -0400 https://choly.ca/post/skybound-stylizer-5-css-editor/ <p>Lets start with a little preface. Prior to finding Stylizer, I was completely happy using a regular text editor (gedit, notepad++) with firebug to do my css coding. I don&rsquo;t really know how I found stylizer, or what motivated me to download it, but I am glad I did. Stylizer is, by far, the best css editor. I went on to try 10+ different editors in hopes of finding a free alternative and nothing even comes close. This is the first piece of software I bought a legitimate licence for and $80 is a lot for a broke ass student.</p> Balsamiq Mockups - wireframing done right https://choly.ca/post/balsamiq-mockups-wireframing-done-right/ Thu, 14 Jul 2011 12:51:14 -0400 https://choly.ca/post/balsamiq-mockups-wireframing-done-right/ <h3 id="senario">Senario:</h3> <p>You&rsquo;re designing some type of user interface. Clients never know what they want (even when they think they do) so it&rsquo;s usually a good idea to come prepared with a basic design to go off. You quickly whip together something in photoshop and think you&rsquo;re good to go.</p> <p><strong>This is how the conversation goes:</strong></p> <p><strong>Me:</strong> I threw together this mockup of a potential design. It&rsquo;s very rough so just look at the basic layout and ignore details.</p> absoluteFudge - ie6 absolute positioning https://choly.ca/post/absolutefudge-ie6-absolute-positioning/ Thu, 14 Jul 2011 12:48:23 -0400 https://choly.ca/post/absolutefudge-ie6-absolute-positioning/ <p>I don&rsquo;t know about you, but here is a snippet of css that I love.</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-css" data-lang="css"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">div</span><span style="color:#000;font-weight:bold">#</span><span style="color:#000">selector</span> <span style="color:#000;font-weight:bold">{</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">position</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#204a87;font-weight:bold">absolute</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">left</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">10</span><span style="color:#204a87;font-weight:bold">px</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">right</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">10</span><span style="color:#204a87;font-weight:bold">px</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">top</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">10</span><span style="color:#204a87;font-weight:bold">px</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span> <span style="color:#204a87;font-weight:bold">bottom</span><span style="color:#000;font-weight:bold">:</span> <span style="color:#0000cf;font-weight:bold">10</span><span style="color:#204a87;font-weight:bold">px</span><span style="color:#000;font-weight:bold">;</span> </span></span><span style="display:flex;"><span><span style="color:#000;font-weight:bold">}</span> </span></span></code></pre></div><p>assuming that the parent element has either relative or absolute positioning, the child div will fit inside with a 10px margin. This is a very powerful technique for creating liquid css layouts.</p> <p><strong>Problem</strong></p> <p>The problem is, that ie doesn&rsquo;t support giving values to all sides (top,bottom,left,right) so you were forced to have a separate stylesheet for ie with a static layout.</p>