mirror of https://github.com/Hilbis/Hilbish
feat: use sinks for all nature commands, add writeln function
parent
0e3fd1ff49
commit
e510b1103f
|
@ -1,15 +1,15 @@
|
|||
local commander = require 'commander'
|
||||
|
||||
commander.register('bg', function()
|
||||
commander.register('bg', function(_, sinks)
|
||||
local job = hilbish.jobs.last()
|
||||
if not job then
|
||||
print 'bg: no last job'
|
||||
sinks.out:writeln 'bg: no last job'
|
||||
return 1
|
||||
end
|
||||
|
||||
local err = job.background()
|
||||
if err then
|
||||
print('bg: ' .. err)
|
||||
sinks.out:writeln('bg: ' .. err)
|
||||
return 2
|
||||
end
|
||||
end)
|
||||
|
|
|
@ -5,7 +5,7 @@ commander.register('cat', function(args, sinks)
|
|||
local exit = 0
|
||||
|
||||
if #args == 0 then
|
||||
print [[
|
||||
sinks.out:writeln [[
|
||||
usage: cat [file]...]]
|
||||
end
|
||||
|
||||
|
@ -13,11 +13,11 @@ usage: cat [file]...]]
|
|||
local f = io.open(fName)
|
||||
if f == nil then
|
||||
exit = 1
|
||||
print(string.format('cat: %s: no such file or directory', fName))
|
||||
sinks.out:writeln(string.format('cat: %s: no such file or directory', fName))
|
||||
goto continue
|
||||
end
|
||||
|
||||
sinks.out:write(f:read '*a')
|
||||
sinks.out:writeln(f:read '*a')
|
||||
::continue::
|
||||
end
|
||||
io.flush()
|
||||
|
|
|
@ -4,9 +4,9 @@ local fs = require 'fs'
|
|||
local dirs = require 'nature.dirs'
|
||||
|
||||
dirs.old = hilbish.cwd()
|
||||
commander.register('cd', function (args)
|
||||
commander.register('cd', function (args, sinks)
|
||||
if #args > 1 then
|
||||
print("cd: too many arguments")
|
||||
sinks.out:writeln("cd: too many arguments")
|
||||
return 1
|
||||
elseif #args > 0 then
|
||||
local path = args[1]:gsub('$%$','\0'):gsub('${([%w_]+)}', os.getenv)
|
||||
|
@ -14,14 +14,14 @@ commander.register('cd', function (args)
|
|||
|
||||
if path == '-' then
|
||||
path = dirs.old
|
||||
print(path)
|
||||
sinks.out:writeln(path)
|
||||
end
|
||||
dirs.setOld(hilbish.cwd())
|
||||
dirs.push(path)
|
||||
|
||||
local ok, err = pcall(function() fs.cd(path) end)
|
||||
if not ok then
|
||||
print(err)
|
||||
sinks.out:writeln(err)
|
||||
return 1
|
||||
end
|
||||
bait.throw('cd', path)
|
||||
|
|
|
@ -3,9 +3,9 @@ local fs = require 'fs'
|
|||
local lunacolors = require 'lunacolors'
|
||||
local dirs = require 'nature.dirs'
|
||||
|
||||
commander.register('cdr', function(args)
|
||||
commander.register('cdr', function(args, sinks)
|
||||
if not args[1] then
|
||||
print(lunacolors.format [[
|
||||
sinks.out:writeln(lunacolors.format [[
|
||||
cdr: change directory to one which has been recently visied
|
||||
|
||||
usage: cdr <index>
|
||||
|
@ -17,21 +17,21 @@ to get a list of recent directories, use {green}{underline}cdr list{reset}]])
|
|||
if args[1] == 'list' then
|
||||
local recentDirs = dirs.recentDirs
|
||||
if #recentDirs == 0 then
|
||||
print 'No directories have been visited.'
|
||||
sinks.out:writeln 'No directories have been visited.'
|
||||
return 1
|
||||
end
|
||||
print(table.concat(recentDirs, '\n'))
|
||||
sinks.out:writeln(table.concat(recentDirs, '\n'))
|
||||
return
|
||||
end
|
||||
|
||||
local index = tonumber(args[1])
|
||||
if not index then
|
||||
print(string.format('Received %s as index, which isn\'t a number.', index))
|
||||
sinks.out:writeln(string.format('Received %s as index, which isn\'t a number.', index))
|
||||
return 1
|
||||
end
|
||||
|
||||
if not dirs.recent(index) then
|
||||
print(string.format('No recent directory found at index %s.', index))
|
||||
sinks.out:writeln(string.format('No recent directory found at index %s.', index))
|
||||
return 1
|
||||
end
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
local commander = require 'commander'
|
||||
|
||||
commander.register('disown', function(args)
|
||||
commander.register('disown', function(args, sinks)
|
||||
if #hilbish.jobs.all() == 0 then
|
||||
print 'disown: no current job'
|
||||
sinks.out:writeln 'disown: no current job'
|
||||
return 1
|
||||
end
|
||||
|
||||
|
@ -10,7 +10,7 @@ commander.register('disown', function(args)
|
|||
if #args < 0 then
|
||||
id = tonumber(args[1])
|
||||
if not id then
|
||||
print 'disown: invalid id for job'
|
||||
sinks.out:writeln 'disown: invalid id for job'
|
||||
return 1
|
||||
end
|
||||
else
|
||||
|
@ -19,7 +19,7 @@ commander.register('disown', function(args)
|
|||
|
||||
local ok = pcall(hilbish.jobs.disown, id)
|
||||
if not ok then
|
||||
print 'disown: job does not exist'
|
||||
sinks.out:writeln 'disown: job does not exist'
|
||||
return 2
|
||||
end
|
||||
end)
|
||||
|
|
|
@ -30,7 +30,7 @@ commander.register('doc', function(args, sinks)
|
|||
f = io.open(moddocPath .. subdocName .. '.md', 'rb')
|
||||
end
|
||||
if not f then
|
||||
sinks.out:write('No documentation found for ' .. mod .. '.')
|
||||
sinks.out:writeln('No documentation found for ' .. mod .. '.')
|
||||
return
|
||||
end
|
||||
end
|
||||
|
@ -74,7 +74,7 @@ commander.register('doc', function(args, sinks)
|
|||
end):gsub('#+.-\n', function(t)
|
||||
return '{bold}{magenta}' .. t .. '{reset}'
|
||||
end))
|
||||
sinks.out:write(formattedFuncs)
|
||||
sinks.out:writeln(formattedFuncs)
|
||||
f:close()
|
||||
|
||||
return
|
||||
|
@ -83,7 +83,7 @@ commander.register('doc', function(args, sinks)
|
|||
return lunacolors.underline(lunacolors.blue(string.gsub(f, '.md', '')))
|
||||
end)
|
||||
|
||||
sinks.out:write [[
|
||||
sinks.out:writeln [[
|
||||
Welcome to Hilbish's doc tool! Here you can find documentation for builtin
|
||||
functions and other things.
|
||||
|
||||
|
@ -92,5 +92,5 @@ A section is a module or a literal section and a subdoc is a subsection for it.
|
|||
|
||||
Available sections: ]]
|
||||
|
||||
sinks.out:write(table.concat(modules, ', ') .. '\n')
|
||||
sinks.out:writeln(table.concat(modules, ', '))
|
||||
end)
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
local commander = require 'commander'
|
||||
|
||||
commander.register('fg', function()
|
||||
commander.register('fg', function(_, sinks)
|
||||
local job = hilbish.jobs.last()
|
||||
if not job then
|
||||
print 'fg: no last job'
|
||||
sinks.out:writeln 'fg: no last job'
|
||||
return 1
|
||||
end
|
||||
|
||||
local err = job.foreground() -- waits for job; blocks
|
||||
if err then
|
||||
print('fg: ' .. err)
|
||||
sinks.out:writeln('fg: ' .. err)
|
||||
return 2
|
||||
end
|
||||
end)
|
||||
|
|
20
sink.go
20
sink.go
|
@ -25,6 +25,7 @@ func setupSinkType(rtm *rt.Runtime) {
|
|||
sinkMethods := rt.NewTable()
|
||||
sinkFuncs := map[string]util.LuaExport{
|
||||
"write": {luaSinkWrite, 2, false},
|
||||
"writeln": {luaSinkWriteln, 2, false},
|
||||
}
|
||||
util.SetExports(l, sinkMethods, sinkFuncs)
|
||||
|
||||
|
@ -58,6 +59,25 @@ func luaSinkWrite(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|||
return c.Next(), nil
|
||||
}
|
||||
|
||||
func luaSinkWriteln(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
if err := c.CheckNArgs(2); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s, err := sinkArg(c, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
data, err := c.StringArg(1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.writer.Write([]byte(data + "\n"))
|
||||
|
||||
return c.Next(), nil
|
||||
}
|
||||
|
||||
func newSinkInput(r io.Reader) *sink {
|
||||
s := &sink{
|
||||
reader: r,
|
||||
|
|
Loading…
Reference in New Issue