Skip to content

Instantly share code, notes, and snippets.

@teddyzetterlund
Last active June 25, 2019 05:45
Show Gist options
  • Save teddyzetterlund/1252195 to your computer and use it in GitHub Desktop.
Save teddyzetterlund/1252195 to your computer and use it in GitHub Desktop.
My CodeBrawl Entry: Command line TODO lists

Do It - Simple Todo List

Do It is a simple todo list application written for the command-line. Right now it doesn't have any fancy features or anything and a simple text-file is probably handier to use for keeping track of your todos. Do It is simply my entry for the Code Brawl competition Command line TODO lists.

Background

This project is my first in more than one way. It's my first entry to Code Brawl and it's my first command-line application. I didn't enter the competition to win it, I entered because it would give me motivation to just jump into it and try to write my first CLI-application.

It turned out I couldn't spare a whole of time this week, but at least I got the essential in and I'm planning on improving this application further and then make it my first gem - just for the sake of learning and experimenting.

I put up two rules for myself when starting to write Do It; I wanted to write clear and organized code and only use the standard library.

Please feel free to criticise everything about the application. Code, usage, apperance (or well, ain't much to talk about there), etc. I'm always eager to improve so feedback is always appricated.

Installation

sudo curl -o /usr/local/bin/doit https://raw.github.com/gist/1252195/14927a66df89abe0ef5a2f5876bec7c2d6eb62ee/doit.rb
sudo chmod +x /usr/local/bin/doit

Configuration

By default Do It saves your todos in a textfile called doit.txt in your home folder. You can change the location of that file by editing /usr/local/bin/doit (if you followed the installation instructions - otherwise use the path of where you put it).

Author

Developed by Teddy Zetterlund.

License

Licened under the MIT License.

#!/usr/bin/env ruby
module Doit
VERSION = '0.0.6'
USAGE = <<EOF
Do It - Simple Todo List
Usage: #{File.basename $0, '.rb'} COMMAND [ARGS...]
COMMAND is one of:
* list - Show the tasks in an ordered list.
usage: doit list
* add - Add a new task to the list.
usage: doit add [TASK]
* remove - Removes task with the specified ID from the list.
usage: doit remove [ID]
* clear - Removes all tasks from the list and gives fresh start.
usage: doit clear
EXAMPLES
# Create a new task called "Do the laundry".
doit add Do the laundry
# Remove task number 4 from the list of tasks.
doit remove 4
For questions and feedback, contact me at http://twitter.com/teddyzetterlund
EOF
class Store
STORAGE_FILE = ENV['HOME'] + '/doit.txt'
def self.read
File.new(STORAGE_FILE, 'w') unless File.exists?(STORAGE_FILE)
File.open(STORAGE_FILE, 'r+')
end
def self.write(list)
File.open(STORAGE_FILE, 'w') do |f|
list.map{|task| f << task}
end
end
end
class List < Array
def initialize
Doit::Store.read.each_line do |task|
self << task
end
end
def add(args)
text = args.join(' ').strip
unless text.empty?
task = Doit::Task.new(text)
self << task
true
else
false
end
end
def remove(task_id)
if task_id.match(/\d/)
self.delete_at(task_id.to_i-1)
end
end
end
class Task
attr_accessor :description
def initialize(description)
@description = description
end
def to_s
"#{description}\n"
end
end
class CLI
ALLOWED_COMMANDS = [:list, :add, :remove, :clear]
def initialize
@arguments = ARGV
@commands = []
@list = Doit::List.new
end
def run
process_command
end
def list(args)
unless @list.empty?
@list.each_with_index do |task, i|
puts "#{i+1}. #{task}"
end
else
puts "Congratulations! Your todo list is empty."
end
end
def add(*args)
@list.add(args) ? save! : output_usage
end
def remove(args)
@list.remove(args[0]) ? save! : output_usage
end
def clear(args)
@list.clear
save!
end
private
def process_command
@arguments.each do |arg|
unless arg.index('-') === 0
@commands << arg
end
end
command = @commands.empty? ? :list : @commands[0].to_sym
if ALLOWED_COMMANDS.include?(command)
self.send(command, @commands[[email protected]])
else
output_usage
end
end
def output_usage
puts USAGE
end
def save!
Doit::Store.write(@list)
true
end
end
end
doit = Doit::CLI.new
doit.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment