<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>Rotendahl&#39;s Blog</title>
<link>https://rotendahl.dk/</link>
<atom:link href="https://rotendahl.dk/index.xml" rel="self" type="application/rss+xml"/>
<description>Posts about programming, math and bad jokes</description>
<generator>quarto-1.4.554</generator>
<lastBuildDate>Sat, 02 Mar 2024 00:00:00 GMT</lastBuildDate>
<item>
  <title>Themes &amp; checklists</title>
  <dc:creator>Benjamin Rotendahl</dc:creator>
  <link>https://rotendahl.dk/posts/themes-and-checklists/themes-and-checklists.html</link>
  <description><![CDATA[ 





<p><img src="https://rotendahl.dk/assets/checklist.jpeg" class="img-fluid"></p>
<section id="checklists-templates" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Checklists &amp; templates</h1>
<p>For the last couple of years, mainly after reading the books <a href="https://jamesclear.com/atomic-habits">Atomic Habits</a> and <a href="https://atulgawande.com/book/the-checklist-manifesto/">Checklist Manifesto</a>, I’ve used templates based checklists, such lists can change habits, behaviors, and of course avoid mistakes.</p>
<p>As an example of the latter, imagine you are on a trip/vacation and realize you forgot to pack some item your favorite sunglasses, phone charger, or something else. What action can you take to avoid this?</p>
<p>By maintaining a <em>trip packing</em> checklist you can add the missing item to this list and be confident that you will not forget it again.</p>
<p>For the behavior change part, I’ve add actions such as <code>read an article/chapter</code> when I wanted to read more, and added <code>plan exercise</code> to my weekly review checklist to help me exercise more. Each day I would invoke my daily checklist template and run through it over the course of the day.</p>
<section id="enter-daily-themes" class="level2" data-number="1.1">
<h2 data-number="1.1" class="anchored" data-anchor-id="enter-daily-themes"><span class="header-section-number">1.1</span> Enter daily themes</h2>
<p>As the current daily checklist has grown, and grown it’s become harder to consistently complete it. I take this a sign that the checklist has worked and more actions just kept creeping in.</p>
<p>To solve this creep I’ve instead split it out to a base checklist with core actions such as <code>Clean unread emails</code> that should be completed every day and a variety of <em>themed</em> checklist. My current themes are:</p>
<ul>
<li>Health: Exercise, relax, and sleep well.</li>
<li>Project: Work on a side project, build something fun.</li>
<li>Learning: Learn something new.</li>
<li>Social: Connect with friends and family.</li>
<li>Admin: Handle laundry, emails, and other administrative tasks/chores.</li>
</ul>
<p>To my weekly review checklist, I’ve added a task to plan the themes for the upcoming week. Not every theme gets a day, I might not have an <em>admin</em> day every week, opting instead for an extra project/health day and two social days over the weekend.</p>
</section>
<section id="setting-up-the-tech-for-checklists" class="level2" data-number="1.2">
<h2 data-number="1.2" class="anchored" data-anchor-id="setting-up-the-tech-for-checklists"><span class="header-section-number">1.2</span> Setting up the tech for checklists</h2>
<p>I’ve set up a system that can manage the checklists templates and handle the logic of choosing which theme to invoke. It can also execute side effects I.e logging the choice of template to a sheet etc. The requirements for the tech stack are:</p>
<ul>
<li>I should be able to easily active the template from anywhere.
<ul>
<li>This means that I should be able to activate it from at least my phone, if it required access to my mac that would suck. It would also be nice if I was still able to active it from my mac, iPad, etc. So I don’t need to get out my phone if I’m already at my mac.</li>
</ul></li>
<li>It should be easy to update templates and add new ones.</li>
<li>It should be stable and require as little maintenance as possible.</li>
</ul>
<p>Given the above requirements, there obvious choice for orchestrating the actions fell on The <em>Shortcuts</em> app for apple’s platforms. This allows me to activate the template from any of my devices, meeting the first requirement.</p>
<p>I did consider making my own backend in Haskell/Rust and getting the templates via an API call from shortcuts. While this could be super fun project, it would require more maintenance than I would like, having to run a server and keep code up to date.</p>
<p>Instead I’ll manage templates via a folder in ICloud Drive, since the templates are plain text in <a href="https://guide.taskpaper.com/getting-started/">task paper</a> format I can update from all my devices, and it will be easy to move somewhere else if I ever want to.</p>
<p>A problem I found after setting this up is that the shortcut “Get File” action fails if the file is not downloaded from ICloud drive. This is super annoying as the fix is to open the files app, find the folder, click download and run the action again. I hope this get’s fixed by running the shortcut often enough that iCloud drives never decides to offload it from local to remote storage. For now I’ve moved all file reading to the top of the script to ensure it side effects are only run if all files are present</p>
<section id="log-of-all-themes" class="level3" data-number="1.2.1">
<h3 data-number="1.2.1" class="anchored" data-anchor-id="log-of-all-themes"><span class="header-section-number">1.2.1</span> Log of all themes</h3>
<p>Only activating a checklist would be fine if all i wanted was a prompt asking which theme I want. I prefer setting all the weeks themes in my calendar and having the system pick the right one. To achieve this I’ve created a new calender delegation called daily themes and added all day events with a selected theme for each day. This works amazingly well with the calendar sets of <a href="https://flexibits.com/fantastical-ios/help/calendar-sets">fantastical</a>. This means I can use my calendar as a way to select themes.</p>
<p>I’ve also created a sheet in <a href="https://www.apple.com/numbers/">apple numbers</a> to log which theme was selected and when it was completed. This enabled me to see distrution of themes over time and if I’m completing the themes I’ve set.</p>
</section>
</section>
<section id="the-daily-checklist-script" class="level2" data-number="1.3">
<h2 data-number="1.3" class="anchored" data-anchor-id="the-daily-checklist-script"><span class="header-section-number">1.3</span> The daily checklist script</h2>
<p>The actions performed by the script are:</p>
<ul>
<li>Invoke the base checklist</li>
<li>Check if my work calendar has any events and if so invoke the <em>work checklist</em></li>
<li>Get theme from calendar, if no theme is set, ask for one and set it in the calendar</li>
<li>Invoke the theme checklist</li>
</ul>
<p>A shortcut that performs the above actions can seen in figure Figure&nbsp;1.</p>
<div id="fig-shortcuts" class="quarto-figure quarto-figure-center quarto-float anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-shortcuts-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://rotendahl.dk/assets/daily-theme-script.png" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-shortcuts-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Script/shortcut for my daily checklist
</figcaption>
</figure>
</div>
<!-- References -->


</section>
</section>

 ]]></description>
  <category>productivity</category>
  <guid>https://rotendahl.dk/posts/themes-and-checklists/themes-and-checklists.html</guid>
  <pubDate>Sat, 02 Mar 2024 00:00:00 GMT</pubDate>
  <media:content url="https://rotendahl.dk/assets/checklist.jpeg" medium="image" type="image/jpeg"/>
