-
Notifications
You must be signed in to change notification settings - Fork 0
/
historian.cr
104 lines (93 loc) · 2.03 KB
/
historian.cr
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
class Historian
HISTORY_PATH = "/home/#{Process.user}/.hist.love"
@@position = -1
@@savedLine = ""
def log(message : String)
if message != ""
histfile = File.new(HISTORY_PATH, "a")
histfile.puts(message)
histfile.close
end
end
def getEntryUp : String
histLength = File.read_lines(HISTORY_PATH).size - 1
histLog = File.read_lines(HISTORY_PATH).reverse
unless @@savedLine == ""
while true
@@position += 1
out = histLog[@@position].to_s
break if out[0..@@savedLine.size - 1] == @@savedLine
if getLength == getPosition + 1
@@position -= 1
break
end
end
else
while true
@@position += 1
out = histLog[@@position].to_s
break if out[0..3] != "#<3#"
if getLength == getPosition + 1
@@position -= 1
break
end
end
end
out
end
def getEntryDown : String
histLog = File.read_lines(HISTORY_PATH).reverse
unless @@savedLine == ""
while true
@@position -= 1
if @@position < 0
@@position = -1
out = @@savedLine
break
end
out = histLog[@@position].to_s
break if out[0..@@savedLine.size - 1] == @@savedLine
end
out
else
while true
@@position -= 1
if @@position < 0
@@position = -1
out = ""
break
end
out = histLog[@@position].to_s
break if out[0..3] != "#<3#"
end
out
end
end
def getCurrentEntry : String
if @@position == -1
out = ""
else
histLog = File.read_lines(HISTORY_PATH).reverse
out = histLog[@@position].to_s
end
out
end
def getLength : Int
File.read_lines(HISTORY_PATH).size
end
def getPosition : Int
@@position
end
def resetPosition
@@position = -1
end
def saveLine(line : String)
@@savedLine = line
end
def loadLine : String
@@savedLine
end
def clearSavedLine
@@savedLine = ""
end
end