Class: Hiiro::TodoManager
- Inherits:
-
Object
- Object
- Hiiro::TodoManager
- Defined in:
- lib/hiiro/todo.rb
Constant Summary collapse
- TODO_FILE =
File.join(Dir.home, '.config', 'hiiro', 'todo.yml')
- ITEM_TEMPLATE =
{ 'text' => '', 'status' => 'not_started', 'tags' => nil }.freeze
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#todo_file ⇒ Object
readonly
Returns the value of attribute todo_file.
Instance Method Summary collapse
- #active ⇒ Object
- #add(text, tags: nil, task_info: nil) ⇒ Object
- #add_items(new_items) ⇒ Object
-
#all ⇒ Object
— High-level API —.
- #change(id_or_index, text: nil, tags: nil, status: nil) ⇒ Object
- #completed ⇒ Object
- #done(id_or_index) ⇒ Object
- #edit_items(items_to_edit = nil, task_info: nil) ⇒ Object
- #filter_by_status(*statuses) ⇒ Object
- #filter_by_tag(tag) ⇒ Object
- #filter_by_task(task_name) ⇒ Object
- #find(id) ⇒ Object
- #find_by_index(index) ⇒ Object
- #format_item(item) ⇒ Object
-
#initialize(file_path: nil, fs: Hiiro::Effects::Filesystem.new) ⇒ TodoManager
constructor
A new instance of TodoManager.
-
#list(items_to_show = nil, show_all: false) ⇒ Object
— List display —.
- #next_id! ⇒ Object
- #remove(id_or_index) ⇒ Object
- #reset(id_or_index) ⇒ Object
- #search(query) ⇒ Object
- #skip(id_or_index) ⇒ Object
- #start(id_or_index) ⇒ Object
Constructor Details
#initialize(file_path: nil, fs: Hiiro::Effects::Filesystem.new) ⇒ TodoManager
Returns a new instance of TodoManager.
132 133 134 135 136 137 138 |
# File 'lib/hiiro/todo.rb', line 132 def initialize(file_path: nil, fs: Hiiro::Effects::Filesystem.new) @fs = fs @todo_file = file_path || TODO_FILE @file_path = @todo_file @items = load_items @next_id = (TodoItem.max(:id) || 0) + 1 end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
130 131 132 |
# File 'lib/hiiro/todo.rb', line 130 def items @items end |
#todo_file ⇒ Object (readonly)
Returns the value of attribute todo_file.
130 131 132 |
# File 'lib/hiiro/todo.rb', line 130 def todo_file @todo_file end |
Instance Method Details
#active ⇒ Object
276 277 278 |
# File 'lib/hiiro/todo.rb', line 276 def active filter_by_status('not_started', 'started') end |
#add(text, tags: nil, task_info: nil) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/hiiro/todo.rb', line 164 def add(text, tags: nil, task_info: nil) item = TodoItem.new( id: next_id!, text: text, tags: , task_name: task_info&.dig(:task_name), subtask_name: task_info&.dig(:subtask_name), tree: task_info&.dig(:tree), branch: task_info&.dig(:branch), session: task_info&.dig(:session) ) @items << item save item end |
#add_items(new_items) ⇒ Object
218 219 220 221 222 |
# File 'lib/hiiro/todo.rb', line 218 def add_items(new_items) @items.concat(new_items) save new_items end |
#all ⇒ Object
— High-level API —
148 149 150 |
# File 'lib/hiiro/todo.rb', line 148 def all items end |
#change(id_or_index, text: nil, tags: nil, status: nil) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/hiiro/todo.rb', line 232 def change(id_or_index, text: nil, tags: nil, status: nil) item = resolve_item(id_or_index) return nil unless item item.text = text if text item. = if item.update_status(status) if status item.updated_at = Time.now.to_s save item end |
#completed ⇒ Object
280 281 282 |
# File 'lib/hiiro/todo.rb', line 280 def completed filter_by_status('done', 'skip') end |
#done(id_or_index) ⇒ Object
248 249 250 |
# File 'lib/hiiro/todo.rb', line 248 def done(id_or_index) change_status(id_or_index, 'done') end |
#edit_items(items_to_edit = nil, task_info: nil) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/hiiro/todo.rb', line 180 def edit_items(items_to_edit = nil, task_info: nil) items_array = if items_to_edit.nil? [ITEM_TEMPLATE.dup] elsif items_to_edit.is_a?(Array) items_to_edit.map { |item| item.is_a?(TodoItem) ? editable_hash(item) : item } else [items_to_edit.is_a?(TodoItem) ? editable_hash(items_to_edit) : items_to_edit] end tmpfile = Tempfile.new(['todo-edit-', '.yml']) tmpfile.write(items_array.to_yaml) tmpfile.close editor = ENV['EDITOR'] || 'safe_nvim' || 'nvim' system(editor, tmpfile.path) updated_data = YAML.safe_load_file(tmpfile.path) tmpfile.unlink return [] if updated_data.nil? updated_array = updated_data.is_a?(Array) ? updated_data : [updated_data] updated_array.filter_map do |h| next if h['text'].nil? || h['text'].to_s.strip.empty? TodoItem.new( id: next_id!, text: h['text'], status: h['status'] || 'not_started', tags: h['tags'], task_name: task_info&.dig(:task_name), subtask_name: task_info&.dig(:subtask_name), tree: task_info&.dig(:tree), branch: task_info&.dig(:branch), session: task_info&.dig(:session) ) end end |
#filter_by_status(*statuses) ⇒ Object
264 265 266 |
# File 'lib/hiiro/todo.rb', line 264 def filter_by_status(*statuses) items.select { |item| statuses.include?(item.status) } end |
#filter_by_tag(tag) ⇒ Object
268 269 270 |
# File 'lib/hiiro/todo.rb', line 268 def filter_by_tag(tag) items.select { |item| item.has_tag?(tag) } end |
#filter_by_task(task_name) ⇒ Object
272 273 274 |
# File 'lib/hiiro/todo.rb', line 272 def filter_by_task(task_name) items.select { |item| item.task_name == task_name || item.full_task_name == task_name } end |
#find(id) ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/hiiro/todo.rb', line 152 def find(id) id_int = id.to_i items.find { |item| item.id == id_int || item.id.to_s.end_with?(id.to_s) } end |
#find_by_index(index) ⇒ Object
160 161 162 |
# File 'lib/hiiro/todo.rb', line 160 def find_by_index(index) items[index.to_i] end |
#format_item(item) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/hiiro/todo.rb', line 294 def format_item(item) status_icon = case item.status when 'not_started' then '[ ]' when 'started' then '[>]' when 'done' then '[x]' when 'skip' then '[-]' end line = "#{item.id} #{status_icon} #{item.text}" line += " [#{item.}]" if item. && !item..empty? line += " (#{item.full_task_name})" if item.has_task_info? line end |
#list(items_to_show = nil, show_all: false) ⇒ Object
— List display —
286 287 288 289 290 291 292 |
# File 'lib/hiiro/todo.rb', line 286 def list(items_to_show = nil, show_all: false) items_to_show ||= show_all ? all : active return "No todo items found." if items_to_show.empty? lines = items_to_show.map { |item| format_item(item) } lines.join("\n") end |
#next_id! ⇒ Object
140 141 142 143 144 |
# File 'lib/hiiro/todo.rb', line 140 def next_id! id = @next_id @next_id += 1 id end |
#remove(id_or_index) ⇒ Object
224 225 226 227 228 229 230 |
# File 'lib/hiiro/todo.rb', line 224 def remove(id_or_index) item = resolve_item(id_or_index) return nil unless item @items.delete(item) save item end |
#reset(id_or_index) ⇒ Object
256 257 258 |
# File 'lib/hiiro/todo.rb', line 256 def reset(id_or_index) change_status(id_or_index, 'not_started') end |
#search(query) ⇒ Object
260 261 262 |
# File 'lib/hiiro/todo.rb', line 260 def search(query) items.select { |item| item.match?(query) } end |
#skip(id_or_index) ⇒ Object
252 253 254 |
# File 'lib/hiiro/todo.rb', line 252 def skip(id_or_index) change_status(id_or_index, 'skip') end |
#start(id_or_index) ⇒ Object
244 245 246 |
# File 'lib/hiiro/todo.rb', line 244 def start(id_or_index) change_status(id_or_index, 'started') end |