Wednesday, October 23, 2013

An interesting LibreOffice Bug

Today I was changing a csv file into an SQL file. I normally do a lot of my work in Sublime Text, and make copious use of the ctrl+shift+L feature to bring up multiple cursors.
This was my general plan:

write the beginning insert sql statement insert into ... () values
then surround each row of the csv with ( and ) using sublimes multi-cursors. This worked after sublime grinded it's gears for 30 or so seconds (did I mention it's a rather large file I'm doing this to?) but then I realized I had forgotten to put in quotes around my text fields. If my data was nice, I would have simply used multiple cursors and ctrl+arrow keys to drop a quote in, skip over the word, then drop the next quote in and be done with it.

Unfortunately, there's more than one word in some of the text values and the number of words per row was different, so no such luck there.

I popped up vim real fast and figured maybe I could do a nice bit of vim-fu to throw down some quotes. After 10 minutes of googling (you can do vi( and vi) to jump around, but you can't do vi, to jump to a comma in visual mode.) I decided that I didn't know the right search terms to do it and so I opened up libre office figuring it might be able to help me out.

I selected all the cells in my column and went to format cells in the context menu, text type, and made a custom formatter for the cells that just threw quotes around the value. Great! Easy! But wait...

A bunch of values in one of my columns didn't bother to quote themselves. I stared at it for a bit, tried redoing the formatting, clearing it. After a bit I realized that the only difference between the cells that the format was applied to was the squiggly red line underneath each non-formatting cell.

The spell checker prevents a custom formater?

No way, I thought. But I told the spell checker to ignore one of the words, and lo and behold the cell was quoted suddenly.  So I shut off the spell checker entirely (it's in options, language, writing aids, and the check while you write option) and had a properly formatted column.

How insane! The Spelling error of a column should most definitely NOT prevent a format from being applied. I wonder if this is a known bug listed somewhere?

Friday, September 27, 2013

Time Catcher Usage Demo

http://www.youtube.com/watch?v=H_UhIJah398

So I made a quick demo of timecatcher today out of boredom. Well, actually, I installed Simple Screen Recorder and wanted to try it out on anything at all, so I made the tutorial 3 times and then chose this one as the best out of all of them and uploaded it.

Granted I forgot to show the finish command, but it functions the same way as the pause command does, and to be honest I don't use it that often. A programmers work is never done after all.

Sunday, September 1, 2013

Time Catcher


 I've begun work on a new project and so far it's coming along nicely. I've been using some software at work to keep track of my hours, it's called office time. It works fine, simple interface, easy to manage for the most part, and at least 20 features I don't use. Ever. So being the kind of guy I am, I decided that I could probably make a simple to use task tracking utility. Here are some of my motivations.
  • I spend about 60-80% of my day in a terminal. So I should be able to access the utility from there
  • I need to keep track of a task per day, and then be able to display it meaningfully later on
  • I want to be able to add notes to myself about the task as I work on it
  • I should only be active on a single task at a time
So let's see. I'm going to working in the command line, well that means I'll want an executable file. I could write a bash script, but bash syntax is not the best for anything complicated. So I want a higher language. I want to sit low, and I like C. So I'll do it in C.

 Keeping track of a single task, well that's simple. Give it a name. I'll have to be able to have it be permanent, and I even have a process running in the background (no) or I make an object of some kind in a file and use that to keep track of things. Now that sounds easy. If it works for git, it will work for me. 

If I want to keep track of my tasks per day, I should store these object files into their own folder, named after the current date. The benefit to this is that if I want to generate spreadsheets or information later on, I can do it by date with these folders. 

Adding a note, well that's easy too, keep track of it in the file for the current task. Being active on only a single task at a time? Now that seems hard to enforce. Because I'll be able to start a task, then end a task with a command. So I could fire off a bunch of tasks then finish them when I wanted to and the timing data would be complete garbage. Ah! So I'll keep track of the last created task in some type of master file. Easy! When a new task is created, the other task will be 'paused' or for starters I won't let a user switch tasks unless they finish one first.

 And this is my first plan. So far it's coming along and I've got the directory to hold this information all good and am writing out the usage documentation, as a guide to me and a guide to anyone else who wants to use it.

Thursday, July 25, 2013

FrankenCode with Rails and Xemark

I've recently created a markup language called xemark. You can find the repository here or the Backus Naur Form grammar here

I've been using it to generate my github pages site which as of the time of this writing is about 75% done.

One of the beautiful things about xemark is that it output's to the console / stdout. Which means you can pipe or direct it's output anywhere you please. Because it prints out it's language to html, this means you can serve up xemark to anything that will pump out html for you.

I just franken coded a rails application to do this.

Now in no way, shape or form, is this good code or practice. But here are the simple steps:


  1. Get a copy of xemark. (You can fork the repository, download it, or whathave you, then just compile parser.c with the -o flag set to xemark
  2. Make yourself a xe file. Heck, use the example.xemark file and rename it example.xe it's up to you.
  3. Next hop to your console and run the commands
    1. rails new railsmark
    2. rails generate controller page showit
  4. Next we need to put in some code to show call xemark so throw this into your page_controller.rb
    class PageController < ApplicationController
      def showit
       p = `./xemark < #{params[:page]}.xe`
    
       respond_to do |format|
        format.html {render :text => p}
       end
      end
    end
    
  5. Finally, move the xemark executable and the example.xe file into the root directory of your rails install.
  6. run rails s or rails server
  7. Navigate to localhost:3000/page/showit?page=example
Tada! You've just served up a xe page through rails. Note that this is a toy example, primarily because if you try to include any style to the xe page they won't link well, and not to mention this doesn't make use of the asset pipeline at all in rails, so no styling for you. Still. It was a fun quick and dirty experiment that turned out surprisingly well.

UPDATE:

I made it serve up my style as well. This is assuming that the xe file links to a stylesheet called style.css (by using ~style in the xe file under the @title)

First off. Change the controller code to have a new function
def style
   p = `cat ./style.css`
   respond_to do |format|
    format.html {render :text => p}
   end
end


Next, add the following to your config/routes.rb file
get "page/style.css", to: "page#style"

And finally, move a file called style.css to the root level of the directory.

And bam. You have style being served.

If I feel like playing more with this, I might make it scan the asset pipeline for the file instead of having it need to be in the root level. For more security, you could do something like this in routes.rb:

get "example", to: "page#showit" , :page => "example"

Once you do that though, you'll have to change your style get to be just style.css instead of page/style.css though.

Wednesday, July 24, 2013

How to grep a log file in realtime / continuously

When hunting down errors and debugging problems that plague a system. Your log file is your best friend. It lets you know what's going on in your application if you've really put the effort into logging all the idiosyncrasies  in your code.

One of my friends once told me: "You have to love your logger" and there's really on true-er statement than that.

Now it's easy to grep a log and look around for what you want. But continuously monitoring a log often poses a problem to people new to the debugging scene. They try out this first:

tail -f logfile | grep "pattern" 

And then they wait 4 days for something to show up. Why? Because of line buffering and the output of tail being buffered before being piped to grep. So how do you get around that? 

I've seen solutions like this:

tail -f logfile | grep --line-buffered "pattern"

but sometimes, depending on the system, they buffer as well and you have to wait.

To get continuous. real time logging you can employ the following shell script fu:

tail -f logfile | while read line ; do echo "$line"| grep "pattern" ; done

Friday, July 5, 2013

A 'proper' use of GOTO

Ever since Knuth criticized goto people have been spouting that GOTO is the worst language construct to ever use.


I'm fairly sure that 80% of them have never used a GOTO statement in their life, nor understand why sometimes it is the most logical thing to do.
For example here is an example:
  It really is logical here and I'll tell you why. Lets look at what I'm doing here in pseudo form:


read in a character:

 read in the next character

  Does this match a comment statement?

    Eat the input until the end of the comment

  No? Then just output it and keep going


Now you might be asking me, why didn't you write it with functions and fully modularize your code and encapsulate all the functionality? Simple. This is an exercise from a book. It is mean to be a simple one script that handles a low level functionality of removing comments. It's code is simple and it doesn't benefit from encapsulation (A point that's arguable but only if you're playing devils advocate)

Also, even using functions and all that to encapsulate each portion isn't going to save you the real trouble here unless you place the second while loop into the function. Now why do I have to use goto here? People unfamiliar with scoping rules should be aware that your instinct to throw a continue statement in in order to skip out of the loop will only skip the current block scope you're in. In other words, a continue in the second while will not break you out of the first while like you need to in order for this algorithm to work. A goto statement ignores scope since it's an unconditional branch.

Let the haters commence:
You might ask me, well obviously that scoping thing makes sense, but the program can be rewritten with functions to be clearer. In which case I'd tell you that Occam's razor holds true for small one off programs like this.

But, just to convince you that the use of goto is a "neccesary evil".  Take a look here I don't know about you, but I trust the kernel programmers. Considering they wrote your operating system, your drivers, and all the things that make your shiny web applications run smoothly.

Thursday, July 4, 2013

Book Review: The C Programming Language

The C Programming Language by two really smart guys

The C Programming Language by Brian W. Kernighan, Dennis M. Ritchie is one of the best books I've ever read.

The manner of instruction and the pace it sets is great. There are good explanations on pretty much every subject covered and gotcha's are pointed out whenever there is one. Considering that Ritchie is the creator of the C language and Kernighan is one of the contributors of the original Unix system it's not surprising that the book is so thorough.

The book is small, it's not meant to teach you everything about programming in C, but it's meant to teach you how to use C to express your programming needs. And it does a damn good job of it. The exercises it gives as problems after code listings aren't crap one off programs that you'd write because it's just in the book. But some of them are actually rather useful. For example, The Word Counting (listing 1.5.4) example is a program existing on unix and it's great to see a program that actually exists in the 'real world' of Unix.

I've done a couple of the Exercises out and I've enjoyed it thoroughly so far. I highly recommend purchasing the book or downloading the pdf from the link I provided above.

https://gist.github.com/EJEHardenberg/5931082