Class: CTodo::Prog

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

Constant Summary collapse

TPAD_FAC =
7
MIN_TPAD =
TPAD_FAC*3
MAX_TPAD =
TPAD_FAC*8

Instance Method Summary collapse

Instance Method Details

#find_git_repo(path) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/ctodo.rb', line 158

def find_git_repo(path)
	git_dir_name = Pathname.new(!ENV['GIT_DIR'].nil? ? ENV['GIT_DIR'] : '.git')
	p = Pathname.new(path)
	until p.root?
		git_dir = p + git_dir_name
		return p.to_s if git_dir.exist? and git_dir.directory?
		p = p.parent
	end
	nil
end

#find_hg_repo(path) ⇒ Object



169
170
171
172
173
174
175
176
177
178
# File 'lib/ctodo.rb', line 169

def find_hg_repo(path)
	hg_dir_name = '.hg'
	p = Pathname.new(path)
	until p.root?
		hg_dir = p + hg_dir_name
		return p.to_s if hg_dir.exist? and hg_dir.directory?
		p = p.parent
	end
	nil
end

#load_config(config_file) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ctodo.rb', line 146

def load_config(config_file)
	conf = {}
	if File.exists?(config_file)
		conf.merge!(YAML.load_file(config_file))
	end
	if @options.key?(:gh_creds)
		u,p = @options[:gh_creds].split(':')
		conf.merge!({:gh_user => u, :gh_pass => p})
	end
	conf
end

#run!Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ctodo.rb', line 66

def run!
	@options = {
		:cs => COLOR_SETS[0],
		:limit_text => false,
		:show_source => false,
		:show_assignee => false
	}
	
	@opts = OptionParser.new do |opts|
		opts.banner =  "Usage: todo.rb [options]"
		opts.banner += "\nOptions:\n"
		
		opts.on('--all', 'Show all todos (some providers limit to a reasonable number)') do @options[:all] = true end
		opts.on('-l', '--limit-text', 'Limit todo text to fixed width') do @options[:limit_text] = true end
		opts.on('-s', '--source', 'Show source of todos') do @options[:show_source] = true end
		opts.on('-a', '--assignee', 'Show assignee of todos (when present)') do @options[:show_assignee] = true end
		opts.on('-u CREDS', '--gh-user CREDS', 'Specify github credentials as USER:PASS') do |u| @options[:gh_creds] = u end
		opts.on('-c CS', '--color-set CS', COLOR_SETS, "There are: #{COLOR_SETS.join(', ')}") do |cs|
			@options[:cs] = cs if ['Fg', 'Bg', 'None'].include?(cs)
		end
		opts.on('-h', '-?', '--help', 'Display this screen') do
			puts opts
			exit
		end
	end
	@opts.parse!
	
	conf = load_config(File.join(ENV['HOME'], '.todo'))
	
	conf[:cur_dir] = Dir.getwd
	conf[:git_repo_dir] = find_git_repo(Dir.getwd)
	conf[:hg_repo_dir] = find_hg_repo(Dir.getwd)
	conf[:all] = @options[:all]
	conf[:cs] = CTodo.const_get("#{@options[:cs]}ColorSet").new
	
	ip = [
		LocalFS.new(conf),
		Github.new(conf),
		Redmine.new(conf), Redmine.new(conf, 'red2'),
		GoogleCodeProjectHosting.new(conf)
	]
	
	issues = []
	ip.each do |ip| ip.get_issues(issues) end
	
	# calculate text spacing
	if @options[:limit_text]
		title_padding = MAX_TPAD
	elsif issues.empty?
		title_padding = MIN_TPAD
	else
		title_lens = issues.map { |i| i.title.length }
		max_title_len = [title_lens.avg, title_lens.median].max
		title_padding = (((max_title_len/TPAD_FAC.to_f).ceil)*TPAD_FAC).to_i.clamp(MIN_TPAD,MAX_TPAD)
	end
	
	issues.each do |i|
		if @options[:limit_text] and i.title.length > title_padding
			title = i.title[0,title_padding-3] + '...'
		else
			title = i.title
		end
		
		tag_list = i.tags.map { |t|
			[ColorUtils.func4rgb(t.color, conf[:cs]).call, t.title, conf[:cs].rst].join
		}
		tag_list.push "@#{i.assignee}" if @options[:show_assignee] and not i.assignee.nil?
		tag_list = (tag_list.empty? ? '' : format('(%s) ', tag_list.join(', ')))
		
		# source may be nil
		if @options[:show_source] and !i.source.nil?
			puts format("- %-#{title_padding}s %sin %s",
				title, tag_list, i.source)
		else
			puts format("- %-#{title_padding}s %s",
				title, tag_list)
		end
	end
end