Class: Console
Constant Summary collapse
- @@log =
File.("~/.emacs.d/console_log.notes")
Class Method Summary collapse
-
.[](command) ⇒ Object
Run the command in a console.
- .append_log(command, dir, prefix = '') ⇒ Object
- .commands ⇒ Object
- .console? ⇒ Boolean
- .custom_history ⇒ Object
- .do_as_execute(options = {}) ⇒ Object
- .do_last_command ⇒ Object
- .enter(command = nil) ⇒ Object
-
.exit ⇒ Object
Kills running server or process in shell.
- .history(bm) ⇒ Object
-
.launch(options = {}) ⇒ Object
Synchronous - mapped to $ launcher.
- .launch_async(options = {}) ⇒ Object
- .log ⇒ Object
- .menu ⇒ Object
- .open(dir = nil) ⇒ Object
-
.prompt? ⇒ Boolean
Whether buffer ends with shell prompt “…$ ”.
-
.run(command, options = {}) ⇒ Object
Runs shell command asynchronously.
- .search_last_commands ⇒ Object
- .ssh_line(path) ⇒ Object
- .sync(command, options = {}) ⇒ Object
- .to_shell_buffer(dir = nil, options = {}) ⇒ Object
- .tree(*args) ⇒ Object
- .wait_until(buffer, options = {}) ⇒ Object
Class Method Details
.[](command) ⇒ Object
Run the command in a console
55 56 57 |
# File 'lib/xiki/console.rb', line 55 def self.[] command self.run command, :sync=>1 end |
.append_log(command, dir, prefix = '') ⇒ Object
389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/xiki/console.rb', line 389 def self.append_log command, dir, prefix='' return if View.name =~ /_log.notes$/ if dir.nil? dir ||= View.dir dir = "#{dir}/" if dir !~ /\/$/ end command = command.dup command.gsub!(/^/, prefix) unless command =~ /^ *!/ command.gsub!(/^/, ' ') txt = "- #{dir}\n#{command}\n" File.open(@@log, "a") { |f| f << txt } rescue nil end |
.commands ⇒ Object
482 483 484 485 486 487 |
# File 'lib/xiki/console.rb', line 482 def self.commands matches = $el.elvar.comint_input_ring.to_s.scan(/#\("(.+?)" /).flatten matches.map!{|o| o.gsub '\\"', '"'} matches end |
.console? ⇒ Boolean
161 162 163 |
# File 'lib/xiki/console.rb', line 161 def self.console? View.mode == :shell_mode end |
.custom_history ⇒ Object
489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
# File 'lib/xiki/console.rb', line 489 def self.custom_history dir = View.dir history = Console.commands history.uniq! unless Keys.prefix_u history = history.join("\n").gsub(/^/, '% ') View.create :u if ! View.list_names.member?("*shell history") View.to_buffer "*shell history" View.kill_all Notes.mode View.insert "#{history}\n" View.to_highest Tree.search end |
.do_as_execute(options = {}) ⇒ Object
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/xiki/console.rb', line 416 def self.do_as_execute ={} if FileTree.handles? && ! Line.matches(/^\s*\|/) # If we're in a file tree path = Tree.construct_path # # Run command inside of dir # if Line.matches(/\/$/) # If a dir # command = Keys.input :prompt=>"Do shell command on '#{file}': " # output = Console.run(command, :dir=>path, :sync=>true) # FileTree.insert_under(output) if options[:insert] # return View.message "Command ran with output: #{output.strip}." # elsif Keys.prefix_n # View.message "Running command on multiple files isn't implemented yet." # return # end file = Line.without_label command = Keys.input :prompt=>"Shell command on this file (_ means the filename): " command = command =~ /\b_\b/ ? command.gsub(/\b_\b/, "\"#{file}\"") : "#{command} \"#{file}\"" output = Console.run(command, :dir=>File.dirname(path), :sync=>true) Tree.under(output, :escape=>'| ') if [:insert] return View. "Command ran with output: #{output.strip}." end command = Keys.input :prompt=>"Do shell command on '#{View.file_name}': " command = "#{command} #{View.file_name}" output = Console.run(command, :dir=>View.dir, :sync=>true) View.insert(output) if [:insert] return View. "Command ran with output: #{output.strip}." end |
.do_last_command ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/xiki/console.rb', line 267 def self.do_last_command # Code.open_log_view if Keys.prefix_u orig = View.index found = self.to_shell_buffer(nil, :no_create=>true) # If not in shell buffer, go to it return View.("No *console buffer was visible") unless found $el.erase_buffer $el.comint_previous_input(1) self.enter View.to_nth orig end |
.enter(command = nil) ⇒ Object
151 152 153 154 155 156 157 158 159 |
# File 'lib/xiki/console.rb', line 151 def self.enter command=nil View.insert command if command begin $el.comint_send_input rescue # Ol << "Console.enter error here!" end end |
.exit ⇒ Object
Kills running server or process in shell
546 547 548 |
# File 'lib/xiki/console.rb', line 546 def self.exit # Kills running server or process in shell $el.comint_interrupt_subjob end |
.history(bm) ⇒ Object
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
# File 'lib/xiki/console.rb', line 461 def self.history bm dir = Bookmarks[bm] dir = Files.dir_of dir console_log = IO.read(@@log) result = [] match = false console_log.split("\n").each do |l| if l =~ /^[+-] / next match = l =~ /\A- #{Regexp.escape dir}/ end result << "#{l}" if match end "@ #{dir}\n"+result.reverse.uniq.join("\n")+"\n" end |
.launch(options = {}) ⇒ Object
Synchronous - mapped to $ launcher
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/xiki/console.rb', line 319 def self.launch ={} trunk = Xiki.trunk # use Tree.path instead # If it looks like error output, just jump to it if trunk[-1] =~ /[^\/+]\/\| \s+from / || trunk[-1] =~ /[^\/+]\/\| +\// return Code.open_as_file end # if not under file # raise RelinquishException.new # Run in current dir if no parent or @$ if trunk[-1] =~ /^\$ / # TODO Run in current dir? # return end # There's a dir in our chunk, so relinquish control if not fire tree # Handle if # no parent in our chunk # parent in our chunk is file tree line = Line.without_label :leave_indent=>true # If indented, check whether file tree, extracting if yes if line =~ /^\s+\$/ orig = View.cursor path = Tree.construct_path(:list=>true) if path[0] =~ /@/ # If there's a @, it's remote self.append_log path[1], path[0] return Remote.command path end if FileTree.handles?(path) while(path.last =~ /^\$ /) do # Remove all $ foo lines from path path.pop end dir = path.join('') # If starts with ./, replace with current dir dir.sub! /^\.\//, "#{View.dir}/" dir = Bookmarks[dir] end View.to orig end line =~ / *@? ?(.*?)\$+ ?(.+)/ dir ||= $1 unless $1.empty? command = $2 return Tree.<<("- Directory doesn't exist) #{dir}", :no_slash=>1) if dir && ! File.exists?(dir) if [:sync] output = Console.run command, :dir=>dir, :sync=>true output.sub!(/\A\z/, "\n") # Add linebreak if blank Keys.prefix == 1 ? output.gsub!(/^/, '|') : output.gsub!(/^/, '| ').gsub!(/^\| +$/, '|') output.sub! /\n*\z/, "\n" # Guarantee exactly 1 linebreak at end Tree.indent(output) Tree.insert_quoted_and_search output else View. Console.run command, :dir=>dir #, :buffer=>"*console #{dir}" end self.append_log command, dir, '$ ' end |
.launch_async(options = {}) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/xiki/console.rb', line 282 def self.launch_async ={} orig = Location.new orig_view = View.index path = Tree.construct_path(:list=>true) path[0] = Bookmarks[path[0]] if path[0] =~ /^(\.\/|\$[\w-])/ # Expand out bookmark or ./, if there if path[0] =~ /^\// # If has dir (local or remote) line = path.join('') dir, command = line.match(/(.+?)[%&] (.*)/)[1..2] self.append_log "#{command}", dir, '% ' return Iterm.run("cd #{dir}\n#{command}", :activate=>1) if Keys.prefix_uu return Iterm.run("cd #{dir}\n#{command}") if Keys.prefix_u || [:iterm] Console.to_shell_buffer dir, :cd_and_wait=>true else # Otherwise, if by itself - meaning on own line? command = Line.without_label.match(/.*?[%&] ?(.*)/)[1] self.append_log("#{command}", dir, '% ') if command.present? return Iterm.run("#{command}", :activate=>1) if Keys.prefix_uu return Iterm.run("#{command}", :activate=>1) if Keys.prefix_u && [:iterm] return Iterm.run("#{command}") if Keys.prefix_u || [:iterm] self.to_shell_buffer # Go to shell if one is visible end return if command.empty? View.to_bottom View.insert command Console.enter orig.go unless orig_view == View.index end |
.menu ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/xiki/console.rb', line 7 def self. %` - .log/ - .tree/ - .history/ - api/ | In console (asynchronously) @ Console.run "ls" @ Console.run "ls", :dir=>"/tmp" | Inline (synchronously) @ Console.sync "ls" @ Console.sync "ls", :dir=>"/etc" - docs/ You can run shell commands by typing things like this... > In current dir @ $ ls > In other dir @ /tmp/ $ ls > Async, in any open console view @ /tmp/ % ls > Async, in other dir @ /tmp/ % ls > Async, in iTerm @ /tmp/ & ls > Commands you've run recently << log/ > Commands from currently open consoles << tree/ ` end |
.open(dir = nil) ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'lib/xiki/console.rb', line 141 def self.open dir=nil View. dir ||= $el.elvar.default_directory dir = File.(dir)+"/" View.to_buffer $el.generate_new_buffer("shell") raise "dir '#{dir}' doesn't exist" unless File.directory?(dir) $el.elvar.default_directory = dir $el.shell $el.current_buffer end |
.prompt? ⇒ Boolean
Whether buffer ends with shell prompt “…$ ”
452 453 454 455 456 457 458 459 |
# File 'lib/xiki/console.rb', line 452 def self.prompt? right = View.bottom left = right - 10 left = 1 if left < 1 txt = View.txt left, right txt =~ /[>#%$] \z/ end |
.run(command, options = {}) ⇒ Object
Runs shell command asynchronously.
Console.run “ls” Console.run “ls”, :dir=>“/tmp/”
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/xiki/console.rb', line 69 def self.run command, ={} dir = [:dir] sync = [:sync] buffer = [:buffer] reuse_buffer = [:reuse_buffer] # Nil out dir if blank dir = nil if dir && dir.length == 0 if dir dir = Bookmarks.(dir) # If relative dir, make current dir be on end of current dir = "#{$el.elvar.default_directory}/#{dir}" unless dir =~ /^\// dir.gsub!(/\/\/+/, '/') # If file, but not dir, try backing up to the dir raise "- Directory '#{dir}' doesn't exist!" if ! File.exists? dir dir.sub!(/[^\/]+$/, '') if ! File.directory?(dir) # If dir exists, continue if File.directory?(dir) # Put slash on end if not there dir = "#{dir}/" unless dir =~ /\/$/ else # Otherwise, exit return puts("#{dir} is not a dir") end else dir = $el ? $el.elvar.default_directory : "/tmp/" end if sync return command if [:no_enter] profile = File.exists?(File.('~/.profile')) ? '. ~/.profile;' : '' stdin, stdout, stderr = Open3.popen3("#{profile}cd \"#{dir}\";#{command}") result = "" result << stdout.readlines.join('') result << stderr.readlines.join('') result.force_encoding("binary") if result.respond_to? :force_encoding result.gsub!("\c@", '.') # Replace out characters that el4r can't handle return result else if View. and ! [:dont_leave_bar] View. end buffer ||= "*console #{dir}" if ! reuse_buffer buffer = $el.generate_new_buffer(buffer) end View.to_buffer buffer $el.erase_buffer if reuse_buffer $el.elvar.default_directory = dir if dir $el.shell $el.current_buffer # Don't prompt with "buffer has a running process" when closing $el.set_process_query_on_exit_flag $el.get_buffer_process($el.current_buffer), nil Move.bottom if command # If nil, just open console $el.insert command Console.enter unless [:no_enter] end end nil end |
.search_last_commands ⇒ Object
504 505 506 507 508 509 510 511 512 513 |
# File 'lib/xiki/console.rb', line 504 def self.search_last_commands bm = Keys.input(:timed => true, :prompt => "bookmark to show commands for (space for currently open): ") return Launcher.open("- console/tree/") if bm == " " if bm == "8" Console.log; View.to_bottom; Search.isearch nil, :reverse=>true return end Launcher.open("- console/history/$#{bm}/") end |
.ssh_line(path) ⇒ Object
404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/xiki/console.rb', line 404 def self.ssh_line path path = path.sub /^\//, '' path.sub! /\/$/, '' if path =~ /(.+):(.+)/ # If port exists (colon) "ssh -p #{$2} #{$1}" # Pull out and pass with -p else "ssh -A #{path}" end end |
.sync(command, options = {}) ⇒ Object
59 60 61 |
# File 'lib/xiki/console.rb', line 59 def self.sync command, ={} self.run command, .merge(:sync=>1) end |
.to_shell_buffer(dir = nil, options = {}) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 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 209 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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/xiki/console.rb', line 165 def self.to_shell_buffer dir=nil, ={} if dir dir = "#{dir}/" unless dir =~ /\/$/ pattern = /^\*console #{Regexp.quote(dir)}(<| |$)/ else # If already in a shell (regardless of buffer name) return true if View.mode == :shell_mode pattern = /^\*console/ end return true if View.name =~ pattern # If already there, do nothing if ! dir # Try to find visible shell buffer in same dir and with prompt View.list.each do |w| $el.set_buffer $el.window_buffer(w) next if View.mode != :shell_mode || ! Console.prompt? next if View.cursor != View.bottom View.to_window(w) return true end end if dir && dir !~ /@/ # If local dir # TODO Make sure there's no ssh or cd in history! View.list.each do |w| $el.set_buffer $el.window_buffer(w) next if View.mode != :shell_mode || ! Console.prompt? next if Tree.slashless(dir) != Tree.slashless(View.dir) next if View.cursor != View.bottom View.to_window(w) return true end # TODO: implement similar finding a dir for remote # else end # Deprecated: # Try to find visible shell buffer with matching name View.list.each do |w| next if $el.buffer_name($el.window_buffer w) !~ pattern View.to_window(w) return true end if Keys.prefix_u(:clear=>true) found = Buffers.list.find do |b| name = Buffers.name b next false unless name =~ pattern view = nil $el.with(:save_window_excursion) do View.to_buffer name next false unless Console.prompt? cd_dir = View.dir cd_dir = "#{cd_dir}/" unless cd_dir =~ /\/$/ next false unless cd_dir == dir next false if Console.commands.join("\n") =~ /^(ssh|ftp) / true end end if found View.to_upper return View.to_buffer(found) end end # Wasn't found among visible, so create new buffer return false if [:no_create] # Don't create it if option says not to if dir =~ /@/ # If there's a @, it's remote View. View.to_buffer $el.generate_new_buffer("*console #{dir}") $el.elvar.default_directory = "/tmp" $el.shell $el.current_buffer if dir =~ /(.+?)(\/.+)/ # Split off dir if there line = self.ssh_line($1) Console.enter line [:cd_and_wait] ? View.insert("cd #{$2} && ") : Console.enter("cd #{$2}") else line = self.ssh_line(dir) Console.enter line end else Console.open dir end return true end |
.tree(*args) ⇒ Object
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
# File 'lib/xiki/console.rb', line 515 def self.tree *args command = args.pop if args[-1] =~ /^\|/ console = args.any? ? args.join("/") : nil if console View.to_buffer console#.sub /\/$/, '' return end txt = "" $el.with(:save_excursion) do Buffers.list.each do |b| next if $el.buffer_file_name b name = $el.buffer_name b $el.set_buffer b next if $el.elvar.major_mode.to_s != 'shell-mode' next if name == "*ol" txt << "- #{name}/\n" self.commands.reverse.each do |h| txt << " | $ #{h}\n" end end end txt end |
.wait_until(buffer, options = {}) ⇒ Object
550 551 552 553 554 555 556 557 558 |
# File 'lib/xiki/console.rb', line 550 def self.wait_until buffer, ={} max = [:max] || 10 = [:message] || "Launching..." while View.txt(:buffer=>buffer) !~ [:contains] View.flash , :times=>1 max -= 1 break if max < 0 end end |