d: utils_response, rely on module (remove class) extend module
[software/sisu] / lib / sisu / develop / utils_response.rb
1 # encoding: utf-8
2 =begin
3
4 * Name: SiSU
5
6 ** Description: documents, structuring, processing, publishing, search
7 *** Request response
8
9 ** Author: Ralph Amissah
10 <ralph@amissah.com>
11 <ralph.amissah@gmail.com>
12
13 ** Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
14 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Ralph Amissah,
15 All Rights Reserved.
16
17 ** License: GPL 3 or later:
18
19 SiSU, a framework for document structuring, publishing and search
20
21 Copyright (C) Ralph Amissah
22
23 This program is free software: you can redistribute it and/or modify it
24 under the terms of the GNU General Public License as published by the Free
25 Software Foundation, either version 3 of the License, or (at your option)
26 any later version.
27
28 This program is distributed in the hope that it will be useful, but WITHOUT
29 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
30 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
31 more details.
32
33 You should have received a copy of the GNU General Public License along with
34 this program. If not, see <http://www.gnu.org/licenses/>.
35
36 If you have Internet connection, the latest version of the GPL should be
37 available at these locations:
38 <http://www.fsf.org/licensing/licenses/gpl.html>
39 <http://www.gnu.org/licenses/gpl.html>
40
41 <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>
42
43 ** SiSU uses:
44 * Standard SiSU markup syntax,
45 * Standard SiSU meta-markup syntax, and the
46 * Standard SiSU object citation numbering and system
47
48 ** Hompages:
49 <http://www.jus.uio.no/sisu>
50 <http://www.sisudoc.org>
51
52 ** Git
53 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=summary>
54 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=blob;f=lib/sisu/develop/utils_response.rb;hb=HEAD>
55
56 =end
57 module SiSU_Response
58 def available_selections_
59 %{'yes', 'no', 'quit' or 'exit'; [ynqx]}
60 end
61 def response?(ask)
62 response='redo'
63 print ask + %{ [#{available_selections_}]: }
64 response=File.new('/dev/tty').gets.strip
65 case response
66 when /^(?:y|yes)$/ then true
67 when /^(?:n|no)$/ then false
68 when /^(?:[qx]|quit|exit)$/ then exit
69 else puts %{[please type: #{available_selections_}]}
70 response?(ask)
71 end
72 end
73 def query
74 def selections_available_(selections=:strict)
75 short_options=(selections == :strict) ? '' : '; [ynqx]'
76 %{'yes', 'no', 'quit' or 'exit'#{short_options}}
77 end
78 def selection_options
79 def response_strict(resp)
80 case resp
81 when /^(?:yes)$/ then true
82 when /^(?:no)$/ then false
83 when /^(?:quit|exit)$/ then exit
84 else
85 puts %{response was: #{resp}}
86 puts %{[please type to select: #{selections_available_(:strict)}]}
87 answer?('',:strict)
88 end
89 end
90 def response_short(resp)
91 case resp
92 when /^(?:y|yes)$/ then true
93 when /^(?:n|no)$/ then false
94 when /^(?:[qx]|quit|exit)$/ then exit
95 else
96 puts %{response was: #{resp}}
97 puts %{[please type to select: #{selections_available_(:short)}]}
98 answer?('',:short)
99 end
100 end
101 self
102 end
103 def answer?(ask,selections=:strict)
104 resp='redo'
105 print ask + %{PROCEED? [#{selections_available_(selections)}]: }
106 resp=File.new('/dev/tty').gets.strip
107 (selections==:strict) \
108 ? selection_options.response_strict(resp)
109 : selection_options.response_short(resp)
110 end
111 self
112 end
113 end
114 __END__