working_times 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1b873d5ea276962e2e2b73cbb9cade749b8ebd8879415b3f9ecb6859a8a95b19
4
- data.tar.gz: 26c9539dc6c44c96f235da3deca0185385227cd94fa3863fe7f92aa8639e5288
3
+ metadata.gz: e5a2fb5e4c26ca5d6f922fc3f1bd01a0557ff60331f34b5e58163de1a13a8284
4
+ data.tar.gz: 1c870e507ac414e53d4a45d873b012226986b9d39c6bd505b31dd0b8ffc9d4b6
5
5
  SHA512:
6
- metadata.gz: eb2e1255c35746e8169308746cacfc8eadd2e59fc6ef79e0b3bbd62e2f48d9623b9dbb0bcc84b07c73036cffe8fd40e4d5fa5283707e71f0cf24f1146eafdd0a
7
- data.tar.gz: c71470e2de46a3727f19e2a9d9c14385a57d32af8a1514f76acc17a341e3db886c4337b6c66467c9815b7bf7bd1d372244c74226bec215ccb6b17cfcfef9f1f3
6
+ metadata.gz: d693128ec3ef10af8c1fcf9dbb139c51d33878bfd3888aa737e3c938ed4a48287b1fe23104ac8ca0bc2b1841c6dd7a980696c5aef62f5b61b0e5829b3451cb56
7
+ data.tar.gz: 7a0296790a7170b47daacf803734942b1cc2af0b6ab8bfe3df6dc676ec34c7842818d4c5ab4c975b453c8eaa2175afd218432ce1bd270f08805007db44e1addd
data/exe/wt CHANGED
@@ -2,18 +2,4 @@
2
2
 
3
3
  require 'working_times'
4
4
 
5
- # mock config
6
- # do not remove ACTUAL configurations
7
- module WorkingTimes::Config
8
- private
9
-
10
- def data_dir
11
- File.expand_path('tmp/.wt')
12
- end
13
-
14
- def default_work
15
- 'default'
16
- end
17
- end
18
-
19
5
  WorkingTimes::CLI.start
@@ -1,11 +1,22 @@
1
1
  require 'thor'
2
+ require 'fileutils'
2
3
 
3
4
  module WorkingTimes
4
5
  class CLI < Thor
5
6
  include State
6
7
 
7
- option :work_on, aliases: ['-w'], desc: 'Specify what group of work on'
8
- desc 'start [COMMENT] <option>', 'Start working with comment.'
8
+ desc 'init WORKON [TERM] [COMPANY]', 'initialize data directory for your working'
9
+ def init(workon, term = 'default', company = '')
10
+ if Dir.exist?(workon)
11
+ puts "WORKON '#{workon}' is already created. Or name conflicted.'"
12
+ return
13
+ end
14
+
15
+ FileUtils.mkdir_p(File.join(workon, 'terms'))
16
+ initialize_wtconf(workon, term, company)
17
+ end
18
+
19
+ desc 'start [COMMENT] ', 'Start working with comment.'
9
20
  def start(comment = '')
10
21
  if working?
11
22
  puts "You are already on working at #{current_work}."
@@ -13,14 +24,13 @@ module WorkingTimes
13
24
  return
14
25
  end
15
26
 
16
- initialize_data_dir
17
- initialize_work_log(options[:work_on])
27
+ initialize_term_log
18
28
 
19
- Record.new(timestamp: DateTime.now, comment: comment, work_on: options[:work_on]).start
20
- start_work(options[:work_on])
29
+ Record.new(timestamp: DateTime.now, comment: comment).start
30
+ start_work
21
31
  end
22
32
 
23
- desc 'st [COMMENT] <option>', 'Short hand for *start*'
33
+ desc 'st [COMMENT]', 'Short hand for *start*'
24
34
  alias st start
25
35
 
26
36
  desc 'finish [COMMENT]', 'Finish working on current group.'
@@ -37,17 +47,8 @@ module WorkingTimes
37
47
  desc 'fi [COMMENT]', 'Short hand for *finish*'
38
48
  alias fi finish
39
49
 
