Module: Lowline
- Defined in:
- lib/ditz/lowline.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#use_editor_if_possible ⇒ Object
Returns the value of attribute use_editor_if_possible.
Instance Method Summary collapse
- #ask(q, opts = {}) ⇒ Object
- #ask_for_many(plural_name, name = nil) ⇒ Object
- #ask_for_selection(stuff, name, to_string = :to_s, many = false) ⇒ Object
- #ask_multiline(q) ⇒ Object
- #ask_multiline_or_editor(q, opts = {}) ⇒ Object
- #ask_via_editor(q, opts = {}) ⇒ Object
- #ask_yon(q) ⇒ Object
- #editor ⇒ Object
- #run_editor {|f| ... } ⇒ Object
Instance Attribute Details
#use_editor_if_possible ⇒ Object
Returns the value of attribute use_editor_if_possible.
57 58 59 |
# File 'lib/ditz/lowline.rb', line 57 def use_editor_if_possible @use_editor_if_possible end |
Instance Method Details
#ask(q, opts = {}) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ditz/lowline.rb', line 85 def ask q, opts={} default_s = case opts[:default] when nil; nil when ""; " (enter for none)" else; " (enter for #{opts[:default].to_s})" end tail = case q when /[:?]$/; " " when /[:?]\s+$/; "" else; ": " end while true prompt = [q, default_s, tail].compact.join if Ditz::has_readline? ans = Readline::readline(prompt) else print prompt ans = STDIN.gets.strip end if opts[:default] ans = opts[:default] if ans.blank? else next if ans.blank? && !opts[:empty_ok] end break ans unless (opts[:restrict] && ans !~ opts[:restrict]) end end |
#ask_for_many(plural_name, name = nil) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/ditz/lowline.rb', line 182 def ask_for_many plural_name, name=nil name ||= plural_name.gsub(/s$/, "") stuff = [] while true puts puts "Current #{plural_name}:" if stuff.empty? puts "None!" else stuff.each_with_index { |c, i| puts " #{i + 1}) #{c}" } end puts ans = ask "(A)dd #{name}, (r)emove #{name}, or (d)one" case ans when "a", "A" ans = ask "#{name.capitalize} name", "" stuff << ans unless ans =~ /^\s*$/ when "r", "R" ans = ask "Remove which #{name}? (1--#{stuff.size})" stuff.delete_at(ans.to_i - 1) if ans when "d", "D" break end end stuff end |
#ask_for_selection(stuff, name, to_string = :to_s, many = false) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/ditz/lowline.rb', line 210 def ask_for_selection stuff, name, to_string=:to_s, many=false if many return [] if stuff.empty? name = name.pluralize(2, false) puts "Choose one or more #{name} (comma separated list):" else return nil if stuff.empty? puts "Choose a #{name}:" end stuff.each_with_index do |c, i| pretty = case to_string when block_given? && to_string # heh yield c when Symbol c.send to_string when Proc to_string.call c else raise ArgumentError, "unknown to_string argument type; expecting Proc or Symbol" end puts " #{i + 1}) #{pretty}" end js = while true is = ask "#{name.capitalize} (1--#{stuff.size})" next unless is is = is.strip.split(/\s*,\s*/).map { |i| i.to_i } break is if is.all? { |i| (1 .. stuff.size).member?(i) } end ss = js.map { |j| stuff[j - 1] } (many)? ss : ss.first end |
#ask_multiline(q) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/ditz/lowline.rb', line 135 def ask_multiline q puts "#{q} (ctrl-d, ., or /stop to stop, /edit to edit, /reset to reset):" ans = "" while true if Ditz::has_readline? line = Readline::readline('> ') else (line = STDIN.gets) && line.strip! end if line if Ditz::has_readline? Readline::HISTORY.push(line) end case line when /^\.$/, "/stop" break when "/reset" return ask_multiline(q) when "/edit" return ask_via_editor(q, :default => ans) else ans << line + "\n" end else puts break end end ans.multistrip end |
#ask_multiline_or_editor(q, opts = {}) ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/ditz/lowline.rb', line 166 def ask_multiline_or_editor q, opts={} if Lowline.use_editor_if_possible && editor ask_via_editor q, :comments => opts[:comments] else ask_multiline q end end |
#ask_via_editor(q, opts = {}) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ditz/lowline.rb', line 115 def ask_via_editor q, opts={} default = opts[:default] comments = opts[:comments] fn = run_editor do |f| if default f.puts default end f.puts f.puts q.gsub(/^/, "## ") f.puts "##" f.puts "## Enter your text above. Lines starting with a '#' will be ignored." if comments f.puts "##" f.puts comments.gsub(/^/, "## ") end end return unless fn IO.read(fn).gsub(/^#.*$/, "").multistrip end |
#ask_yon(q) ⇒ Object
174 175 176 177 178 179 180 |
# File 'lib/ditz/lowline.rb', line 174 def ask_yon q while true print "#{q} (y/n): " a = STDIN.gets.strip break a if a =~ /^[yn]$/i end =~ /y/i end |
#editor ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/ditz/lowline.rb', line 61 def editor @editor ||= if ENV["EDITOR"] && !ENV["EDITOR"].empty? ENV["EDITOR"] else %w(/usr/bin/sensible-editor /usr/bin/vi).find { |e| File.exist?(e) } end end |
#run_editor {|f| ... } ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ditz/lowline.rb', line 70 def run_editor raise Error, "no editor" unless editor f = Tempfile.new "ditz" yield f f.close cmd = "#{editor} #{f.path.inspect}" mtime = File.mtime f.path system cmd or raise Error, "cannot execute command: #{cmd.inspect}" File.mtime(f.path) == mtime ? nil : f.path end |