dtas.git  about / heads / tags
duct tape audio suite for *nix
blob 1ce18def5f1a041ffe1280297250c1e24bfe447d 5211 bytes (raw)
$ git show v0.16.1:bin/dtas-tl	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  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
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 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
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
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
 
#!/usr/bin/env ruby
# Copyright (C) 2013-2016 all contributors <dtas-all@nongnu.org>
# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
# frozen_string_literal: true
# encoding: binary
# WARNING: totally unstable API, use dtas-ctl for scripting (but the protocol
# itself is also unstable, but better than this one probably).
require 'dtas/unix_client'
require 'shellwords'

def get_track_ids(c)
  track_ids = c.req("tl tracks")
  # we could get more, but SEQPACKET limits size...
  track_ids = track_ids.split(' ')
  track_ids.shift
  track_ids
end

def fix_enc!(str, enc)
  str.force_encoding(enc)
  str.force_encoding(Encoding::ASCII_8BIT) unless str.valid_encoding?
end

def do_edit(c)
  require 'dtas/edit_client'
  require 'yaml'
  require 'tempfile'
  extend DTAS::EditClient
  tmp = Tempfile.new(%w(dtas-tl-edit .txt))
  tmp.binmode
  tmp_path = tmp.path
  orig = []
  orig_idx = {}
  enc = Encoding.default_external

  get_track_ids(c).each_slice(128) do |track_ids|
    res = c.req("tl get #{track_ids.join(' ')}")
    res = Shellwords.split(res.sub!(/\A\d+ /, ''))
    while line = res.shift
      line.sub!(/\A(\d+)=/n, '') or abort "unexpected line=#{line.inspect}\n"
      fix_enc!(line, enc)
      track_id = $1.to_i
      orig_idx[track_id] = orig.size
      orig << track_id
      tmp.write("#{Shellwords.escape(line)} =#{track_id}\n")
    end
  end
  tmp.flush

  ed = editor
  # jump to the line of the currently playing track if using vi or vim
  # Patches for other editors welcome: dtas-all@nongnu.org
  if ed =~ /vim?\z/
    cur = YAML.load(c.req('current'))
    if tl = cur['tracklist']
      if pos = tl['pos']
        ed += " +#{pos + 1}"
      end
    end
  end
  # Run the editor and let the user edit!
  system("#{ed} #{Shellwords.escape tmp_path}") or return

  edit = []
  edit_idx = {}
  add = []
  # editor may rename/link a new file into place
  File.open(tmp_path) do |fp|
    fp.binmode
    while line = fp.gets
      line.chomp!
      if line.sub!(/ =(\d+)\z/n, '') # existing tracks
        track_id = $1.to_i
        if edit_idx[track_id] # somebody copy+pasted an existing line
          add << [ line, edit.last ]
        else # moved line
          edit_idx[track_id] = edit.size
          edit << track_id
        end
      else # entirely new line
        add << [ line, edit.last ]
      end
    end
  end
  edit.each_with_index do |track_id, i|
    oi = orig_idx[track_id] or warn("unknown track_id=#{track_id}") or next
    next if oi == i # no change, yay!
    prev_track_id = orig[i] or warn("unknown index at #{i}") or next
    c.req("tl swap #{track_id} #{prev_track_id}")
    orig_idx[track_id] = i
    orig_idx[prev_track_id] = oi
    orig[i] = track_id
    orig[oi] = prev_track_id
  end
  orig.each do |track_id|
    edit_idx[track_id] or c.req("tl remove #{track_id}")
  end

  prev_added_id = last_after_id = nil
  non_existent = []
  add.each do |path, after_id|
    orig = path
    path = Shellwords.split(path)[0]
    path = File.expand_path(path)
    unless File.exist?(path)
      path = orig.dup
      fix_enc!(path, enc)
      path = Shellwords.split(path)[0]
      path = File.expand_path(path)
    end

    if File.exist?(path)
      cmd = %W(tl add #{path})
      id = after_id == last_after_id ? prev_added_id : after_id
      cmd << id.to_s if id
      prev_added_id = c.req(cmd).to_i
      last_after_id = after_id
    else
      non_existent << orig
    end
  end
  if non_existent[0]
    $stderr.puts "Failed to add #{non_existent.size} paths"
    non_existent.each { |path| $stderr.puts path }
  end
ensure
  tmp.close! if tmp
end

def add_after(c, argv, last_id)
  argv.each do |path|
    path = File.expand_path(path)
    req = %W(tl add #{path})
    req << last_id.to_s if last_id
    res = c.req(req)
    print "#{path} #{res}\n"
    last_id = res if res =~ /\A\d+\z/
  end
end

c = DTAS::UNIXClient.new
case cmd = ARGV[0]
when "cat"
  enc = Encoding.default_external
  get_track_ids(c).each_slice(128) do |track_ids|
    res = c.req("tl get #{track_ids.join(' ')}")
    res = Shellwords.split(res.sub!(/\A\d+ /, ''))
    while line = res.shift
      fix_enc!(line, enc)
      print "#{line}\n"
    end
  end
when 'aac' # add-after-current
  ARGV.shift
  rv = c.req(%w(tl current-id))
  last_id = rv =~ %r{\A\d+\z} ? rv.to_i : nil
  add_after(c, ARGV, last_id)
when "addhead"
  ARGV.shift
  ARGV.reverse_each do |path|
    path = File.expand_path(path)
    res = c.req(%W(tl add #{path}))
    print "#{path} #{res}\n"
  end
when "addtail"
  ARGV.shift
  track_ids = get_track_ids(c)
  last_id = track_ids.pop
  add_after(c, ARGV, last_id)
when "reto"
  fixed = ARGV.delete("-F")
  ignorecase = ARGV.delete("-i")
  re = ARGV[1]
  time = ARGV[2]
  re = Regexp.quote(re) if fixed
  re = ignorecase ? %r{#{re}}i : %r{#{re}}
  get_track_ids(c).each do |track_id|
    res = c.req("tl get #{track_id}")
    res.sub!(/\A1 \d+=/, '')
    if re =~ res
      req = %W(tl goto #{track_id})
      req << time if time
      res = c.req(req)
      puts res
      exit(res == "OK")
    end
  end
  warn "#{re.inspect} not found"
  exit 1
when 'edit' then do_edit(c)
else
  # act like dtas-ctl for now...
  puts c.req([ "tl", *ARGV ])
end

git clone git://80x24.org/dtas.git
git clone https://80x24.org/dtas.git