40
- desc 'rest DURATION', 'Record resting time. e.g. \'wt rest 1h30m\''
41
- def rest(duration = nil)
42
- if duration.nil?
43
- puts <<~MSG
44
- Please specify duration of resting.
45
- e.g. wt rest 1h30m
46
- e.g. wt rest '1 hour 30 minutes'
47
- MSG
48
- return
49
- end
50
-
50
+ desc 'rest DURATION', 'Record resting time. e.g. \'wt rest 1h30m\'\'wt rest 1 hour 30 minutes\''
51
+ def rest(duration)
51
52
  unless working?
52
53
  puts 'You are not starting work. Execute "wt start" to start working.'
53
54
  return
@@ -55,5 +56,24 @@ module WorkingTimes
55
56
 
56
57
  Record.new(timestamp: DateTime.now, duration: duration).rest
57
58
  end
59
+
60
+ private
61
+
62
+ def initialize_wtconf(workon, term, company)
63
+ # on initializing, we shouldn't use path helper e.g. Config#data_dir
64
+ data_dir = File.expand_path(workon)
65
+ File.write(File.join(data_dir, 'wtconf.json'), <<~WTCONF)
66
+ {
67
+ "term": "#{term}",
68
+ "company": "#{company}"
69
+ }
70
+ WTCONF
71
+ end
72
+
73
+ def initialize_term_log
74
+ return if File.exist?(path_current_term)
75
+
76
+ File.write(path_current_term, SCHEMA.join(',') + "\n")
77
+ end
58
78
  end
59
79
  end
@@ -1,43 +1,39 @@
1
+ require 'json'
2
+
1
3
  module WorkingTimes
2
4
  module Config
3
5
  private
4
6
 
5
- # return where data directory is
6
7
  def data_dir
7
- File.expand_path(wtconf['DATADIR'])
8
+ File.expand_path('.')
9
+ end
10
+
11
+ def path_wtconf
12
+ File.join(data_dir, 'wtconf.json')
13
+ end
14
+
15
+ def term_dir
16
+ File.join(data_dir, 'terms')
17
+ end
18
+
19
+ def path_working_flag
20
+ File.join(data_dir, '.working')
21
+ end
22
+
23
+ def current_term
24
+ wtconf['term']
25
+ end
26
+
27
+ def path_current_term
28
+ File.join(term_dir, current_term)
8
29
  end
9
30
 
10
- # return default working project/task/etc...
11
- def default_work
12
- wtconf['DEFAULTWORK']
31
+ def current_company
32
+ wtconf['company']
13
33
  end
14
34
 
15
- # parse ~/.wtconf
16
35
  def wtconf
17
- conf = default_conf
18
- begin
19
- File
20
- .readlines(File.expand_path('~/.wtconf'))
21
- .map(&:chomp)
22
- .each do |row|
23
- k, v = row.split('=')
24
- conf[k] = v
25
- end
26
- rescue Errno::ENOENT
27
- puts '~/.wtconf not found, generated.'
28
- generate_wtconf
29
- end
30
- conf
31
- end
32
-
33
- # default configurations of .wtconf
34
- def default_conf
35
- { 'DATADIR' => File.expand_path('~/.wt'), 'DEFAULTWORK' => 'default' }
36
- end
37
-
38
- # generate configuration file to ~/.wtconf when does not exist
39
- def generate_wtconf
40
- File.open(File.expand_path('~/.wtconf'), 'w') { |f| f.puts(default_conf.map { |k, v| "#{k}=#{v}" }) }
36
+ JSON.parse(File.read(path_wtconf))
41
37
  end
42
38
  end
43
39
  end
@@ -1,5 +1,5 @@
1
1
  module WorkingTimes
2
- VERSION = '0.5.1'.freeze
2
+ VERSION = '0.6.0'.freeze
3
3
 
