-
Notifications
You must be signed in to change notification settings - Fork 0
/
oswin.rb
104 lines (81 loc) · 2.28 KB
/
oswin.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
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
require 'readline'
class Oswin
def initialize
@h = Hash.new([])
@events = Hash.new([])
end
def load(file)
sub_hash = Hash.new([])
invalid = []
line_number = 0
if file == "tasks.txt" then
File.open(file).each do |line|
line_number = line_number + 1
if line =~ /^\[(.+)\] (\d+)h(\d+)m$/ and [email protected]_key?($1) and !sub_hash.has_key?($1) then
sub_hash.store($1, (60 * $2.to_i() + $3.to_i()))
else
invalid << line_number
end
end
if invalid.empty? then
@h.merge!(sub_hash)
return nil
else
print("Error: Invalid Lines\n")
print(invalid)
sub_hash.clear
return invalid
end
elsif file == "events.txt" then
File.open(file).each do |line|
line_number = line_number + 1
if line =~ /^\[(.+)\] (\d+:\d+)$/ and [email protected]_key?($2) and !sub_hash.has_key?($2) then
sub_hash.store($1, $2)
else
invalid << line_number
end
end
if invalid.empty? then
@events.merge!(sub_hash)
return nil
else
print("Error: Invalid Lines\n")
print(invalid)
sub_hash.clear
return invalid
end
end
end
def tasks()
@h.sort{|a,b| a[1]<=>b[1]}.each do |elem|
puts "#{elem[1]}, #{elem[0]}"
end
end
def schedule()
@events.sort{|a,b| a[1]<=>b[1]}.each do |elem|
puts "#{elem[1]}, #{elem[0]}"
end
end
end
# shell code
o = Oswin.new
o.load("tasks.txt")
print("tasks loaded\n")
o.load("events.txt")
print("events loaded\n")
while input = Readline.readline("$ ", true)
if input == "exit"
break
elsif input == "update tasks"
o.load("tasks.txt")
print("Done\n")
elsif input == "update events"
o.load("events.txt")
print("Done\n")
elsif input == "show tasks"
o.tasks()
elsif input == "show events"
o.schedule()
end
system(input)
end