</item>
<item>
  <title>Kick-ass terminal setup</title>
  <dc:creator>Benjamin Rotendahl</dc:creator>
  <link>https://rotendahl.dk/posts/terminal-setup/</link>
  <description><![CDATA[ 





<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://imgs.xkcd.com/comics/borrow_your_laptop.png" class="img-fluid figure-img"></p>
<figcaption>https://xkcd.com/1806</figcaption>
</figure>
</div>
<section id="introduction" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introduction</h1>
<p>Back at university the first programming course was taught using emacs in a terminal, none of that fancy GUI editors. My TA strongly encouraged everyone to follow their mantra of:</p>
<blockquote class="blockquote">
<p>If it can be done in the terminal, it should be done in the terminal</p>
</blockquote>
<p>I took this to heart and have spent way to much time over the years playing around and customizing my shell. In this post I will describe how my shell is configured and how the individual parts work. I’ll assume that you have some familiarity with the shell and not focus on specific workflows. My setup is for MacOS, but most should apply to linux/WSL as well.</p>
</section>
<section id="picking-a-terminal-emulator" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> Picking a terminal emulator</h1>
<p>The first step is to pick a program for our OS to display a terminal window. Your OS comes with one built in, for MacOs it’s the aptly named Terminal.app. The built in app is <em>fine</em> but has a limited feature set. I use <a href="https://iterm2.com">iterm2</a>, and have done so for almost a decade. Iterm is Mac only, if you want a cross platform emulator I’ve heard good things about the electron based <a href="https://hyper.is/">hyper.is</a>.</p>
<section id="setting-up-iterm2" class="level2" data-number="2.1">
<h2 data-number="2.1" class="anchored" data-anchor-id="setting-up-iterm2"><span class="header-section-number">2.1</span> Setting up Iterm2</h2>
<p>First install Iterm2 either via the download link on their <a href="https://iterm2.com">website</a> or via brew as described in Section&nbsp;3.</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode zsh code-with-copy"><code class="sourceCode zsh"><span id="cb1-1">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">brew</span> install iterm2</span></code></pre></div>
<p>Iterm2 has an almost comical amount of configuration options and features. For a full list see their <a href="https://iterm2.com/documentation.html">documentation</a> and <a href="https://iterm2.com/features.html">feature overview</a>. I’ll go over the most important parts.</p>
</section>
<section id="fonts" class="level2" data-number="2.2">
<h2 data-number="2.2" class="anchored" data-anchor-id="fonts"><span class="header-section-number">2.2</span> Fonts</h2>
<p>Fonts are becoming a more integral part of the terminal experience. Fonts are no longer just how letters are rendered. Via ligatures, icons, and powerline glyphs fonts affect how the terminal looks and functions. The <a href="https://nerdfonts.com">Nerd fonts</a> project is a collection of programmer friendly fonts with all the features above enabled. To get them on your mac you can use brew:</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">brew</span> tap homebrew/cask-fonts</span>
<span id="cb2-2">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">brew</span> install font-hack-nerd-font</span></code></pre></div>
<p>I prefer the <a href="https://github.com/tonsky/FiraCode">Fira code</a> font, but all the nerd fonts are quite nice. In iterm you have to set the font for each profile you use and enable ligatures. For a better powerline/glyph experience don’t use the monospaced version of the fonts as the icons will be to small.</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rotendahl.dk/posts/terminal-setup/sample-font.png" class="img-fluid figure-img"></p>
<figcaption>Some Haskell code in the Fira Code font, with ligatures</figcaption>
</figure>
</div>
</section>
<section id="profiles-and-themes" class="level2" data-number="2.3">
<h2 data-number="2.3" class="anchored" data-anchor-id="profiles-and-themes"><span class="header-section-number">2.3</span> Profiles and themes</h2>
<p>I have the following three profiles in iterm:</p>
<ul>
<li><strong>Free floating:</strong> My default profile used for new windows (<span class="visually-hidden">Cmd-n</span>).</li>
<li><strong>Hotkey window:</strong> Pressing (<span class="visually-hidden">Ctrl-Ctrl</span>), drops down a window in “visor mode” from the top of the screen. Once you get used to always having a shell a quick double tap away it’s hard to go back.</li>
<li><strong>Danger Zone:</strong> A profile automatically trigged for when my K8s context is my non default.</li>
</ul>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true">Free Floating</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false">Hot key window</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-3" aria-controls="tabset-1-3" aria-selected="false">Danger Zone</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div id="fig-profile" class="quarto-figure quarto-figure-center quarto-float anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-profile-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://rotendahl.dk/posts/terminal-setup/free.png" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-profile-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: The default “floating” window profile
</figcaption>
</figure>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><video src="visor.mp4" class="img-fluid" controls=""><a href="visor.mp4">Video</a></video></p>
<figcaption>Visor drop down mode</figcaption>
</figure>
</div>
</div>
<div id="tabset-1-3" class="tab-pane" aria-labelledby="tabset-1-3-tab">
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rotendahl.dk/posts/terminal-setup/danger-zone.png" class="img-fluid figure-img"></p>
<figcaption>Theme automatically activated, once I’m in a non local k8s context</figcaption>
</figure>
</div>
</div>
</div>
</div>
<section id="automatic-profile-switching" class="level4">
<h4 class="anchored" data-anchor-id="automatic-profile-switching">Automatic profile switching</h4>
<p>At my current job each developer has their own kubernetes cluster and we often use each other’s or switch to the staging/test cluster. I’ve setup that any non default cluster activates the <em>danger zone</em> profile. Having a clear visual distinction between default and non default contexts makes me more comfortable running commands such as <code>helm rollback</code>.</p>
<p>The feature to <a href="https://iterm2.com/documentation-automatic-profile-switching">automatically switch profile</a> in Iterm is not made for k8s. Instead it listens to username and hosts which works for ssh based workflows. My workaround has been to add a <a href="https://iterm2.com/documentation-triggers.html">trigger</a> to all my profiles that report the username and host as <code>prod@prod</code> whenever the regEx <code>\udb84\udcfe (?!brotual)</code><sup>1</sup> does not match my prompt.</p>
</section>
</section>
<section id="theme" class="level2" data-number="2.4">
<h2 data-number="2.4" class="anchored" data-anchor-id="theme"><span class="header-section-number">2.4</span> Theme</h2>
<p>Besides the <em>eye catching</em> <a href="https://raw.githubusercontent.com/mbadolato/iTerm2-Color-Schemes/master/schemes/Unikitty.itermcolors">Unikitty</a> theme used for the Danger Zone profile my theme of choice is the is the awesome <a href="https://draculatheme.com">dracula theme</a>. Besides beeing pretty but not to flashy, there are implementations for almost any tool.</p>
<section id="embracing-the-light" class="level3" data-number="2.4.1">
<h3 data-number="2.4.1" class="anchored" data-anchor-id="embracing-the-light"><span class="header-section-number">2.4.1</span> Embracing the light</h3>
<p>Like a true programmer I prefer dark mode but from time to time I use a light theme for programming. Another benifit is the ability to quickly switch to a light theme, during a presentation/screencast to help you viewers see the code.</p>
<p>For the times when I’m in the mood for a light theme I use the light version of the <a href="https://ethanschoonover.com/solarized/">Solarized theme</a>. This is built in to Iterm. I’ve setup custom functions to quickly switch my entire computer between light and dark via the terminal.</p>
</section>
</section>
<section id="cool-iterm-features" class="level2" data-number="2.5">
<h2 data-number="2.5" class="anchored" data-anchor-id="cool-iterm-features"><span class="header-section-number">2.5</span> Cool Iterm features</h2>
<ul>
<li><span class="visually-hidden">cmd-⌥b</span> activates instant replay allowing you to scroll back through the output of the terminal. This is usefull when you want to go back to a point of time in an interactive tool such as <a href="https://k9scli.io">k9s</a></li>
<li><span class="visually-hidden">cmd-shift-m</span> Sets a <em>mark</em> in the shell allowing you to jump back to the mark with <span class="visually-hidden">cmd-shift-j</span>. Usefull for when you want to be able to quickly go back to specific part of a build output or training run.</li>
<li><span class="visually-hidden">cmd-⌥v</span> opens the advanced paste menu, allowing you to format your clipboard before entering it. Usefull for stripping newlines or escape sequences.</li>
<li>Split panes with <span class="visually-hidden">cmd-d</span> and <span class="visually-hidden">cmd-shift-d</span></li>
<li><span class="visually-hidden">cmd-shift-c</span> enters a vim like copy mode, usefull for making a selection without touching the mouse.</li>
<li>If you have the <a href="https://iterm2.com/documentation-shell-integration.html">shell integration</a> you can setup a shortcut to show the most recently used directories allowing you to quickly enter a directory you’ve been in recently.</li>
</ul>
</section>
<section id="meta-and-option" class="level2" data-number="2.6">
<h2 data-number="2.6" class="anchored" data-anchor-id="meta-and-option"><span class="header-section-number">2.6</span> Meta and option</h2>
<p>Shells have the concept of a <em>meta</em> <span class="visually-hidden">M</span> key, which when pressed with some letter key triggers some action. For instance in fish <span class="visually-hidden">M-e</span> does not mean type a special “é” but open the current line in the <code>$EDITOR</code>. This is a problem for us programmers plebs that use a non us keyboard layout. In Denmark we don’t have keys for <code>|,{},[]</code> instead they are written with some combination of <code>opt, shift, ctrl</code> and other keys. This means that we can’t use the meta key for it’s intended purpose directly. Iterm does not have a nice way of solving this, it can be setup such that the left option key is meta and the right one for special characters. This sucks, instead I made a small hack and added key sequences for the most common special characters. That and mapping caps lock to control gives a nice keyboard experience.</p>
</section>
<section id="saving-and-restoring-configurations" class="level2" data-number="2.7">
<h2 data-number="2.7" class="anchored" data-anchor-id="saving-and-restoring-configurations"><span class="header-section-number">2.7</span> Saving and restoring configurations</h2>
<p>All configuration in iterm2 is saved in a plist file. This is a xml file where configuration changes can be automatically saved and tracked. This makes it’s easy to set up a new machine when required and it’s easy to share a setup where the recipient only has to make minimal changes to get up and running.</p>
<p><a href="https://github.com/Rotendahl/dotfiles/blob/master/com.googlecode.iterm2.plist">My setup</a> is available on github. In the settings for iterm2 there is a section called “General” where you can load in the file above to get my config.</p>
</section>
</section>
<section id="sec-packagemanger" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Package manager</h1>
<p>Mac does not come with a package manager out of the box, luckily <a href="https://brew.sh">homebrew</a> exists. It’s an awesome package manager capable of installing cli tools, gui apps and even fonts. Common use case are:</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb3-1">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">brew</span> install mono <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Installs mono</span></span>
<span id="cb3-2">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">brew</span> search node <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Searches after all packages that has node in its name</span></span>
<span id="cb3-3">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">brew</span> update <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Updates brews list of packages</span></span>
<span id="cb3-4">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">brew</span> upgrade <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Upgrades all installed packages to their newest version</span></span>
<span id="cb3-5">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">brew</span> doctor <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># Built in troubleshooting, if you ever have problems run this</span></span></code></pre></div>
</section>
<section id="picking-a-shell" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Picking a Shell</h1>
<p>For the choice of shell the three common options are <a href="https://www.gnu.org/software/bash/">bash</a>, <a href="https://www.zsh.org">zsh</a> and <a href="https://fishshell.com">fish</a>. I’ve used all three at one point in time and settled on fish. Its the most modern of the three, nice syntax, good autocompletion and documentation. The only downside is that it’s not POSIX compliant, which means that scripts are not portable. In these devOps, infrastructure as code, times I’ve found it to be a non issue. In fish everything is a function, aliases are functions, completions are functions, etc. This limits the arcane concepts to learn and makes it easy to extend. I.e you don’t need to configure <code>$PS1</code> to get a nice prompt, just create a <code>fish_prompt</code> function.</p>
<section id="configuration" class="level2" data-number="4.1">
<h2 data-number="4.1" class="anchored" data-anchor-id="configuration"><span class="header-section-number">4.1</span> Configuration</h2>
<p>Where <em>bash</em> and <em>zsh</em> shells have an <code>.rc</code> file for configuration, fish has a directory <code>.config/fish</code>. This is where you put your functions, completions etc. Configuration in fish works differently than in <em>bash</em> and <em>zsh</em>, instead of setting variables and path each time on start, I.e in <code>.zshrc</code> you would have a lot of lines of the form:</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">export</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PATH</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"/Users/rotendahl/Tools:"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$PATH</span></span>
<span id="cb4-2"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">export</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PATH</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$PATH</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">:/Users/rotendahl/.dotnet/tools"</span></span>
<span id="cb4-3"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">export</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">PATH</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$HOME</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/.cabal/bin:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$HOME</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">/.ghcup/bin:</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$PATH</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb4-4"></span>
<span id="cb4-5"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">export</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">VISUAL</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>vim</span>
<span id="cb4-6"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">export</span> <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">EDITOR</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$VISUAL</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span></code></pre></div>
<p>This would then be executed each time you start a new shell. In fish you instead use <code>set -u &lt;varname&gt; &lt;value&gt;</code> to set a variable or <code>fish_add_path &lt;path&gt;</code>. This only need to be run once and the variable is set for all future shells. This is achieved via the <code>fish_variables</code> file. I set my values wia the <a href="https://github.com/Rotendahl/dotfiles/blob/master/install.sh">install script</a> in my config repo. While this approach feels more clean in that you only need to set a variable once, it can be tricky if you want to remove/delete a variable, as it’s not clear where it’s set besides the <code>fish_variables</code> file. Other fish specific configuration is adding aliases and abbreviations. <a href="https://fishshell.com/docs/current/cmds/abbr.html">Abbreviations</a> are a way to shorten commands, i.e <code>k</code> for <code>kubectl</code> but have it expand, I like this feature. You don’t have to type the full command but still get the knowledge of what is actually happining. I have set an abbreviation for <code>abbr --add rm trash</code> this means that when I type <code>rm</code> it will replace it with <code>trash</code> which is a tool that moves entities to the trash instead of deleting them immediately. By using an abbreviation instead of an alias I still get my muscle memory of typing <code>rm</code> but it’s clear that I’m not actually deleting the file. Had I instead just set an alias for <code>rm</code> to <code>trash</code> it would not be clear what’s going on.</p>
</section>
<section id="setting-up-completions" class="level2" data-number="4.2">
<h2 data-number="4.2" class="anchored" data-anchor-id="setting-up-completions"><span class="header-section-number">4.2</span> Setting up completions</h2>
<p>Fish has awesome autocompletion, it’s fast and works out of the box, it uses <em>man</em> pages to get a list of options and arguments which gives decent completions for most cli’s with zero configuration. Typing a partial command and pressing <span class="visually-hidden">tab</span> starts the autocomplete, if you press <span class="visually-hidden">shift-tab</span> it searches the description of flags and arguments. As an example if I want to go back to a previous revions of a deploy via helm I can search for it with <code>helm &lt;shift-tab&gt; (revions)</code> to give me the following:</p>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://rotendahl.dk/posts/terminal-setup/helm.png" class="img-fluid figure-img"></p>
<figcaption>Searching through completions</figcaption>
</figure>
</div>
<p>As mentioned all completions are functions which mean they can be as complex as the implementer wants. The <code>kubectl</code> autocompletions is a good example of this, when picking a deployment pressing tab will fetch current deployments from the cluster and present it. These dynamic completions are very powerfull and can be hard to go without once you’ve gotten used to them. Setting up these completions are typically done via the tool itself. For instance <code>kubectl</code> has a command <code>kubectl completion fish</code> which outputs the completions. My <a href="https://github.com/Rotendahl/dotfiles/blob/master/install.sh#L4">install file</a> adds these completions for the tools I use.</p>
</section>
<section id="custom-functions" class="level2" data-number="4.3">
<h2 data-number="4.3" class="anchored" data-anchor-id="custom-functions"><span class="header-section-number">4.3</span> Custom Functions</h2>
<p>Fish makes it easy to write custom functions, my functions can be found in the <a href="https://github.com/Rotendahl/dotfiles/tree/master/fish/functions">config repo</a>. The functions are all fairly simple and small quality of life improvements. For instance, there is a custom cat function that uses <a href="https://github.com/eddieantonio/imgcat">imgcat</a> to display images when catting them, <a href="https://github.com/sharkdp/bat">bat</a> if it’s interactive, otherwise just the normal cat. There is a function for opening a google search with the args i.e</p>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb5-1">  <span class="ex" style="color: null;
background-color: null;
font-style: inherit;">google</span> how to quit vim<span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">?</span></span></code></pre></div>
<p>opens a browser with the search results. The only non custom function is the <code>fish_greeting.fish</code> which is a special function that is run on shell startup.</p>
</section>
<section id="oh-my-posh" class="level2" data-number="4.4">
<h2 data-number="4.4" class="anchored" data-anchor-id="oh-my-posh"><span class="header-section-number">4.4</span> Oh my Posh</h2>
<p>The last part of my setup is the prompt, beeing fish the prompt is also a function and not just a string saved as <code>$PS1</code>. The prompt is setup via <a href="https://ohmyposh.dev">oh my posh</a> which is a framework for bulding prompts via a yaml file. My prompt shows the current directory, git branch, if there are uncommited changes, the version of relevant libraries (i.e net7, etc) and the current kubernetes context, powerlevel and exit code. Oh-my-posh is extremly customizable and can be a time sink, for my config see <a href="https://github.com/Rotendahl/dotfiles/blob/master/oh-my-posh.yaml">oh-my-posh.yaml</a></p>
</section>
</section>
<section id="password-cli" class="level1" data-number="5">
<h1 data-number="5"><span class="header-section-number">5</span> 1password CLI</h1>
<p>The <a href="https://developer.1password.com/docs/cli/">1password CLI</a> allows me to use my password manager in my shell. It can be used in three different ways:</p>
<ul>
<li><p>Injecting secrets into a process: Runing this command:</p>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">op</span> run <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">$API_KEY</span>=DevVault/serviceA/testKey/-- python main.py</span></code></pre></div>
<p>will inject the secret <code>testKey</code> from the vault <code>serviceA</code> into the process as the environment variable <code>API_KEY</code>. Any output to stdout containing the key will be concealed.</p></li>
<li><p>As an SSH agent: My SSH key is stored in 1password and not in <code>~/.ssh</code> getting the key is now just a matter of running a command and placing my finger on the touch id sensor, this key is used for ssh access, authenticating git and signing commits.</p></li>
<li><p>As a CLI wrapper: <a href="https://cli.github.com">The github cli (gh)</a> the <code>gh</code> cli plugin normally saves a file with a token in <code>~/.config/gh/</code>. This token gives access to my github account. Instead I use the <a href="https://developer.1password.com/docs/cli/shell-plugins/github">1password cli plugin for gh</a>, this way no token is written to disk.</p></li>
</ul>
<p>The 1password cli provides several security benefits it while still being easy to use and in some cases increasing usability. For instance injecting secrets means I don’t have to create a bunch of <code>.env.local</code> files or similar. I can just clone a repo, inject the secrets into the process and run the code. The only downside to the CLI is that you need to authenticate many times. If you don’t have a bio-metric sensor such as touch ID, your milage may vary on the usability.</p>
</section>
<section id="conclusion" class="level1" data-number="6">
<h1 data-number="6"><span class="header-section-number">6</span> Conclusion</h1>
<p>Hope you found this interesting, and make a cool terminal setup of your own. k, thx, bye.</p>
<!--  References -->


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>The begining part is the unicode number for the kubernetes icon of the nerd font.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>terminal</category>
  <category>setup</category>
  <guid>https://rotendahl.dk/posts/terminal-setup/</guid>
  <pubDate>Fri, 07 Jul 2023 00:00:00 GMT</pubDate>
  <media:content url="https://rotendahl.dk/posts/terminal-setup/window.png" medium="image" type="image/png" height="117" width="144"/>
</item>
<item>
  <title>Overengineering a start page</title>
  <dc:creator>Benjamin Rotendahl</dc:creator>
  <link>https://rotendahl.dk/posts/start-page-generator/</link>
  <description><![CDATA[ 





<section id="introduction" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introduction</h1>
<p>The default start page in my browser of choice (safari) suck. The page looks nice but does not sync between devices, and shows a lot of useless information. Figure&nbsp;1 shows the safari start page. The “favorites” section is a copy of the links in my favorites bar, which are already visible in the browser. The “Shared with you” section is a list of links that my partner has shared with me, they are all old and was never relevant. I don’t use the reading list in safari, the entries are all read or added by mistake. The bottom row is the most useful part, as it allows me to access tabs from my iPad and iPhone, but the <a href="https://support.apple.com/en-gb/guide/safari/ibrwa2d73908/mac">tab group feature in safari</a> superseeds this feature.</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" aria-controls="tabset-1-1" aria-selected="true">Bad Safari start page</a></li><li class="nav-item"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" aria-controls="tabset-1-2" aria-selected="false">Awesome generated start page</a></li></ul>
<div class="tab-content">
<div id="tabset-1-1" class="tab-pane active" aria-labelledby="tabset-1-1-tab">
<div id="fig-safari" class="quarto-figure quarto-figure-center quarto-float anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-safari-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://rotendahl.dk/posts/start-page-generator/safari.png" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-safari-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;1: Safari’s bad startpage
</figcaption>
</figure>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" aria-labelledby="tabset-1-2-tab">
<div id="fig-generated" class="quarto-figure quarto-figure-center quarto-float anchored">
<figure class="quarto-float quarto-float-fig figure">
<div aria-describedby="fig-generated-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<img src="https://github.com/Rotendahl/Start-Page-Generator/blob/gh-pages/example.png?raw=true" class="img-fluid figure-img">
</div>
<figcaption class="quarto-float-caption-bottom quarto-float-caption quarto-float-fig" id="fig-generated-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Figure&nbsp;2: My Generated webpage
</figcaption>
</figure>
</div>
</div>
</div>
</div>
</section>
<section id="the-solution" class="level1" data-number="2">
<h1 data-number="2"><span class="header-section-number">2</span> The solution</h1>
<p>I would like my start page to present the configured links in a grid with the same layout on all devices. The links should be static to ensure “muscle memory” of their placement, similar to app placement on my phone home screen. The themeing should switch between light and dark mode depending on the system configuration. A design that meets these simple requirements can also be seen in figure Figure&nbsp;1.</p>
<p>Implementing this design is can be done with the following html and css:</p>
<div class="tabset-margin-container"></div><div class="panel-tabset">
<ul class="nav nav-tabs"><li class="nav-item"><a class="nav-link active" id="tabset-2-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-1" aria-controls="tabset-2-1" aria-selected="true">HTML</a></li><li class="nav-item"><a class="nav-link" id="tabset-2-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-2" aria-controls="tabset-2-2" aria-selected="false">style.css</a></li><li class="nav-item"><a class="nav-link" id="tabset-2-3-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-3" aria-controls="tabset-2-3" aria-selected="false">variables.css</a></li></ul>
<div class="tab-content">
<div id="tabset-2-1" class="tab-pane active" aria-labelledby="tabset-2-1-tab">
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode html code-with-copy"><code class="sourceCode html"><span id="cb1-1"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;!DOCTYPE</span> html<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-2"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">html</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-3">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">head</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-4">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">meta</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> charset</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"utf-8"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">/&gt;</span></span>
<span id="cb1-5">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">title</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>Start Page<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">title</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-6">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">link</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> rel</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stylesheet"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> href</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"variables.css"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">/&gt;</span></span>
<span id="cb1-7">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">link</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> rel</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"stylesheet"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> href</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"style.css"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">/&gt;</span></span>
<span id="cb1-8">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">head</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-9"></span>
<span id="cb1-10">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">body</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-11">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">div</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> id</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"container"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-12">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">a</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> href</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://site-a.com"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-13">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">img</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> src</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://logo-a.png"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">/&gt;</span></span>
<span id="cb1-14">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">p</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>Site A<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">p</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-15">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">a</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-16"></span>
<span id="cb1-17">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">a</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> href</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://site-b.com"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-18">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">img</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> src</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://logo-b.png"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">/&gt;</span></span>
<span id="cb1-19">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">p</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>Site B<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">p</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-20">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">a</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-21"></span>
<span id="cb1-22">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">a</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> href</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://site-c.com"</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-23">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">img</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> src</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"https://logo-c.png"</span><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;"> </span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">/&gt;</span></span>
<span id="cb1-24">        <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">p</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span>Site C<span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">p</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-25">      <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">a</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-26">    <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">div</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-27">  <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">body</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span>
<span id="cb1-28"><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&lt;/</span><span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">html</span><span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">&gt;</span></span></code></pre></div>
</div>
<div id="tabset-2-2" class="tab-pane" aria-labelledby="tabset-2-2-tab">
<script src="https://emgithub.com/embed-v2.js?target=https%3A%2F%2Fgithub.com%2FRotendahl%2FStart-Page-Generator%2Fblob%2Fmaster%2Fout%2Fstyle.css&amp;style=github-dark&amp;type=code&amp;showBorder=on&amp;showLineNumbers=on&amp;showFileMeta=on&amp;showFullPath=on&amp;showCopy=on"></script>
</div>
<div id="tabset-2-3" class="tab-pane" aria-labelledby="tabset-2-3-tab">
<script src="https://emgithub.com/embed-v2.js?target=https%3A%2F%2Fgithub.com%2FRotendahl%2FStart-Page-Generator%2Fblob%2Fmaster%2Fout%2Fvariables.css&amp;style=github-dark&amp;type=code&amp;showBorder=on&amp;showLineNumbers=on&amp;showFileMeta=on&amp;showFullPath=on&amp;showCopy=on"></script>
</div>
</div>
</div>
<p>To make the start page “responsive” we add a flexbox to the container div and set each link with a fixed max width. Adding the <code>flex-wrap: wrap</code> property ensures that the links flow nicely to the next row when making it work on phones and smaller browser windows as well.</p>
<section id="configuring-the-start-page" class="level2" data-number="2.1">
<h2 data-number="2.1" class="anchored" data-anchor-id="configuring-the-start-page"><span class="header-section-number">2.1</span> Configuring the start page</h2>
<p>To make the above html fit your taste requires:</p>
<ul>
<li>Setting your color theme in the <code>variables.css</code> file.</li>
<li>Adding <code>&lt;a&gt;</code> tags with each site you want to include.</li>
</ul>
<p>This last point is a bit tedious, but could be done in a few minutes, but that’s no fun.</p>
</section>
</section>
<section id="overengineering-the-solution" class="level1" data-number="3">
<h1 data-number="3"><span class="header-section-number">3</span> Overengineering the solution</h1>
<p>The above solution of manually adding the <code>&lt;a&gt;</code> tags would be the smart way to proceed. Instead we are going to overengineer a tool that will do this for us, while stretching our haskell muscles. We will create a custom data format, a parser and a template engine to generate the html above. Lastly we will setup a CI/CD pipeline to automatically deploy the start page to github pages.</p>
<section id="defining-the-favorite-sites-grammar." class="level2" data-number="3.1">
<h2 data-number="3.1" class="anchored" data-anchor-id="defining-the-favorite-sites-grammar."><span class="header-section-number">3.1</span> Defining the favorite sites grammar.</h2>
<p>A favorite site consists of a url, a display name and a logo, the last two are optional. We could have used a format such as json or csv as the configuration file format, but instead we create our own format. Listing&nbsp;1 shows an example of the format. The idea of the grammar is to have the non optional url in the center of a line, if there is a display name it precedes the url and <em>points</em><code>-&gt;</code> to the url defining the favorite. A logo optionally succeeds the url and <em>points</em> back <code>&lt;-</code> to it.</p>
<div id="lst-grammar-example" class="txt listing quarto-float">
<figure class="quarto-float quarto-float-lst figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-lst" id="lst-grammar-example-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Listing&nbsp;1: Example grammar instance
</figcaption>
<div aria-describedby="lst-grammar-example-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="sourceCode" id="lst-grammar-example" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="lst-grammar-example-1">some arbitrary text</span>
<span id="lst-grammar-example-2">* youtube.com/feed/subscriptions &lt;- &lt;logo-url&gt;</span>
<span id="lst-grammar-example-3">* Github.com</span>
<span id="lst-grammar-example-4">* Data Science -&gt; towardsdatascience.com</span>
<span id="lst-grammar-example-5">* Sentry.io</span>
<span id="lst-grammar-example-6">* Azure DevOps -&gt; dev.azure.com &lt;- &lt;logo-url&gt;</span></code></pre></div>
</div>
</figure>
</div>
<p>A links file can contain two types of text lines: comments and favorites. A comment is any line that does not start with a <code>*</code> and a favorite is any line that does. A favorite must have a url, but can optionally have a display name and a logo as specified above. The grammar for the favorite sites file can be seen in <a href="https://en.wikipedia.org/wiki/Backus–Naur_form">BNF</a> in Listing&nbsp;2.</p>
<div id="lst-grammar-bnf" class="txt listing quarto-float">
<figure class="quarto-float quarto-float-lst figure">
<figcaption class="quarto-float-caption-top quarto-float-caption quarto-float-lst" id="lst-grammar-bnf-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
Listing&nbsp;2: The favorite sites grammar in BNF form
</figcaption>
<div aria-describedby="lst-grammar-bnf-caption-0ceaefa1-69ba-4598-a22c-09a6ac19f8ca">
<div class="sourceCode" id="lst-grammar-bnf" style="background: #f1f3f5;"><pre class="sourceCode txt code-with-copy"><code class="sourceCode default"><span id="lst-grammar-bnf-1">  favorites ::= * &lt;favorite&gt; \n &lt;favorites&gt; | ε</span>
<span id="lst-grammar-bnf-2">  favorite ::= &lt;display-name&gt; -&gt; &lt;site&gt; | &lt;site&gt;</span>
<span id="lst-grammar-bnf-3">  display-name ::= &lt;text&gt;</span>
<span id="lst-grammar-bnf-4">  site ::= &lt;url&gt; | &lt;url&gt; &lt;- &lt;url&gt;</span>
<span id="lst-grammar-bnf-5">  url ::= &lt;url-spec&gt;</span></code></pre></div>
</div>
</figure>
</div>
<p>Where <code>&lt;text&gt;</code> is any string of characters that does not contain <code>-&gt;</code> or <code>&lt;-</code> or newlines and <code>&lt;url-spec&gt;</code> is any valid url. The grammar follows the <a href="https://hackage.haskell.org/package/parsec-3.1.3/docs/Text-Parsec-Token.html#v%3alexeme">lexeme</a> convention of parsing, where we allow arbitrary whitespace between tokens, and treat any lines not prefixed with a <code>*</code> as whitespace.</p>
<p>The grammar is non left recursive, non ambiguous requiring no precedence rules, the only downside of the grammar is that it requires lookahead for display names. When reading the lexeme following a <code>*</code> we don’t know if it is a display name or url until we observe a <code>-&gt;</code> or the lack of it. Since the display names are short this will not be a problem in practice<sup>1</sup>.</p>
<section id="full-grammer-file" class="level3" data-number="3.1.1">
<h3 data-number="3.1.1" class="anchored" data-anchor-id="full-grammer-file"><span class="header-section-number">3.1.1</span> Full grammer file</h3>
<script src="https://emgithub.com/embed-v2.js?target=https%3A%2F%2Fgithub.com%2FRotendahl%2FStart-Page-Generator%2Fblob%2Fmaster%2Flinks.txt&amp;style=github-dark&amp;type=code&amp;showBorder=on&amp;showLineNumbers=on&amp;showFileMeta=on&amp;showFullPath=on&amp;showCopy=on&amp;"></script>
</section>
</section>
<section id="creating-the-project" class="level2" data-number="3.2">
<h2 data-number="3.2" class="anchored" data-anchor-id="creating-the-project"><span class="header-section-number">3.2</span> Creating the project</h2>
<p>To setup our haskell project we use <a href="https://www.haskell.org/cabal/">cabal</a> and initialize the project with</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">cabal</span> init</span></code></pre></div>
<p>This will create a <a href="https://github.com/Rotendahl/Start-Page-Generator/blob/master/Start-Page-Generator.cabal">start-page-generator.cabal</a> file along with <em>lib</em> and <em>test</em> folders.</p>
<p>In typical haskell fashion we start by creating the types we need for our parser/(template engine). The types can be seen below:</p>
<script src="https://emgithub.com/embed-v2.js?target=https%3A%2F%2Fgithub.com%2FRotendahl%2FStart-Page-Generator%2Fblob%2Fmaster%2Flib%2FSiteTypes.hs&amp;style=github-dark&amp;type=code&amp;showBorder=on&amp;showLineNumbers=on&amp;showFileMeta=on&amp;showFullPath=on&amp;showCopy=on"></script>
<p>Note that the logo type is a type alias for the url. The <code>ParsedSite</code> type has the optional display name and logo url via the maybe monad. Our template engine will map between the <code>ParsedSite</code> and <code>Site</code> types filling in the optional fields. For the display name we use the hostname of the url if no display name is present. A missing logo url is filled in with <code>logo.clearbit.com//&lt;hostname&gt;</code> which provides a logo for most sites.</p>
<section id="testing" class="level3" data-number="3.2.1">
<h3 data-number="3.2.1" class="anchored" data-anchor-id="testing"><span class="header-section-number">3.2.1</span> Testing</h3>
Being a functional langauge Haskell has many great testing libaries from simple <a href="https://hackage.haskell.org/package/HUnit">unit testing</a> to <a href="https://hackage.haskell.org/package/QuickCheck">property based testing</a>. For this project we use the BDD style <a href="https://hspec.github.io/">hspec</a> library. The tests are simple assertions that given the specific input the code produces the expected output.
<script src="https://emgithub.com/embed-v2.js?target=https%3A%2F%2Fgithub.com%2FRotendahl%2FStart-Page-Generator%2Fblob%2Fmaster%2Ftest%2FTemplateEngineSpec.hs&amp;style=github-dark&amp;type=code&amp;showBorder=on&amp;showLineNumbers=on&amp;showFileMeta=on&amp;showFullPath=on&amp;showCopy=on"></script>
</section>
</section>
<section id="parsing-the-links-file" class="level2" data-number="3.3">
<h2 data-number="3.3" class="anchored" data-anchor-id="parsing-the-links-file"><span class="header-section-number">3.3</span> Parsing the links file</h2>
<p>Haskell is excellent for writing parsers, instead of writing clunky regexes that are hard to read and maintain we use the parser combinator library: <a href="https://hackage.haskell.org/package/parsec">parsec</a>.</p>
<p>The main idea behind parser combinators is that you build a parser for each part of the grammar and then combine them to build a parser for the entire grammar. For parsing a site we need to parse a ‘*’ character, an optional display name, followed by an url and an optional logo url. This gives us the parser</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode haskell code-with-copy"><code class="sourceCode haskell"><span id="cb3-1"><span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">parseSite ::</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">Parser</span> <span class="dt" style="color: #AD0000;
background-color: null;
font-style: inherit;">ParsedSite</span></span>
<span id="cb3-2">parseSite <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">do</span></span>
<span id="cb3-3">  _ <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> string <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"*"</span></span>
<span id="cb3-4">  whiteSpace</span>
<span id="cb3-5">  name <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> optionMaybe <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span> try parseDisplayName</span>
<span id="cb3-6">  url <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> parseUrl</span>
<span id="cb3-7">  logo <span class="ot" style="color: #003B4F;
background-color: null;
font-style: inherit;">&lt;-</span> optionMaybe <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">$</span> try parseLogo</span>
<span id="cb3-8">  <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">return</span> (name, url, logo)</span></code></pre></div>
<p>Which is much easier to read than a regex. See the full parser below.</p>
<script src="https://emgithub.com/embed-v2.js?target=https%3A%2F%2Fgithub.com%2FRotendahl%2FStart-Page-Generator%2Fblob%2Fmaster%2Flib%2FFavoritesParser.hs&amp;style=default&amp;type=code&amp;showBorder=on&amp;showLineNumbers=on&amp;showFileMeta=on&amp;showFullPath=on&amp;showCopy=on"></script>
</section>
<section id="generating-the-html" class="level2" data-number="3.4">
<h2 data-number="3.4" class="anchored" data-anchor-id="generating-the-html"><span class="header-section-number">3.4</span> Generating the html</h2>
<p>To map from the parsed we map from the parsed sites to the site type and replace the template tag in the html with the generated html.</p>
<script src="https://emgithub.com/embed-v2.js?target=https%3A%2F%2Fgithub.com%2FRotendahl%2FStart-Page-Generator%2Fblob%2Fmaster%2Flib%2FTemplateEngine.hs&amp;style=github-dark&amp;type=code&amp;showBorder=on&amp;showLineNumbers=on&amp;showFileMeta=on&amp;showFullPath=on&amp;showCopy=on"></script>
</section>
</section>
<section id="deploying-the-site" class="level1" data-number="4">
<h1 data-number="4"><span class="header-section-number">4</span> Deploying the site</h1>
<p>For deploying static html githubs pages is the obvious choice. Prior to setting up a deployment pipeline we first setup a testing pipeline. The testing pipeline uses the <a href="https://github.com/haskell/actions">Setup haskell</a> action which gives us a ghc environment. The test pipeline runs the tests and runs the <a href="https://github.com/ndmitchell/hlint">hlint</a> utility which ensures that the code is idiomatic haskell.</p>
<p>For the deploy pipeline it builds the code, persists the output to a branch called gh-pages and deploys the site to github pages. Both pipelines can be found on <a href="https://github.com/Rotendahl/Start-Page-Generator/tree/master/.github/workflows">github</a> and the deployed code can be found at <a href="https://rotendahl.dk/Start-Page-Generator/">rotendahl.dk/Start-Page-Generator/</a></p>


</section>


<div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">Footnotes</h2>

<ol>
<li id="fn1"><p>I’m not overengineering my grammer for my overengineered project.↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>front-end</category>
  <category>code</category>
  <category>haskell</category>
  <guid>https://rotendahl.dk/posts/start-page-generator/</guid>
  <pubDate>Sat, 13 May 2023 00:00:00 GMT</pubDate>
  <media:content url="https://github.com/Rotendahl/Start-Page-Generator/blob/gh-pages/example.png?raw=true" medium="image"/>
</item>
<item>
  <title>Mobile first games in processing</title>
  <dc:creator>Benjamin Rotendahl</dc:creator>
  <link>https://rotendahl.dk/posts/games-with-processing/games-with-processing.html</link>
  <description><![CDATA[ 





<p><img src="https://github.com/Rotendahl/brainGames/raw/master/screenshot.png" class="img-fluid"></p>
<section id="introduction" class="level1" data-number="1">
<h1 data-number="1"><span class="header-section-number">1</span> Introduction</h1>
<p>This post describes the implementation of three mobile-first web games created using Processing, a simple tool for building interactive drawings. The games were designed to be played on smartphones and were part of a larger campaign called “Who Controls Your Brain,” directed at middle school children to teach them about the brain. The digital games were a part of this educational effort and tasked the children with various cognitive tasks. The project was paid for by the school service of the University of Copenhagen.</p>
<p>The games works best on smartphones and can be played here: <a href="https://rotendahl.dk/brainGames/">https://rotendahl.dk/brainGames/</a> and the code is on <a href="https://github.com/Rotendahl/brainGames">github</a></p>
<section id="about-the-project" class="level2" data-number="1.1">
<h2 data-number="1.1" class="anchored" data-anchor-id="about-the-project"><span class="header-section-number">1.1</span> About the project</h2>
<p>The project was a freelance job for the university of Copenhagen’s School Service. It was part of a larger campaign called “Who controls your brain”, the campaign was directed at middle school children with the purpose of teaching them about their brains. These games were the digital part of the project and tasked them with various cognitive tasks.</p>
<section id="the-choice-of-framework" class="level3" data-number="1.1.1">
<h3 data-number="1.1.1" class="anchored" data-anchor-id="the-choice-of-framework"><span class="header-section-number">1.1.1</span> The choice of framework</h3>
<p>The kids were invited to a series of events where they would play them. We did not expect them to be able to install apps on their phones (Parental controls etc.) which meant that the games had to be web based. From my work in Coding Pirates I knew about <a href="http://processingjs.org">Processing</a> which is a simple way to create interactive drawings. A quick look at the <a href="http://processingjs.org/reference/">documentation</a> for processing reveals its main strength, the functions are few and basic enough that the learning curve is almost nonexistent. As an example the following snippet creates small circles in different colors based on the position of the mouse.</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode js code-with-copy"><code class="sourceCode javascript"><span id="cb1-1">setup <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>(){</span>
<span id="cb1-2">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">size</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">400</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-3">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">framerate</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-4">}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">background</span>(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-7"></span>
<span id="cb1-8">draw <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-style: inherit;">function</span>() {</span>
<span id="cb1-9">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">fill</span>(mouseX<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> mouseY<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-10">    <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">ellipse</span>(mouseX<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> mouseY<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span><span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">,</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">50</span>)<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span>
<span id="cb1-11">}<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">;</span></span></code></pre></div>
<p>A programmer armed with the documentation can start building games at once and not focus on lengthy tutorials. The downside of processing is that it’s mainly made for educational purposes and interactive art. This gave the following problems:</p>
<ul>
<li><p>Touch events are not natively supported, procressing only supports mouse clicks and can’t handle multitouch. This was mitigated by using the position of the <em>mouse</em>, for each frame the mouse position is reset so a change in position indicates a click. This is</p></li>
<li><p>It’s hard to get it to work in a responsive manner.</p></li>
<li><p>Processing was originally implemented in java and the port is not web first, leading to bad errors.</p></li>
<li><p>It’s Hard to interact with the DOM.</p></li>
</ul>
<p>A way to get around some of these problems is to use this excellent <a href="https://github.com/GildasP/jProcessingJS">extension</a> which makes it responsive and mobile friendly.</p>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Since I built my project the javascript version of processing has gotten a successor <a href="https://p5js.org">p5</a> which solves these problems.</p>
</div>
</div>
</section>
</section>
<section id="the-structure-of-a-game" class="level2" data-number="1.2">
<h2 data-number="1.2" class="anchored" data-anchor-id="the-structure-of-a-game"><span class="header-section-number">1.2</span> The structure of a game</h2>
<p>Each game starts with a html file that loads the framework and the css required to make it responsive, lastly it loads <em>.pde</em> files and creates a canvas</p>
<p>The next step is to load the images required using the preprocessing directive &gt; <code>/* @pjs preload="image1.jpg"; */</code></p>
<p>Using these directives it’s simple to create a loading screen and scale the images once they are loaded. The <code>setup</code> and <code>draw</code> functions are placed in the file <em>code.pde</em> which controls the game’s state and the core game logic. Each game has a file <em>game.pde</em> that contains the data structures and classes for each game.</p>
<section id="the-memory-game" class="level3" data-number="1.2.1">
<h3 data-number="1.2.1" class="anchored" data-anchor-id="the-memory-game"><span class="header-section-number">1.2.1</span> The memory game</h3>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://github.com/Rotendahl/brainGames/raw/master/assets/huske.png" class="quarto-figure quarto-figure-center figure-img" height="500"></p>
</figure>
</div>
<p>This game task the player to remember an ever increasing sequence of tiles. In the first level the user is shown three tiles numbered from one to three. After the timer runs out the numbers are hidden and the player must remember their order.</p>
<p>The game was made to show that we can “trick” our mind into remembering more information by storing it efficiently. First the kids had to check how they performed when trying to remember the positions of each tile. Afterwards they instead were told to remember the path and notice how their performance increased.</p>
<section id="implementation" class="level4">
<h4 class="anchored" data-anchor-id="implementation">Implementation</h4>
<p>The game’s logic starts with creating a grid of a <code>tile</code> class, which holds the position and value for each tile, it exposes methods for hiding/showing and drawing it self. The game randomly picks tiles corresponding to <img src="https://latex.codecogs.com/png.latex?levelNumber%20+%202"> ranks them from <img src="https://latex.codecogs.com/png.latex?%5B1,levelNumber%5D"> and draws them.</p>
<p>The state loop in <em>game.pde</em> starts a timer and turns tiles when needed. When the player loses they are given the option of posting their score or trying again.</p>
</section>
</section>
<section id="the-tabel-game" class="level3" data-number="1.2.2">
<h3 data-number="1.2.2" class="anchored" data-anchor-id="the-tabel-game"><span class="header-section-number">1.2.2</span> The “tabel” game</h3>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://github.com/Rotendahl/brainGames/raw/master/assets/tabel.png" class="quarto-figure quarto-figure-center figure-img" height="500"></p>
</figure>
</div>
<p>This game presents the player with at table that keeps scrolling down, at the bottom of the screen is the current “task” for the bottom row. The task can be picking the sum of two numbers, picking a number <img src="https://latex.codecogs.com/png.latex?n"> such that <img src="https://latex.codecogs.com/png.latex?3%20%5Cmid%20n">, etc. If the player fails a task the row goes red and one extra row will stay red for the rest of the game.</p>
<p>The goal of the game was to have the kids find a way of avoiding doing the actual computation and find a pattern that could solve the task, I.e <img src="https://latex.codecogs.com/png.latex?5%20%5Cmid%20n"> can be solved by finding a number that ends in 5 or 0.</p>
<section id="implementation-1" class="level4">
<h4 class="anchored" data-anchor-id="implementation-1">Implementation</h4>
<p>The game creates 5 rows containing 6 instances of the tile class. The rows scroll down and when they hit the bottom of the screen they are moved to the top again. When a row is “reset” the numbers on the tile are changed so they continue the sequence from the row nearest the top that isn’t red. The non red row closets to the bottom is the active one that the current task should be performed on. If a task is completed the row turns blue and active row and current task is changed.</p>
</section>
</section>
<section id="the-reaction-game" class="level3" data-number="1.2.3">
<h3 data-number="1.2.3" class="anchored" data-anchor-id="the-reaction-game"><span class="header-section-number">1.2.3</span> The reaction game</h3>
<div class="quarto-figure quarto-figure-center">
<figure class="figure">
<p><img src="https://github.com/Rotendahl/brainGames/raw/master/assets/reaktion.png" class="quarto-figure quarto-figure-center figure-img" height="600"></p>
</figure>
</div>
<p>This game tests the players reactions and ability to perform simple cognitive tasks. It was inspired by the <a href="http://www.batak.com">Batak board</a></p>
<p>The player has to hit a specified number of nodes in the graph, before the time runs out.</p>


</section>
</section>
</section>

 ]]></description>
  <category>front-end</category>
  <category>code</category>
  <category>games</category>
  <guid>https://rotendahl.dk/posts/games-with-processing/games-with-processing.html</guid>
  <pubDate>Thu, 13 Jul 2017 00:00:00 GMT</pubDate>
  <media:content url="https://github.com/Rotendahl/brainGames/raw/master/screenshot.png" medium="image" type="image/png"/>
</item>
</channel>
</rss>
