Jekyll2019-04-29T17:05:37+00:00https://til.jaz-lounge.com/atom.xmlToday I learnedThis site is my way of sharing and remembering learnings I had during the ongoing development of web applications for fun and profit.…to use the original Webster dictionary under linux.2019-04-28T10:00:00+00:002019-04-28T10:00:00+00:00https://til.jaz-lounge.com/linux/linux-webster-dictionary<p>Today I read a <a href="http://jsomers.net/blog/dictionary">brilliant article about English dictionaries and how to use them</a>. It was inspiring in its colorfulness use of words and very well written, it really left me with the feeling, I need to write more. It also had an appendix about how to set up an online version of it. But it uses MacOS of course, so I had to check what works on Linux, to get a dictionary running as well. Here it is:</p>
<p>On Linux there have been multiple Dictionaries out there. Some were discontinued or abandoned after legal conflicts, some were left because of better alternatives. So it looks like most people settled to use <a href="http://goldendict.org">GoldenDict</a> as the standard dictionary app to use.</p>
<p>So you can install it via <code class="highlighter-rouge">apt</code> like so:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">sudo </span>apt-get install goldendict
</code></pre></div></div>
<p>Since that sparking article really left me with the desire to use the Webster dictionary from 1913, we have to download this as well. Luckily in the appendix of that article James provides an <a href="https://s3.amazonaws.com/jsomers/dictionary.zip">Download link to his MacOS App which contains what we need</a>. This is an archive containing a <code class="highlighter-rouge">tar.bz2</code>-archive within the <code class="highlighter-rouge">dictionary</code> folder which we extract somewhere, our GoldenDict app can use it.</p>
<p>The last thing we have to do is teaching GoldenDict to use that specific dictionary. We can do that through the menu entry <code class="highlighter-rouge">Edit -> Dictionary</code>, hitting <code class="highlighter-rouge">add</code>, and choosing the directory of our previously extracted dictionary.</p>
<p>E voilá, if we search now within GoldenDict we get those really great definitions that really are interesting to read and explain so colorful, that a word really means.</p>Today I read a brilliant article about English dictionaries and how to use them. It was inspiring in its colorfulness use of words and very well written, it really left me with the feeling, I need to write more. It also had an appendix about how to set up an online version of it. But it uses MacOS of course, so I had to check what works on Linux, to get a dictionary running as well. Here it is:…that you can and should use localeCompare to sort strings alphabetically.2019-03-14T15:20:00+00:002019-03-14T15:20:00+00:00https://til.jaz-lounge.com/javasscript/javascript-localecompare<p>Imagine you want to sort some German strings alphabetically. (Take this for example:)</p>
<div class="language-js highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">const</span> <span class="nx">countries</span> <span class="o">=</span> <span class="p">[</span>
<span class="s1">'Deutschland'</span><span class="p">,</span>
<span class="s1">'Ägypten'</span><span class="p">,</span>
<span class="s1">'Norwegen'</span><span class="p">,</span>
<span class="s1">'Albanien'</span>
<span class="p">]</span>
</code></pre></div></div>
<p>If you would sort them using <code class="highlighter-rouge">Array.sort()</code> method, you will get this result, which might not be what you want:</p>
<div class="language-js highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">countries</span><span class="p">.</span><span class="nx">sort</span><span class="p">()</span>
<span class="c1">// results in: ['Albanien', 'Deutschland', 'Norwegen', 'Ägypten']</span>
</code></pre></div></div>
<p>But if you the <code class="highlighter-rouge">.localeCompare</code> method that is defined on any instance of <code class="highlighter-rouge">String</code> you can sort the string with a more locale aware algorithm. Try this:</p>
<div class="language-js highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nx">countries</span><span class="p">.</span><span class="nx">sort</span><span class="p">((</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span><span class="p">)</span> <span class="o">=></span> <span class="nx">a</span><span class="p">.</span><span class="nx">localeCompare</span><span class="p">(</span><span class="nx">b</span><span class="p">))</span>
<span class="c1">// results in: ['Ägypten', 'Albanien', 'Deutschland', 'Norwegen']</span>
</code></pre></div></div>
<p>The <code class="highlighter-rouge">.localeCompare</code> method takes even more options (in most browsers), to adjust its comparing algorithm. You can read all about it on the fabulous <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare">MDN documentation</a> about it.</p>Imagine you want to sort some German strings alphabetically. (Take this for example:)…that you can use a .gemrc file to not install documentation.2019-03-04T09:50:00+00:002019-03-04T09:50:00+00:00https://til.jaz-lounge.com/ruby/ruby-gem-no-document<p>Everyone loves [Rubygems][Rubygems]. And you probably have heard of the <code class="highlighter-rouge">--no-rdoc</code> & <code class="highlighter-rouge">--no-ri</code> flags, right? The flags that fasten up your gem installation since you do not have to generate the RDoc and ri documentation? Ah, I see you also have heard about it. Well, I figured those flags are now deprecated. Time to replace them with new stuff. It is called <code class="highlighter-rouge">--no-document</code> now.</p>
<p>Best thing to add it to your <code class="highlighter-rouge">~/.gemrc</code> file (and create one if you don’t have one yet). Create a <code class="highlighter-rouge">~/.gemrc</code> file like this:</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>gem: --no-document
</code></pre></div></div>
<p>Now you don’t have to worry about this anymore.</p>Everyone loves [Rubygems][Rubygems]. And you probably have heard of the --no-rdoc & --no-ri flags, right? The flags that fasten up your gem installation since you do not have to generate the RDoc and ri documentation? Ah, I see you also have heard about it. Well, I figured those flags are now deprecated. Time to replace them with new stuff. It is called --no-document now.…about ‘set -o pipefail’ in bash scripts.2019-02-28T11:00:00+00:002019-02-28T11:00:00+00:00https://til.jaz-lounge.com/linux/linux-set-pipefail<p>If you use <code class="highlighter-rouge">set -e</code> to let your bash script fail when a command fails and you have some pipes within your script, you might also want to run <code class="highlighter-rouge">set -o pipefail</code> as well, since it allows bash to also discover that pipes fail.</p>
<p>See <a href="http://petereisentraut.blogspot.com/2010/11/pipefail.html">here</a> for more details.</p>If you use set -e to let your bash script fail when a command fails and you have some pipes within your script, you might also want to run set -o pipefail as well, since it allows bash to also discover that pipes fail.…that there is the only flag for rubocop.2019-02-22T11:00:00+00:002019-02-22T11:00:00+00:00https://til.jaz-lounge.com/ruby/ruby-rubocop-only<p><a href="https://github.com/rubocop-hq/rubocop">Rubocop</a> is a great tool to keep you code consistent, sane and informs you about some performance pitfalls.</p>
<p>Lately, I wanted to slowly add rubocop to an existing codebase. Rubocop has the <code class="highlighter-rouge">-a</code> parameter – which is short for <code class="highlighter-rouge">--auto-correct</code> – which corrects all the offenses. Having a thousand offenses and trying to auto fix them all, will generate quite the Pull Request. But there is also the <code class="highlighter-rouge">--only</code> parameter that allows you to check (and fix) only some or one type of offense.</p>
<p>So a neat way to add all the missing magic <code class="highlighter-rouge">frozen_string_literal: true</code> comments everywhere, might look like:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>rubocop <span class="nt">--only</span> Style/FrozenStringLiteralComment <span class="nt">-a</span>
</code></pre></div></div>Rubocop is a great tool to keep you code consistent, sane and informs you about some performance pitfalls.…how to add functionality to existing rake tasks.2019-02-14T11:00:00+00:002019-02-14T11:00:00+00:00https://til.jaz-lounge.com/ruby/ruby-enhance-rake-tasks<p>Sometimes you just want to add some extra functionality to some existing rake task.</p>
<p>So image having these rake tasks defined:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="n">namespace</span> <span class="ss">:do</span> <span class="k">do</span>
<span class="n">task</span> <span class="ss">:foo</span> <span class="k">do</span>
<span class="nb">puts</span> <span class="s2">"foo"</span>
<span class="k">end</span>
<span class="n">task</span> <span class="ss">:after</span> <span class="k">do</span>
<span class="nb">puts</span> <span class="s2">"after"</span>
<span class="k">end</span>
<span class="n">task</span> <span class="ss">:before</span> <span class="k">do</span>
<span class="nb">puts</span> <span class="s2">"before"</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre></div></div>
<p>We can prepend the <code class="highlighter-rouge">do:before</code> task to the <code class="highlighter-rouge">do:foo</code> task like this:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="no">Rake</span><span class="o">::</span><span class="no">Task</span><span class="p">[</span><span class="s2">"do:foo"</span><span class="p">].</span><span class="nf">enhance</span> <span class="p">[</span><span class="s2">"do:before"</span><span class="p">]</span>
<span class="c1"># output would be:</span>
<span class="c1"># before</span>
<span class="c1"># foo</span>
</code></pre></div></div>
<p>And let something run after a task like this:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="no">Rake</span><span class="o">::</span><span class="no">Task</span><span class="p">[</span><span class="s2">"do:foo"</span><span class="p">].</span><span class="nf">enhance</span> <span class="k">do</span>
<span class="no">Rake</span><span class="o">::</span><span class="no">Task</span><span class="p">[</span><span class="s2">"do:after"</span><span class="p">].</span><span class="nf">invoke</span>
<span class="k">end</span>
<span class="c1"># output would be:</span>
<span class="c1"># foo</span>
<span class="c1"># after</span>
</code></pre></div></div>
<p>Really neat if you want to add some more stuff, to existing rails tasks for example. I got that idea <a href="https://www.dan-manges.com/blog/modifying-rake-tasks">from here</a>, btw.</p>Sometimes you just want to add some extra functionality to some existing rake task.…React v16.8 introduces hooks to counter class awkwardness.2019-02-12T11:00:00+00:002019-02-12T11:00:00+00:00https://til.jaz-lounge.com/react/react-hooks<p>A neat little feature which comes with version 16.8 of <a href="https://reactjs.org/">React</a> are <a href="https://reactjs.org/docs/hooks-intro.html">hooks</a>.</p>
<h3 id="what-are-hooks">What are Hooks?</h3>
<p>Hooks allow you to add state to functional components, or make side effects work in those. Or even bundle up your own stateful logic into an own hook method, that makes the logic be reusable without having to leak the state all over the place.</p>
<p>I could reproduce the highlights of hooks here, but the React people are explaining it well enough. Just [read their overview][<a href="https://reactjs.org/docs/hooks-overview.html">hooks-overview</a>].</p>A neat little feature which comes with version 16.8 of React are hooks.…where to put app specific configs and files.2019-02-03T15:00:00+00:002019-02-03T15:00:00+00:00https://til.jaz-lounge.com/programming/programming-app-specific.configs<p>…and the answer is not necessarily in the users home directory.</p>
<p>Imagine you write a desktop app and you need to persist some configuration the user has chosen. Where do you put those files? My default answer – as I have been a developer on a Mac – would have been <code class="highlighter-rouge">~/Library/Application Support/com.example.AwesomeApp/</code>. But since I am working with Linux now, and those people have different ideas where to put stuff and like to have total control, I would have said something like: “In a dotfile maybe?”</p>
<p>But there seems to be a <a href="https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html">specification</a> for handling this – of course, there is. So the short answer is, if there is the environment variable <code class="highlighter-rouge">$XDG_CONFIG_HOME</code> defined, use that value, if not, default to <code class="highlighter-rouge">~/.config</code> for config files. And for other app-specific files use <code class="highlighter-rouge">$XDG_DATA_HOME</code> or <code class="highlighter-rouge">~/.local/share</code></p>
<p>So if you build a desktop app, you know now, where to put stuff.</p>…and the answer is not necessarily in the users home directory.…dmenu is a great way to optimize cli scripting.2019-01-28T09:00:00+00:002019-01-28T09:00:00+00:00https://til.jaz-lounge.com/linux/linux-dmenu<p><a href="https://tools.suckless.org/dmenu/">Dmenu</a> is a simple but elegant tool from the <a href="https://suckless.org/">suckless</a> community. What it does is simply generating a dynamic menu – hence the name – using the input (text file, or piped) where every line is one menu item and let you choose from those options via keyboard cursor keys or typing the whole or partial words and in the end returning the result to stdout.</p>
<p>So this would simply generate a menu with two options (and a question):</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">echo</span> <span class="s2">"yes</span><span class="se">\n</span><span class="s2">no"</span> | dmenu <span class="nt">-p</span> <span class="s2">"Are you ok?"</span>
yes
</code></pre></div></div>
<p>Why is that so need? Well. I had two scripts fixing the screen setup for me, one called <code class="highlighter-rouge">screenhome</code> and the other <code class="highlighter-rouge">screenwork</code>. While that works, typing it out is annoying if you change between both setups a lot – since auto completion will suggest the last used one. Plus that are two scripts. With dmenu I could simply but them in one little script. It looks something like that:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c">#!/bin/bash</span>
<span class="c"># This script is for fixing my screen setup.</span>
<span class="nv">choices</span><span class="o">=</span><span class="s2">"home</span><span class="se">\n</span><span class="s2">work"</span>
<span class="nv">chosen</span><span class="o">=</span><span class="k">$(</span><span class="nb">echo</span> <span class="nt">-e</span> <span class="s2">"</span><span class="nv">$choices</span><span class="s2">"</span> | dmenu <span class="nt">-i</span><span class="k">)</span>
<span class="k">case</span> <span class="s2">"</span><span class="nv">$chosen</span><span class="s2">"</span> <span class="k">in
</span>home<span class="p">)</span>
xrandr <span class="nt">--output</span> HDMI-1 <span class="nt">--above</span> eDP-1 <span class="o">&&</span>
xfconf-query <span class="nt">-c</span> xfce4-panel <span class="nt">-p</span> /panels/panel-1/length <span class="nt">-s</span> 100 <span class="p">;;</span>
work<span class="p">)</span>
xrandr <span class="nt">--output</span> HDMI-1 <span class="nt">--above</span> eDP-1 <span class="o">&&</span>
xfconf-query <span class="nt">-c</span> xfce4-panel <span class="nt">-p</span> /panels/panel-1/length <span class="nt">-s</span> 75
<span class="k">esac</span>
</code></pre></div></div>
<p>I know, fixing the screen setup like this might not be the most elegant solution there could be, but that’s the one I found so far. But that is just for illustrating what you can do with it.</p>
<p>It can be installed by all regular means or build by hand – which is needed when you want to change the configuration like colors and stuff, since they are set on compile time (see <a href="https://www.youtube.com/watch?v=R9m723tAurA">example here</a>).</p>Dmenu is a simple but elegant tool from the suckless community. What it does is simply generating a dynamic menu – hence the name – using the input (text file, or piped) where every line is one menu item and let you choose from those options via keyboard cursor keys or typing the whole or partial words and in the end returning the result to stdout.…bundler can be used without a gemfile.2019-01-22T11:00:00+00:002019-01-22T11:00:00+00:00https://til.jaz-lounge.com/ruby/ruby-bundler-inline<p>I didn’t know that up until now, but obviously, you can inline bundler within your ruby scripts. No need to have a dedicated Gemfile or to call bundler before calling your script. Just do:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">require</span> <span class="s1">'bundler/inline'</span>
<span class="n">gemfile</span> <span class="k">do</span>
<span class="n">source</span> <span class="s1">'https://rubygems.org'</span>
<span class="n">gem</span> <span class="s1">'rest-client'</span>
<span class="k">end</span>
<span class="nb">puts</span> <span class="no">RestClient</span><span class="p">.</span><span class="nf">get</span><span class="p">(</span><span class="s1">'https://news.ycombinator.com/'</span><span class="p">)</span>
</code></pre></div></div>
<p>You can see <a href="https://github.com/bundler/bundler/blob/master/lib/bundler/inline.rb">the source on their GitHub repo</a>. This becomes espescially interesting not only for small scripts, but also to have small executable files, that behave like a regular executable.</p>
<h3 id="a-small-example">A small example</h3>
<p>Image being able to do this from your command line:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span>weather berlin
Today it is <span class="nt">-2</span>.51°C <span class="k">in </span>Berlin with few clouds.
</code></pre></div></div>
<p>Easily done:</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">#!/usr/bin/env ruby</span>
<span class="nb">require</span> <span class="s1">'bundler/inline'</span>
<span class="n">gemfile</span> <span class="k">do</span>
<span class="n">source</span> <span class="s1">'https://rubygems.org'</span>
<span class="n">gem</span> <span class="s1">'json'</span>
<span class="n">gem</span> <span class="s1">'rest-client'</span>
<span class="k">end</span>
<span class="no">API_KEY</span> <span class="o">=</span> <span class="s1">'XXX'</span> <span class="c1"># put your api key here</span>
<span class="n">city</span> <span class="o">=</span> <span class="no">ARGV</span><span class="p">.</span><span class="nf">count</span> <span class="o">></span> <span class="mi">0</span> <span class="p">?</span> <span class="no">ARGV</span><span class="p">.</span><span class="nf">first</span> <span class="p">:</span> <span class="s1">'Hamburg,de'</span>
<span class="n">body</span> <span class="o">=</span> <span class="no">RestClient</span><span class="p">.</span><span class="nf">get</span><span class="p">(</span><span class="s2">"https://api.openweathermap.org/data/2.5/weather?q=</span><span class="si">#{</span><span class="n">city</span><span class="si">}</span><span class="s2">&appid=</span><span class="si">#{</span><span class="no">API_KEY</span><span class="si">}</span><span class="s2">&units=metric"</span><span class="p">)</span>
<span class="n">response</span> <span class="o">=</span> <span class="no">JSON</span><span class="p">.</span><span class="nf">parse</span><span class="p">(</span><span class="n">body</span><span class="p">)</span>
<span class="n">temp</span> <span class="o">=</span> <span class="n">response</span><span class="p">[</span><span class="s1">'main'</span><span class="p">][</span><span class="s1">'temp'</span><span class="p">]</span>
<span class="n">city</span> <span class="o">=</span> <span class="n">response</span><span class="p">[</span><span class="s1">'name'</span><span class="p">]</span>
<span class="n">weather</span> <span class="o">=</span> <span class="n">response</span><span class="p">[</span><span class="s1">'weather'</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s1">'description'</span><span class="p">]</span>
<span class="nb">puts</span> <span class="s2">"Today it is </span><span class="si">#{</span><span class="n">temp</span><span class="si">}</span><span class="s2">°C in </span><span class="si">#{</span><span class="n">city</span><span class="si">}</span><span class="s2"> with </span><span class="si">#{</span><span class="n">weather</span><span class="si">}</span><span class="s2">."</span>
</code></pre></div></div>
<p>Put this into a <code class="highlighter-rouge">weather</code> file, make it exectuable <code class="highlighter-rouge">chmod +x weather</code> and then put it into your <code class="highlighter-rouge">PATH</code> variable.</p>I didn’t know that up until now, but obviously, you can inline bundler within your ruby scripts. No need to have a dedicated Gemfile or to call bundler before calling your script. Just do: