Skip to content

Commit

Permalink
output to different streams at eval/runtime
Browse files Browse the repository at this point in the history
Close #13
  • Loading branch information
s-ol committed Apr 18, 2020
1 parent dd02ee9 commit 2403b5b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
4 changes: 2 additions & 2 deletions alv/builtin.moon
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ print = ValueStream.meta
tick: =>
if @inputs.value\metatype! == 'event'
for msg in *@inputs.value!
print msg
L\print msg
else
print @inputs.value!
L\print @inputs.value!

{
:doc
Expand Down
8 changes: 5 additions & 3 deletions alv/copilot.moon
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ class Copilot
@root\tick_io!
@root\tick!
if not ok
print error
L\print error
@registry\end_tick!

eval: =>
@registry\begin_eval!
ok, ast = Error.try "parsing '#{@file}'", program\match, slurp @file
if not (ok and ast)
print ast or Error 'syntax', "failed to parse"
L\print ast or Error 'syntax', "failed to parse"
@registry\rollback_eval!
return

scope = Scope globals
ok, root = Error.try "evaluating '#{@file}'", ast\eval, scope, @registry
if not ok
print root
L\print root
@registry\rollback_eval!
return

Expand All @@ -76,8 +76,10 @@ class Copilot
return

if @last_modification < modification
L.stream = io.stderr
L\log "#{@file} changed at #{modification}"
@eval!
L.stream = io.stdout
@last_modification = os.time!

{
Expand Down
19 changes: 12 additions & 7 deletions alv/logger.moon
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,30 @@ class Logger
else
error unpack res

--- the stream to write to.
-- @tfield file stream

--- static functions
-- @section static

--- create a new Logger.
-- @classmethod
-- @tparam string level the log-level to log at.
new: (level='log') =>
-- @tparam file stream the stream to write to.
new: (level='log', @stream=io.stdout) =>
@level = levels[level] or level
@prefix = ''

for name, level in pairs levels
@[name] = (first, ...) =>
@[name] = (msg) =>
return unless @level <= level

where = debug.traceback '', 2
msg = tostring msg
@stream\write @prefix, msg, '\n'
if level == levels.error or @level == levels.debug
print @prefix .. first, ...
print where
else
print @prefix .. first, ...
where = debug.traceback '', 2
@stream\write where, '\n'
@stream\flush!

if level == levels.print
@push = (fn, ...) => fn ...
Expand All @@ -74,6 +78,7 @@ class Logger
-- - `'silent'`
--
-- @tparam ?string level the level to initialize the logger at.
-- @tparam ?file stream the output stream (stdout).
@init: (...) ->
L = Logger ...

Expand Down
5 changes: 2 additions & 3 deletions bin/alv-copilot
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ CONF=$tmpdir/conf

cat > "$CONF" << 'EOF'
split
screen -t evaltime sh -c 'tty > "$FIFO"; read done < "$FIFO"; screen -X quit'
focus
screen -t evaltime sh -c 'tty > "$FIFO"; read done < "$FIFO"'
focus
screen -t runtime sh -c 'read tty < "$FIFO"; "alv" $ARGS 2> "$tty"; echo "[press enter to exit]"; read prompt; echo done > "$FIFO"'
screen -t runtime sh -c 'read tty < "$FIFO"; "alv" $ARGS 2> "$tty"; echo "[press enter to exit]"; read prompt; screen -X quit'
EOF

mkfifo "$FIFO"
Expand Down

0 comments on commit 2403b5b

Please sign in to comment.