-
Notifications
You must be signed in to change notification settings - Fork 2
/
history.rb
47 lines (41 loc) · 1.06 KB
/
history.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module Pastie
class History
attr_reader :path
attr_accessor :links
def initialize
@path = "#{RUBY_PLATFORM =~ /mswin32|mingw32/ ? ENV['USERPROFILE'] : ENV['HOME']}/.pastie"
@links = []
self.load
end
# Load history
def load
if File.exists?(@path) && File.readable?(@path)
@links = File.open(@path).readlines("\r\n").collect { |l| l.strip }
else
File.open(@path, 'w').close if File.writable?(File.dirname(@path))
end
end
# Add new paste to the history
def add(url)
@links << url
end
# Flush history
def flush
@links = []
end
# Save history
def save
File.open(@path, 'w') { |f| f.write(@links.join("\r\n")) }
end
# Print all existing pastes
def print
puts "====== Pastes History ======".green
unless @links.empty?
@links.each_with_index { |l, i| puts "#{i+1}. #{l}" }
else
puts "your history is empty..."
end
puts "============================".green
end
end
end