about summary refs log tree commit homepage
path: root/bin/dtas-sourceedit
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2015-01-17 11:30:51 +0000
committerEric Wong <e@80x24.org>2015-01-19 09:56:52 +0000
commitb5bc0240f47dba01ff0a09263c210e5d391f88dc (patch)
tree8ae74163f66a575d36ba80acc615d6e37c116dc0 /bin/dtas-sourceedit
parent8f07678251b20f41063cd0a8fbf81f3b4bd39967 (diff)
downloaddtas-b5bc0240f47dba01ff0a09263c210e5d391f88dc.tar.gz
This allows changes in the source YAML file to be reflected
immediately in player after the user saves the file in their
favorite $EDITOR.  Previously, a user would need to:

	1) start dtas-sourceedit, spawning $EDITOR
	2) edit the file
	3) save changes
	4) exit $EDITOR
	5) repeat starting from 1) until happy with the results

Now, the workflow allows avoiding the context switch between their
$EDITOR and terminal to restart dtas-sourcedit:

	1) start dtas-sourceedit, spawning $EDITOR
	2) edit the file
	3) save changes
	4) repeat starting from 1) until happy with the results
	5) exit $EDITOR

In my experience, this greatly speeds up tuning of the playback
change, giving all the repeatability and flexibility of editing text
files while having the immediacy of an interactive UI.

Keep in mind this can cause problems for those with auto-save
enabled in their $EDITOR buffer at inopportune times, so a
-N/--no-watch option is added.
Diffstat (limited to 'bin/dtas-sourceedit')
-rwxr-xr-xbin/dtas-sourceedit84
1 files changed, 62 insertions, 22 deletions
diff --git a/bin/dtas-sourceedit b/bin/dtas-sourceedit
index b411b11..1980141 100755
--- a/bin/dtas-sourceedit
+++ b/bin/dtas-sourceedit
@@ -1,16 +1,32 @@
 #!/usr/bin/env ruby
-# Copyright (C) 2013-2014, Eric Wong <e@80x24.org> and all contributors
+# Copyright (C) 2013-2015, all contributors <dtas-all@nongnu.org>
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
+require 'optparse'
 require 'dtas/edit_client'
+require 'dtas/sigevent'
+require 'dtas/watchable'
 include DTAS::EditClient
 c = client_socket
 sources = c.req('source ls') || "(unknown)"
-usage = "Usage: #{DTAS_PROGNAME} [-n|--dry-run][-V|--verbose] SOURCENAME\n" \
-        "available SOURCENAME values: #{sources}"
+usage = "Usage: #{DTAS_PROGNAME} [OPTIONS] SOURCENAME\n" \
+        "available SOURCENAME values: #{sources}\n" \
 
-# use a real option parser if we have anything more complex
-dry_run = !!(ARGV.delete('-n') || ARGV.delete('--dry-run'))
-verbose = !!(ARGV.delete('-V') || ARGV.delete('--verbose'))
+dry_run = verbose = false
+watch = defined?(DTAS::Watchable)
+OptionParser.new('', 24, '  ') do |op|
+  op.banner = usage
+  watch and
+    op.on('-N', '--no-watch', 'disable inotify support') { watch = false }
+
+  op.on('-n', '--dry-run', 'only print commands, do not run them') {
+    dry_run = true
+  }
+  op.on('-V', '--verbose', 'print out commands sent to change the source') {
+    verbose = true
+  }
+  op.on('-h', '--help') { puts(op.to_s); exit }
+  op.parse!(ARGV)
+end
 
 ARGV.size <= 1 or abort usage
 name = ARGV[0] || "sox"
@@ -21,27 +37,51 @@ buf = c.req(%W(source cat #{name}))
 abort(buf) if buf =~ /\AERR/
 orig = YAML.load(buf)
 
+commit_update = lambda do |buf|
+  source = YAML.load(buf)
+  cmd = %W(source ed #{name})
+  update_cmd_env(cmd, orig, source)
+
+  # nil OK
+  %w(tryorder command).each { |field| cmd << "#{field}=#{source[field]}" }
+
+  warn(Shellwords.join(cmd)) if verbose || dry_run
+  c.req_ok(cmd) unless dry_run
+  orig = source
+end
+
 if st_in.file? || st_in.pipe?
   buf = $stdin.read
+  commit_update.call(buf)
 else
   tmp = tmpyaml
+  tmp_path = tmp.path
+  do_update = lambda { commit_update.call(File.read(tmp_path)) }
   tmp.write(buf << DTAS_DISCLAIMER)
-  cmd = "#{editor} #{tmp.path}"
-  system(cmd) or abort "#{cmd} failed: #$?"
-  buf = File.read(tmp.path)
-end
+  cmd = "#{editor} #{tmp_path}"
 
-source = YAML.load(buf)
-cmd = %W(source ed #{name})
-update_cmd_env(cmd, orig, source)
+  sev = DTAS::Sigevent.new
+  rset = [ sev ]
+  if watch
+    ino = DTAS::Watchable::InotifyReadableIter.new
+    ino.watch_file(tmp_path, do_update)
+    rset << ino
+  end
 
-# nil OK
-%w(tryorder command).each do |field|
-  cmd << "#{field}=#{source[field]}"
+  trap(:CHLD) { sev.signal }
+  pid = Process.spawn(cmd)
+  begin
+    r = IO.select(rset) or next
+    r[0].each do |io|
+      case io
+      when sev
+        _, status = Process.waitpid2(pid)
+        status.success? or abort "#{cmd} failed: #{status.inspect}"
+        do_update.call
+        exit
+      when ino
+        ino.readable_iter # calls do_update
+      end
+    end
+  end while true
 end
-
-if verbose || dry_run
-  warn Shellwords.join(cmd)
-end
-
-c.req_ok(cmd) unless dry_run