Skip to content

Instantly share code, notes, and snippets.

@mattak
Created June 6, 2013 02:21
Show Gist options
  • Save mattak/5718877 to your computer and use it in GitHub Desktop.
Save mattak/5718877 to your computer and use it in GitHub Desktop.
clip temporary message.
#!/usr/bin/env ruby
require 'json'
require 'optparse'
def clipfile
return File.join(Dir.home, ".cliplog")
end
def load
return File.exists?(clipfile) ? File.open(clipfile, "r").read : "{}"
end
def save(content)
File.open(clipfile, "w") do |f|
f.write content
end
end
def readstr()
begin
return STDIN.read.chomp
rescue Interrupt, Errno::ENTER
exit 1
end
end
def clipush(key, value)
json = JSON.parse(load())
json[key] = value
save(JSON.generate(json))
end
def clipop(key)
json = JSON.parse(load())
json[key] == nil ? "" : json[key]
end
def clipclear()
File.delete(clipfile)
end
def cliplist
json = JSON.parse(load())
json.each do |key, value|
puts "#{key}\t#{value}"
end
end
def clipkeys
json = JSON.parse(load())
puts json.keys
end
def clipvalues
json = JSON.parse(load())
puts json.values
end
# -------
# main
# -------
#
# usage:
# echo "hello" clip push hoge
# clip pop hoge
#
command = nil
if ARGV.size > 0
command = ARGV.shift.to_sym
end
if command == nil
command = :pop
end
case command
when :push
key = ARGV.size > 0 ? ARGV[0] : "default"
value = readstr()
clipush(key, value)
when :pop
key = ARGV.size > 0 ? ARGV[0] : "default"
print clipop(key)
when :clear
clipclear()
when :list
cliplist()
when :keys
clipkeys()
when :values
clipvalues()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment