Class: SRT::Shell
- Inherits:
-
Object
- Object
- SRT::Shell
- Defined in:
- lib/srt/shell.rb,
lib/srt/shell/version.rb
Constant Summary collapse
- SAVE_HOOK_FILE =
::File.('~/.srt_shell_hook')
- BOM_STRING =
"\xEF\xBB\xBF"- BOM_REGEX =
/#{BOM_STRING}/- USAGE_MSG =
<<USAGE Usage: #{$0} [SRT_FILENAME] Commands: EX: load 'SRT_FILENAME' EX: interval 90 EX: upshift|u 50 5000 EX: forward|f 50 5000 EX: remove 50 EX: save EX: show|s 50 EX: search TERM EX: help|h EX: exit USAGE
- VERSION =
"0.0.2"
Instance Method Summary collapse
- #eval_command(cmd) ⇒ Object
- #forward(index, time) ⇒ Object
-
#initialize(path = nil, save_hook = SAVE_HOOK_FILE) ⇒ Shell
constructor
A new instance of Shell.
- #load_path(path) ⇒ Object
- #remove(index) ⇒ Object
- #rewind(index, time) ⇒ Object
- #save(path = @path) ⇒ Object
- #scan_interval(input_time) ⇒ Object
- #search(term) ⇒ Object
- #show(index) ⇒ Object
- #show_all ⇒ Object
- #timeshift(index, timecode) ⇒ Object
Constructor Details
#initialize(path = nil, save_hook = SAVE_HOOK_FILE) ⇒ 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.exists?(save_hook) ? save_hook : nil end |
Instance Method Details
#eval_command(cmd) ⇒ Object
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 |
# File 'lib/srt/shell.rb', line 134 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
77 78 79 |
# File 'lib/srt/shell.rb', line 77 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.(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
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/srt/shell.rb', line 98 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. end |
#rewind(index, time) ⇒ Object
73 74 75 |
# File 'lib/srt/shell.rb', line 73 def rewind(index, time) timeshift(index, "-#{time}ms") end |
#save(path = @path) ⇒ Object
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/srt/shell.rb', line 123 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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/srt/shell.rb', line 81 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
109 110 111 112 113 114 115 116 117 |
# File 'lib/srt/shell.rb', line 109 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. end |
#show_all ⇒ Object
119 120 121 |
# File 'lib/srt/shell.rb', line 119 def show_all puts @file end |
#timeshift(index, timecode) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# 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 rescue IndexError => error puts error. end |