djinni 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b1341fff2b9dd3479fb8b3afadad006838a1015
4
- data.tar.gz: 4af280ccacdfb71cbbb1f512c126ec96614fde37
3
+ metadata.gz: a224fb2b8ea422604880f3b74da2707faa48e851
4
+ data.tar.gz: ec43fdee763e00f9f3dc7a8bab358deca75c7e3e
5
5
  SHA512:
6
- metadata.gz: c9d81c0316e853e3424e531dfa1876e2b497c125fe9e9e36c066d5584513f1c77033cfeb40bc37e8d1569287217e5749a90e6ed9030fb88f5f03c9c8391b187c
7
- data.tar.gz: fc60962b1c36d8b5ebe09a19b5c052f55bda9d00d094afdb03b5a7ac4cf29166584d28a85bcb54e8d7b7c045f7d06f0750b80d6893e18125236ace99fd22f87e
6
+ metadata.gz: e90df809fa48da8d9fcf31c6aab30f293a6608e442ab157411b648323be4549adfb3e2e7f5c804a3070d2a5e166a9cfe34c874b979b982f51fb925a4c6a750a3
7
+ data.tar.gz: 0b6da65ffe237038e73398aa68c2fc1f471f001fba8cfb2325c076ae271b1071f9e809a6eaa04a5e11b397d26d4b4397df49d3a16dd23e334293b71aa0bbb61e
@@ -0,0 +1,39 @@
1
+ require "djinni"
2
+
3
+ class DjinniHelpWish < DjinniWish
4
+ def aliases
5
+ return [ "?", "help" ]
6
+ end
7
+
8
+ def description
9
+ return "Show helpful information for a wish or wishes"
10
+ end
11
+
12
+ def execute(args, env = {})
13
+ if (args.nil? || args.empty?)
14
+ env["djinni_wishes"].sort.map do |aliaz, wish|
15
+ puts "#{aliaz}\t#{wish.description}"
16
+ end
17
+ elsif (args.split(" ").length == 1)
18
+ env["djinni_wishes"].sort.map do |aliaz, wish|
19
+ if (aliaz == args)
20
+ wish.usage
21
+ return
22
+ end
23
+ end
24
+ puts "Wish #{args} not found!"
25
+ else
26
+ usage
27
+ end
28
+ end
29
+
30
+ def tab_complete(input, env = {})
31
+ return input
32
+ end
33
+
34
+ def usage
35
+ puts "help [wish]"
36
+ puts "\tPrint usage for specified wish. If no wish is"
37
+ puts "\tspecified, print description of all wishes."
38
+ end
39
+ end
@@ -0,0 +1,65 @@
1
+ require "djinni"
2
+
3
+ class DjinniHistoryWish < DjinniWish
4
+ def aliases
5
+ return [ "hist", "history" ]
6
+ end
7
+
8
+ def description
9
+ return "Show history or execute commands from history"
10
+ end
11
+
12
+ def execute(args, env = {})
13
+ djinni = env["djinni"]
14
+ history = env["djinni_history"]
15
+
16
+ case args
17
+ when nil, ""
18
+ history.each_index do |index|
19
+ puts "#{index}:\t#{history[index]}"
20
+ end
21
+ when "clear"
22
+ history.clear
23
+ when %r{^[0-9]+( [0-9]+)*$}
24
+ args.split(" ").each do |arg|
25
+ index = arg.to_i
26
+ if ((index >= 0) && (index < history.length))
27
+ djinni.grant_wish("#{history[index]}\n")
28
+ else
29
+ puts "Index out of bounds"
30
+ end
31
+ end
32
+ else
33
+ usage
34
+ end
35
+ end
36
+
37
+ def tab_complete(input, env = {})
38
+ history = env["djinni_history"]
39
+
40
+ included = input.split(" ")
41
+ completions = (0...history.length).to_a
42
+
43
+ included.each do |item|
44
+ completions.delete(item.to_i)
45
+ end
46
+
47
+ if (input.empty? || input.end_with?(" "))
48
+ puts
49
+ completions.sort.each do |index|
50
+ puts "#{index}:\t#{history[index]}"
51
+ end
52
+ return input
53
+ end
54
+
55
+ return "#{input} "
56
+ end
57
+
58
+ def usage
59
+ puts "history [option]"
60
+ puts "\t#{description}."
61
+ puts "\tOPTIONS"
62
+ puts "\t\t[0-9]+\tExecute command from history"
63
+ puts "\t\tclear\tClear history"
64
+ end
65
+ end
@@ -0,0 +1,24 @@
1
+ require "djinni"
2
+
3
+ class DjinniQuitWish < DjinniWish
4
+ def aliases
5
+ return [ "bye", "exit", "q", "quit" ]
6
+ end
7
+
8
+ def description
9
+ return "Quit"
10
+ end
11
+
12
+ def execute(args, env = {})
13
+ exit 0
14
+ end
15
+
16
+ def tab_complete(input, env = {})
17
+ return input
18
+ end
19
+
20
+ def usage
21
+ puts aliases.join(", ")
22
+ puts "\t#{description}."
23
+ end
24
+ end
data/lib/djinni.rb CHANGED
@@ -2,17 +2,17 @@ require "io/console"
2
2
  require "pathname"
3
3
  require "terminfo"
4
4
 
5
+ require_relative "djinni_exit"
5
6
  require_relative "djinni_wish"
6
7
 
7
- class Exit
8
- GOOD = 0
9
- UNKNOWN_WISH = 1
10
- end
11
-
12
8
  class Djinni
13
9
  def grant_wish(input, env = {})
14
10
  return "" if (input.nil? || input.empty?)
15
11
 
12
+ env["djinni"] = self
13
+ env["djinni_history"] = @history
14
+ env["djinni_wishes"] = @wishes
15
+
16
16
  case input[-1]
17
17
  when "\x03" # ^C
18
18
  puts if (@interactive)
@@ -33,8 +33,6 @@ class Djinni
33
33
  return "#{name} #{wish.tab_complete(args, env)}"
34
34
  else
35
35
  wishes = @wishes.keys
36
- wishes.push("help")
37
- wishes.push("history")
38
36
  wishes.sort.each do |wish|
39
37
  if (wish.start_with?(input))
40
38
  return wish
@@ -47,29 +45,16 @@ class Djinni
47
45
  puts if (@interactive)
48
46
  return "" if (input.empty?)
49
47
 
50
- # Only keep newest wish if repeat
51
- @history.delete(input)
52
- @history.push(input)
53
- @hist_index = nil
54
-
55
48
  name, args = input.split(" ", 2)
56
49
 
57
- if ((name == "help") || (name == "?"))
58
- print_help(args)
59
- return ""
60
- end
61
-
62
- if ((name == "history") || (name == "hist"))
63
- print_history
64
- return ""
65
- end
66
-
67
50
  @wishes.sort.map do |aliaz, wish|
68
51
  if (aliaz == name)
69
52
  wish.execute(args, env)
53
+ store_history(input)
70
54
  return ""
71
55
  end
72
56
  end
57
+ store_history(input)
73
58
  return nil
74
59
  when "\e" # Arrow keys
75
60
  code = "\e"
@@ -93,10 +78,10 @@ class Djinni
93
78
  return ""
94
79
  end
95
80
  when "\e[C" # Right arrow
96
- # TODO maybe
81
+ # TODO maybe implement right arrow
97
82
  return input[0..-2]
98
83
  when "\e[D" # Left arrow
99
- # TODO maybe
84
+ # TODO maybe implement left arrow
100
85
  return input[0..-2]
101
86
  else
102
87
  return input[0..-2]
@@ -120,6 +105,41 @@ class Djinni
120
105
  @width = TermInfo.screen_size[1]
121
106
  end
122
107
  )
108
+
109
+ load_builtins
110
+ end
111
+
112
+ def load_builtins
113
+ builtins = {
114
+ "help": "DjinniHelpWish",
115
+ "history": "DjinniHistoryWish",
116
+ "quit": "DjinniQuitWish"
117
+ }
118
+
119
+ builtins.each do |file, wish|
120
+ require_relative "builtin/#{file}"
121
+ load_wish(wish)
122
+ end
123
+ end
124
+
125
+ def load_wish(clas)
126
+ return if (clas.nil?)
127
+ clas.strip!
128
+ return if (clas.empty?)
129
+
130
+ wish = nil
131
+ begin
132
+ wish = Object::const_get(clas).new
133
+ rescue NameError => e
134
+ puts "Unknown wish class #{clas}!"
135
+ exit DjinniExit::UNKNOWN_WISH
136
+ end
137
+
138
+ return if (wish.nil?)
139
+
140
+ wish.aliases.each do |aliaz|
141
+ @wishes[aliaz] = wish
142
+ end
123
143
  end
124
144
 
125
145
  def load_wishes(dir)
@@ -136,55 +156,13 @@ class Djinni
136
156
  \grep -E "class .* \< DjinniWish" #{file} | \
137
157
  awk '{print $2}'
138
158
  ).each_line do |clas|
139
- next if (clas.nil?)
140
- clas.strip!
141
- next if (clas.empty?)
142
-
143
- wish = nil
144
- begin
145
- wish = Object::const_get(clas).new
146
- rescue NameError => e
147
- puts "Unknown wish class #{clas}!"
148
- exit Exit::UNKNOWN_WISH
149
- end
150
-
151
- next if (wish.nil?)
152
-
153
- wish.aliases.each do |aliaz|
154
- @wishes[aliaz] = wish
155
- end
159
+ load_wish(clas)
156
160
  end
157
161
  end
158
162
 
159
163
  @loaded_from.push(dir)
160
164
  end
161
165
 
162
- def print_help(name = nil)
163
- if (name.nil?)
164
- @wishes.sort.map do |aliaz, wish|
165
- puts "#{aliaz}\t#{wish.description}"
166
- end
167
- elsif (name.split(" ").length > 1)
168
- puts "help [wish]"
169
- puts "\tPrint usage for specified wish. If no wish is"
170
- puts "\tspecified, print description of all wishes."
171
- else
172
- @wishes.sort.map do |aliaz, wish|
173
- if (aliaz == name)
174
- wish.usage
175
- return
176
- end
177
- end
178
- puts "Wish #{name} not found!"
179
- end
180
- end
181
-
182
- def print_history
183
- @history.each do |wish|
184
- puts wish
185
- end
186
- end
187
-
188
166
  def prompt(prompt_sym = "$ ")
189
167
  @interactive = true
190
168
 
@@ -218,4 +196,12 @@ class Djinni
218
196
  end
219
197
  end
220
198
  end
199
+
200
+ def store_history(input)
201
+ # Only keep newest wish if repeat
202
+ # @history.delete(input)
203
+
204
+ @history.push(input)
205
+ @hist_index = nil
206
+ end
221
207
  end
@@ -0,0 +1,4 @@
1
+ class DjinniExit
2
+ GOOD = 0
3
+ UNKNOWN_WISH = 1
4
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: djinni
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Whittaker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-08 00:00:00.000000000 Z
11
+ date: 2015-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
@@ -58,7 +58,11 @@ executables: []
58
58
  extensions: []
59
59
  extra_rdoc_files: []
60
60
  files:
61
+ - lib/builtin/help.rb
62
+ - lib/builtin/history.rb
63
+ - lib/builtin/quit.rb
61
64
  - lib/djinni.rb
65
+ - lib/djinni_exit.rb
62
66
  - lib/djinni_wish.rb
63
67
  homepage: http://mjwhitta.github.io/djinni
64
68
  licenses: