Class: Thyme

Inherits:
Object
  • Object
show all
Defined in:
lib/thyme.rb

Constant Summary collapse

VERSION =
'0.0.13'
CONFIG_FILE =
"#{ENV['HOME']}/.thymerc"
PID_FILE =
"#{ENV['HOME']}/.thyme-pid"
TMUX_FILE =
"#{ENV['HOME']}/.thyme-tmux"
OPTIONS =
[:interval, :timer, :timer_break, :tmux, :tmux_theme, :warning, :warning_color]

Instance Method Summary collapse

Constructor Details

#initializeThyme

Returns a new instance of Thyme.



11
12
13
14
15
16
17
18
19
20
# File 'lib/thyme.rb', line 11

def initialize
  @break = false
  @interval = 1
  @timer = 25 * 60
  @timer_break = 5 * 60
  @tmux = false
  @tmux_theme = "#[default]#[fg=%s]%s#[default]" 
  @warning = 5 * 60
  @warning_color = "red,bold"
end

Instance Method Details

#after(&block) ⇒ Object



52
53
54
# File 'lib/thyme.rb', line 52

def after(&block)
  @after = block
end

#before(&block) ⇒ Object



48
49
50
# File 'lib/thyme.rb', line 48

def before(&block)
  @before = block
end

#break!Object



30
31
32
# File 'lib/thyme.rb', line 30

def break!
  @break = true
end

#daemon?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/thyme.rb', line 39

def daemon?
  !!@daemon
end

#daemonize!Object



34
35
36
37
# File 'lib/thyme.rb', line 34

def daemonize!
  @daemon = true
  Process.daemon
end

#load_config(optparse) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/thyme.rb', line 67

def load_config(optparse)
  return if !File.exists?(CONFIG_FILE)
  app = self
  Object.class_eval do
    define_method(:set) { |opt,val| app.set(opt,val) }
    define_method(:before) { |&block| app.before(&block) }
    define_method(:after) { |&block| app.after(&block) }
    define_method(:tick) { |&block| app.tick(&block) }
    define_method(:option) { |sh,lo,desc,&b| app.option(optparse,sh,lo,desc,&b) }
  end
  load(CONFIG_FILE, true)
end

#option(optparse, short, long, desc, &block) ⇒ Object



60
61
62
63
64
65
# File 'lib/thyme.rb', line 60

def option(optparse, short, long, desc, &block)
  optparse.on("-#{short}", "--#{long}", desc) do |*args|
    self.instance_exec(*args, &block)
    exit if !@run
  end
end

#run(force = false) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/thyme.rb', line 22

def run(force=false)
  if force
    running? ? stop_timer : start_timer
  else
    @run = true
  end
end

#running?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/thyme.rb', line 80

def running?
  File.exists?(PID_FILE)
end

#set(opt, val) ⇒ Object

Raises:



43
44
45
46
# File 'lib/thyme.rb', line 43

def set(opt, val)
  raise ThymeError.new("Invalid option: #{opt}") if !OPTIONS.include?(opt.to_sym)
  self.instance_variable_set("@#{opt}", val)
end

#tick(&block) ⇒ Object



56
57
58
# File 'lib/thyme.rb', line 56

def tick(&block)
  @tick = block
end