d: utils_response, rely on module (remove class) extend module
[software/sisu] / lib / sisu / current / 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/current/utils_response.rb;hb=HEAD>
55
56 =end
57 module SiSU_Response
58 class Response
59 def response?(ask) #move to more appropriate directory later
60 response='redo'
61 print ask + " ['yes', 'no' or 'quit']: "
62 response=File.new('/dev/tty').gets.strip
63 ans=if response=='yes' then true
64 elsif response=='no' then false
65 elsif response =~/^quit|exit$/ then exit
66 else puts "[please type: 'yes', 'no' or 'quit']"
67 response?(ask)
68 end
69 ans
70 end
71 end
72 def query
73 def selections_available_(selections=:strict)
74 short_options=(selections == :strict) ? '' : '; [ynqx]'
75 %{'yes', 'no', 'quit' or 'exit'#{short_options}}
76 end
77 def selection_options
78 def response_strict(resp)
79 case resp
80 when /^(?:yes)$/ then true
81 when /^(?:no)$/ then false
82 when /^(?:quit|exit)$/ then exit
83 else
84 puts %{response was: #{resp}}
85 puts %{[please type to select: #{selections_available_(:strict)}]}
86 answer?('',:strict)
87 end
88 end
89 def response_short(resp)
90 case resp
91 when /^(?:y|yes)$/ then true
92 when /^(?:n|no)$/ then false
93 when /^(?:[qx]|quit|exit)$/ then exit
94 else
95 puts %{response was: #{resp}}
96 puts %{[please type to select: #{selections_available_(:short)}]}
97 answer?('',:short)
98 end
99 end
100 self
101 end
102 def answer?(ask,selections=:strict)
103 resp='redo'
104 print ask + %{PROCEED? [#{selections_available_(selections)}]: }
105 resp=File.new('/dev/tty').gets.strip
106 (selections==:strict) \
107 ? selection_options.response_strict(resp)
108 : selection_options.response_short(resp)
109 end
110 self
111 end
112 end
113 __END__