about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2013-08-26 05:59:07 +0000
committerEric Wong <normalperson@yhbt.net>2013-08-26 05:59:07 +0000
commit57bd6f96e007097219cdd31b034f8ca386927ed6 (patch)
treeef7bf91aeecae88f5bd3a2514b8c3e755b10f812 /lib
parent2907cc6dd0b0d2d80d44ae292a157651d6e05ee7 (diff)
downloaddtas-57bd6f96e007097219cdd31b034f8ca386927ed6.tar.gz
These two are similar enough that it's possible to share
some code between them and also increase user-friendliness
at the same time.
Diffstat (limited to 'lib')
-rw-r--r--lib/dtas/disclaimer.rb7
-rw-r--r--lib/dtas/edit_client.rb48
2 files changed, 52 insertions, 3 deletions
diff --git a/lib/dtas/disclaimer.rb b/lib/dtas/disclaimer.rb
index 608956d..5747971 100644
--- a/lib/dtas/disclaimer.rb
+++ b/lib/dtas/disclaimer.rb
@@ -2,17 +2,18 @@
 # :enddoc:
 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net>
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
+DTAS_PROGNAME = File.basename($0)
 DTAS_DISCLAIMER = <<EOF
 # WARNING!
 #
-# Ignorant or improper use of #$0 may lead to
+# Ignorant or improper use of #{DTAS_PROGNAME} may lead to
 # data loss, hearing loss, and damage to audio equipment.
 #
 # Please read and understand the documentation of all commands you
 # attempt to configure.
 #
-# #$0 will never prevent you from doing stupid things.
+# #{DTAS_PROGNAME} will never prevent you from doing stupid things.
 #
-# There is no warranty, the developers of #$0
+# There is no warranty, the developers of #{DTAS_PROGNAME}
 # are not responsible for your actions.
 EOF
diff --git a/lib/dtas/edit_client.rb b/lib/dtas/edit_client.rb
new file mode 100644
index 0000000..3a6a9e9
--- /dev/null
+++ b/lib/dtas/edit_client.rb
@@ -0,0 +1,48 @@
+# -*- encoding: binary -*-
+# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net>
+# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
+require 'tempfile'
+require 'yaml'
+require_relative 'unix_client'
+require_relative 'disclaimer'
+
+# common code between dtas-sourceedit and dtas-sinkedit
+module DTAS::EditClient # :nodoc:
+  def editor
+    %w(VISUAL EDITOR).each do |key|
+      v = ENV[key] or next
+      v.empty? and next
+      return v
+    end
+    "vi"
+  end
+
+  def client_socket
+    DTAS::UNIXClient.new
+  rescue
+    e = "DTAS_PLAYER_SOCK=#{DTAS::UNIXClient.default_path}"
+    abort "dtas-player not running on #{e}"
+  end
+
+  def tmpyaml
+    tmp = Tempfile.new(%W(#{File.basename($0)} .yml))
+    tmp.sync = true
+    tmp.binmode
+    tmp
+  end
+
+  def update_cmd_env(cmd, orig, updated)
+    if env = updated["env"]
+      env.each do |k,v|
+        cmd << (v.nil? ? "env##{k}" : "env.#{k}=#{v}")
+      end
+    end
+
+    # remove deleted env
+    if orig_env = orig["env"]
+      env ||= {}
+      deleted_keys = orig_env.keys - env.keys
+      deleted_keys.each { |k| cmd << "env##{k}" }
+    end
+  end
+end