Class: SRT::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/srt/shell.rb,
lib/srt/shell/version.rb

Constant Summary collapse

SAVE_HOOK_FILE =
::File.expand_path('~/.srt_shell_hook')
BOM_STRING =
"\xEF\xBB\xBF"
BOM_REGEX =
/#{BOM_STRING}/
USAGE_MSG =
"Usage: \#{$0} [SRT_FILENAME]\n    Commands:\n        EX: load 'SRT_FILENAME'\n        EX: interval 90\n        EX: upshift|u 50 5000\n        EX: forward|f 50 5000\n        EX: remove 50\n        EX: save\n        EX: show|s 50\n        EX: search TERM\n        EX: help|h\n        EX: exit\n"
VERSION =
"0.0.5"

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, save_hook = SAVE_HOOK_FILE) ⇒ Shell

Returns a new instance of Shell.



25
26
27
28
29
30
# File 'lib/srt/shell.rb', line 25

def initialize(path = nil, save_hook=SAVE_HOOK_FILE)
  @file, @path = nil, nil
  @bom = false
  load_path(path) if path
  @save_hook = ::File.exist?(save_hook) ? save_hook : nil
end

Instance Method Details

#eval_command(cmd) ⇒ 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
165
166
167
168
169
170
171
# File 'lib/srt/shell.rb', line 135

def eval_command(cmd)
  case cmd
  when /^\s*(?:help|h)\s*$/
    puts USAGE_MSG
    return
  when /^\s*exit\s*$/
    exit 0
  when /^\s*load\s+\'?([^']+)\'?\s*$/
    load_path($1)
    return
  end

  if @file
    case cmd
    when /^\s*(?:show|s)\s+(\d+)\s*$/
      show($1.to_i)
    when /^\s*(?:showall)\s*$/
      show_all
    when /^\s*interval\s+(\d+)\s*$/
      scan_interval($1.to_i)
    when /^\s*(?:u|rewind)\s+(\d+)\s+(\d+)\s*$/
      rewind($1.to_i, $2.to_i)
    when /^\s*(?:f|forward)\s+(\d+)\s+(\d+)\s*$/
      forward($1.to_i, $2.to_i)
    when /^\s*(?:remove)\s+(\d+)\s*$/
      remove($1.to_i)
    when /^\s*save\s*$/
      save
    when /^\s*search\s*(.*)$/
      search($1)
    else
      puts "Invalid command"
    end
  else
    puts "File is not loaded. Load a file using the 'load' command"
  end
end

#forward(index, time) ⇒ Object



78
79
80
# File 'lib/srt/shell.rb', line 78

def forward(index, time)
  timeshift(index, "+#{time}ms")
end

#load_path(path) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/srt/shell.rb', line 32

def load_path(path)
  path = ::File.expand_path(path)
  @path = path

  # Test if file contains BOM
  ::File.open(path) do |file|
    lines = file.read.split("\n")
    unless lines.empty?
      if lines[0].match(BOM_REGEX)
        lines[0].sub!(BOM_REGEX, '')
        @bom = true
      end
      @file = SRT::File.parse(lines.join("\n"))
    else
      raise ArgumentError, 'Invalid SRT file'
    end
  end
  self
end

#remove(index) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/srt/shell.rb', line 99

def remove(index)
  check_index(index)
  index -= 1
  @file.lines.delete_at(index)
  @file.lines[index..-1].each do |line|
    line.sequence -= 1
  end
rescue IndexError => error
  puts error.message
end

#rewind(index, time) ⇒ Object



74
75
76
# File 'lib/srt/shell.rb', line 74

def rewind(index, time)
  timeshift(index, "-#{time}ms")
end

#save(path = @path) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/srt/shell.rb', line 124

def save(path=@path)
  ::File.open(path, 'w') do |file|
    file.print BOM_STRING if @bom
    file.print @file.to_s.split("\n").join("\r\n"), "\r\n\r\n"
  end
  if @save_hook
    output = `sh #{@save_hook}`
    puts output unless output.empty?
  end
end

#scan_interval(input_time) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/srt/shell.rb', line 82

def scan_interval(input_time)
  unless time = Parser.timespan("#{input_time}ms")
    puts "Invalid time used #{input_time}"
    return
  end
  end_time = 0
  result = []
  @file.lines.each do |line|
    interval = line.start_time - end_time
    if interval >= time
      result << "index: #{line.sequence} time: #{line.time_str} gap: #{interval}"
    end
    end_time = line.end_time
  end
  puts result.join("\n")
end

#search(term) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/srt/shell.rb', line 110

def search(term)
  result = []
  @file.lines.each do |line|
    if line.text.find { |text| text[term] }
      result << line.to_s
    end
  end
  puts result.join("\n") + "\n"
end

#show(index) ⇒ Object



52
53
54
55
56
57
# File 'lib/srt/shell.rb', line 52

def show(index)
  check_index(index)
  puts @file.lines[index - 1].to_s + "\n"
rescue IndexError => error
  puts error.message
end

#show_allObject



120
121
122
# File 'lib/srt/shell.rb', line 120

def show_all
  puts @file
end

#timeshift(index, timecode) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/srt/shell.rb', line 59

def timeshift(index, timecode)
  check_index(index)
  if time = Parser.timespan(timecode)
    @file.lines[index-1..-1].each do |line|
      line.start_time += time
      line.end_time += time
    end
  else
    puts "Invalid timeshift input (#{index}, #{timecode})"
  end
  show(index)
rescue IndexError => error
  puts error.message
end