Snippets

Leslie Krause TTYSyncHandler Class for Lua

Created by Leslie Krause
The MIT License (MIT)
 
Copyright (c) 2022, Leslie Krause

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

For more details:
https://opensource.org/licenses/MIT
local uv = require( "uv" )

coroutine.wrap( function ( )
	local input
	local term = TTYSyncHandler( )

	term.write( "Continue? " )
        term.read_char( function ( char )
                if string.byte( char ) == 3 then
                        term.writeln( "^C" )
                        os.exit( )
		elseif char == "y" or char == "n" then
			input = char
			term.writeln( char )
			return true
		end
	end, 5 )

	if input == "n" then
		term.writeln( "Task aborted." )
	else
		term.write( "Please wait, processing..." )
		term.pause( 5 )
		term.writeln( )
		term.writeln( "Task completed." )
	end
end )( )

uv.run( )
local uv = require( "uv" )

coroutine.wrap( function ( )
	local input
	local term = TTYSyncHandler( )

	term.write( "What is your name? " )
        term.read_line( function ( line )
		input = line
		return true
	end, 10 )

	if not input then
		term.writeln( )
	elseif input ~= "" then
		term.writeln( "Hello " .. input .. ", welcome back!" )
	end
end )( )

uv.run( )
--------------------------
-- TTYSyncHandler Class --
--------------------------

local stdin = assert( uv.new_tty( 0, true ) )
local stdout = assert( uv.new_tty( 1, false ) )

function TTYSyncHandler( )
	local thread = coroutine.running( )
	local self = { }

	local function resume_thread( )
		stdin:read_stop( )
		stdin:set_mode( 0 )
		coroutine.resume( thread )
	end

	self.write = function ( str )
		stdout:write( str )
	end

	self.writeln = function ( str )
		if str then
			stdout:write( str )
		end
		stdout:write( "\n" )
	end

	self.read_line = function ( callback, timeout )
		local timer = uv.new_timer( )
		if timeout then
			timer:start( timeout * 1000, 0, function ( )
				resume_thread( )
			end )
		end
		stdin:read_start( function ( err, line )
			if callback( string.sub( line, 1, -2 ) ) then
				timer:stop( )
				resume_thread( )
			end
		end )
		coroutine.yield( )
	end

	self.read_char = function ( callback, timeout )
		stdin:set_mode( 1 )
		local timer = uv.new_timer( )
		if timeout then
			timer:start( timeout * 1000, 0, function ( )
				resume_thread( )
			end )
		end
		stdin:read_start( function ( err, char )
			if callback( char ) then
				timer:stop( )
				resume_thread( )
			end
		end )
		coroutine.yield( )
	end

	self.pause = function ( timeout )
		stdin:set_mode( 1 )
		local timer = uv.new_timer( )
		timer:start( timeout * 1000, 0, function ( )
			resume_thread( )
		end )
		stdin:read_start( function ( err, str )
			timer:stop( )
			resume_thread( )
		end )
		coroutine.yield( )
	end

	return self
end

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.