799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
|
# File 'lib/hiiro/tasks.rb', line 799
def self.build_hiiro(parent_hiiro, tm)
task_hiiro = parent_hiiro.make_child do |h|
resolve_task = lambda do |opts, positional|
if opts.find
sel = tm.select_task_interactive
next [nil, positional] unless sel
task = sel.is_a?(Hiiro::Task) ? sel : nil
[task, positional]
elsif opts.task
[tm.resolve_name(opts.task), positional]
elsif positional.any?
found = tm.resolve_name(positional.first)
found ? [found, positional[1..]] : [tm.current_task, positional]
else
[tm.current_task, positional]
end
end
task_opts_block = proc {
option(:task, short: :t, desc: 'Task name')
flag(:find, short: :f, desc: 'Choose task interactively')
flag(:all, short: :a, desc: 'Operate on all tasks (positional args = prefix filters)')
}
h.add_subcmd(:list) do |*args|
opts = Hiiro::Options.parse(args) { option(:tag, short: 't', desc: 'filter by tag (OR when multiple)', multi: true) }
tm.list(tags_filter: Array(opts.tag).reject(&:empty?))
end
h.add_subcmd(:ls) do |*args|
opts = Hiiro::Options.parse(args) { option(:tag, short: 't', desc: 'filter by tag (OR when multiple)', multi: true) }
tm.list(tags_filter: Array(opts.tag).reject(&:empty?))
end
h.add_subcmd(:tag) do |*raw_args|
tag_store = Hiiro::Tags.new(:task)
opts = Hiiro::Options.parse(raw_args) { flag(:edit, short: 'e', desc: 'open YAML editor to bulk-tag tasks') }
if opts.edit
task_names = tm.tasks.map(&:name)
task_lines = task_names.map { |n| "- #{n}" }
yaml_content = "# Select tasks to tag.\n\ntasks:\n#{task_lines.join("\n")}\n\ntags:\n-\n"
input = InputFile.yaml_file(hiiro: h, content: yaml_content, prefix: 'task-tag-')
input.edit
parsed = input.parsed_file
input.cleanup
selected = Array(parsed&.dig('tasks')).map(&:to_s).reject(&:empty?)
new_tags = Array(parsed&.dig('tags')).map(&:to_s).reject(&:empty?)
if selected.empty? || new_tags.empty?
puts "Aborted: need at least one task and one tag"
next
end
selected.each { |n| tag_store.add(n, *new_tags); puts "Tagged #{n} with: #{new_tags.join(', ')}" }
else
task_name = opts.args[0] || tm.current_task&.name
tag_names = opts.args[1..]
if task_name.nil? || tag_names.empty?
puts "Usage: h task tag [task_name] <tag> [tag2 ...]"
next
end
result = tag_store.add(task_name, *tag_names)
puts "Tagged #{task_name} with: #{tag_names.join(', ')}"
puts " Tags now: #{result.join(', ')}"
end
end
h.add_subcmd(:untag) do |*raw_args|
tag_store = Hiiro::Tags.new(:task)
task_name = raw_args[0] || tm.current_task&.name
tag_names = raw_args[1..]
if task_name.nil?
puts "Usage: h task untag [task_name] [tag ...]"
next
end
tag_store.remove(task_name, *tag_names)
puts tag_names.empty? ? "Cleared all tags from #{task_name}" : "Removed #{tag_names.join(', ')} from #{task_name}"
end
h.add_subcmd(:tags) do
tag_store = Hiiro::Tags.new(:task)
all = tag_store.all
if all.empty?
puts "No tagged tasks."
next
end
by_tag = Hash.new { |h2, k| h2[k] = [] }
all.each { |task, tags| tags.each { |t| by_tag[t] << task } }
by_tag.sort.each do |tag, tasks|
puts "#{Hiiro::Tags.badges([tag])} (#{tasks.length})"
tasks.each { |t| puts " #{t}" }
end
end
h.add_subcmd(:sparse) do |*args|
opts = Hiiro::Options.parse(args) do
flag(:list, short: 'l', desc: 'list available sparse groups and their directories')
flag(:disable, short: 'd', desc: 'disable sparse checkout on the current task worktree')
end
if opts.help
puts opts.help_text
exit 1
end
if opts.list
groups = Hiiro::SparseGroups.load
if groups.empty?
puts "No sparse groups configured."
puts " Config: #{Hiiro::SparseGroups::FILE}"
next
end
puts "Sparse groups:"
groups.each do |name, dirs|
puts " #{name}"
Array(dirs).each { |d| puts " #{d}" }
end
next
end
task = tm.current_task
unless task
puts "Not in a task session"
next
end
tree = tm.environment.find_tree(task.tree_name)
path = tree&.path
unless path
puts "Could not find worktree path for task '#{task.name}'"
next
end
if opts.disable
Hiiro::Git.new(nil, path).disable_sparse_checkout(path)
puts "Disabled sparse checkout for '#{task.name}'"
next
end
group_names = opts.args
if group_names.empty?
current = `git -C #{Shellwords.shellescape(path)} sparse-checkout list 2>/dev/null`.strip
if current.empty?
puts "No sparse checkout active for '#{task.name}'."
puts " Usage: h task sparse <group> (e.g. h task sparse default)"
puts " Groups: h task sparse -l"
else
puts "Sparse checkout for '#{task.name}':"
current.split("\n").each { |d| puts " #{d}" }
end
next
end
tm.apply_sparse_checkout(path, group_names)
end
h.add_subcmd(:start) do |*raw_args|
opts = Hiiro::Options.parse(raw_args) do
option(:sparse, short: :s, desc: 'Sparse checkout group (may be repeated)', multi: true)
end
task_name = opts.args[0]
app_name = opts.args[1]
tm.start_task(task_name, app_name: app_name, sparse_groups: opts.sparse)
end
h.add_subcmd(:switch) do |*raw_args|
opts = Hiiro::Options.parse(raw_args) do
option(:force, short: :f, type: :flag, desc: 'Switch even if session is already attached')
end
task_name = opts.args[0]
app_name = opts.args[1]
if task_name.nil?
selected = tm.select_task_interactive
next unless selected
case selected
when Hiiro::Tmux::Session
if selected.attached? && !opts.force
puts "Session '#{selected.name}' is already attached. Use -f to switch anyway."
exit 1
end
h.start_tmux_session(selected.name)
puts "Switched to session '#{selected.name}'"
next
when Hiiro::Task
tm.switch_to_task(selected, app_name: app_name, force: opts.force)
next
end
end
task = tm.task_by_name(task_name)
unless task
session = tm.environment.find_session(task_name)
if session
if session.attached? && !opts.force
puts "Session '#{session.name}' is already attached. Use -f to switch anyway."
exit 1
end
h.start_tmux_session(session.name)
puts "Switched to session '#{session.name}'"
next
end
end
unless task
proj_dirs = Dir.glob(File.expand_path('~/proj/*/'))
dir_names = proj_dirs.map { |d| File.basename(d) }
result = Hiiro::Matcher.by_prefix(dir_names, task_name)
if result.one?
dir_name = result.first.item
h.start_tmux_session(dir_name, start_directory: File.expand_path("~/proj/#{dir_name}"))
puts "Switched to ~/proj/#{dir_name}"
next
elsif result.ambiguous?
puts "Ambiguous match for '#{task_name}': #{result.matches.map(&:item).join(', ')}"
exit 1
end
end
tm.switch_to_task(task, app_name: app_name, force: opts.force)
end
h.add_subcmd(:app) do |app_name=nil|
if app_name.nil?
names = tm.environment.all_apps.map(&:name)
app_name = h.fuzzyfind(names)
next unless app_name
end
tm.open_app(app_name)
end
h.add_subcmd(:apps) { tm.list_apps }
h.add_subcmd(:cd) do |*raw_args|
opts = Hiiro::Options.parse(raw_args, &task_opts_block)
task, positional = resolve_task.call(opts, opts.args)
unless task
puts "No task found"
next
end
tree_path = tm.resolve_path(task)
app_name = positional[0]
if app_name.nil?
tm.send_cd(tree_path)
else
result = tm.environment.app_matcher.find(app_name)
unless result.match?
STDERR.puts "App '#{app_name}' not found"
next
end
tm.send_cd(File.join(tree_path, result.first.item.relative_path))
end
end
h.add_subcmd(:path) do |*raw_args|
opts = Hiiro::Options.parse(raw_args, &task_opts_block)
if opts.all
tm.filter_tasks(opts.args).each do |task|
path = tm.resolve_path(task)
puts path if path
end
next
end
task, positional = resolve_task.call(opts, opts.args)
unless task
STDERR.puts "No task found"
next
end
tree_path = tm.resolve_path(task)
app_name = positional[0]
globs = positional[1..]
if app_name.nil?
print tree_path
next
end
result = tm.environment.app_matcher.find(app_name)
unless result.match?
STDERR.puts "App '#{app_name}' not found"
next
end
app_path = File.join(tree_path, result.first.item.relative_path)
if globs.empty?
print app_path
next
end
globs.each do |pattern|
Dir.glob(File.join(app_path, pattern)).sort.each { |f| puts f }
end
end
print_task_value = lambda do |raw_args, attr|
opts = Hiiro::Options.parse(raw_args, &task_opts_block)
if opts.all
tm.filter_tasks(opts.args).each do |t|
val = t.public_send(attr)
puts val if val
end
else
task_name = opts.task || opts.args.first
print tm.value_for_task(task_name) { |t| t.public_send(attr) }
end
end
h.add_subcmd(:branch) { |*raw_args| print_task_value.call(raw_args, :branch) }
h.add_subcmd(:tree) { |*raw_args| print_task_value.call(raw_args, :tree_name) }
h.add_subcmd(:session) { |*raw_args| print_task_value.call(raw_args, :session_name) }
h.add_subcmd(:name) { |*raw_args| print_task_value.call(raw_args, :name) }
h.add_subcmd(:current) do
task = tm.current_task
next STDERR.puts("Not in a task") unless task
print task.name
end
h.add_subcmd(:cbranch) do
task = tm.current_task
next STDERR.puts("Not in a task") unless task
print task.branch if task.branch
end
h.add_subcmd(:ctree) do
task = tm.current_task
next STDERR.puts("Not in a task") unless task
print task.tree_name if task.tree_name
end
h.add_subcmd(:csession) do
task = tm.current_task
next STDERR.puts("Not in a task") unless task
print task.session_name if task.session_name
end
h.add_subcmd(:sh) do |*raw_args|
require 'shellwords'
sh_opts_block = proc {
instance_exec(&task_opts_block)
option(:session, short: :s, desc: 'Run in a new window in this tmux session')
}
opts = Hiiro::Options.parse(raw_args, &sh_opts_block)
task, positional = resolve_task.call(opts, opts.args)
unless task
puts "Not in a task session (use -t or -f to specify)"
next
end
path = tm.resolve_path(task)
if opts.session
session_name = opts.session
window_args = ['tmux', 'new-window', '-t', session_name, '-c', path]
window_args << positional.shelljoin unless positional.empty?
system(*window_args)
tmux_client.open_session(session_name)
else
Dir.chdir(path)
positional.empty? ? exec(ENV['SHELL'] || 'zsh') : exec(*positional)
end
end
h.add_subcmd(:status) { tm.status }
h.add_subcmd(:st) { tm.status }
h.add_subcmd(:save) { tm.save }
h.add_subcmd(:prune) do |*raw_args|
opts = Hiiro::Options.parse(raw_args) {
flag(:force, short: :f, desc: 'Actually delete (default is dry-run)')
}
to_remove = tm.environment.all_tasks.select do |task|
next true unless task.tree_name
path = tm.resolve_path(task)
path.nil? || !Dir.exist?(path)
end
if to_remove.empty?
puts "No tasks to prune"
next
end
to_remove.each do |task|
path = task.tree_name ? tm.resolve_path(task) : '(no tree)'
if opts.force
tm.config.remove_task(task.name)
puts "Pruned: #{task.name} (#{path})"
else
puts "Would prune: #{task.name} (#{path})"
end
end
unless opts.force
puts
puts "Re-run with -f to actually delete."
end
end
h.add_subcmd(:color) do
task = tm.current_task
unless task
puts "Not in a task session"
next
end
unless task.color_index
puts "No color assigned to task '#{task.name}'"
next
end
Hiiro::TaskColors.apply(task.session_name, task.color_index)
colors = Hiiro::TaskColors.for_index(task.color_index)
puts "Applied color theme #{task.color_index}: bg=#{colors[:bg]} fg=#{colors[:fg]}"
end
h.add_subcmd(:stop) do |task_name=nil|
if task_name.nil?
selected = tm.select_task_interactive
next unless selected
task_name = selected.name
end
task = tm.task_by_name(task_name)
tm.stop_task(task)
end
h.add_subcmd(:resume) do |tree_name=nil|
available = tm.environment.all_trees.reject { |t|
tm.environment.all_tasks.any? { |task| task.tree_name == t.name }
}
if available.empty?
puts "No available worktrees to resume"
next
end
tree = if tree_name
Hiiro::Matcher.new(available, :name).by_prefix(tree_name).first&.item
else
h.fuzzyfind_from_map(available.each_with_object({}) { |t, m| m[t.name] = t })
end
unless tree
puts tree_name ? "No available worktree matching '#{tree_name}'" : "No worktree selected"
next
end
tm.resume_task(tree)
end
h.add_subcmd(:edit) do
h.edit_files(__FILE__)
end
h.add_subcmd(:todo) do |*todo_args|
todo_manager = Hiiro::TodoManager.new
task = tm.current_task
task_info = if task
{
task_name: task.name,
tree: task.tree_name,
branch: task.branch,
session: task.session_name
}
end
todo_subcmd = todo_args.shift
case todo_subcmd
when 'ls', 'list', nil
show_all = todo_args.delete('-a') || todo_args.delete('--all')
items = if show_all
todo_manager.all
elsif task
todo_manager.filter_by_task(task.name).select { |i| %w[not_started started].include?(i.status) }
else
todo_manager.active
end
if items.empty?
puts task ? "No todo items for task '#{task.name}'." : "No todo items found."
else
puts todo_manager.list(items)
end
when 'add'
if todo_args.empty?
new_items = todo_manager.edit_items(task_info: task_info)
if new_items.empty?
puts "No items added."
next
end
todo_manager.add_items(new_items)
if new_items.length == 1
puts "Added: #{todo_manager.format_item(new_items.first)}"
else
puts "Added #{new_items.length} items:"
new_items.each { |item| puts " #{todo_manager.format_item(item)}" }
end
next
end
tags = nil
text_parts = []
while todo_args.any?
arg = todo_args.shift
case arg
when '-t', '--tags'
tags = todo_args.shift
else
text_parts << arg
end
end
text = text_parts.join(' ')
item = todo_manager.add(text, tags: tags, task_info: task_info)
puts "Added: #{todo_manager.format_item(item)}"
when 'rm', 'remove'
id_or_index = todo_args.shift
unless id_or_index
puts "Usage: h #{tm.scope} todo rm <id|index>"
next
end
item = todo_manager.remove(id_or_index)
puts item ? "Removed: #{item.text}" : "Item not found: #{id_or_index}"
when 'start'
id_or_index = todo_args.shift
unless id_or_index
puts "Usage: h #{tm.scope} todo start <id|index>"
next
end
item = todo_manager.start(id_or_index)
puts item ? "Started: #{todo_manager.format_item(item)}" : "Item not found: #{id_or_index}"
when 'done'
id_or_index = todo_args.shift
unless id_or_index
puts "Usage: h #{tm.scope} todo done <id|index>"
next
end
item = todo_manager.done(id_or_index)
puts item ? "Done: #{todo_manager.format_item(item)}" : "Item not found: #{id_or_index}"
when 'skip'
id_or_index = todo_args.shift
unless id_or_index
puts "Usage: h #{tm.scope} todo skip <id|index>"
next
end
item = todo_manager.skip(id_or_index)
puts item ? "Skipped: #{todo_manager.format_item(item)}" : "Item not found: #{id_or_index}"
when 'search'
query = todo_args.join(' ')
if query.empty?
puts "Usage: h #{tm.scope} todo search <query>"
next
end
items = todo_manager.search(query)
if items.empty?
puts "No items matching: #{query}"
else
puts todo_manager.list(items)
end
else
puts "Usage: h #{tm.scope} todo <ls|add|rm|start|done|skip|search> [args]"
end
end
h.add_subcmd(:queue) do |*queue_args|
task = tm.current_task
task_info = if task
{
task_name: task.name,
tree_name: task.tree_name,
session_name: task.session_name
}
end
q = Hiiro::Queue.current(parent_hiiro)
Hiiro::Queue.build_hiiro(h, q, task_info: task_info).run
end
h.add_subcmd(:service) do |*svc_args|
sm = Hiiro::ServiceManager.new
Hiiro::ServiceManager.build_hiiro(h, sm, task_manager: tm).run
end
h.add_subcmd(:run) do |*run_args|
rt = Hiiro::RunnerTool.new
Hiiro::RunnerTool.build_hiiro(h, rt, git: parent_hiiro.git).run
end
h.add_subcmd(:file) do |*file_args|
af = Hiiro::AppFiles.new
Hiiro::AppFiles.build_hiiro(h, af, environment: tm.environment).run
end
h.add_subcmd(:prs) do |*args|
exec('h', 'pr', *args)
end
end
task_hiiro
end
|