Class: Cline::Command

Inherits:
Thor
  • Object
show all
Defined in:
lib/cline/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(args = ARGV) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cline/command.rb', line 8

def start(args = ARGV, *)
  return super unless server_available?(args)

  Cline::Client.start args
rescue => e
  Cline.logger.fatal :cline do
    %(#{e.class} #{e.message}\n#{e.backtrace.join($/)})
  end

  raise
end

Instance Method Details

#collectObject



91
92
93
94
95
96
97
98
99
# File 'lib/cline/command.rb', line 91

def collect
  pid = Process.fork {
    Cline.collectors.each &:collect

    Notification.clean(Cline.notifications_limit) if Cline.notifications_limit
  }

  Process.waitpid pid
end

#initObject



102
103
104
105
106
107
108
109
110
# File 'lib/cline/command.rb', line 102

def init
  Cline.establish_database_connection

  ActiveRecord::Base.connection.create_table(:notifications) do |t|
    t.text     :message, null: false, default: ''
    t.integer  :display_count, null: false, default: 0
    t.datetime :notified_at, null: false
  end
end

#open(alias_string = options[:hint]) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cline/command.rb', line 70

def open(alias_string = options[:hint])
  require 'launchy'

  notification = Notification.by_alias_string(alias_string).last

  if notification && url = notification.detect_url
    Launchy.open url
  else
    say 'No URL found', :red
  end
end

#recent(limit = options[:limit] || 10) ⇒ Object



114
115
116
117
118
# File 'lib/cline/command.rb', line 114

def recent(limit = options[:limit] || 10)
  Notification.recent_notified.limit(limit).each do |notification|
    puts notification.display_message
  end
end

#search(keyword = optoins[:query]) ⇒ Object



62
63
64
65
66
# File 'lib/cline/command.rb', line 62

def search(keyword = optoins[:query])
  Notification.by_keyword(keyword).each do |notification|
    puts notification.display_message
  end
end

#server(command = :start) ⇒ Object



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
# File 'lib/cline/command.rb', line 126

def server(command = :start)
  case command.intern
  when :start
    puts 'starting cline server'

    Server.start
  when :stop
    puts 'stopping cline server'

    Server.stop
  when :reload
    puts 'reloading configuration'

    Cline.tap do |c|
      c.load_config_if_exists
      c.load_default_config
    end
  when :status
    if Server.running?
      puts "Socket file exists"
      puts "But server isn't responding" if Server.client_process?
    else
      puts "Server isn't running"
    end
  else
    puts 'Usage: cline server (start|stop)'
  end
end

#show(offset = options[:offset] || 0) ⇒ Object



46
47
48
# File 'lib/cline/command.rb', line 46

def show(offset = options[:offset] || 0)
  notify Notification.display!(offset)
end

#statusObject



83
84
85
86
87
88
# File 'lib/cline/command.rb', line 83

def status
  puts "displayed : #{Notification.displayed.count}"
  puts "total     : #{Notification.count}"

  server :status
end

#tick(interval = options[:interval] || 5, offset = options[:offset] || 0) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/cline/command.rb', line 52

def tick(interval = options[:interval] || 5, offset = options[:offset] || 0)
  loop do
    show offset

    sleep Integer(interval)
  end
end

#versionObject



121
122
123
# File 'lib/cline/command.rb', line 121

def version
  puts "cline version #{Cline::VERSION}"
end