Tag Archives: ruby

Rainbow Terminal Text With Lolcat

Like seeing rainbows all over the place? Does it make you especially happy to have a colorful terminal? Yeah, me too. There’s a little Ruby program called lolcat and it will help you out (in the happiness department).

lolcat_rainbow_terminal_colorful_pandora_cli_pianobar

Steps to a super colorful terminal:

  1. Install lolcat in one of two ways.
    1. As a Ruby gem using the standard Ruby utility.  gem install lolcat
    2. Through your system’s package manager.  sudo apt-get install lolcat
  2. Pipe your program through lolcat.
    • cat cool_file.txt | lolcat

That’s really it. The only problem I’ve come across is when piping through a program that is still in control of the terminal output.  My main example is with Pianobar, the Pandora CLI app. It doesn’t actually break anything, you just can’t see the last line of the terminal. This means the song progress can’t be seen, and it can be a problem if the user is expected to enter information. I’ve gotten used to it though, it’s really worth it for the colors!

Have fun with rainbows all up in your face!

git_help_lolcat_rainbow_colors_cli_terminal

 

From Ruby to Python

For a while I’ve been thinking about switching to Python, especially when I see so many programs I like to use written in it. My favorite terminal Guake comes to mind first and of course my all-time favorite editor Sublime Text. The community around Ruby is so heavily focused on web development that other areas seem to be neglected. When I first started getting into coding, web seemed the way to go. I got caught up in the hype and forgot what my gut told me. I love CLI apps and sysadmin tasks. I’ve also found I really enjoy GUI toolkits.  As a language Ruby is really wonderful, but the Python ecosystem just seems like a better fit for me. In January I decided to take the plunge. I came down with something and was confined to bed for a week so it was the perfect time to spend learning. I’m so happy with the change already. I’m writing more and better code in Python than I ever did in Ruby. First step was a beginner’s tutorial. I started with the free Google Python class. I usually prefer purely reading material, but the videos were really enjoyable. Made me feel like I was back in a classroom environment again.

Python is amazing me every day with how many modules and packages are available. After doing a bit of scripting, I started picking up the Kivy framework. It’s an “Open source Python library for rapid development of applications that make use of innovative user interfaces, such as multi-touch apps.” Python makes me feel empowered to create any coding idea I can dream up. If you’re thinking of learning to code or want to try a new language, I strongly suggest checking out the list of tutorials on the Python wiki. Python is amazingly fun to write and so clean to read. White space as syntax makes for such simple and readable code. I’ve been using it barely over three weeks and already I want to rewrite all of my utility scripts from Ruby to Python.

Let me know in the comments if you have any questions about starting out in either Python or Ruby! And for those interested I have a large list of links to tutorials and useful learning materials on my Pinboard.

Public Code Number One

Today I finally released my first public code into the wild. I’ve been teaching myself programming, and throughout the learning process, my code has been in private repositories with Bitbucket. It was a struggle to feel confident enough to put anything of my own on Github, but I realized I have nothing to be afraid of. As a newcomer to the programming world, I’ve had what many refer to as impostor syndrome. Even though I’ve read style guidelines and paid close attention to how others publish code, there’s been an underlying fear of criticism without any constructive suggestions. What I realized is how silly that sounds. In my time browsing Github, I don’t recall thinking anyone was a total jerk. There have been disagreements, sure, but no trolling and certainly nothing like a lot of other places on the internet. I know my script works, so I added a license, a nice README.md, and pushed it out into the world!

The repo I pushed is a little script that checks whether or not a website can be reached. It then logs the result to a text file and, if provided with an email, sends an alert when the site is down. I have it set up to run as a cronjob on my home server. Every ten minutes it checks my site and that of my wonderful partner in everything, Kenzie. Her web host unfortunately put her site on a server that was having a lot of downtime problems. In order to get an idea of how bad the problem was, I thought it would be perfect to have a log we could refer to. The notification email wasn’t 100% necessary, but it seemed like a neat idea and I really wanted to learn how to send emails from my machine! I was right, it turned out to be a fun feature and I had a great time setting up my first Postfix server.

Overall I feel really good about this first step. It may just be a simple script, but it represents much more than that to me. A metaphorical door in my mind has been opened to feeling comfortable doing this again. There’s another project I’ve been working on that’s getting close to ready. This one, however, is a command-line app. That means it will also be released as a Gem, Ruby’s software packaging system. The code won’t just be hosted on Github, but also on rubygems.org. It would be usable by anyone with Ruby installed, simply using ‘gem install’. Makes me a bit more nervous about it, but I really like how the program is coming out and I’m actually getting pretty excited to share it!

Poetic Ruby Method

Here is a limerick for you.

Goodnight.

Using Ruby Find To Look For Files

It’s only been a few months since I started learning Ruby, and I’ve found myself using Find more than any other standard library. It’s likely because I’m mostly doing scripting work to organize my files. If you’re also new to Ruby, you might not know about the Find library and its ability to seek out all files and folders recursively.

The core library’s Dir class has ‘Dir.entries’, but you only get an array of the files and folders in that top directory. When trying to interact with a lot of files in multiple directories, Find is your friend.

This little method will return an array of all the directories within the program’s pwd(present working directory) or any directory supplied to the method. Find has a ::prune method that stops looking any further into the given directory. It’s perfect for ignoring the .git folder incase find comes across any repositories.

This ‘find_files’ method is almost identical, but it finds files instead of directories. I know, very self explanatory, but I included this code here to point out something that came up when I first ran it. Most of my photo library comes from my Windows machine. I used Google Picasa for organizing, which makes a little hidden file inside any folder containing an image. Never really matters until you’re making a script to compare and move around some photos! Skipping any file path containing the word “picasa” fixes the problem easily.

I’ve found this useful quite a few times in both repeated use and one-time-use scripts. This technique could also be modified to return a hash instead of an array, containing extra metadata about each file. The returned info can be used in any number of ways, whether the desire is information or alteration.

I’ve had a lot of fun making simple scripts and command line tools. I write this in the hopes that a fellow budding Rubyist can be inspired to write fun scripts for their system. Please feel free to leave a comment, I’d love to hear about any cool things you’ve done with Find!