Skip to content

Instantly share code, notes, and snippets.

@4zv4l

4zv4l/todo.rb Secret

Last active July 7, 2022 07:50
Show Gist options
  • Save 4zv4l/0dd1ab967acfd3cb217a16ecc9026d07 to your computer and use it in GitHub Desktop.
Save 4zv4l/0dd1ab967acfd3cb217a16ecc9026d07 to your computer and use it in GitHub Desktop.
Simple todo list script in ruby
#!/bin/ruby
require 'base64'
require 'readline'
require "curses"
# setup readline for tab-completion and ncurse window
cmds = ["exit", "save"]
complete = proc {|str| cmds.grep(/^#{str}/); }
Readline.completion_proc = complete
Readline.completion_append_character = ''
Curses.init_screen
height, width = Curses.lines, Curses.cols
win = Curses::Window::new(height, width, 0, 0)
win.refresh
# get the todos from file
path = File.expand_path("~/.todo.mine")
todos = Base64.decode64(File.read(path)).split("\n") || Array.new
# main loop
loop do
# show the todos
win.clear
todos.each_with_index {|todo, index| win.addstr "#{index+1}. #{todo}\n"}
win.refresh
# read input from user
line = Readline.readline("-> ", true)
# check for commands
case
when line == "exit"
win.close
exit 0
when line == "save"
File.write(path, Base64.encode64(todos.join("\n")))
next
end
# add todo to list if not already in or del if already in
if line.to_i >= 1 then todos = todos.reject{|e| e == todos[line.to_i-1]}
else todos.append(line) unless line.strip.empty? end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment