<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Josh O&#39;Rourke</title>
  <subtitle>Writing about software, machine learning, and healthcare.</subtitle>
  <link href="https://jpo.github.io/feed.xml" rel="self"/>
  <link href="https://jpo.github.io/"/>
  
    <updated>2012-08-09T00:00:00Z</updated>
  
  <id>https://jpo.github.io</id>
  <author>
    <name>Josh O&#39;Rourke</name>
    
      <email>dingier_baton.0p@icloud.com</email>
    
  </author>
  
    
    <entry>
      <title>Getting Started With Authlogic on Rails 3</title>
      <link href="https://jpo.github.io/2012/06/13/getting-started-with-authlogic-on-rails-3/"/>
      <updated>2012-06-13T00:00:00Z</updated>
      <id>https://jpo.github.io/2012/06/13/getting-started-with-authlogic-on-rails-3/</id>
      <content type="html">
        <![CDATA[
      <p><a href="http://github.com/binarylogic/authlogic">Authlogic</a> is “a clean, simple, and unobtrusive ruby authentication solution”. I’ve used it successfully in several Rails projects. It’s easy to setup, it’s easy to test, it’s flexible, and it has decent documentation. There’s even an <a href="http://github.com/binarylogic/authlogic_example">example application</a>.</p>
<p>In this post, I’m going to walkthrough how to quickly get up and running with Authlogic on Rails 3. This guide is setup so that at the end of each step you will have working software. When you’re finished, you will have a basic application that can authenticate users. Let’s get started!</p>
<h2>Prerequisites</h2>
<p>To follow along in this example, you’ll want to make sure that you have the following installed:</p>
<pre class="language-text"><code class="language-text">Ruby 1.9.3
Rails 3.2.3
Bundler 1.1.0</code></pre>
<h2>Step 1: A New Rails Project</h2>
<p>The first thing we’re going to do is generate a new Rails application. Open your terminal and run the command below in the directory of your choice.</p>
<pre class="language-bash"><code class="language-bash">rails new authlogic_example</code></pre>
<p>Next, let’s configure our new project to use the Authlogic gem. In your text editor, open the <code>Gemfile</code> add the line below.</p>
<pre class="language-ruby"><code class="language-ruby">gem <span class="token string-literal"><span class="token string">'authlogic'</span></span></code></pre>
<p>Finally, let’s install the gem using Bundler. In your terminal, run the command below.</p>
<pre class="language-bash"><code class="language-bash">bundle <span class="token function">install</span></code></pre>
<p>Let’s test our changes. In your terminal run the rails server command. Then, open you browser and navigate to <code>http://localhost:3000</code>. If the application is configured properly you should see the Rails welcome page.</p>
<h2>Step 2: The Models</h2>
<p>Our application will use two models: <code>User</code> and <code>UserSession</code>. The <code>User</code> model will be responsible for persisting users in the database and the <code>UserSession</code> model will be responsible for authenticating users and managing their sessions.</p>
<p>To generate these models, run the commands below in your terminal.</p>
<pre class="language-bash"><code class="language-bash">rails generate model User
rails generate authlogic:session UserSession</code></pre>
<p>Several files should have been created. For now we’re only interested in the <code>CreateUsers</code> migration that was created in the <code>db/migrate</code> folder. We’re going to add some columns that Authlogic will recognize and use to authenticate our users.</p>
<p>In your text editor, modify the <code>CreateUsers</code> migration to match the example below.</p>
<pre class="language-ruby"><code class="language-ruby"><span class="token comment"># db/migrate/*_create_users.rb</span>
<span class="token keyword">class</span> <span class="token class-name">CreateUsers</span> <span class="token operator">&lt;</span> ActiveRecord<span class="token double-colon punctuation">::</span>Migration
  <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">change</span></span>
    create_table <span class="token symbol">:users</span> <span class="token keyword">do</span> <span class="token operator">|</span>t<span class="token operator">|</span>
      t<span class="token punctuation">.</span>string <span class="token symbol">:email</span><span class="token punctuation">,</span> <span class="token symbol">null</span><span class="token operator">:</span> <span class="token boolean">false</span>
      t<span class="token punctuation">.</span>string <span class="token symbol">:crypted_password</span><span class="token punctuation">,</span> <span class="token symbol">null</span><span class="token operator">:</span> <span class="token boolean">false</span>
      t<span class="token punctuation">.</span>string <span class="token symbol">:password_salt</span><span class="token punctuation">,</span> <span class="token symbol">null</span><span class="token operator">:</span> <span class="token boolean">false</span>
      t<span class="token punctuation">.</span>string <span class="token symbol">:persistence_token</span><span class="token punctuation">,</span> <span class="token symbol">null</span><span class="token operator">:</span> <span class="token boolean">false</span>
      t<span class="token punctuation">.</span>timestamps
    <span class="token keyword">end</span>
<span class="token keyword">end</span></code></pre>
<p>Now let’s configure the User model. We’ll add the <code>acts_as_authlogic</code> macro to let Authlogic know that this is the model we’ll be authenticating with. We’ll also use <code>attr_accessible</code> to make the <code>email</code>, <code>password</code>, and <code>password_confirmation</code> attributes accessible to our controllers.</p>
<p>In your text editor, modify the <code>User</code> model to match the example below.</p>
<pre class="language-ruby"><code class="language-ruby"><span class="token comment"># app/models/user.rb</span>
<span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token operator">&lt;</span> ActiveRecord<span class="token double-colon punctuation">::</span>Base
  acts_as_authentic
  attr_accessible <span class="token symbol">:email</span><span class="token punctuation">,</span> <span class="token symbol">:password</span><span class="token punctuation">,</span> <span class="token symbol">:password_confirmation</span>
<span class="token keyword">end</span></code></pre>
<p>Next, we’re going to modify our database seed file and create a test user that we’ll use to authenticate. In your text editor, modify the seed file to match the example below.</p>
<pre class="language-ruby"><code class="language-ruby"><span class="token comment"># db/seeds.rb</span>
User<span class="token punctuation">.</span>create<span class="token punctuation">(</span><span class="token symbol">email</span><span class="token operator">:</span> <span class="token string-literal"><span class="token string">'josh@example.com'</span></span><span class="token punctuation">,</span> <span class="token symbol">password</span><span class="token operator">:</span> <span class="token string-literal"><span class="token string">'passw0rd'</span></span><span class="token punctuation">,</span> <span class="token symbol">password_confirmation</span><span class="token operator">:</span> <span class="token string-literal"><span class="token string">'passw0rd'</span></span><span class="token punctuation">)</span></code></pre>
<p>Lastly, we’re going to migrate our changes to the database and then seed it with our test user. In your terminal, run the commands below.</p>
<pre class="language-bash"><code class="language-bash">rake db:migrate
rake db:seed</code></pre>
<p>Let’s test our changes. In your terminal run the <code>rails console</code> commands to load our application. Then, enter the following statement: <code>User.first</code>. This will query the database and return the first user found. If the application is working properly, the user found should be the user that you used to seed the database.</p>
<h2>Step 3: The Controller</h2>
<p>With the models finished, we can now setup the controller. In this step, we’re going to create a <code>UsersSessionsController</code> that will be used to allow users to sign in and out of the application.</p>
<p>To generate the <code>UserSessionsController</code>, open your terminal and run the command below.</p>
<pre class="language-ruby"><code class="language-ruby">rails generate controller UserSessions</code></pre>
<p>Our controller will have four actions. Two will allow users to sign in, one will allow users to sign out, and one will show information about the user that’s currently signed in.</p>
<p>In your text editor, modify the <code>UserSessionsController</code> to match the example below.</p>
<pre class="language-ruby"><code class="language-ruby"><span class="token comment"># app/controllers/user_sessions_controller.rb</span>
<span class="token keyword">class</span> <span class="token class-name">UserSessionsController</span> <span class="token operator">&lt;</span> ApplicationController
  <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">new</span></span>
    <span class="token variable">@user_session</span> <span class="token operator">=</span> <span class="token class-name">UserSession</span><span class="token punctuation">.</span><span class="token keyword">new</span>
  <span class="token keyword">end</span>

  <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">create</span></span>
    <span class="token variable">@user_session</span> <span class="token operator">=</span> <span class="token class-name">UserSession</span><span class="token punctuation">.</span><span class="token keyword">new</span><span class="token punctuation">(</span>params<span class="token punctuation">[</span><span class="token symbol">:user_session</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token keyword">if</span> <span class="token variable">@user_session</span><span class="token punctuation">.</span>save
      flash<span class="token punctuation">[</span><span class="token symbol">:notice</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Sign in successful!"</span></span>
      redirect_to user_session_url
    <span class="token keyword">else</span>
      render <span class="token string-literal"><span class="token string">'new'</span></span>
    <span class="token keyword">end</span>
  <span class="token keyword">end</span>

  <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">show</span></span>
    user_session  <span class="token operator">=</span> UserSession<span class="token punctuation">.</span>find
    <span class="token variable">@current_user</span> <span class="token operator">=</span> user_session<span class="token punctuation">.</span>user
  <span class="token keyword">end</span>

  <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">destroy</span></span>
    user_session <span class="token operator">=</span> UserSession<span class="token punctuation">.</span>find
    user_session<span class="token punctuation">.</span>destroy
    redirect_to root_url
  <span class="token keyword">end</span>
<span class="token keyword">end</span></code></pre>
<p>Next, we’re going to create two views. The new view will display our sign in form and the show view will be what the user sees when they sign in.</p>
<p>In your text editor, create the new and show views as shown below.</p>
<pre class="language-erb"><code class="language-erb"><span class="token comment">&lt;!-- app/views/user_sessions/new.html.erb --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">></span></span>Sign In<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">></span></span>

<span class="token erb language-erb"><span class="token delimiter punctuation">&lt;%</span><span class="token ruby language-ruby"> <span class="token keyword">if</span> <span class="token variable">@user_session</span><span class="token punctuation">.</span>errors<span class="token punctuation">.</span>any<span class="token operator">?</span> </span><span class="token delimiter punctuation">%></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>error-explanation<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h3</span><span class="token punctuation">></span></span>The following errors occurred:<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h3</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span><span class="token punctuation">></span></span>
      <span class="token erb language-erb"><span class="token delimiter punctuation">&lt;%</span><span class="token ruby language-ruby"> <span class="token variable">@user_session</span><span class="token punctuation">.</span>errors<span class="token punctuation">.</span>full_messages<span class="token punctuation">.</span><span class="token keyword">each</span> <span class="token keyword">do</span> <span class="token operator">|</span>msg<span class="token operator">|</span> </span><span class="token delimiter punctuation">%></span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">></span></span><span class="token erb language-erb"><span class="token delimiter punctuation">&lt;%=</span><span class="token ruby language-ruby"> msg </span><span class="token delimiter punctuation">%></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">></span></span>
      <span class="token erb language-erb"><span class="token delimiter punctuation">&lt;%</span><span class="token ruby language-ruby"> <span class="token keyword">end</span> </span><span class="token delimiter punctuation">%></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
<span class="token erb language-erb"><span class="token delimiter punctuation">&lt;%</span><span class="token ruby language-ruby"> <span class="token keyword">end</span> </span><span class="token delimiter punctuation">%></span></span></code></pre>
<pre class="language-erb"><code class="language-erb"><span class="token erb language-erb"><span class="token delimiter punctuation">&lt;%=</span><span class="token ruby language-ruby"> form_for <span class="token variable">@user_session</span><span class="token punctuation">,</span> <span class="token symbol">url</span><span class="token operator">:</span> user_session_path <span class="token keyword">do</span> <span class="token operator">|</span>f<span class="token operator">|</span> </span><span class="token delimiter punctuation">%></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>
    <span class="token erb language-erb"><span class="token delimiter punctuation">&lt;%=</span><span class="token ruby language-ruby"> f<span class="token punctuation">.</span>label <span class="token symbol">:email</span> </span><span class="token delimiter punctuation">%></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">/></span></span>
    <span class="token erb language-erb"><span class="token delimiter punctuation">&lt;%=</span><span class="token ruby language-ruby"> f<span class="token punctuation">.</span>text_field <span class="token symbol">:email</span> </span><span class="token delimiter punctuation">%></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>
    <span class="token erb language-erb"><span class="token delimiter punctuation">&lt;%=</span><span class="token ruby language-ruby"> f<span class="token punctuation">.</span>label <span class="token symbol">:password</span> </span><span class="token delimiter punctuation">%></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">/></span></span>
    <span class="token erb language-erb"><span class="token delimiter punctuation">&lt;%=</span><span class="token ruby language-ruby"> f<span class="token punctuation">.</span>password_field <span class="token symbol">:password</span> </span><span class="token delimiter punctuation">%></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>

  <span class="token erb language-erb"><span class="token delimiter punctuation">&lt;%=</span><span class="token ruby language-ruby"> f<span class="token punctuation">.</span>submit <span class="token string-literal"><span class="token string">'Sign In'</span></span> </span><span class="token delimiter punctuation">%></span></span>
<span class="token erb language-erb"><span class="token delimiter punctuation">&lt;%</span><span class="token ruby language-ruby"> <span class="token keyword">end</span> </span><span class="token delimiter punctuation">%></span></span></code></pre>
<pre class="language-erb"><code class="language-erb"><span class="token comment">&lt;!-- app/views/user_sessions/show.html.erb --></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h2</span><span class="token punctuation">></span></span>Your Session<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h2</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>
  <span class="token erb language-erb"><span class="token delimiter punctuation">&lt;%=</span><span class="token ruby language-ruby"> flash<span class="token punctuation">[</span><span class="token symbol">:notice</span><span class="token punctuation">]</span> </span><span class="token delimiter punctuation">%></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>p</span><span class="token punctuation">></span></span>
  You're signed in as: <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>b</span><span class="token punctuation">></span></span><span class="token erb language-erb"><span class="token delimiter punctuation">&lt;%=</span><span class="token ruby language-ruby"> <span class="token variable">@current_user</span><span class="token punctuation">.</span>email </span><span class="token delimiter punctuation">%></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>b</span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>br</span><span class="token punctuation">/></span></span>
  <span class="token erb language-erb"><span class="token delimiter punctuation">&lt;%=</span><span class="token ruby language-ruby"> link_to <span class="token string-literal"><span class="token string">'Sign Out'</span></span><span class="token punctuation">,</span> user_session_path<span class="token punctuation">,</span> <span class="token symbol">method</span><span class="token operator">:</span> <span class="token string-literal"><span class="token string">'delete'</span></span> </span><span class="token delimiter punctuation">%></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>p</span><span class="token punctuation">></span></span></code></pre>
<p>Finally, we need to add the controller as a resource in our routes file. In your text editor, modify the routes files to match the example below.</p>
<pre class="language-ruby"><code class="language-ruby"><span class="token comment"># config/routes.rb</span>
resource <span class="token symbol">:user_session</span>
root to<span class="token operator">:</span> <span class="token string-literal"><span class="token string">'user_session#new'</span></span></code></pre>
<p>Let’s test our changes. In your terminal, run the <code>rails server</code> command. Then, open your browser and navigate to <code>http://localhost:3000</code>. You should see a login form. If the application is working properly you should be able to sign in with the test user that you created in the previous step.</p>
<h2>Step 4: Tests</h2>
<p>This final step will demonstrate how to test with Authlogic. We’re going to create a user to test with and a functional test that will verify that users can sign in and out of the application.</p>
<p>First, let’s create the test user. In your text editor, modify the users fixture to match the example below.</p>
<pre class="language-yaml"><code class="language-yaml"><span class="token comment"># test/fixtures/users.yml</span>
<span class="token key atrule">josh</span><span class="token punctuation">:</span>
  <span class="token key atrule">email</span><span class="token punctuation">:</span> josh@example.com
  <span class="token key atrule">password_salt</span><span class="token punctuation">:</span> &lt;%= salt = Authlogic<span class="token punctuation">:</span><span class="token punctuation">:</span>Random.hex_token %<span class="token punctuation">></span>
  <span class="token key atrule">crypted_password</span><span class="token punctuation">:</span> &lt;%= Authlogic<span class="token punctuation">:</span><span class="token punctuation">:</span>CryptoProviders<span class="token punctuation">:</span><span class="token punctuation">:</span>Sha512.encrypt('passw0rd' + salt) %<span class="token punctuation">></span>
  <span class="token key atrule">persistence_token</span><span class="token punctuation">:</span> &lt;%= Authlogic<span class="token punctuation">:</span><span class="token punctuation">:</span>Random.hex_token %<span class="token punctuation">></span></code></pre>
<p>Now, let’s write the tests. In your text editor, modify the <code>UserSessionsControllerTest</code> to match the example below.</p>
<pre class="language-ruby"><code class="language-ruby"><span class="token comment"># test/functional/user_sessions_controller_test.rb</span>
<span class="token keyword">require</span> <span class="token string-literal"><span class="token string">'test_helper'</span></span>
<span class="token keyword">require</span> <span class="token string-literal"><span class="token string">'authlogic/test_case'</span></span>

<span class="token keyword">class</span> <span class="token class-name">UserSessionsControllerTest</span> <span class="token operator">&lt;</span> ActionController<span class="token double-colon punctuation">::</span>TestCase
  setup <span class="token symbol">:activate_authlogic</span>

  test <span class="token string-literal"><span class="token string">'it should sign a user in on create'</span></span> <span class="token keyword">do</span>
    post <span class="token symbol">:create</span><span class="token punctuation">,</span> <span class="token symbol">user_session</span><span class="token operator">:</span> <span class="token punctuation">{</span>
      <span class="token symbol">email</span><span class="token operator">:</span> <span class="token string-literal"><span class="token string">'josh@example.com'</span></span><span class="token punctuation">,</span>
      <span class="token symbol">password</span><span class="token operator">:</span> <span class="token string-literal"><span class="token string">'passw0rd'</span></span><span class="token punctuation">,</span>
      <span class="token symbol">password_confirmation</span><span class="token operator">:</span> <span class="token string-literal"><span class="token string">'passw0rd'</span></span>
    <span class="token punctuation">}</span>

    assert_equal <span class="token string-literal"><span class="token string">'Sign in successful!'</span></span><span class="token punctuation">,</span> flash<span class="token punctuation">[</span><span class="token symbol">:notice</span><span class="token punctuation">]</span>
    assert_redirected_to home_url
  <span class="token keyword">end</span>

  test <span class="token string-literal"><span class="token string">'it should sign a user out on destroy'</span></span> <span class="token keyword">do</span>
    UserSession<span class="token punctuation">.</span>create<span class="token punctuation">(</span>users<span class="token punctuation">(</span><span class="token symbol">:josh</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    delete <span class="token symbol">:destroy</span>
    assert_nil UserSession<span class="token punctuation">.</span>find
    assert_redirected_to root_url
  <span class="token keyword">end</span>
<span class="token keyword">end</span></code></pre>
<p>Let’s test our changes. In your terminal, run the <code>rake test</code> command. If the tests are setup properly, both should pass.</p>
<h2>Conclusion</h2>
<p>Congratulations! You should be up and running with Authlogic. For more information about Authlogic, <a href="http://github.com/binarylogic/authlogic">check out the project on GitHub</a>. For a more in-depth example, <a href="http://github.com/binarylogic/authlogic_example">check out the example application</a>.</p>

    ]]>
      </content>
    </entry>
  
    
    <entry>
      <title>Vim Tip: How to Display Whitespace Characters</title>
      <link href="https://jpo.github.io/2012/06/15/vim-tip-how-to-display-whitespace-characters/"/>
      <updated>2012-06-15T00:00:00Z</updated>
      <id>https://jpo.github.io/2012/06/15/vim-tip-how-to-display-whitespace-characters/</id>
      <content type="html">
        <![CDATA[
      <p>One of the things that took me awhile to figure out when I started using Vim was how to display whitespace characters. Whitespace characters are characters that take up space in a document but are not visible to the user. Examples include spaces, tabs, and new-lines characters.</p>
<p>In this post, I’m going to explain how to configure Vim to display whitespace characters. I’ll also touch on problems you may run into and how to avoid them.</p>
<h2>Turning On Whitespace Characters</h2>
<p>Vim refers to the mode that is used to display whitespace characters as list mode. By default, it’s turned off. To turn it on you can enter set list on the Vim command line or add it to your vimrc file. To turn it off again, you can use the <code>set nolist</code> command.</p>
<h2>Setting Which Whitespace Characters Are Displayed</h2>
<p>List mode is configured to only show the end-of-line characters ($) by default. If you want to show more characters or configure how the characters are displayed you can set the <code>listchars</code> variable.</p>
<p><code>listchars</code> is a global variable whose value can be set to a comma-separated list of key/value pairs. The key represents the whitespace character and the value represents how it will be displayed. For example, the default value for <code>listchars</code> is <code>eol:$</code>. This means that when list mode is on end-of-line characters will show up as $.</p>
<p>Options that you can use with <code>listchars</code> are below. They were taken from the Vim help documentation.</p>
<pre class="language-text"><code class="language-text">eol - Character to show at the end of each line.
tab - Two characters to be used to show a tab.
trail - Character to show for trailing spaces.
extends - Character to show in the last column when the line continues beyond the right of the screen.
precedes - Character to show in the first column when the line continues beyond the left of the screen.
conceal - Character to show in place of concealed text, when conceallevel is set to 1.
nbsp - Character to show for a space.</code></pre>
<p>Here is how I’ve set <code>listchars</code> in my configuration:</p>
<pre class="language-vim"><code class="language-vim"><span class="token keyword">set</span> <span class="token builtin">listchars</span><span class="token operator">=</span><span class="token builtin">eol</span><span class="token punctuation">:</span>$<span class="token punctuation">,</span>nbsp<span class="token punctuation">:</span>_<span class="token punctuation">,</span><span class="token keyword">tab</span><span class="token punctuation">:</span><span class="token operator">></span><span class="token operator">-</span><span class="token punctuation">,</span>trail<span class="token punctuation">:</span>~<span class="token punctuation">,</span>extends<span class="token punctuation">:</span><span class="token operator">></span><span class="token punctuation">,</span>precedes<span class="token punctuation">:</span><span class="token operator">&lt;</span></code></pre>
<h2>It’s Not Working!</h2>
<p>If you’ve turned on list mode and can’t see whitespace characters, the issue may be with your colorscheme. List mode uses the <code>NonText</code> and <code>SpecialKey</code> highlighting groups to determine what color the whitespace characters will be when displayed. In some cases, these highlighting groups may be set to the same color as your background. In short, if the background is black and whitespace characters are black, you won’t see them! Check your colorscheme and make sure these groups are set to a distinct color.</p>
<h2>Further Reading</h2>
<p>The Vim help documentation is an excellent resource if you know how to find what you’re looking for. You can find more information about <code>list</code> and a <code>listchars</code> by entering <code>help list</code> or <code>help listchars</code> on the Vim command line.</p>

    ]]>
      </content>
    </entry>
  
    
    <entry>
      <title>Parsing Bookmarks With Nokogiri</title>
      <link href="https://jpo.github.io/2012/06/28/parsing-bookmarks-with-nokogiri/"/>
      <updated>2012-06-28T00:00:00Z</updated>
      <id>https://jpo.github.io/2012/06/28/parsing-bookmarks-with-nokogiri/</id>
      <content type="html">
        <![CDATA[
      <p>I’ve been working on a feature for one of my side projects that requires parsing bookmarks files that have been exported from a web browser. Most browsers export bookmarks in the Netscape Bookmark File Format. This format is an HTML document where the bookmarks are organized according to a standard structure. Microsoft has some helpful documentation <a href="http://msdn.microsoft.com/en-us/library/aa753582(v=vs.85).aspx">MSDN</a>.</p>
<p>While considering how I would implement the feature, I spent a few minutes searching for an existing Ruby gem that already provided the functionality I needed. But, I came up empty-handed. Knowing that Nokogiri was a popular choice among Rubyists for parsing HTML files, I decided to try to implement the feature with it instead.</p>
<p><a href="http://nokogiri.org/">Nokogiri</a>, for those of you who may be new to it, is “an HTML, XML, and SAX parser with the ability to search documents via XPath and CSS selectors” 1. It’s very fast and very powerful. The poject’s site has a few examples of how it can be used to <a href="http://nokogiri.org/tutorials/parsing_an_html_xml_document.html">parse</a>, <a href="http://nokogiri.org/tutorials/searching_a_xml_html_document.html">search</a>, and <a href="http://nokogiri.org/tutorials/modifying_an_html_xml_document.html">modify</a> HTML and XML documents.</p>
<p>If all you’re interested in is the URLs then parsing a Netscape Bookmark File is trivial. This can easily be done with Nokogiri in a few lines of code. Here’s an example:</p>
<pre class="language-ruby"><code class="language-ruby"><span class="token keyword">require</span> <span class="token string-literal"><span class="token string">'nokogiri'</span></span>
document <span class="token operator">=</span> Nokogiri<span class="token double-colon punctuation">::</span><span class="token constant">HTML</span><span class="token punctuation">(</span><span class="token builtin">File</span><span class="token punctuation">.</span>open<span class="token punctuation">(</span><span class="token string-literal"><span class="token string">'bookmarks.html'</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span>
document<span class="token punctuation">.</span>css<span class="token punctuation">(</span><span class="token string-literal"><span class="token string">'dt > a'</span></span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token keyword">each</span> <span class="token punctuation">{</span> <span class="token operator">|</span>a<span class="token operator">|</span> puts a<span class="token punctuation">[</span><span class="token string-literal"><span class="token string">'href'</span></span><span class="token punctuation">]</span> <span class="token punctuation">}</span></code></pre>
<p>This example uses CSS selectors to find all of the anchor tags (or bookmarks) in the document and retuns the text value of each one.</p>
<p>However, in addition to the bookmarks, I was interested in the folders as well. The reason being that I wanted the ability to use the folder names to categorize the bookmarks. This complicated things a bit. Because the bookmarks may be deeply nested within several levels of folders, I would have to traverse the document recursively much like you would if you were traversing a file system.</p>
<p>My first attempt at this was to use Nokogiri’s <a href="http://nokogiri.rubyforge.org/nokogiri/Nokogiri/XML/Node.html#M000308">traverse</a> feature to recursively walk through all of the nodes in the document. The problem with this approach was that traverse descends in to each node of the document as it’s encountered. Since HTML elements containing folder name and folder items were “siblings”, it was very difficult to determine what folder each bookmark belonged to.</p>
<p>After some trial and error, I ended up using a combination of recursion and XPath selectors. This approach allowed me to identify all the bookmarks in the current level of nesting, process them, and then descend to the next level. It works great and I’m pleased with the results. Here’s what I ended up with:</p>
<pre class="language-ruby"><code class="language-ruby"><span class="token comment"># bookmark_reader.rb</span>
<span class="token keyword">require</span> <span class="token string-literal"><span class="token string">'nokogiri'</span></span>

<span class="token keyword">class</span> <span class="token class-name">Bookmark</span>
  attr_accessor <span class="token symbol">:title</span><span class="token punctuation">,</span> <span class="token symbol">:url</span><span class="token punctuation">,</span> <span class="token symbol">:path</span>

  <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">initialize</span></span><span class="token punctuation">(</span>path<span class="token punctuation">,</span> title<span class="token punctuation">,</span> url<span class="token punctuation">)</span>
    <span class="token variable">@path</span>  <span class="token operator">=</span> path
    <span class="token variable">@title</span> <span class="token operator">=</span> title
    <span class="token variable">@url</span>   <span class="token operator">=</span> url
  <span class="token keyword">end</span>
<span class="token keyword">end</span>

<span class="token keyword">class</span> <span class="token class-name">BookmarkReader</span>
  <span class="token keyword">include</span> Enumerable

  <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">initialize</span></span><span class="token punctuation">(</span>path<span class="token punctuation">)</span>
    <span class="token variable">@path</span> <span class="token operator">=</span> path
  <span class="token keyword">end</span>

  <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">each</span></span>
    doc  <span class="token operator">=</span> Nokogiri<span class="token double-colon punctuation">::</span><span class="token constant">HTML</span><span class="token punctuation">(</span><span class="token builtin">File</span><span class="token punctuation">.</span>open<span class="token punctuation">(</span><span class="token variable">@path</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    node <span class="token operator">=</span> doc<span class="token punctuation">.</span>at_xpath<span class="token punctuation">(</span><span class="token string-literal"><span class="token string">'//html/body'</span></span><span class="token punctuation">)</span>
    traverse<span class="token punctuation">(</span>node<span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">'/'</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token operator">|</span>b<span class="token operator">|</span> <span class="token keyword">yield</span> b <span class="token punctuation">}</span>
  <span class="token keyword">end</span>

  <span class="token keyword">private</span>

  <span class="token keyword">def</span> <span class="token method-definition"><span class="token function">traverse</span></span><span class="token punctuation">(</span>node<span class="token punctuation">,</span> path<span class="token punctuation">,</span> <span class="token operator">&amp;</span>block<span class="token punctuation">)</span>
    anchors <span class="token operator">=</span> node<span class="token punctuation">.</span>search<span class="token punctuation">(</span><span class="token string-literal"><span class="token string">'./dt//a'</span></span><span class="token punctuation">)</span>
    folder_names <span class="token operator">=</span> node<span class="token punctuation">.</span>search<span class="token punctuation">(</span><span class="token string-literal"><span class="token string">'./dt/h3'</span></span><span class="token punctuation">)</span>
    folder_items <span class="token operator">=</span> node<span class="token punctuation">.</span>search<span class="token punctuation">(</span><span class="token string-literal"><span class="token string">'./dl'</span></span><span class="token punctuation">)</span>

    anchors<span class="token punctuation">.</span><span class="token keyword">each</span> <span class="token keyword">do</span> <span class="token operator">|</span>anchor<span class="token operator">|</span>
      <span class="token keyword">yield</span> <span class="token class-name">Bookmark</span><span class="token punctuation">.</span><span class="token keyword">new</span><span class="token punctuation">(</span>path<span class="token punctuation">,</span> anchor<span class="token punctuation">.</span>text<span class="token punctuation">,</span> anchor<span class="token punctuation">[</span><span class="token string-literal"><span class="token string">'href'</span></span><span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token keyword">end</span>

    folder_items<span class="token punctuation">.</span>size<span class="token punctuation">.</span>times <span class="token keyword">do</span> <span class="token operator">|</span>i<span class="token operator">|</span>
      folder_name <span class="token operator">=</span> folder_names<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
      folder_item <span class="token operator">=</span> folder_items<span class="token punctuation">[</span>i<span class="token punctuation">]</span>
      next_path   <span class="token operator">=</span> folder_name<span class="token punctuation">.</span><span class="token keyword">nil</span><span class="token operator">?</span> <span class="token operator">?</span> path <span class="token operator">:</span> <span class="token punctuation">[</span>path<span class="token punctuation">,</span> folder_name<span class="token punctuation">]</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token string-literal"><span class="token string">'/'</span></span><span class="token punctuation">)</span>
      traverse<span class="token punctuation">(</span>folder_item<span class="token punctuation">,</span> next_path<span class="token punctuation">,</span> <span class="token operator">&amp;</span>block<span class="token punctuation">)</span>
    <span class="token keyword">end</span>
  <span class="token keyword">end</span>
<span class="token keyword">end</span></code></pre>
<pre class="language-ruby"><code class="language-ruby"><span class="token comment"># USAGE</span>
reader <span class="token operator">=</span> <span class="token class-name">BookmarkReader</span><span class="token punctuation">.</span><span class="token keyword">new</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">'bookmarks.html'</span></span><span class="token punctuation">)</span>
reader<span class="token punctuation">.</span><span class="token keyword">each</span> <span class="token punctuation">{</span> <span class="token operator">|</span>b<span class="token operator">|</span> puts b<span class="token punctuation">.</span>title <span class="token punctuation">}</span></code></pre>
<p>Parsing the Netscape Bookmark File with Nokogiri ended up being a fun exercise and more of a challenge than I expected. If you have a better solution, I would love to hear from you.</p>

    ]]>
      </content>
    </entry>
  
    
    <entry>
      <title>Vim Tip: How to Use Vertical Guides</title>
      <link href="https://jpo.github.io/2012/06/29/vim-tip-how-to-use-vertical-guides/"/>
      <updated>2012-06-29T00:00:00Z</updated>
      <id>https://jpo.github.io/2012/06/29/vim-tip-how-to-use-vertical-guides/</id>
      <content type="html">
        <![CDATA[
      <p>Vertical guides are a feature of most modern text editors that are useful when aligning text. Programmers typically use these to ensure that their code is indented properly or doesn’t exceed a certain line length (typically 80 or 100 characters).</p>
<p>In Vim, this feature is called <code>colorcolumn</code>. When set, <code>colorcolumn</code> will change the color of one or more columns in the current window. Using this feature is easy. Simply set the <code>colorcolumn</code> variable to the value of the column you want to highlight in the vim command line. Below is an example of setting the <code>colorcolumn</code> to 80.</p>
<pre class="language-vim"><code class="language-vim"><span class="token punctuation">:</span><span class="token keyword">set</span> colorcolumn<span class="token operator">=</span><span class="token number">80</span></code></pre>
<p>If you want to highlight multiple columns, you can give <code>colorcolumn</code> a comma-seperated list of values.</p>
<pre class="language-vim"><code class="language-vim"><span class="token punctuation">:</span><span class="token keyword">set</span> colorcolumn<span class="token operator">=</span><span class="token number">80</span><span class="token punctuation">,</span><span class="token number">100</span></code></pre>
<p>To turn <code>colorcolumn</code> off, you can simply set the value back to zero.</p>
<pre class="language-vim"><code class="language-vim"><span class="token punctuation">:</span><span class="token keyword">set</span> colorcolumn<span class="token operator">=</span><span class="token number">0</span></code></pre>
<p>Finally, if you want to change the color, you can use the <code>highlight</code> command.</p>
<pre class="language-vim"><code class="language-vim"><span class="token punctuation">:</span><span class="token builtin">highlight</span> ColorColumn guibg<span class="token operator">=</span><span class="token keyword">red</span></code></pre>
<h2>Further Reading</h2>
<p>The Vim help documentation is an excellent resource if you know how to find what you’re looking for. You can find more information about <code>colorcolumn</code> by entering <code>:help colorcolumn</code> on the Vim command line.</p>

    ]]>
      </content>
    </entry>
  
    
    <entry>
      <title>A Brief Introduction to Ruby&#39;s Kernel.fork</title>
      <link href="https://jpo.github.io/2012/07/17/a-brief-introduction-to-rubys-kernel-fork/"/>
      <updated>2012-07-17T00:00:00Z</updated>
      <id>https://jpo.github.io/2012/07/17/a-brief-introduction-to-rubys-kernel-fork/</id>
      <content type="html">
        <![CDATA[
      <p>Recently, a coworker asked if I could help with manipulating some large CSV files. Each file contained approximately 1.2 million records and was over 200 megabytes in size. I’ve never worked with such large CSV files before, so I had no idea how long they would take to process.</p>
<p>I decided to start by writing a Ruby script that would parse and manipulate only a subset of the data. I used <a href="http://ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html">Benchmark</a> to figure out how long it would take the program to run. I then did some quick calculations and the result was that it would take roughly 25 minutes to process the entire data set.</p>
<p>My first attempt looked like this:</p>
<pre class="language-ruby"><code class="language-ruby"><span class="token keyword">require</span> <span class="token string-literal"><span class="token string">'benchmark'</span></span>
<span class="token keyword">require</span> <span class="token string-literal"><span class="token string">'csv'</span></span>

<span class="token comment"># Read each record in sample.csv, manipulate it,</span>
<span class="token comment"># and write it to sample.out.csv</span>
bm <span class="token operator">=</span> Benchmark<span class="token punctuation">.</span>realtime <span class="token keyword">do</span>
  <span class="token builtin">File</span><span class="token punctuation">.</span>open<span class="token punctuation">(</span><span class="token string-literal"><span class="token string">'sample.out.csv'</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">'w+'</span></span><span class="token punctuation">)</span> <span class="token keyword">do</span> <span class="token operator">|</span>output<span class="token operator">|</span>
    <span class="token constant">CSV</span><span class="token punctuation">.</span>foreach<span class="token punctuation">(</span><span class="token string-literal"><span class="token string">'sample.csv'</span></span><span class="token punctuation">)</span> <span class="token keyword">do</span> <span class="token operator">|</span>input<span class="token operator">|</span>
      <span class="token comment"># Do some manipulations...</span>
      output<span class="token punctuation">.</span>write<span class="token punctuation">(</span>input<span class="token punctuation">.</span>to_csv<span class="token punctuation">)</span>
    <span class="token keyword">end</span>
  <span class="token keyword">end</span>
<span class="token keyword">end</span>

puts bm</code></pre>
<p>Coincidentally, I had been reading about working with threads and processes in Ruby in the evenings. I thought that this would be a great opportunity to use some of my new knowledge.</p>
<h2>About Kernel.fork</h2>
<p>According to <a href="http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-fork">Ruby’s documentation</a>, <code>Kernel.fork</code> “creates a subprocess. If a block is specified, the block is run in the subprocess…” The documentation also points out that it should be used with <code>Process.wait</code> or <code>Process.detach</code> to avoid accumulating zombie processes.</p>
<p>Here is an example:</p>
<pre class="language-ruby"><code class="language-ruby">fork <span class="token keyword">do</span>
  <span class="token comment"># Do stuff in a subprocess</span>
<span class="token keyword">end</span>

<span class="token comment"># Wait for the subprocess(es) to finish</span>
Process<span class="token punctuation">.</span>wait</code></pre>
<h2>Reworking the Code</h2>
<p>Integrating <code>Kernel.fork</code> into my existing solution was very simple. All I had to do was insert a strategically placed <code>fork</code> block into my code. The result being that I saved myself and my coworker several extra minutes of waiting.</p>
<p>Here’s what I ended up with:</p>
<pre class="language-ruby"><code class="language-ruby"><span class="token keyword">require</span> <span class="token string-literal"><span class="token string">'benchmark'</span></span>
<span class="token keyword">require</span> <span class="token string-literal"><span class="token string">'csv'</span></span>

bm <span class="token operator">=</span> Benchmark<span class="token punctuation">.</span>realtime <span class="token keyword">do</span>
  <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">'file1.csv'</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">'file2.csv'</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">'file3.csv'</span></span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token keyword">each</span> <span class="token keyword">do</span> <span class="token operator">|</span>input_file<span class="token operator">|</span>
    fork <span class="token keyword">do</span>
      output_file <span class="token operator">=</span> input_file <span class="token operator">+</span> <span class="token string-literal"><span class="token string">'.out'</span></span>
      <span class="token builtin">File</span><span class="token punctuation">.</span>open<span class="token punctuation">(</span>output_file<span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">'w+'</span></span><span class="token punctuation">)</span> <span class="token keyword">do</span> <span class="token operator">|</span>output<span class="token operator">|</span>
        <span class="token constant">CSV</span><span class="token punctuation">.</span>foreach<span class="token punctuation">(</span>input_file<span class="token punctuation">)</span> <span class="token keyword">do</span> <span class="token operator">|</span>input<span class="token operator">|</span>
          <span class="token comment"># Do some manipulations...</span>
          output<span class="token punctuation">.</span>write<span class="token punctuation">(</span>input<span class="token punctuation">.</span>to_csv<span class="token punctuation">)</span>
        <span class="token keyword">end</span>
      <span class="token keyword">end</span>
    <span class="token keyword">end</span>
  <span class="token keyword">end</span>
<span class="token keyword">end</span>

puts bm</code></pre>
<h2>Bonus Tip</h2>
<p>You can achieve the same result in <a href="http://hacktux.com/bash/ampersand">Bash using ampersands</a> (&amp;). In Bash, the ampersand is a control operator used to fork processes. This means that if I were to modify the previous example to accept an input file and output file as arguments, I could then run the following command in Bash and achieve similar results:</p>
<pre class="language-bash"><code class="language-bash">ruby parse.rb in1.csv out1.csv <span class="token operator">&amp;</span> <span class="token punctuation">\</span>
ruby parse.rb in2.csv out2.csv <span class="token operator">&amp;</span> <span class="token punctuation">\</span>
ruby parse.rb in3.csv out3.csv <span class="token operator">&amp;</span></code></pre>

    ]]>
      </content>
    </entry>
  
    
    <entry>
      <title>Vim Tip: Temporary Key Mappings</title>
      <link href="https://jpo.github.io/2012/07/25/vim-tip-temporary-key-mappings/"/>
      <updated>2012-07-25T00:00:00Z</updated>
      <id>https://jpo.github.io/2012/07/25/vim-tip-temporary-key-mappings/</id>
      <content type="html">
        <![CDATA[
      <p>One trick that I often use with Vim is temporarily mapping keys for tasks I need to perform in my current context.</p>
<p>For example, when working on a small Ruby script, I may want to run it a few times to check the output. In Vim, I can temporarily map <code>&lt;Leader&gt;r</code> to run the script.</p>
<pre class="language-vim"><code class="language-vim"><span class="token punctuation">:</span>map <span class="token punctuation">,</span><span class="token keyword">r</span> <span class="token punctuation">:</span><span class="token operator">!</span><span class="token keyword">ruby</span> myscript<span class="token operator">.</span>rb<span class="token operator">&lt;</span><span class="token keyword">cr</span><span class="token operator">></span></code></pre>
<p>Here's another example. When working with Rails, I may only want to run unit tests on a specific model. In Vim, I can temporarily map <code>&lt;Leader&gt;t</code> to run only the tests I need.</p>
<pre class="language-vim"><code class="language-vim"><span class="token punctuation">:</span>map <span class="token punctuation">,</span><span class="token keyword">t</span> <span class="token punctuation">:</span><span class="token operator">!</span>rake test TEST<span class="token operator">=</span>test<span class="token operator">/</span>unit<span class="token operator">/</span>product_test<span class="token operator">&lt;</span><span class="token keyword">cr</span><span class="token operator">></span></code></pre>
<p>Using temporary key mappings has saved me many keystrokes. To find out more about this trick (and a lot of other helpful Vim-fu), I highly recommend watching <a href="https://peepcode.com/products/play-by-play-bernhardt">Peepcode's Play By Play with Gary Berhardt</a>. You can also refer to Vim’s documentation on the map command by entering <code>:help map</code> in Vim’s command line.</p>

    ]]>
      </content>
    </entry>
  
    
    <entry>
      <title>How to Create Terminal Progress Indicators in Ruby</title>
      <link href="https://jpo.github.io/2012/08/01/how-to-create-terminal-progress-indicators-in-ruby/"/>
      <updated>2012-08-01T00:00:00Z</updated>
      <id>https://jpo.github.io/2012/08/01/how-to-create-terminal-progress-indicators-in-ruby/</id>
      <content type="html">
        <![CDATA[
      <p>When you’re developing a terminal application, it’s usually helpful to indicate the progress of a long running task. Over the years, I’ve done this in a number of ways. Most of them involve printing some kind of status message, such as “Processing file1.csv…” or “Processing 10 of 1,000,000…” etc.</p>
<p>One technique that I’ve always liked, but never took time to learn, was displaying a dynamic progress indicator. Chances are you’re seen this yourself. It’s used in popular applications such as Git and Homebrew. The indicators come in various shapes and sizes. I’ve seen progress bars, percentages, spinners, and others.</p>
<h2>The Technique In Action</h2>
<p>I’ve created a simple example below so that you can see this technique in action. When you run it, you will see a percentage that dynamically updates in-place as it gradually increases from 0 to 100 over a period of 10 seconds. To try it, open your terminal, start a new irb session, and enter the code below.</p>
<pre class="language-ruby"><code class="language-ruby"><span class="token comment"># Print 1 to 100 percent in place in the console using "dynamic output"</span>
<span class="token comment"># technique</span>
<span class="token number">1.</span>upto<span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token keyword">do</span> <span class="token operator">|</span>i<span class="token operator">|</span>
  printf<span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"\rPercentage: %d%"</span></span><span class="token punctuation">,</span> i<span class="token punctuation">)</span>
  sleep<span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">)</span>
<span class="token keyword">end</span>
puts</code></pre>
<h2>How It Works</h2>
<p>The above example probably looks very similar to code you’ve written in the past. What’s the secret? The key is the <code>\r</code> character in the first argument of the printf command. This character is known as a carriage return. According to Wikipedia, a <a href="http://en.wikipedia.org/wiki/Carriage_return">carriage return</a> “moves the position of the cursor to the first position on the same line.” This means that each time printf is called inside the loop, the previous line is overwritten - in-place - by a new line. If the carriage return were not included, the new output would be continuously appended to the previous output and the result would look like this: <code>Percentage: 0%Percentage 1%...</code></p>
<h2>More Examples</h2>
<p>Below, I’ve included some additional examples that I’ve seen before in other applications. I’ve also combined them into a <a href="https://gist.github.com/jpo/3212901">gist on GitHub</a>. Feel free to use them or extend them as you see fit.</p>
<h3>Progress Bar</h3>
<pre class="language-ruby"><code class="language-ruby"><span class="token comment"># Prints a text-based progress bar representing 0 to 100 percent. Each "="</span>
<span class="token comment"># sign represents 5 percent.</span>
<span class="token number">0.</span>step<span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span> <span class="token keyword">do</span> <span class="token operator">|</span>i<span class="token operator">|</span>
  printf<span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"\rProgress: [%-20s]"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"="</span></span> <span class="token operator">*</span> <span class="token punctuation">(</span>i<span class="token operator">/</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  sleep<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">)</span>
<span class="token keyword">end</span>
puts</code></pre>
<h3>Spinner</h3>
<pre class="language-ruby"><code class="language-ruby"><span class="token comment"># Prints a text-based "spinner" element while work occurs.</span>
spinner <span class="token operator">=</span> <span class="token class-name">Enumerator</span><span class="token punctuation">.</span><span class="token keyword">new</span> <span class="token keyword">do</span> <span class="token operator">|</span>e<span class="token operator">|</span>
  loop <span class="token keyword">do</span>
    e<span class="token punctuation">.</span><span class="token keyword">yield</span> <span class="token string-literal"><span class="token string">'|'</span></span>
    e<span class="token punctuation">.</span><span class="token keyword">yield</span> <span class="token string-literal"><span class="token string">'/'</span></span>
    e<span class="token punctuation">.</span><span class="token keyword">yield</span> <span class="token string-literal"><span class="token string">'-'</span></span>
    e<span class="token punctuation">.</span><span class="token keyword">yield</span> <span class="token string-literal"><span class="token string">'\\'</span></span>
  <span class="token keyword">end</span>
<span class="token keyword">end</span>

<span class="token number">1.</span>upto<span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token keyword">do</span> <span class="token operator">|</span>i<span class="token operator">|</span>
  printf<span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"\rSpinner: %s"</span></span><span class="token punctuation">,</span> spinner<span class="token punctuation">.</span><span class="token keyword">next</span><span class="token punctuation">)</span>
  sleep<span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">)</span>
<span class="token keyword">end</span></code></pre>
<h3>Combination</h3>
<pre class="language-ruby"><code class="language-ruby"><span class="token comment"># Prints a combination of the progress bar, spinner, and percentage examples.</span>
spinner <span class="token operator">=</span> <span class="token class-name">Enumerator</span><span class="token punctuation">.</span><span class="token keyword">new</span> <span class="token keyword">do</span> <span class="token operator">|</span>e<span class="token operator">|</span>
  loop <span class="token keyword">do</span>
    e<span class="token punctuation">.</span><span class="token keyword">yield</span> <span class="token string-literal"><span class="token string">'|'</span></span>
    e<span class="token punctuation">.</span><span class="token keyword">yield</span> <span class="token string-literal"><span class="token string">'/'</span></span>
    e<span class="token punctuation">.</span><span class="token keyword">yield</span> <span class="token string-literal"><span class="token string">'-'</span></span>
    e<span class="token punctuation">.</span><span class="token keyword">yield</span> <span class="token string-literal"><span class="token string">'\\'</span></span>
  <span class="token keyword">end</span>
<span class="token keyword">end</span>

<span class="token number">1.</span>upto<span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span> <span class="token keyword">do</span> <span class="token operator">|</span>i<span class="token operator">|</span>
  progress <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"="</span></span> <span class="token operator">*</span> <span class="token punctuation">(</span>i<span class="token operator">/</span><span class="token number">5</span><span class="token punctuation">)</span> <span class="token keyword">unless</span> i <span class="token operator">&lt;</span> <span class="token number">5</span>
  printf<span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"\rCombined: [%-20s] %d%% %s"</span></span><span class="token punctuation">,</span> progress<span class="token punctuation">,</span> i<span class="token punctuation">,</span> spinner<span class="token punctuation">.</span><span class="token keyword">next</span><span class="token punctuation">)</span>
  sleep<span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">)</span>
<span class="token keyword">end</span></code></pre>

    ]]>
      </content>
    </entry>
  
    
    <entry>
      <title>Vim Tip: Handling Long Lines of Text</title>
      <link href="https://jpo.github.io/2012/08/09/vim-tip-handling-long-lines-of-text/"/>
      <updated>2012-08-09T00:00:00Z</updated>
      <id>https://jpo.github.io/2012/08/09/vim-tip-handling-long-lines-of-text/</id>
      <content type="html">
        <![CDATA[
      <p>When you’re using Vim to read log files or write blog posts, one problem that you may come up against is wrapping long lines of text. Vim, being the powerful editor that it is, can handle this in multiple ways. Today, I’d like to talk about two of those options. One changes the way that text is displayed in the buffer. The other changes the way the text is formatted in the file.</p>
<h2>Option 1: Wrap</h2>
<p>If you are concerned with the way long lines are displayed in Vim, then the <code>wrap</code> setting is for you. It’s turned on by default. When a long line of text reaches the edge of the window, Vim will continue displaying the text on the next line in the same way that Microsoft Word or any other rich text editor would. I find this setting useful when reading log files. Below is an example of how to change it.</p>
<pre class="language-vim"><code class="language-vim"><span class="token comment">" Default. Turn word-wrapping on.  </span>
<span class="token keyword">set</span> <span class="token builtin">wrap</span>

<span class="token comment">" Turn word-wrapping off.  </span>
<span class="token keyword">set</span> <span class="token builtin">nowrap</span></code></pre>
<h2>Option 2: TextWidth</h2>
<p>If you are concerned with the way long lines are formatted in the buffer, you can use the <code>textwidth</code> setting. It’s set to 0 by default. When set to a specific character limit, such as 80, Vim will force any text exceeding that limit onto a new line in the buffer. I find this setting useful when writing a long article, such as a blog post. Below is an example of how to change it.</p>
<pre class="language-vim"><code class="language-vim"><span class="token comment">" Start a new line at 80 characters.  </span>
<span class="token keyword">set</span> <span class="token builtin">textwidth</span><span class="token operator">=</span><span class="token number">80</span>

<span class="token comment">" Do not use textwidth</span>
<span class="token keyword">set</span> <span class="token builtin">textwidth</span><span class="token operator">=</span><span class="token number">0</span></code></pre>
<h2>Bonus: Reformatting All Long Lines In A File</h2>
<p>What if you’re working with a file that has a lot of long lines and you’d like to reformat them all to a specific number of characters? This simplest way to do this is with the <code>gq</code> command. When used in conjunction with the <code>textwidth</code> setting, <code>gq</code> will reformat the selected lines to the desired length. Below is an example of how you can reformat all of the lines in a file.</p>
<pre class="language-vim"><code class="language-vim"><span class="token comment">" Set the textwidth</span>
<span class="token keyword">set</span> <span class="token builtin">textwidth</span><span class="token operator">=</span><span class="token number">80</span>

<span class="token comment">" Move the cursor to the top of the file</span>
gg

<span class="token comment">" Select all text in the file</span>
VG

<span class="token comment">" Reformat all of the lines to the length specified by textwidth</span>
gq</code></pre>
<h2>Find Out More</h2>
<p>Vim provides many other settings that can be used in conjunction with <code>wrap</code> and <code>textwidth</code>. To find out more, you can reference Vim’s help documentation by entering <code>:help wrap</code> or <code>:help textwidth</code> on Vim’s command line.</p>

    ]]>
      </content>
    </entry>
  
</feed>
