Skip to content

Instantly share code, notes, and snippets.

@bradylove
Created October 2, 2011 22:51
Show Gist options
  • Save bradylove/1258077 to your computer and use it in GitHub Desktop.
Save bradylove/1258077 to your computer and use it in GitHub Desktop.

Kansas - A simple command line todo list

Setup

curl -O https://raw.github.com/gist/1258077/9fd0955cc10c4d91da208fd1015b673377049940/kansas

mv kansas /usr/local/bin

sudo chmod +x /usr/local/bin/kansas

Usage

Get a list of available commands kansas help

###Create a new todo: kansas # Text for new todo

Kansas supports giving your todo a priority 0-9

(replace # with a number 0-9, numbers greater than 9 are automatically changed to 9)

Example: kansas 1 Write a better README for Kansas this will create a new todo with 1 being the priority.

###List todo's: kansas list

Kansas will list all todo's in order of priority and creation date

###Destroy todo: kansas destroy #

Replace # with the id number of the todo you want to destroy. Get the id number from kansas list, the first number on the row

Or you can destroy all the todo's in your list

kansas destroy all You will then be asked to confirm with a Y (case sensitive)

#!/usr/bin/env ruby
class Kansas
KANSAS_DIR = Dir.home + "/.kansas"
ARCHIVE_DIR = KANSAS_DIR + "/archive"
def initialize(command)
@todos = []
@command = command
kansas_dir
build_list
case @command
when "create"
create_todo(ARGV[1])
when "list"
puts "Todo List"
list_todos
puts " "
when "destroy"
if ARGV[1] == "all"
destroy_all_todos
else
destroy_todo(ARGV[1].to_i)
end
else
puts "Commands"
puts "------------------------------------------------"
puts "list - To list all your todos."
puts "create # new todo - To create a new todo. # is priority, it can be 0 thru 9. 0 being high priority."
puts "destroy # - To destroy a todo."
end
end
def kansas_dir
Dir.mkdir(KANSAS_DIR) unless Dir.exists?(KANSAS_DIR)
end
def create_todo(priority)
if priority.to_i > 9
priority = "9"
end
todo = ARGV[2..ARGV.size].join(" ")
file_name = KANSAS_DIR + "/" + priority + "_" + Time.now.to_i.to_s + ".txt"
File.open(file_name, "w") { |f| f.puts todo }
puts "Created new todo: #{todo}"
end
def list_todos
count = 1
@todos.each do |f|
priority = File.basename(f)[0]
todo = File.read(f)
puts "#{count}.#{" " unless count > 9} (#{priority}) #{todo}"
count += 1
end
end
def build_list
@todos.clear
Dir[KANSAS_DIR + '/*'].each do |f|
@todos << f
end
end
def destroy_all_todos
# Clear ARGV to avoid issue with gets.chomp
ARGV.clear
puts "Are you sure you want to destroy all todo\'s? Y/N?"
confirmed = gets.chomp
case confirmed
when "Y"
@todos.each_index { |t| destroy_todo(t + 1) }
else
puts "Cancelled \"destroy all\""
exit
end
end
def destroy_todo(index)
todo = @todos[index - 1]
todo_body = File.read(todo)
File.delete(todo)
puts "Destroyed todo: #{todo_body}"
end
end
Kansas.new(ARGV[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment