lots of changes to get it working with zcc.record instead of marc.record objects
[zcc.git] / lib / zcc / cli_display.rb
blob197b03411bd46fce90ebc326b25322b5f0253d63
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     #puts rec_copy.class
12     #puts rec_copy.inspect
13     #STDIN.gets
14     #rec_copy = rec_copy.sort_by {|rec| rec['245']['a']}
15     clear = %x{clear}
16     #print clear
17     
18     # Help statements:
19     
20     help_n = "Next Z39.50 server/group of zservers.\n>  n"
21     
22     help_num = "View that record by typing in the index number\n> 16." # this represents the '#'
23     
24     help_p = "Pick the record at that index position into the result set to work on and save."
26     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"
28     #help_v = "View the record. Shows the full MARC record."
30     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."
32     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 '-'"
34     help_l = "Lint the record."
36     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."
38     help_f = "Forwards through the result set"
39     help_b = "backwards through the result set"
40     help_all = "Select all the records in the result set to work on and save."
41     help_none = "Select none of the records from the final set."
43     not_implemented = "This feature is not yet implemented."
45     #take_how_many = 'multi' #'one' # 'multi'
47     recs_to_return = []
49     loop do
50       choose do |menu|
51         print $clear_code
52         
53         rec_copy.each_index do |index|
54           ZCC.display_menu(rec_copy, index)
55           puts "\n\n"
56         end
57         
58         recs_length = rec_copy.length
59         index_range = (0..recs_length - 1)
61         whichrec = ''
65         menu.layout = :one_line
66         menu.readline = true
67         menu.shell  = true
68         menu.prompt = "Enter "
69         menu.select_by = :name
70         
71         # # => view
72               menu.choice('#'.intern, help_num) do |command, details|
73           say_help(help_num)
74         end
75         for x in index_range
76                 menu.hidden("#{x}".intern, help_num) do |cmd, details|
77             print "\a"
78                   say("#{ZCC.zcc_marc_str_bold(rec_copy[cmd.to_s.to_i].marc.to_s, 'record')}")
79             ask("<%= color('Hit ENTER to continue...', :headline) %> ") 
80           end
81         end
83         # next
84               if take_how_many == 'multi'
85           menu.choice(:n, help_n) do |command, details|
86             return recs_to_return
87           end
88         end
90         # pick
91               menu.choice("p#".intern, help_p) { |cmd, d|  say_help(help_p) }
92               menu.hidden(:pick, help_p) { |cmd, d|  say_help(help_p) }
93         for x in index_range
94                 menu.hidden("p#{x}") do |cmd, d|
95             num_picked = cmd[1,99]
96                   recs_to_return << rec_copy[num_picked.to_i]
97             if take_how_many == 'one'
98               return recs_to_return
99             end
100           end
101         end
104         # sort
105               menu.hidden('sort', help_s) do |command, details|
106           say(help_s)
107           say(not_implemented)
108           #sort_menu(rec_copy)
109         end
110               
111         
112         # remove
113         menu.hidden(:remove, help_r) {|cmd, d| puts "this command doesn't work yet. :("}
114         
115         # compare
116               menu.choice('c#-#', help_c) do |command, details|
117           say_help(help_c)
118         end
119         comparison = []
120         for x in (0..recs_length-1)
121           for y in (0..recs_length-1)
122             unless x == y
123               comparison << 'c' + x.to_s + '-' + y.to_s
124             end
125           end
126         end
127         comparison.each do |compare|
128           menu.hidden(compare, help_c) do |cmd, details|
129             say("<%= color('comparison:', :headline) %>")
130             cmd = cmd[1,99]
131             compare_nums = cmd.split('-')
132             compare_marc(rec_copy[compare_nums[0].to_i], rec_copy[compare_nums[1].to_i])
133             ask("<%= color('Hit ENTER to continue...', :headline) %> ")
134             #STDIN.gets
135             next
136           end
137         end
139         # lint
140               menu.choice('l#', help_l) do |cmd, d|
141           say_help(help_l)
142           #ask("Which record do you want to lint? ")
143         end
144               menu.hidden(:lint) {|cmd, d| say(help_l)}
145         for x in (0..recs_length - 1)
146                 menu.hidden("l#{x}") do |cmd, d| 
147             rec_copy[cmd[1,99].to_i].marc.linter
148             ask("<%= color('Hit ENTER to continue...', :headline) %> ")
149           end
150         end
152         # done => selected as many 
153               unless take_how_many == 'one'
154           menu.choice(:d, help_d) do |cmd, d|
155             say("You will not search any more z-servers!")
156             recs_to_return << "done"
157             return recs_to_return
158           end
159         end
161         # none -- only for final record taking
162               if take_how_many == 'one'    
163                 menu.choice(:none, help_none) do |cmd, d| 
164             say("Since you cannot decide on a good record, please refer the book to a cataloger.")
165             return "none"       
166           end
167         end
168         
169         # forwards
170         menu.hidden(:f, help_f) {|cmd, d| say(not_implemented)}
171   
172         # backwards
173         menu.hidden(:b, help_b) {|cmd, d| say(not_implemented)}
174         
175         # ALL => select all from the result set
176         menu.hidden(:all, help_all) do |cmd, d| 
177           say(not_implemented)
178           
179         end
181         # quit
182               menu.choice(:quit, "Exit program.") { |cmd, d| exit}
183         
184       end
185     end
186   end
188   def display_menu(rec_copy, index)
189     #puts rec_copy.class
190     #puts rec_copy[index].class
191     #STDIN.gets
192     field_width = $term_width - 8
193     say("<%= color(\"#{index}\", :index) %> ")
194     puts rec_copy[index].zserver.to_s
195     ['245', '260', '300'].each do |field|
196       string = rec_copy[index].marc[field].to_s
197       string.rstrip!
198       string.lstrip!
199       if string.length < field_width
200         say("\t#{ZCC.zcc_marc_str_bold(string, field)}")
201       else
202         better_string = ZCC.wrap_field(string, field_width)
203         #puts better_string
204         say("\t#{ZCC.zcc_marc_str_bold(better_string, field)}")
205         
206       end 
207     end        
208   end
209   
210   #currently this goes word by word. how difficult to go field by subfield?
211   def wrap_field(s, width)
212     lines = []
213     line = ""
214     smaller_width = width - 7
215     s.split(/\s+/).each do |word|
216             if (line.size + word.size) >= (width - 3)
217         lines << line 
218         line = word
219         width = smaller_width
220             elsif line.empty?
221         line = word
222             else
223         line << " " << word
224       end
225           end
226           lines << line if line
227     return lines.join("\n\t\t")
228   end
229   
230   
231   def zcc_marc_str_bold(string, field)
232     #puts field
233     string.gsub!("'", "\'")
234     #string.gsub!("(", "\(")
235     #string.gsub!(")", "\)")
236     if field == '245'
237       string.gsub!(/(\$a\s)(.*?)(\$.)/, "\\1<%= color(\"\\2\", :field_hilite) %>\\3")
238     elsif field == '260'
239       #puts 'gets here'
240       string.gsub!(/(\$c\s)([\[0-9A-Za-z\]]*)(.|$)/,  "\\1<%= color(\"\\2\", :field_hilite) %>\\3")
241       
242       #string.gsub!(/(\$c)(.*)(\$\s)/,  "\\1<%= color(\"\\2\", :field_hilite) %>\\3")
243       #string.gsub!(/(\$c)(.*)(\.|$)/,  "\\1<%= color(\"\\2\", :field_hilite) %>\\3")
244     elsif field == '300'
245       string.gsub!(/(\$a)(.*?)(\$|$)/, "\\1<%= color(\"\\2\", :field_hilite) %>\\3")
246     elsif field == 'record'
247       string.sub!(/(LEADER.{19})(.{1})/, "\\1<%= color(\"\\2\", :headline) %>") #colorizes the value of the standard (AACR2, ISBD, or none)
248     end
249     
250     string.gsub!( /\$(.)/, "<%= color('$\\1', :marc_tag) %>" )
251     string.gsub!( /^(\d\d\d)\s/, "<%= color('\\1 ', :marc_tag) %>")
252     string
253   end
254   
255   def not_valid_value
256     print $clear_code
257     say("\a<%= color(\"That's not a valid value. Try again.\", :headline) %> ")
258     sleep 1
259     print "\a"
260   end
263   def say_help h
264     say(h)
265     ask("Hit enter to continue.")
266   end
267   
268   def sort_menu records
269     loop do
270       choose do |menu|
271         menu.layout = :menu_only
272         menu.readline = true
273         menu.shell  = true
274         #menu.prompt = "> "
275         menu.select_by = :index_or_name
276         menu.choice(:author){|cmd, d| say("not implemented yet") }
277         menu.choice(:date){|cmd, d| say("not implemented yet")}
278         menu.choice(:subfield, "in the form of 245a"){|cmd, d| say("not implemented yet")}
279         menu.choice(:relevancy){|cmd, d| say("not implemented yet")}
280       end
281     end
282   end
283