<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alex Hunt on Medium]]></title>
        <description><![CDATA[Stories by Alex Hunt on Medium]]></description>
        <link>https://medium.com/@huntie?source=rss-e54e8fe3cbbe------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DPsg4psWoHYGDkrU47IF0g.jpeg</url>
            <title>Stories by Alex Hunt on Medium</title>
            <link>https://medium.com/@huntie?source=rss-e54e8fe3cbbe------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 22:24:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@huntie/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A concise guide to configuring React Native with Yarn Workspaces]]></title>
            <link>https://medium.com/@huntie/a-concise-guide-to-configuring-react-native-with-yarn-workspaces-d7efa71b6906?source=rss-e54e8fe3cbbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/d7efa71b6906</guid>
            <category><![CDATA[monorepo]]></category>
            <category><![CDATA[metro-bundler]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[migration]]></category>
            <category><![CDATA[yarn]]></category>
            <dc:creator><![CDATA[Alex Hunt]]></dc:creator>
            <pubDate>Sun, 01 Mar 2020 09:29:01 GMT</pubDate>
            <atom:updated>2020-03-03T09:18:39.754Z</atom:updated>
            <content:encoded><![CDATA[<p>Monorepos are brilliant in the right use cases, but often not without a cause-questioning level of setup complication. In the JavaScript ecosystem, Yarn Workspaces provides a mature tool for managing dependencies near-seamlessly, however this is not pairable with a React Native app by default.</p><p>Last year I undertook a migration to merge our previously separate frontend repositories into (what has become) our largest codebase. Amongst these was a vanilla (non-Expo) React Native app. This post aims to share the essentials learned about setting up React Native in a Yarn (classic) Workspaces monorepo, by equipping you with the <strong><em>why</em></strong> and <strong><em>what to do</em></strong> about each of the major stumbling blocks encountered.</p><h3>1. Nohoist dependencies with native modules</h3><h4>Why?</h4><p>React Native expects non-JS (i.e. linked) dependencies to live at a predictable location relative to each native project (within the android and ios directories) in a standard installation — referring to files and running scripts at ../node_modules. However, because Yarn will “hoist” dependencies that can be used across workspaces, by default most modules will be installed in the node_modules directory at the root of the repository, <strong><em>not</em></strong> local to your app’s workspace as expected.</p><h4>What to do</h4><p>We can avoid this with a Yarn feature called <a href="https://classic.yarnpkg.com/blog/2018/02/15/nohoist/"><strong><em>nohoist</em></strong></a>. In essence, this is a blacklist in your app project’s package.json under the workspaces.nohoist key.</p><p>This list should include at least react-native and react-native/** (dependencies of react-native), in addition to any dependencies that need to <strong><em>bundle native modules</em></strong> or <strong><em>run native scripts</em></strong><em>.</em></p><figure><img alt="Snippet in package.json defining the list of packages to nohoist." src="https://cdn-images-1.medium.com/max/1024/1*SfDeyhxZ-4fD6Ea_Lh_FMg.png" /><figcaption><a href="https://gist.github.com/huntie/85ea491763b444bfa1bdc8e997fc2765#file-package-json-L12-L21">https://gist.github.com/huntie/85ea491763b444bfa1bdc8e997fc2765#file-package-json-L12-L21</a></figcaption></figure><p>From React Native 0.60, native dependencies that are hoisted are still detected due to autolinking updates, and this may no longer be strictly necessary for some<em> </em>packages. However, for predictable paths in native code, Jetifier, and for consistency, I recommend nohoisting every native dependency.</p><p><em>In general, it’s nice to keep our workarounds both in the native workspace (not affecting others) and in config files (that leave remaining project files “thinking” they are in a regular setup, and keeps our workspace isolated and detachable later). This trend continues below.</em></p><h3>2. Configure the Metro Bundler</h3><h4>Why?</h4><p>The Metro Bundler is the tool that, well, bundles your JavaScript application to be executed on the device or in a debug session. To do this, it creates a mapping of module dependencies to their locations on the file system and serves them dynamically. In our monorepo setup, this means Metro sees our local node_modules directory but misses all symlinks that Yarn has created to hoisted dependencies, and to the other workspaces and their dependencies.</p><h4>What to do</h4><p>We need to configure Metro to include additional paths in which modules may be found- i.e. each workspace + the repo root. Thankfully, there is a config option for this: <a href="https://facebook.github.io/metro/docs/en/configuration#watchfolders">watchFolders</a>, although we must figure out what the other paths are. Since React Native 0.59, we do this in a metro.config.js file at the root of the app project.</p><p>The snippet below defines the extra dirs for watchFolders— the location of the root node_modules dir and each adjacent workspace (from a prior call to <a href="https://www.npmjs.com/package/get-yarn-workspaces">get-yarn-workspaces</a>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Eawc1b7Bdt9OwtnAvZY_SQ.png" /><figcaption><a href="https://gist.github.com/huntie/85ea491763b444bfa1bdc8e997fc2765#file-metro-config-js-L15-L22">https://gist.github.com/huntie/85ea491763b444bfa1bdc8e997fc2765#file-metro-config-js-L15-L22</a></figcaption></figure><p>The use of filter prevents duplicating the app dir as a watch path. Depending on your setup, you may want to selectively exclude other workspaces too, such as if you have a web app or for unrelated workspaces.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bQWLEjckjCsxGJCXPp4yUA.png" /><figcaption>When configured, Metro will print all the directories it scans for modules, which now include the root + our other workspaces.</figcaption></figure><h3>3. Deal with the potential fallout from widening your module paths</h3><h4>Why?</h4><p>If you’re lucky, you might be sorted having done the first two steps. But it’s likely that as your shared workspaces evolve, so may new problems emerge out of these workarounds.</p><p>Our module map now includes multiple node_modules directories, and due to any nohoist entries, there is potential for dependencies to exist twice and at different versions. Often, this is not a massive problem, but let’s look at one breaking example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BAnB31OQkgS3IeVL0g6hfA.png" /><figcaption>See <a href="https://github.com/react-native-community/react-native-svg/issues/591">https://github.com/react-native-community/react-native-svg/issues/591</a></figcaption></figure><p>Here, a shared workspace requires react-native-svg, which is nohoisted in our app package. Due to our configured watchFolders, Metro has bundled two instances of react-native-svg at the same version — which isn’t the issue — but the package <a href="https://github.com/react-native-community/react-native-svg/issues/591">runs a side-effect on require</a> that now gets called twice.</p><h4>What to do</h4><p>We can fix this with a combination of blacklistRE and extraNodeModules in metro.config.js to essentially coerce the bundler to use a specific dependency instance when resolving a module — no matter which package required it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rsYexV28M52HiXhQ7NLjPw.png" /><figcaption>Abbreviated from <a href="https://gist.github.com/huntie/85ea491763b444bfa1bdc8e997fc2765#file-metro-config-js-L26-L51">https://gist.github.com/huntie/85ea491763b444bfa1bdc8e997fc2765#file-metro-config-js-L26-L51</a></figcaption></figure><p>This pattern is useful for other resolution fixes: we can also get the bundler to resolve a specific version of a dependency needed at runtime, for example. For our project, we additionally had to map react-native in an identical way to the single version in the native app folder (this is so likely that it’s also included in the Gist for this post). Additionaly, we needed to resolve to one version of core-js and another in the web app workspace (N.B. this method can be a necessary alternative to Yarn’s resolutions).</p><h3>Extras and final thoughts</h3><p>Hopefully, the above is sufficient to get your project up and running, or at least will help you understand these initial technical blockers when considering React Native in a monorepo project yourself.</p><h4>What else might I need to do?</h4><p>We faced other significant challenges in practice, including migrating CI in parallel for our web project, aligning deployment workflows, and configuring this setup to work with App Center (spoiler: this was not fun).</p><h4>Monorepo Tools</h4><p>If you’re migrating existing Git projects to a monorepo, <a href="https://github.com/shopsys/monorepo-tools">monorepo-tools</a> did the trick, building a new repo with rewritten file paths and preserving all existing commits. Our migration happened during active development in the source repos, and I was also able to sync back against these — meaning PRs from the day before could simply be reopened when the new repo was ready.</p><h4>Was this all worth it?</h4><p>Emphatically <em>yes</em>. Whilst the migration was a challenging effort, it was one a couple of devs could focus on over a few weeks. It has been paying off not only in our team’s day-to-day velocity, but in our ability to evolve design decisions across apps.</p><p>The example files providing full context for the snippets shared can be found in <a href="https://gist.github.com/huntie/85ea491763b444bfa1bdc8e997fc2765">this Gist</a>.</p><p>Cross-platform React + React Native development is just part of the awesome work happening at Unmind Engineering. Want to join in? <a href="https://home.unmind.com/careers">We’re hiring</a> ;).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d7efa71b6906" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[New year, new shell]]></title>
            <link>https://medium.com/@huntie/new-year-new-shell-f62cd3f15fe7?source=rss-e54e8fe3cbbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/f62cd3f15fe7</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[unix]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Alex Hunt]]></dc:creator>
            <pubDate>Thu, 10 Jan 2019 19:48:26 GMT</pubDate>
            <atom:updated>2019-01-16T18:20:56.305Z</atom:updated>
            <content:encoded><![CDATA[<h4>Getting started with fish, the friendly interactive shell</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1l7iPfcwUt4dpZtTl66BIA.png" /></figure><p>So far in my time using Unix systems I have stuck with Bash as my preferred shell environment. This is for various reasons, but mostly for its ubiquity. There are well-known alternative shell projects such as zsh, which build upon Bash with an “add more” attitude, but that hadn’t swayed me to become a long-term user in the past.</p><p><a href="https://fishshell.com/">Fish</a> is a significant departure from Bash, released in 2005 with the brilliant slogan, <em>“Finally, a command line shell for the 90s”</em>. With a bit of time and curiosity, it seemed worthwhile to give it a quick try.</p><h3>Keeping minimal</h3><p>The need for a plugin framework or patched <a href="https://github.com/ryanoasis/nerd-fonts">nerd font</a> to configure my shell environment never really appealed to me. With Bash I had only built up a humble selection of aliases, exports and functions, with the aim of minimalism and portability (setup on new systems involves simply running stow bash from my <a href="https://github.com/huntie/dotfiles"><em>dotfiles</em></a> repository). With fish I’ve ended up needing to configure less things, and what I have is noticeably cleaner.</p><h3>Features and impressions</h3><blockquote>In general, fish provides a great experience for the average user out of the box.</blockquote><h4>Interactive completions</h4><p>As you type, fish offers inline completion for your expressions, including an optimistic default completion where possible using entries from history. It’s addictive.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*nIsLeWQNi2A3xK-efhtNUw.gif" /></figure><ul><li>Use <em>Tab</em> to select an option for the first-level completion.</li><li>Use <em>Ctrl+F</em> to fully expand the current completion.</li><li>Use <em>Up</em> to cycle through history, scoped by what you’ve entered so far.</li></ul><p>One awesome command to use with completions in fish is abbr. This is an optional alternative to alias that instead will expand a user-defined shortcut when used.</p><pre>abbr -a -g rsync &#39;rsync --progress&#39;</pre><h4>Functions</h4><p>Any <em>.fish</em> script added under <em>~/.config/fish/functions</em> will be autoloaded and available in your shell.</p><p>Compared to bash, I find the scripting syntax for defining functions in fish to be slightly more elegant. I also love that you can include a description that will appear when browsing for the function in the completion UI.</p><pre>function fe --description &#39;Open fzf and edit file on selection&#39;<br>    fzf | read -l result</pre><pre>    if [ $result ]<br>        $EDITOR $result<br>    end<br>end</pre><p>Fish generally uses functions over variables. For instance, you can create your own implementation of fish_prompt (and there’s a fish_right_prompt!), or just choose to redefine prompt_pwd which this composes. To me, this makes sense and feels more well-designed.</p><p>There’s also more room to rejoice in working without raw terminal escape sequences! set_color supports both named colours and hex values, and bind has simple expressions for modifier keys such as Ctrl (\c).</p><p>Fish also offers <a href="https://fishshell.com/docs/current/commands.html#emit"><em>events</em></a>, a higher level abstraction to define functions that are event handlers, triggered via emit event_name.</p><h4>Docs and configuration</h4><p>Fish reads user-level configuration from <em>~/.config/fish/config.fish</em>. Here, you can run any command on init to override variables, set key bindings, define aliases, and source other files as in Bash. Fish will also read <em>~/.profile</em> and <em>~/.inputrc</em> by default. Lovely.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z73c5z19ZvEQTiIDj6pPbg.png" /><figcaption>The minimal input prompt lives on, with a fish-flavoured ‘&gt;’.</figcaption></figure><p>If you need, or are just curious to see what’s available by default, fish also offers a web app to configure itself via fish_config.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NH4Owaq4gFQtJ1w0_NTsyg.png" /><figcaption><strong>fish_config</strong> provides a fantastic web interface, even if I only play with it once.</figcaption></figure><p>Everything else you can configure for fish by default is documented at <a href="https://fishshell.com/docs/current/index.html">fishshell.com/docs</a> and <a href="https://fishshell.com/docs/current/commands.html">fishshell.com/docs/current/commands</a>.</p><h4>Compatibility</h4><p>From a pure practicality standpoint, it’s important for me that <strong><em>most</em></strong> everyday expressions and commands overlap between fish and Bash. On balance I think fish has done right in this regard.</p><p>Operators [|, &lt;, &gt;, &gt;&gt;] and most commands remain identical to Bash, which is a large enough piece of the equation in daily use. However, scripting as we have seen quickly exposes fish’s syntax differences, and Bash scripts will be mostly incompatible.</p><p>Not all commands are equivalent either. Notably, echo behaves differently around variable interpolation and quotes. Command substitutions use brackets only rather than $().</p><pre>echo $USER@(hostname)</pre><p>Saying this, it has not been a hard process to migrate my Bash customisations to fish.</p><h3>The verdict</h3><pre>chsh -s /usr/bin/fish</pre><p>After one week of use and adaptation, I ended up switching shell for good. The experience isn’t life changing, but fish provides great creature comforts out of the box and feels genuinely more modern in use and in configuration.</p><p>Shell differences have been nearly a non-occurrence, especially when one is mostly spending the day using Unix commands. At the same time, I will continue to maintain versions of my scripts for Bash, but configure new stuff fish on my development hosts.</p><p>If you’re interested in any of the settings shown here for fish, feel free to browse my <em>dotfiles</em> repository <a href="https://github.com/huntie/dotfiles/tree/master/fish">here</a>.</p><h3>Extras</h3><h4>For plugin junkies</h4><p>The <a href="https://github.com/oh-my-fish">Oh My Fish!</a> project offers a plugin framework to extend fish with a large selection of themes and plugins. 🎸</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/365/1*yqrkObaDuPIGr5zYcLSVXQ.png" /></figure><h4>vi mode</h4><p>Okay, this was in Bash but works flawlessly as well in fish. 😉</p><pre>set -g fish_key_bindings fish_vi_key_bindings</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f62cd3f15fe7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[10 essential Vim plugins]]></title>
            <link>https://medium.com/@huntie/10-essential-vim-plugins-for-2018-39957190b7a9?source=rss-e54e8fe3cbbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/39957190b7a9</guid>
            <category><![CDATA[top-10]]></category>
            <category><![CDATA[vim]]></category>
            <category><![CDATA[unix]]></category>
            <category><![CDATA[vim-plugins]]></category>
            <category><![CDATA[text-editor]]></category>
            <dc:creator><![CDATA[Alex Hunt]]></dc:creator>
            <pubDate>Thu, 11 Jan 2018 19:14:15 GMT</pubDate>
            <atom:updated>2020-05-19T11:25:14.081Z</atom:updated>
            <content:encoded><![CDATA[<p>Vim remains a powerful and ubiquitous application to tackle any number of text editing tasks conveniently from the terminal (<em>some</em> learning required). If you’re looking to add Vim to your toolkit this year, here is my list of absolutely essential plugins to begin supercharging your workflow.</p><p>If you’ve never set up a Vim plugin before, see the <em>Installing Vim plugins</em> section later on for a runthrough.</p><h3>1. fzf</h3><p><a href="https://github.com/junegunn/fzf.vim">https://github.com/junegunn/fzf.vim</a></p><p>Working on a substantial codebase usually involves traversing several files at a time. fzf<em> </em>stands for “fuzzy finder” and works similarly to the <em>Goto Anything</em> menu in Sublime Text, allowing you to open a file instantly after typing a rough representation of its name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RoYld2a27k4GvXtw_lsr6w.gif" /></figure><p>Whilst <a href="https://github.com/kien/ctrlp.vim">CtrlP</a><em> </em>has existed for a while, fzf offers significantly better performance. You can activate the search pane with the :Files command, but I use this so often that I’ve mapped it to a single key.</p><pre>map ; :Files&lt;CR&gt;</pre><p><a href="https://github.com/junegunn/fzf">fzf</a> is actually its own terminal command as well, which you can use to get the same awesome file searching functionality anywhere.</p><h3>2. lightline</h3><p><a href="https://github.com/itchyny/lightline.vim">https://github.com/itchyny/lightline.vim</a></p><p>Many users like to use a plugin to replace their statusline — whether as an improved visual aid (particularly in split panes), or just to make their editor more attractive. For me, lightline is an elegant and versatile choice.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HZFdxYLMRd1ib-eI2tGD9A.png" /><figcaption>Image source: <a href="https://github.com/itchyny/lightline.vim">lightline repository</a></figcaption></figure><p>The default layout is pretty clean, displaying the current mode with some nice colour feedback, as well as the file name, file properties and cursor position. It’s easy to configure what to show in each section in your <em>.vimrc</em> file, and there’s also a choice of built-in colour schemes.</p><pre>let g:lightline = {<br>  \     &#39;active&#39;: {<br>  \         &#39;left&#39;: [[&#39;mode&#39;, &#39;paste&#39; ], [&#39;readonly&#39;, &#39;filename&#39;, &#39;modified&#39;]],<br>  \         &#39;right&#39;: [[&#39;lineinfo&#39;], [&#39;percent&#39;], [&#39;fileformat&#39;, &#39;fileencoding&#39;]]<br>  \     }<br>  \ }</pre><h3>3. vim-multiple-cursors</h3><p><a href="https://github.com/terryma/vim-multiple-cursors">https://github.com/terryma/vim-multiple-cursors</a></p><p>This is another plugin bringing a much-loved Sublime feature to Vim, doing exactly what it says in the name. You’ve got to love multiple selections.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/1*riGGLxW_H1VfJ-U_xNOH7Q.gif" /></figure><p>To make a basic selection, use the Ctrl+N keystroke in normal mode, followed by a motion:</p><ul><li>c – change text.</li><li>I – insert at start of range.</li><li>A – insert at end of range.</li></ul><p>More actions can be found in the plugin’s <a href="https://github.com/terryma/vim-multiple-cursors#quick-start">quick start</a> information.</p><h3>4. vim-eunuch</h3><p><a href="https://github.com/tpope/vim-eunuch">https://github.com/tpope/vim-eunuch</a></p><p>This cryptically-named plugin adds a dozen core Unix file operations as Vim commands in the context of the current file. :!mv no more!</p><pre>:Rename new_name.sh<br>:Chmod +x<br>:SudoWrite</pre><p>These small helpers add up fast to not breaking your Vim workflow. All commands are documented in the <a href="https://github.com/tpope/vim-eunuch">project README</a>.</p><h3>5. surround</h3><p><a href="https://github.com/tpope/vim-surround">https://github.com/tpope/vim-surround</a></p><p>In a graphical text editor, typing an open bracket or quotes on a selected region will wrap or <em>surround</em> that region with with a matching pair of characters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*irUO17VQPMrJRK5LKnTIVA.gif" /></figure><p>With this plugin, you can do that and more — with motions to add, change or remove surrounding characters and strings in a variety of ways. Definitely check out <a href="https://github.com/tpope/vim-surround">the docs</a> for this one.</p><h3>6. NERDTree</h3><p><a href="https://github.com/scrooloose/nerdtree">https://github.com/scrooloose/nerdtree</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MUyy8PyijV6teba92mATrg.png" /></figure><p>NERDTree is a popular plugin to display an interactive file tree view in a side panel, which can be useful when working in larger project. I have the NERDTreeToggle command mapped to Ctrl+O.</p><pre>map &lt;C-o&gt; :NERDTreeToggle&lt;CR&gt;</pre><h3>7. EditorConfig</h3><p><a href="https://github.com/editorconfig/editorconfig-vim">https://github.com/editorconfig/editorconfig-vim</a></p><p>EditorConfig is a <a href="http://editorconfig.org/">multi-editor tool</a> for defining base file handling and code style preferences in a project and aligning these between editors. With this plugin, rulesets defined in a local <em>.editorconfig</em> file will automatically configure Vim settings such as indentation size, and apply formatting such as removing trailing whitespace on save.</p><pre>root = true</pre><pre>[*]<br>charset = utf-8<br>indent_style = space<br>indent_size = 4<br>trim_trailing_whitespace = true<br>insert_final_newline = true</pre><pre>[*.md]<br>trim_trailing_whitespace = false</pre><p>This is a boon for everyone, and prevents incoming developers from altering their default preferences manually when working between projects. Recommended for every editor you use.</p><h3>8. Emmet</h3><p><a href="https://github.com/mattn/emmet-vim">https://github.com/mattn/emmet-vim</a></p><p>Another plugin you’re likely to have used in another editor is Emmet. Emmet is a powerful completion tool for HTML, CSS and JavaScript which allows you to make dynamic completions from a shorthand expression.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*Fwplb-elRqxffoeeiRMJfw.gif" /></figure><p>In Vim, enter a expression such as the above in an HTML file and use Ctrl+Y, , from normal mode to expand it. If you’re not familiar with the abbreviation syntax, there’s a short demo video as well as documentation on the <a href="https://www.emmet.io/">Emmet website</a>. Along with Vim’s snippets feature, this plugin can seriously up your code completion game, and again is essential if you’re working on web projects in other editors.</p><h3>9. ALE</h3><p><a href="https://github.com/w0rp/ale">https://github.com/w0rp/ale</a></p><p>ALE (Asynchronous Lint Engine) is a comprehensive code analysis plugin for Vim. As you edit, it’ll run the current file through an external tool of choice and displays marks for any errors and warnings directly in Vim’s sign column. Navigate to these or toggle a summary pane with :ALEDetail to see a specific message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AifpzFxsXggIzG-Q_oqO8Q.png" /></figure><p>Built-in are strong defaults for matching common tools for most programming and config languages if they are present on your system. It’s also fairly easy to reconfigure these mappings. Remember, you can define per-file-type settings for Vim in ~/.vim/ftplugin/{filetype}.vim. Here, I’ve extended the available linters for Python files:</p><pre>let b:ale_linters = [&#39;pyflakes&#39;, &#39;flake8&#39;, &#39;pylint&#39;]</pre><p>You can also configure <em>fixers</em> for applying formatting on save:</p><pre>let b:ale_fixers = [&#39;eslint&#39;]<br>let b:ale_fix_on_save = 1</pre><h3>10. vim-gitgutter</h3><p><a href="https://github.com/airblade/vim-gitgutter">https://github.com/airblade/vim-gitgutter</a></p><p>This plugin adds a column to the left margin indicating lines changed in the active file since the last Git revision. As in other editors, this can be a useful bit of context during editing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*XMEE5_I3A415OrLktZq0ng.png" /></figure><p><em>vim-gitgutter </em>also lets you jump between changes and stage/discard individual hunks. Here are the default mappings:</p><pre>nmap ]c &lt;Plug&gt;GitGutterNextHunk<br>nmap [c &lt;Plug&gt;GitGutterPrevHunk<br>nmap &lt;Leader&gt;hs &lt;Plug&gt;GitGutterStageHunk<br>nmap &lt;Leader&gt;hu &lt;Plug&gt;GitGutterUndoHunk</pre><h3>Installing Vim plugins</h3><p>There are several to manage plugins Vim, including <a href="https://github.com/tpope/vim-pathogen">Pathogen</a>, <a href="https://github.com/VundleVim/Vundle.vim">Vundle</a>, and Vim 8’s native package loading. I recommend <a href="https://github.com/junegunn/vim-plug"><strong>vim-plug</strong></a>, which can be installed using curl, or <a href="https://github.com/huntie/dotfiles/blob/f8a129f8e306c853ab52234f605b1d872b0448b1/vim/.vimrc#L72-L77">auto-installed in .vimrc</a>:</p><pre>curl -fLo ~/.vim/autoload/plug.vim --create-dirs \<br>    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim</pre><p>With vim-plug, I create a <em>~/.vim/plugins.vim</em> file and add a Plug line for each dependency to install, between the required plug#begin and plug#endcalls.</p><pre>call plug#begin(&#39;~/.vim/plugged&#39;)</pre><pre>Plug &#39;airblade/vim-gitgutter&#39;<br>Plug &#39;editorconfig/editorconfig-vim&#39;<br>Plug &#39;itchyny/lightline.vim&#39;<br>Plug &#39;junegunn/fzf&#39;<br>Plug &#39;junegunn/fzf.vim&#39;<br>Plug &#39;mattn/emmet-vim&#39;<br>Plug &#39;scrooloose/nerdtree&#39;<br>Plug &#39;terryma/vim-multiple-cursors&#39;<br>Plug &#39;tpope/vim-eunuch&#39;<br>Plug &#39;tpope/vim-surround&#39;<br>Plug &#39;w0rp/ale&#39;</pre><pre>call plug#end()</pre><p>Source this by adding the following statement to your <em>.vimrc</em>.</p><pre>so ~/.vim/plugins.vim</pre><p>Finally, launch Vim and run :PlugInstall.</p><h3>Closing notes</h3><p>These are just some of my picks for general-purpose Vim editing, and I hope most people who read this will find at least one useful thing. Feel free to comment if you think I missed anything! <strong>Update</strong>: I’ve made two substitutions thanks to great suggestions by @AntonK52 and @frioux — thanks guys.</p><p>Vim still isn’t quite my primary editor, but over a period of years I’ve slowly been using it more and more. Thanks to some looking around and great help from the community above, I’ve been able to significantly improve and personalise my Vim experience with each new setting and plugin. This also takes time. If you’ve read this article and want more, definitely explore the vast selection of other plugins out there for yourself — <a href="https://vimawesome.com/"><strong>VimAwesome</strong></a> is the place to look<strong><em>.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39957190b7a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Snapshot testing in Python for Sublime Text packages]]></title>
            <link>https://medium.com/@huntie/snapshot-testing-in-python-for-sublime-text-packages-ef8aaecde945?source=rss-e54e8fe3cbbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/ef8aaecde945</guid>
            <category><![CDATA[sublimetext]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Alex Hunt]]></dc:creator>
            <pubDate>Wed, 27 Dec 2017 23:37:19 GMT</pubDate>
            <atom:updated>2018-01-13T20:18:26.695Z</atom:updated>
            <content:encoded><![CDATA[<p>A few months ago I authored a small Sublime Text plugin called <a href="https://github.com/huntie/sublime-tmux"><em>Sublime tmux</em></a>, providing a set of commands to open windows within <a href="https://github.com/tmux/tmux#readme">tmux</a> sessions directly from the editor. Even though it seemed mostly finished upon release, I ended up introducing several small things which unveiled a non-trivial number of edge cases which I’d wished were covered by tests. I hadn’t approached unit testing within a Sublime package before (or in Python!), but this was a definite time to get started.</p><p>At <a href="https://digital-detox.co.uk/">Digital Detox</a> we currently compose our JavaScript tests using <a href="https://facebook.github.io/jest/">Jest</a>, which allows us to rapidly iterate thanks the use of <a href="https://facebook.github.io/jest/docs/en/snapshot-testing.html">test snapshots</a>. This involves writing select data fragments (snapshots) to a file and diffing against these in subsequent runs, putting any changes to an API surface up for review. This approach would be a really perfect fit here, and as such I was determined try to make this — as far as I can tell — the first use of snapshot testing in a Sublime package.</p><h3>Writing unit tests for Sublime</h3><h4>Install SublimeText/UnitTesting</h4><p>This is the main testing framework for Sublime Text 3. It hosts tests written for Python’s built-in <em>unittest</em> framework and allows tests to interact with Sublime API features in the running editor. It is distributed itself as a Sublime package <a href="https://packagecontrol.io/packages/UnitTesting">installable from Package Control</a>. As of recently, it now lives under the official Sublime Text GitHub organisation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vQNSInwyoEr8i8sDXJY4CA.png" /><figcaption><a href="https://github.com/SublimeText/UnitTesting">UnitTesting on GitHub</a></figcaption></figure><h4>Define a test case</h4><p>With direction from the provided <a href="https://github.com/randy3k/UnitTesting-example/tree/master/tests">test examples</a>, I created a first <em>tests/test_open_tmux.py</em> module in my project, defining a test class inheriting from <em>unittest.TestCase</em>. With this, running <em>UnitTesting: Test Current Package</em> from the Command Palette executes all tests found in an output pane.</p><pre>test_run_current_file (test_open_tmux.TestOpenTmuxCommand) ... ok</pre><p>This first test sets a mock return value for the current directory path and invokes the <em>tmux_open</em> application command. Depending on what a particular plugin does, the availability of the <a href="https://www.sublimetext.com/docs/3/api_reference.html">Sublime API library</a> here provides the integration hook for calling whatever logic needs to be asserted.</p><p>I found mocking APIs with Python’s <a href="http://devdocs.io/python~3.6/library/unittest.mock#module-unittest.mock"><em>unittest.mock</em></a> library convenient to use. Here however, details for <em>self.subproc_popen_mock</em> have been omitted for conciseness. <em>TestCase </em>also provides a complete set of assertion methods.</p><pre>import sublime<br>from unittest import TestCase, mock</pre><pre>class TestOpenTmuxCommand(TestCase):<br>    @mock.patch(&#39;os.path.dirname&#39;)<br>    def test_run_current_file(self, os_dirname_mock):<br>        os_dirname_mock.return_value = &#39;/home/user/example-project&#39;<br>        sublime.active_window().run_command(&#39;open_tmux&#39;)<br> <br>        self.assertTrue(self.subproc_popen_mock.called)</pre><p>Pretty cool — but this is not the end. Next, we’re going to assert what was called using snapshots.</p><h3>Snapshot testing for Python is a thing!</h3><p>If you’re following what the subject package does, it ultimately makes command calls to the running tmux server:</p><blockquote>tmux new-window -c /home/alex/Development/dotfiles</blockquote><p>During execution, this takes the form of a <em>subprocess.Popen</em> instance with a given set of command arguments. So writing a snapshot assertion storing the <em>call_args</em> sent is all we need to do to cover these actions in their entirety for each test.</p><h4>The SnapshotTest library</h4><p>This all depended on the existence of a Jest-like snapshot test library for Python. Thankfully, the creator of <em>graphene</em> had exactly this in mind with <a href="https://github.com/syrusakbary/snapshottest"><em>SnapshotTest</em></a>, a relatively new testing module. We can install this using <em>pip</em>.</p><pre>pip install snapshottest</pre><h4>Updating our tests</h4><p><em>snapshottest.TestCase</em> is based on <em>unittest.TestCase</em>, which means we can substitute one for the other in our class definition, leaving all standard assertion methods available.</p><pre>import sublime<br>from unittest import mock<br>import snapshottest</pre><pre>class TestOpenTmuxCommand(snapshottest.TestCase):<br>    ...</pre><p>Our test code now becomes, in fact, less interesting. A single <em>self.assertMatchSnapshot</em> assertion call is all that’s required. This example also shows additional use and mocking of the Sublime window API.</p><pre>def test_run_unsaved_file_outside_project(self):<br>    view = sublime.active_window().new_file()<br> <br>    with mock.patch(&#39;sublime.Window.folders&#39;) as window_folders_mock:<br>        window_folders_mock.return_value = []<br> <br>        with mock.patch(&#39;os.path.expanduser&#39;) as home_dir_mock:<br>            home_dir_mock.return_value = &#39;/home/user&#39;<br>            sublime.active_window().run_command(&#39;open_tmux&#39;)<br> <br>    view.window().run_command(&#39;close_file&#39;)<br>    self.assertMatchSnapshot(self.subproc_popen_mock.call_args[0])</pre><p>When this runs (still from the <em>UnitTesting</em> plugin) an entry is written to the newly created file <em>tests/snapshots/snap_test_open_tmux.py</em> with the serialised snapshot data.</p><pre>snapshots[&#39;TestOpenTmuxCommand::test_run_unsaved_file_outside_project 1&#39;] = (<br>    [<br>        &#39;tmux&#39;,<br>        &#39;new-window&#39;,<br>        &#39;-c&#39;,<br>        &#39;/home/user&#39;<br>    ]<br>)</pre><p>Looks like the right command arguments (sent as a list)! And this saved output stands to assert it stays that way in future. Happy days.</p><h3>In review</h3><p>It worked! With this workflow validated, I finished writing a full set of snapshot tests covering the majority of <em>Sublime tmux</em>’s command scenarios which are <a href="https://github.com/huntie/sublime-tmux/tree/develop/tests">now merged</a>, and am now in a much better position to make future changes.</p><p>Storing snapshots for each test saved authoring time over defining custom assertions, but this was still a fairly lengthy exercise overall. Specific to this plugin, I definitely spent too much time working out how to correctly shape mock <em>Popen </em>calls for precheck commands, which led me to skip mocking a few more complicated test conditions for now.</p><p>For the most part, the <em>SnapshotTest</em> library worked well, and will most likely be a part of my next general Python project. More work is needed to get tests working in CI however, as the Sublime <em>UnitTesting</em> package runs slightly differently <a href="https://github.com/randy3k/UnitTesting-example/blob/master/.circleci/config.yml">as configured</a> and can’t find the <em>snapshottest</em> dependency. This is something I hope to resolve soon though.</p><p>All the tests written<em> </em>can be viewed <a href="https://github.com/huntie/sublime-tmux/tree/develop/tests">here</a>. If you’re a Sublime package maintainer and are inspired to try any of this out for yourself, please feel free comment below with any questions or feedback, or message me <a href="https://twitter.com/alxhnt">on Twitter</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ef8aaecde945" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Representing a UUID as a Base62 Hash ID for short, pretty URLs]]></title>
            <link>https://medium.com/@huntie/representing-a-uuid-as-a-base-62-hash-id-for-short-pretty-urls-c30e66bf35f9?source=rss-e54e8fe3cbbe------2</link>
            <guid isPermaLink="false">https://medium.com/p/c30e66bf35f9</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[hashids]]></category>
            <category><![CDATA[uuid]]></category>
            <dc:creator><![CDATA[Alex Hunt]]></dc:creator>
            <pubDate>Fri, 06 Jan 2017 20:04:26 GMT</pubDate>
            <atom:updated>2021-01-11T12:13:27.411Z</atom:updated>
            <content:encoded><![CDATA[<p>UUIDs are awesome — they provide a non-sequential, comprehensively unique identifier that can be generated separately from the source database. However, if you need to include one in a URL they can appear somewhat inelegant.</p><blockquote>2b8b9396-0cdf-4b9c-a03d-b25e1d93601b</blockquote><p>Recently, I was migrating a PHP application and database from auto increment to UUID 4 strings. In several instances, resource IDs had been referenced in the URL and obfuscated to a short, clean hash ID padded to six characters in length. These identifiers still needed to exist, and we needed to use UUIDs — so would they need to be displayed everywhere in full?</p><p>The Hash IDs package used generates identifiers from characters in the range [0-9A-Za-z]. This is an encoding with a 62 digit base; a subset of the characters valid in a URL path. So remove the fixed-position separators from the UUID above, and it turns out that you have a hexadecimal value that can be shortened significantly in this denser space.</p><h4>First attempt: Convert to base 10 to pass an integer to Hashids</h4><p>Initially, I looked to simply convert the de-hyphenated UUID to a decimal number, and leave the <em>Hashids</em> implementation unchanged.</p><pre>// Not decodable<br>Hashids::encode((int) base_convert(str_replace(&#39;-&#39;, &#39;&#39;, &#39;2b8b9396-0cdf-4b9c-a03d-b25e1d93601b&#39;), 16, 10));</pre><p>Sadly, the intermediate value above can exceed the size of PHP’s <em>int</em> type, and the returned string will be sliced before it is passed to the Hashids <em>encode</em> function. Furthermore, <em>base_convert</em> internally doesn’t support anything above base 32.</p><h4>Fixing the base conversion</h4><p>This was going to require higher precision numbering. We are going to use the PHP GMP library to write a new helper function:</p><pre>function gmp_base_convert(string $value, int $initialBase, int $newBase): string<br>{<br>    return gmp_strval(gmp_init($value, $initialBase), $newBase);<br>}</pre><p>Now, the UUID string can be suitably reduced:</p><pre>public static function encode(string $uuid): string<br>{<br>    return gmp_base_convert(str_replace(&#39;-&#39;, &#39;&#39;, $uuid), 16, 62);<br>}</pre><p>This returns an identifier of 1KASJZ7DWmusQjtvucYnoh. Not as short as six characters, but 61% of the length of the original UUID. Even at a maximum value consisting solely of the character f, this hash ID representation won’t exceed 22 characters. And with the UUID implementation, it’s highly unlikely that it will generate one smaller either.</p><p>To decode, it’s just as simple, with a bit more code to reinsert the UUID separators.</p><pre>public static function decode(string $hashid): string<br>{<br>    return array_reduce([20, 16, 12, 8], function ($uuid, $offset) {<br>        return substr_replace($uuid, &#39;-&#39;, $offset, 0);<br>    }, str_pad(gmp_base_convert($hashid, 62, 16), 32, &#39;0&#39;, STR_PAD_LEFT));<br>}</pre><p>We now have a simple implementation for accurately encoding and decoding UUIDs between a compact, obscured format!</p><h3>Going further</h3><p>For now, this solution is certainly practical enough for our needs. The <a href="http://hashids.org/">hashids.org specification</a> does a bit more – notably supporting a salt<em> </em>value to shuffle the output alphabet. Setting a minimum padded size is now a smaller concern, since data is distributed along the UUID string.</p><p>The final string for our URLs isn’t as short as before, but certainly acceptable. Nothing smaller can be generated without the tradeoff of switching away from full-length UUIDs, or otherwise by building something more bespoke. If you’re considering that route, I found <a href="https://eager.io/blog/how-long-does-an-id-need-to-be/">“How Long Does an ID Need To Be”</a> a great read.</p><p><strong>Update 07 Feb 2017<br></strong>When decoding, a shorter hex string representation would be returned from <em>gmp_strval</em> if it had a leading zero. <em>str_pad</em> has been added to the <em>decode</em> method above to fix this.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c30e66bf35f9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>