Class: Gigawatt::Commands::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/gigawatt/commands/status.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings, options) ⇒ Status

Returns a new instance of Status.



28
29
30
31
32
33
34
35
36
# File 'lib/gigawatt/commands/status.rb', line 28

def initialize(settings, options)
  @settings = settings
  @options = options

  @access_key = OAuth.token(@settings.access_key)
  @cache = Cache.new(settings, @access_key)

  @project = Gigawatt::ProjectFile.new.project
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/gigawatt/commands/status.rb', line 4

def options
  @options
end

Class Method Details

.run!(settings) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gigawatt/commands/status.rb', line 6

def self.run!(settings)
  options = Trollop::options do
    banner <<-EOS
88 Miles Command line application - http://88miles.net

Get status about the linked project

Usage:
  88miles status [options]

options:
    EOS

    opt :sync, "Sync the data from the server first. Uses the cache if false", :type => :flag
    opt :foreground, "Don't exit - just refresh the timer", :type => :flag
    opt :time, "Only return the time", :type => :flag
  end

  instance = self.new(settings, options)
  return instance.get_settings
end

Instance Method Details

#get_settingsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gigawatt/commands/status.rb', line 38

def get_settings
  unless @project
    say("No project found.")
    return NO_PROJECT_EXIT_CODE
  end

  if @options[:sync]
    sync = Gigawatt::Commands::Sync.new(@settings, @options)
    begin
      sync.sync
      sync.sync_current
    rescue OAuth2::Error => e
      say "Access to your 88 Miles may have been revoked. Please run <%= color('88miles setup', BOLD) %> again."
      return INVALID_OAUTH_TOKEN_EXIT_CODE
    end
  end

  if @options[:foreground]
    while(1)
      print_status
      sleep(1)
    end
    print "\n"
  else
    print_status
    print "\n"
  end

  return OK_EXIT_CODE
end


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/gigawatt/commands/status.rb', line 69

def print_status
  company = @cache.companies(true)[@project["company_uuid"]]

  grand_total = @project["grand_total"]
  grand_total += (Time.now.to_i - @project["started_at"]) if @project["running"]
  overdue = grand_total > @project["time_limit"] if @project["time_limit"]

  clock_string = to_clock_s(grand_total, true)
  clock_string = " [#{HighLine::String.new(clock_string).red}]" if overdue

  str = ""
  if @options[:time]
     str = clock_string
  else
    str += "#{company["name"]}: #{@project["name"]}"
    str += " [#{clock_string}]"
    str += " #{HighLine::String.new("Running").green}" if @project["running"]
  end

  print "\e[0K\r#{str}" if @options[:foreground]
  print str unless @options[:foreground]
end

#to_clock_s(time, show_seconds = false) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/gigawatt/commands/status.rb', line 92

def to_clock_s(time, show_seconds = false)
  hour = (time.abs / 3600).floor
  minute = (time.abs / 60 % 60).floor
  seconds = (time.abs % 60).floor if show_seconds

  return (time != 0 && (time / time.abs) == -1 ? "-" : "") + hour.to_s.rjust(2, '0') + ":" + minute.to_s.rjust(2, '0') + (show_seconds ? ":" + seconds.to_s.rjust(2, '0') : '')
end