Bläddra i källkod

add getChar support, flush in Lib.print and Lib.println

frabbit 11 år sedan
förälder
incheckning
49476b0e17

+ 2 - 0
std/python/Lib.hx

@@ -58,11 +58,13 @@ class Lib
     var str = Std.string(v);
 
     untyped __python__('_hx_sys.stdout.buffer.write(("%s"%str).encode(\'utf-8\'))');
+    untyped __python__('_hx_sys.stdout.flush()');
   }
 
   public static function println(v:Dynamic):Void {
     var str = Std.string(v);
     untyped __python__('_hx_sys.stdout.buffer.write(("%s\\n"%str).encode(\'utf-8\'))');
+    untyped __python__('_hx_sys.stdout.flush()');
   }
 
     public static function toPythonIterable <T>(it:Iterable<T>):python.lib.Types.NativeIterable<T>

+ 25 - 1
std/python/_std/Sys.hx

@@ -73,7 +73,31 @@ class Sys {
 	}
 
 	public static function getChar( echo : Bool ) : Int {
-		return 0;
+		trace(python.lib.Sys.platform);
+		var ch = switch (python.lib.Sys.platform) {
+			case "linux" | "darwin":
+				var fd = python.lib.Sys.stdin.fileno();
+				var old = python.lib.Termios.tcgetattr(fd);
+
+				var restore = python.lib.Termios.tcsetattr.bind(fd, python.lib.Termios.TCSADRAIN, old);
+
+				try {
+					python.lib.Tty.setraw(fd);
+					var ch = python.lib.Sys.stdin.read(1);
+					restore();
+					ch;
+				} catch (e:Dynamic) {
+					trace("error" + e);
+					restore();
+					String.fromCharCode(0);
+				}
+
+			case "win32" | "cygwin":
+				python.lib.Msvrt.getch();
+			case x :
+				throw "platform " + x + " not supported";
+		}
+		return ch.charCodeAt(0);
 	}
 
 	public static function stdin() : haxe.io.Input {

+ 15 - 0
std/python/lib/Msvrt.hx

@@ -0,0 +1,15 @@
+
+package python.lib;
+
+extern class Msvrt {
+
+	public static function getch ():String;
+
+	static function __init__ ():Void
+	{
+		try {
+			python.Macros.importAs("msvrt", "python.lib.Msvrt");
+		} catch (e:Dynamic) {}
+	}
+
+}

+ 2 - 0
std/python/lib/Sys.hx

@@ -13,8 +13,10 @@ extern class Sys {
 	public static function getfilesystemencoding():String;
 
 	public static var version:String;
+	public static var platform:String;
 
 	public static var stdout(default, never):TextIOBase;
+	public static var stdin(default, never):TextIOBase;
 
 
 	public static function getsizeof (t:Dynamic):Int;

+ 23 - 0
std/python/lib/Termios.hx

@@ -0,0 +1,23 @@
+
+package python.lib;
+
+abstract TermiosSettings(Dynamic) {}
+
+extern class Termios {
+
+	public static var TCSADRAIN : Int;
+	public static var ECHO : Int;
+
+	public static function tcgetattr (fileNo:Int):TermiosSettings;
+
+	public static function tcsetattr (fileNo:Int, when:Int, settings:TermiosSettings):Void;
+
+	static function __init__ ():Void
+	{
+		try {
+			python.Macros.importAs("termios", "python.lib.Termios");
+		}
+		catch (e:Dynamic) {}
+	}
+
+}

+ 15 - 0
std/python/lib/Tty.hx

@@ -0,0 +1,15 @@
+
+package python.lib;
+
+extern class Tty {
+	public static function setraw (fileNo:Int):Void;
+
+
+	static function __init__ ():Void
+	{
+		try {
+			python.Macros.importAs("tty", "python.lib.Tty");
+		} catch (e:Dynamic) {}
+
+	}
+}

+ 1 - 0
std/python/lib/io/IOBase.hx

@@ -12,5 +12,6 @@ extern class IOBase {
 	public function tell():Int;
 	public function writable():Bool;
 	public function seekable():Bool;
+	public function fileno():Int;
 
 }