Skip to content

Commit

Permalink
allows comments in project files starting with #
Browse files Browse the repository at this point in the history
  • Loading branch information
pstaender committed Sep 9, 2023
1 parent 26c5275 commit a530658
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
20 changes: 13 additions & 7 deletions lib/punchcard.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true

# (c) 2017-2019 by Philipp Staender
# (c) 2017-2023 by Philipp Staender

require 'date'
require 'time'
Expand All @@ -13,7 +13,7 @@ class PunchCard
HOURLY_RATE_PATTERN = /^\s*(\d+)([^\d]+)*\s*/i.freeze
TIME_POINT_PATTERN = /^((\d+|.+?\s[\+\-]\d{4}?\s*)(\-)*(\d+|\s.+\d?)*)$/.freeze
META_KEY_PATTERN = /^([a-zA-Z0-9]+)\:\s*(.*)$/.freeze
VERSION = '1.3.1'
VERSION = '1.3.2'

attr_accessor :title

Expand Down Expand Up @@ -108,7 +108,7 @@ def csv(start_at: nil, end_at: nil)
find_or_make_file
durations = []
last_activity = nil
project_data.map do |line|
project_data_without_comments.map do |line|
points = line_to_time_points(line)
next unless points

Expand Down Expand Up @@ -179,7 +179,7 @@ def set(key, value)

def total(start_at: nil, end_at: nil)
total = 0
project_data.map do |line|
project_data_without_comments.map do |line|
points = line_to_time_points(line)
next unless points

Expand Down Expand Up @@ -297,7 +297,7 @@ def string_to_timestamp(str)
end

def last_entry
project_data.last
project_data_without_comments.last
end

def timestamp
Expand All @@ -309,12 +309,14 @@ def timestamp_to_time(timestamp)
end

def read_project_data
title = nil
title = File.basename(project_file)
timestamps = []
i = 0
File.open(project_file, 'r').each_line do |line|
line.strip!
if i.zero?
next if line.start_with?('#')

if i.zero? && !line.match(TIME_POINT_PATTERN)
title = line
elsif line.match(META_KEY_PATTERN)
set line.match(META_KEY_PATTERN)[1], line.match(META_KEY_PATTERN)[2]
Expand All @@ -332,6 +334,10 @@ def project_data
File.open(project_file).each_line.map(&:strip)
end

def project_data_without_comments
project_data.filter { |l| !l.start_with?('#')}
end

def write_string_to_project_file!(string)
File.open(project_file, 'w') { |f| f.write(string) }
end
Expand Down
14 changes: 7 additions & 7 deletions spec/punchcard_spec.rb
Expand Up @@ -84,18 +84,18 @@ def two_seconds_tracking
expect(my_project_file.lines[-2]).to match(/^\d+/)
end

it 'should convert names to underscore with special characters' do
PunchCard.new 'Playing Motörhead'
expect(my_project_file('playing_mot_rhead').strip).to eq('Playing Motörhead')
project = PunchCard.new 'Playing*'
expect(project.project).to eq('playing_mot_rhead')
it 'ignores lines starting with #' do
start_and_stop
File.write("#{example_settings_dir}/my_project", my_project_file + "\n# A comment line")
start_and_stop
expect(my_project_file).to match("\n# A comment line")
end

xit 'should read and write utf8 names' do
it 'should convert names to underscore with special characters' do
PunchCard.new 'Playing Motörhead'
expect(my_project_file('playing_mot_rhead').strip).to eq('Playing Motörhead')
project = PunchCard.new 'Playing*'
expect(project.project).to eq('Playing Motörhead')
expect(project.project).to eq('playing_mot_rhead')
end

it 'should set hourlyRate' do
Expand Down

0 comments on commit a530658

Please sign in to comment.