4
4
  START_MSG = [
5
5
  'Have a nice work!',
@@ -7,44 +7,43 @@ module WorkingTimes
7
7
 
8
8
  OPTIONS = { headers: true, return_headers: true, write_headers: true }.freeze
9
9
 
10
- attr_reader :timestamp, :comment, :duration, :work_on
10
+ attr_reader :timestamp, :comment, :duration
11
11
 
12
- def initialize(timestamp:, comment: nil, duration: nil, work_on: nil)
12
+ def initialize(timestamp:, comment: nil, duration: nil)
13
13
  @timestamp = timestamp
14
14
  @comment = comment
15
15
  @duration = duration
16
- @work_on = work_on.nil? ? default_work : work_on
17
16
  end
18
17
 
19
18
  def start
20
- CSV.open("#{data_dir}/#{work_on}", 'a+', OPTIONS) do |csv|
19
+ CSV.open(path_current_term, 'a+', OPTIONS) do |csv|
21
20
  csv.puts([timestamp.rfc3339, '', 0, comment])
22
21
  end
23
22
  end
24
23
 
25
24
  def finish
26
25
  updated_csv = ''
27
- CSV.filter(File.open("#{data_dir}/#{current_work}"), updated_csv, OPTIONS) do |row|
26
+ CSV.filter(File.open(path_current_term), updated_csv, OPTIONS) do |row|
28
27
  next if row.header_row?
29
28
  next unless row['finished_at'].empty?
30
29
 
31
30
  row['finished_at'] = timestamp.rfc3339
32
31
  row['comment'] = comment
33
32
  end
34
- File.write("#{data_dir}/#{current_work}", updated_csv)
33
+ File.write(path_current_term, updated_csv)
35
34
  end
36
35
 
37
36
  def rest
38
37
  parse_rest_sec
39
38
 
40
39
  updated_csv = ''
41
- CSV.filter(File.open("#{data_dir}/#{current_work}"), updated_csv, OPTIONS) do |row|
40
+ CSV.filter(File.open(path_current_term), updated_csv, OPTIONS) do |row|
42
41
  next if row.header_row?
43
42
  next unless row['finished_at'].empty?
44
43
 
45
44
  row['rest_sec'] = @rest_sec
46
45
  end
47
- File.write("#{data_dir}/#{current_work}", updated_csv)
46
+ File.write(path_current_term, updated_csv)
48
47
  end
49
48
 
50
49
  private
@@ -4,54 +4,28 @@ module WorkingTimes
4
4
 
5
5
  include Config
6
6
 
7
- # generate data directory when does not exist
8
- # it is usually called by 'start' command
9
- def initialize_data_dir
10
- return if exist_data_dir?
11
-
12
- puts 'data directory .wt not found, generated.'
13
- Dir.mkdir(data_dir)
14
- end
15
-
16
- # creates csv with header
17
- def initialize_work_log(work_on)
18
- work_on = work_on.nil? ? default_work : work_on
19
- return if File.exist?("#{data_dir}/#{work_on}")
20
-
21
- File.write("#{data_dir}/#{work_on}", SCHEMA.join(',') + "\n")
22
- end
23
-
24
- def exist_data_dir?
25
- File.exist?(data_dir)
26
- end
27
-
28
7
  def working?
29
- File.exist?("#{data_dir}/.working")
8
+ File.exist?(path_working_flag)
30
9
  end
31
10
 
32
11
  # return what kind of working on
33
12
  def current_work
34
- File.readlines("#{data_dir}/.working").last.chomp
13
+ File.readlines(path_working_flag).last.chomp
35
14
  end
36
15
 
37
- # create ~/.wt/.working include what you working on
38
- # and show 'started' message
39
- def start_work(work_on)
40
- work_on = work_on.nil? ? default_work : work_on
41
- File.open("#{data_dir}/.working", 'w+') { |f| f.puts work_on }
16
+ def start_work
17
+ File.write(path_working_flag, current_term)
42
18
  puts START_MSG.sample
43
19
  end
44
20
 
45
- # delete 'working' flag
46
- # and show 'finished' message
47
21
  def finish_work
48
22
  puts "You were working about #{worked_time}."
49
- File.delete("#{data_dir}/.working")
23
+ File.delete(path_working_flag)
50
24
  puts FINISH_MSG.sample
51
25
  end
52
26
 
53
27
  def worked_time
54
- last_record = CSV.read("#{data_dir}/#{current_work}").last
28
+ last_record = CSV.read(path_current_term).last
55
29
  started_at = Time.parse(last_record[0])
56
30
  finished_at = Time.parse(last_record[1])
57
31
  duration = (finished_at - started_at).to_i
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: working_times
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aoshi Fujioka
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-24 00:00:00.000000000 Z
11
+ date: 2020-03-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport