Skip to content

Instantly share code, notes, and snippets.

@manpages
Created March 23, 2012 01:51
Show Gist options
  • Save manpages/2166128 to your computer and use it in GitHub Desktop.
Save manpages/2166128 to your computer and use it in GitHub Desktop.
recursive todo script (verypositive software)
<?
$fp = fopen('todo.tsv', 'r');
while ($line = fgetcsv($fp, 0, "\t")) {
foreach ($line as &$li) $li = trim($li);
unset($li);
if ($line[1]) {
$items[$line[1]] = array(
'tag' => $line[1],
'desc' => $line[3],
'deps' => $line[2] ? explode(',', $line[2]) : array(),
'done' => strpos($line[0], '@') !== false,
'prio' => max(trim($line[0], '@'), 1),
'allows' => array(),
'unsat' => array(),
);
$lastt = $line[1];
}
else {
$items[$lastt]['desc'] = trim($items[$lastt]['desc']."\n".$line[3]);
}
}
function deps($tag, $done, $dep, $add) {
global $items;
$done[$tag]++;
if (!$items[$tag]['tag']) {
$items[$tag]['tag'] = $tag;
$items[$tag]['desc'] = "Orphan $tag";
$items[$tag]['prio'] += 1;
$items[$tag]['deps'] = array();
}
if ($items[$tag]['done'])
return;
foreach ($items[$tag]['deps'] as $dtag) {
if ($done[$dtag]) {
echo 'Circular!';
print_r($done);
}
if (!$items[$dtag]['allows'][$dep]++) {
// echo str_repeat(' ', count($done))."$tag $dtag ".$items[$dtag]['prio']." + ".$add.".\n";
$items[$dtag]['prio'] += $add; //Suspiciously simple
deps($dtag, $done, $dep, $add);
}
}
}
foreach ($items as $tag => $item) {
foreach ($item['deps'] as $dtag) {
if (!$items[$dtag]['done'])
$items[$tag]['unsat'][$dtag]++; //TODO recursive
}
deps($tag, array($tag => 1), $tag, $item['prio']);
}
usort($items, function ($a, $b) {
if (count($a['unsat']) != count($b['unsat']))
return count($a['unsat']) > count($b['unsat']);
if ($a['prio'] != $b['prio'])
return $a['prio'] < $b['prio'];
return count($a['deps']) > count($b['deps']);
});
foreach ($items as $item) {
if ($item['done'])
continue;
echo "$item[desc]\n"
." ".$item[tag]."\n";
if ($item['allows'])
echo " -> ".implode(' ', array_keys($item['allows']))."\n";
if ($item['unsat'])
echo " <= ".implode(' ', array_keys($item['unsat']))."\n";
echo "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment