Skip to content

Instantly share code, notes, and snippets.

@dtan4
Last active December 24, 2015 17:39
Show Gist options
  • Save dtan4/6837565 to your computer and use it in GitHub Desktop.
Save dtan4/6837565 to your computer and use it in GitHub Desktop.
Manage daily memos from console
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require "thor"
require "term/ansicolor"
DAILY_MEMO_DIR = ENV["HOME"] + "/Dropbox/memo/daily/"
class DailyCli < Thor
desc "edit [NAME]", "Edit memo in Emacs", aliases: "e"
def create(name = today)
name += ".md" unless name =~ /\.md$/
puts "Open in Emacs..."
`emacsclient -n #{DAILY_MEMO_DIR + name}`
end
desc "write MESSAGE", "Write message", aliases: "w"
def write(message)
path = DAILY_MEMO_DIR + "#{today}.md"
open(path, "a") { |f| f.puts "* #{message}" }
end
desc "list", "List memos", aliases: "l"
def list
Dir.glob(DAILY_MEMO_DIR + "*").each { |name| puts name.sub(DAILY_MEMO_DIR, "") }
end
desc "search KEYWORD", "Search memos", aliases: "s"
option :around, type: :boolean, aliases: "-a"
def search(keyword)
Dir.glob(DAILY_MEMO_DIR + "*").each do |name|
text = open(name).read
text.lines.each_with_index do |line, idx|
show_result(name, text, idx, options[:around]) if line =~ /#{keyword}/i
end
end
end
desc "view [NAME]", "View memo", aliases: "v"
def view(name = today)
name += ".md" unless name =~ /\.md$/
path = DAILY_MEMO_DIR + name
unless File.exists? path
$stderr.puts "File not found: #{name}"
exit 1
end
puts open(path).read
end
private
def today
Time.now.strftime("%Y-%m-%d")
end
def show_result(name, text, idx, around)
c = Term::ANSIColor
if around
(idx - 1..idx + 1).each do |i|
puts c.yellow + "#{name.sub(DAILY_MEMO_DIR, "")} [#{i + 1}]:" + c.clear +
" #{text.lines[i]}" if (i >= 0) && (i < text.lines.length)
end
puts ""
else
puts c.yellow + "#{name.sub(DAILY_MEMO_DIR, "")} [#{idx + 1}]:" + c.clear + " #{text.lines[idx]}"
end
end
end
DailyCli.start ARGV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment