Class: Episode

Inherits:
Object
  • Object
show all
Defined in:
lib/episode.rb,
lib/episode/config.rb

Defined Under Namespace

Classes: CommandError, Config, NoCommandError, NotLocal

Constant Summary collapse

VERSION =
1
DEFAULT_CFG =
{
  viewer: 'mpv',
  index_from_zero: false,
  pointer: '*',
  formats: %w[mkv mp4 avi]
}

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Episode

Returns a new instance of Episode.



12
13
14
15
16
17
# File 'lib/episode.rb', line 12

def initialize(opts) 
  @is_name = opts[:name] || false
  @is_update = opts[:update] != false
  @is_global = opts[:global] || false
  @viewer = opts[:viewer]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object (private)

Raises:



140
141
142
# File 'lib/episode.rb', line 140

def method_missing(*args)
  raise NoCommandError, args.first
end

Instance Method Details

#cfgObject Also known as: c



97
98
99
100
# File 'lib/episode.rb', line 97

def cfg
  cfg_h = global? ? config.global.to_h : config.to_h
  cfg_h.each { |param, val| puts "#{param}: #{val}" }
end

#lastObject Also known as: l



63
64
65
# File 'lib/episode.rb', line 63

def last
  play 
end

#lsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/episode.rb', line 19

def ls
  if episodes.empty?
    raise CommandError, 'No episodes found in the directory'
  end

  total = episodes.size
  padding = Math.log10(config.index_from_zero ? total - 1 : total).floor + 1

  episodes.each_with_index do |filename, id|
    id_fixed = config.index_from_zero ? id : id + 1
    id_formatted = id_fixed.to_s.rjust(padding, '0')
    separator = (config.last && id == last_id) ? config.pointer : '|'
    puts "#{id_formatted} #{separator} #{filename}"
  end
end

#nextObject Also known as: n



69
70
71
72
# File 'lib/episode.rb', line 69

def next
  config.last = config.last ? episode_by_id(last_id + 1) : episodes.first
  play 
end

#no(n_str) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/episode.rb', line 83

def no(n_str)
  n =
    begin
      Integer(n_str.gsub /^0*(\d)/, '\\1') 
    rescue ArgumentError
      raise CommandError, <<~EOS
        Invalid value '#{n_str}'.
        `no` command expects natural number.
      EOS
    end
  config.last = episode_by_id(n - 1)
  play
end

#prevObject Also known as: p



76
77
78
79
# File 'lib/episode.rb', line 76

def prev
  config.last = episode_by_id(last_id - 1)
  play 
end

#reset(param = nil) ⇒ Object Also known as: r



125
126
127
128
129
130
131
132
133
134
# File 'lib/episode.rb', line 125

def reset(param = nil)
  cfg_path = global? ? CFG_GLOBAL_PATH : CFG_FILENAME  

  if param.nil?
    puts "Reset all config parameters (delete #{cfg_path})? (y|N)"
    FileUtils.rm_f(cfg_path) if 'y' == $stdin.getch
  else
    set(param, nil)
  end
end

#set(param, value) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/episode.rb', line 104

def set(param, value)
  unless config.respond_to? param
    raise CommandError, <<~EOS
      Unknown config parameter '#{param}'.
      Run `#{PROGRAM_NAME} cfg` to see the list of available parameters.
    EOS
  end

  cfg, cfg_path =
    if global? 
      [config.global, CFG_GLOBAL_PATH]
    else
      [config, CFG_FILENAME]
    end

  cfg.send "#{param}=", parse_config_value(param, value)
  config_save_safe(cfg_path, cfg)
rescue NotLocal
  raise CommandError, "Parameter '#{param}' is not global"
end

#statusObject Also known as: s



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
# File 'lib/episode.rb', line 35

def status
  puts "#{config.index_from_zero ? last_id : last_id + 1} #{config.pointer} #{config.last}"

  unless config.last_played_at
    puts 'Time unknown'
    return
  end

  seconds_ago = (Time.now - config.last_played_at).floor
  days_ago = seconds_ago / (3600 * 24)
  hours_ago = (seconds_ago % (3600 * 24)) / 3600
  minutes_ago = (seconds_ago % 3600) / 60
  time_passed = 
    if seconds_ago < 60
      "#{seconds_ago} second#{seconds_ago == 1 ? '' : 's'}" 
    else
      [
        days_ago > 0 ? "#{days_ago} day#{days_ago == 1 ? '' : 's'}" : nil,
        hours_ago > 0 ? "#{hours_ago} hour#{hours_ago == 1 ? '' : 's'}" : nil,
        minutes_ago > 0 ? "#{minutes_ago} minute#{minutes_ago == 1 ? '' : 's'}" : nil
      ].compact.join(" ")
    end
    
  puts "#{config.last_played_at} (#{time_passed} ago)" 
end