Antoine COMBET
3 years ago
2 changed files with 128 additions and 0 deletions
@ -0,0 +1,90 @@ |
|||
#!/usr/bin/env lua |
|||
|
|||
local usage = function(stream) |
|||
stream:write(arg[0] .. [[ -- manage a todo stack |
|||
todo pop |
|||
todo peek |
|||
todo push <task> |
|||
|
|||
for more info see `man todo.1` |
|||
]]) |
|||
end |
|||
|
|||
local peek = function(stack) |
|||
if #stack == 0 then os.exit(2) end |
|||
io.write(stack[#stack]) |
|||
io.write("\n") |
|||
return stack |
|||
end |
|||
|
|||
local pop = function(stack) |
|||
peek(stack) |
|||
stack[#stack] = nil |
|||
return stack |
|||
end |
|||
|
|||
local push = function(stack, args) |
|||
if not args[2] then |
|||
io.stderr:write("Invalid syntax\nUsage:\n") |
|||
usage(io.stderr) |
|||
os.exit(1) |
|||
end |
|||
stack[#stack + 1] = args[2] |
|||
return stack |
|||
end |
|||
|
|||
local exists = function(path) |
|||
return os.rename(path,path) or false |
|||
end |
|||
|
|||
local read_stack = function(path) |
|||
local f = io.open(path, "r") |
|||
if not f then os.exit(1) end |
|||
local t = {} |
|||
for l in f:lines() do |
|||
t[#t + 1] = l |
|||
end |
|||
f:close() |
|||
return t |
|||
end |
|||
|
|||
local write_stack = function(path, stack) |
|||
local f = io.open(path, "w") |
|||
if not f then os.exit(1) end |
|||
for _, v in ipairs(stack) do |
|||
f:write(v) |
|||
f:write("\n") |
|||
end |
|||
f:close() |
|||
end |
|||
|
|||
local stack_path = os.getenv("HOME") .. "/.local/share/todostack" |
|||
|
|||
if not exists(stack_path) then |
|||
local f = io.open(stack_path, "w") |
|||
f:close() |
|||
end |
|||
|
|||
local commands = { |
|||
peek = peek, |
|||
pop = pop, |
|||
push = push, |
|||
help = function() |
|||
usage(io.stdout) |
|||
end |
|||
} |
|||
|
|||
local c = commands[arg[1]] |
|||
if not c then |
|||
if arg[1] then |
|||
io.stderr:write("unknown command: " .. arg[1] .. "\nUsage:\n") |
|||
else |
|||
io.stderr:write("Please specify a command\n Usage:\n") |
|||
end |
|||
usage(io.stderr) |
|||
os.exit(1) |
|||
else |
|||
local stack = read_stack(stack_path) |
|||
stack = c(stack, arg) |
|||
write_stack(stack_path, stack) |
|||
end |
@ -0,0 +1,38 @@ |
|||
.\" Man page for the todo utility ./todo.1 |
|||
.TH TODO 1 |
|||
|
|||
.SH NAME |
|||
todo \- manages a todo stack |
|||
.SH SYNOPSIS |
|||
.B todo |
|||
.IR command " [" option "]" |
|||
.SH DESCRIPTION |
|||
.P |
|||
.B todo |
|||
is a small utility to manage a todo stack. It can push onto the stack, read the top element, and pop the top element. |
|||
.SH OPTIONS |
|||
.TP |
|||
.BI push " task" |
|||
Pushes a new task on the user's todo stack. |
|||
.TP |
|||
.B peek |
|||
Outputs the task at the top of the stack. |
|||
Does not alter the stack. |
|||
.TP |
|||
.B pop |
|||
Outputs the task at the top of the stack, then pops it off. |
|||
.SH EXIT STATUS |
|||
.TP |
|||
.B 0 |
|||
Operation successful. |
|||
.TP |
|||
.B 1 |
|||
General error |
|||
.TP |
|||
.B 2 |
|||
Todo stack underflow on a \fBpop\fR command or a \fBpeek\fR command. |
|||
.SH FILES |
|||
.TP |
|||
.B $HOME/.local/share/todostack |
|||
The user's todo stack. |
|||
.SH |
Write
Preview
Loading…
Cancel
Save
Reference in new issue