added loop for winnowing result set and a method to remove unselected records from...
[zcc.git] / lib / zcc / cli_display.rb
blobd290417b7209dae368b0f491ada3ee3810d706a3
1 module ZCC
2   
3   def zcc_select_good_marc(results, take_how_many)
4     #puts self[0]
5     #unless taken[0].is_a? MARC::Record
6     #  raise ArgumentError, "This Array doesn't have a MARC::Record!" 
7     #end
8     #puts self
9     
10     rec_copy = results.records
11     if results.records.empty?
12       puts "Your search returned no records!"
13       return nil
14     end
15     #puts rec_copy.class
16     #puts rec_copy.inspect
17     #STDIN.gets
18     #rec_copy = rec_copy.sort_by {|rec| rec['245']['a']}
19     clear = %x{clear}
20     #print clear
21     
22     # Help statements:
23     
24     help_n = "Next Z39.50 server/group of zservers.\n>  n"
25     
26     help_num = "View that record by typing in the index number\n> 16." # this represents the '#'
27     
28     help_p = "Pick the record at that index position into the result set to work on and save."
30     help_s = "Select sort. Hit 's' to get a menu of sort possibilities. Possible sorts will include: relevancy, title, author, date, date_fixed, subfield (in form of 245a)...\n>  s5"
32     #help_v = "View the record. Shows the full MARC record."
34     help_r = "Remove the record from the result set. This is a way to narrow the result set.\n> r4\nRemoves record 4 from the result set."
36     help_c = "Compare two records. Compares the records line by line. Lines with an 'm' match each other. Lines with a plus sign are in the first record but not the second. Lines with a minus sign '-'"
38     help_l = "Lint the record."
40     help_d = "Done selecting records. If at least one record has been selected you continue on. If no records have been selected you are presented with a search prompt again."
42     help_f = "Forwards through the result set"
43     help_b = "backwards through the result set"
44     help_all = "Select all the records in the result set to work on and save."
45     help_none = "Select none of the records from the final set."
46     help_u = "Unselect a record from the result set."
47     not_implemented = "This feature is not yet implemented."
49     #take_how_many = 'multi' #'one' # 'multi'
51     recs_to_return = []
53     loop do
54       choose do |menu|
55         print $clear_code
56         
57         rec_copy.each_index do |index|
58           ZCC.display_menu(rec_copy, index)
59           puts "\n\n"
60         end
61         
62         recs_length = rec_copy.length
63         index_range = (0..recs_length - 1)
65         whichrec = ''
69         menu.layout = :one_line
70         menu.readline = true
71         menu.shell  = true
72         menu.prompt = "Enter "
73         menu.select_by = :name
74         
75         # # => view
76               menu.choice('#'.intern, help_num) do |command, details|
77           say_help(help_num)
78         end
79         for x in index_range
80                 menu.hidden("#{x}".intern, help_num) do |cmd, details|
81             print "\a"
82                   say("#{ZCC.zcc_marc_str_bold(rec_copy[cmd.to_s.to_i].marc.to_s, 'record')}")
83             ask("<%= color('Hit ENTER to continue...', :headline) %> ") 
84           end
85         end
87         # next
88               if take_how_many == 'multi'
89           menu.choice(:n, help_n) do |command, details|
90             return 'next'
91           end
92         end
94         # pick
95               menu.choice("p#".intern, help_p) { |cmd, d|  say_help(help_p) }
96               menu.hidden(:pick, help_p) { |cmd, d|  say_help(help_p) }
97         for x in index_range
98                 menu.hidden("p#{x}") do |cmd, d|
99             num_picked = cmd[1,99]
100                   rec_copy[num_picked.to_i].selected = true
101             if take_how_many == 'one'
102               return results
103             end
104           end
105         end
106         
107         
108         # remove
109         menu.hidden(:remove, help_r) {|cmd, d| puts "this command doesn't work yet. :("}
110         
111         
112         menu.choice("u#".intern, help_u) { |cmd, d|  say_help(help_u) }
113               menu.hidden(:unselect, help_u) { |cmd, d|  say_help(help_u) }
114         for x in index_range
115                 menu.hidden("u#{x}", help_u) do |cmd, d|
116             num_picked = cmd[1,99]
117                   rec_copy[num_picked.to_i].selected = false
118             return
119           end
120         end
122         # sort
123               menu.hidden('sort', help_s) do |command, details|
124           say(help_s)
125           say(not_implemented)
126           #sort_menu(rec_copy)
127         end
128               
129         
130         
131         
132         # compare
133               menu.choice('c#-#', help_c) do |command, details|
134           say_help(help_c)
135         end
136         comparison = []
137         for x in (0..recs_length-1)
138           for y in (0..recs_length-1)
139             unless x == y
140               comparison << 'c' + x.to_s + '-' + y.to_s
141             end
142           end
143         end
144         comparison.each do |compare|
145           menu.hidden(compare, help_c) do |cmd, details|
146             say("<%= color('comparison:', :headline) %>")
147             cmd = cmd[1,99]
148             compare_nums = cmd.split('-')
149             compare_marc(rec_copy[compare_nums[0].to_i], rec_copy[compare_nums[1].to_i])
150             ask("<%= color('Hit ENTER to continue...', :headline) %> ")
151             #STDIN.gets
152             next
153           end
154         end
156         # lint
157               menu.choice('l#', help_l) do |cmd, d|
158           say_help(help_l)
159           #ask("Which record do you want to lint? ")
160         end
161               menu.hidden(:lint) {|cmd, d| say(help_l)}
162         for x in (0..recs_length - 1)
163                 menu.hidden("l#{x}") do |cmd, d| 
164             rec_copy[cmd[1,99].to_i].linter
165             ask("<%= color('Hit ENTER to continue...', :headline) %> ")
166           end
167         end
169         # done => selected as many 
170               unless take_how_many == 'one'
171           menu.choice(:d, help_d) do |cmd, d|
172             say("You will not search any more z-servers!")
173             #recs_to_return << "done"
174             return 'done'
175           end
176         end
178         # none -- only for final record taking
179               if take_how_many == 'one'    
180                 menu.choice(:none, help_none) do |cmd, d| 
181             say("Since you cannot decide on a good record, please refer the book to a cataloger.")
182             return "none"       
183           end
184         end
185         
186         # forwards
187         menu.hidden(:f, help_f) {|cmd, d| say(not_implemented)}
188   
189         # backwards
190         menu.hidden(:b, help_b) {|cmd, d| say(not_implemented)}
191         
192         # ALL => select all from the result set
193         menu.hidden(:all, help_all) do |cmd, d| 
194           say(not_implemented)
195           
196         end
198         # quit
199               menu.choice(:quit, "Exit program.") { |cmd, d| exit}
200         
201       end
202     end
203   end
205   def display_menu(rec_copy, index)
206     #puts rec_copy.class
207     #puts rec_copy[index].class
208     #STDIN.gets
209     field_width = $term_width - 8
210     say("<%= color(\"#{index}\", :index) %> ")
211     puts rec_copy[index].zserver.to_s
212     ['245', '260', '300'].each do |field|
213       #puts rec_copy[index].marc.to_s
214       string = rec_copy[index].marc[field].to_s
215       #puts string
216       string.rstrip!
217       string.lstrip!
218       begin
219       if string.length < field_width
220         say("\t#{ZCC.zcc_marc_str_bold(string, field)}")
221       else
222         better_string = ZCC.wrap_field(string, field_width)
223         #puts better_string
224         say("\t#{ZCC.zcc_marc_str_bold(better_string, field)}")
225       end
226       rescue
227         puts "Skipping record ##{index}"
228         next
229       end 
230     end        
231   end
232   
233   #currently this goes word by word. how difficult to go field by subfield?
234   def wrap_field(s, width)
235     lines = []
236     line = ""
237     smaller_width = width - 7
238     s.split(/\s+/).each do |word|
239             if (line.size + word.size) >= (width - 3)
240         lines << line 
241         line = word
242         width = smaller_width
243             elsif line.empty?
244         line = word
245             else
246         line << " " << word
247       end
248           end
249           lines << line if line
250     return lines.join("\n\t\t")
251   end
252   
253   
254   def zcc_marc_str_bold(string, field)
255     #puts field
256     string.gsub!("'", "\'")
257     unless field == 'record'
258       string.gsub!('"', '\"')
259     end
260     #string.gsub!("(", "\(")
261     #string.gsub!(")", "\)")
262     if field == '245'
263       string.gsub!(/(\$a\s)(.*?)(\$.)/, "\\1<%= color(\"\\2\", :field_hilite) %>\\3")
264     elsif field == '260'
265       #puts 'gets here'
266       string.gsub!(/(\$c\s)([\[0-9A-Za-z\]]*)(.|$)/,  "\\1<%= color(\"\\2\", :field_hilite) %>\\3")
267       
268       #string.gsub!(/(\$c)(.*)(\$\s)/,  "\\1<%= color(\"\\2\", :field_hilite) %>\\3")
269       #string.gsub!(/(\$c)(.*)(\.|$)/,  "\\1<%= color(\"\\2\", :field_hilite) %>\\3")
270     elsif field == '300'
271       string.gsub!(/(\$a)(.*?)(\$|$)/, "\\1<%= color(\"\\2\", :field_hilite) %>\\3")
272     elsif field == 'record'
273       string.sub!(/(LEADER.{19})(.{1})/, "\\1<%= color(\"\\2\", :headline) %>") #colorizes the value of the standard (AACR2, ISBD, or none)
274     end
275     
276     string.gsub!( /\$(.)/, "<%= color('$\\1', :marc_tag) %>" )
277     string.gsub!( /^(\d\d\d)\s/, "<%= color('\\1 ', :marc_tag) %>")
278     string
279   end
280   
281   def not_valid_value
282     print $clear_code
283     say("\a<%= color(\"That's not a valid value. Try again.\", :headline) %> ")
284     sleep 1
285     print "\a"
286   end
289   def say_help h
290     say(h)
291     ask("Hit enter to continue.")
292   end
293   
294   def sort_menu records
295     loop do
296       choose do |menu|
297         menu.layout = :menu_only
298         menu.readline = true
299         menu.shell  = true
300         #menu.prompt = "> "
301         menu.select_by = :index_or_name
302         menu.choice(:author){|cmd, d| say("not implemented yet") }
303         menu.choice(:date){|cmd, d| say("not implemented yet")}
304         menu.choice(:subfield, "in the form of 245a"){|cmd, d| say("not implemented yet")}
305         menu.choice(:relevancy){|cmd, d| say("not implemented yet")}
306       end
307     end
308   end
309