diff --git a/CHANGELOG.md b/CHANGELOG.md index 236908d08..18245508f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -169,6 +169,9 @@ * Adds `leftmost` and `rightmost` to `Control.Order`, a generalisation of `min` and `max`. +* Adds `getTermCols` and `getTermLines` to the base library. They return the size of the + terminal if either stdin or stdout is a tty. + #### System * Changes `getNProcessors` to return the number of online processors rather than diff --git a/libs/base/System/Term.idr b/libs/base/System/Term.idr new file mode 100644 index 000000000..0c0a50605 --- /dev/null +++ b/libs/base/System/Term.idr @@ -0,0 +1,27 @@ +module System.Term + +%default total + +libterm : String -> String +libterm s = "C:" ++ s ++ ", libidris2_support, idris_term.h" + +%foreign libterm "idris2_setupTerm" +prim__setupTerm : PrimIO () + +%foreign libterm "idris2_getTermCols" +prim__getTermCols : PrimIO Int + +%foreign libterm "idris2_getTermLines" +prim__getTermLines : PrimIO Int + +export +setupTerm : IO () +setupTerm = primIO prim__setupTerm + +export +getTermCols : IO Int +getTermCols = primIO prim__getTermCols + +export +getTermLines : IO Int +getTermLines = primIO prim__getTermLines diff --git a/libs/base/base.ipkg b/libs/base/base.ipkg index 81f829bfb..920834668 100644 --- a/libs/base/base.ipkg +++ b/libs/base/base.ipkg @@ -127,4 +127,5 @@ modules = Control.App, System.File.Virtual, System.Info, System.REPL, - System.Signal + System.Signal, + System.Term diff --git a/src/Libraries/Utils/Term.idr b/src/Libraries/Utils/Term.idr index 3a8eec70d..69b08d79e 100644 --- a/src/Libraries/Utils/Term.idr +++ b/src/Libraries/Utils/Term.idr @@ -2,6 +2,8 @@ module Libraries.Utils.Term %default total +-- TODO: remove this file and use System.Term after version following 0.6.0 is released + libterm : String -> String libterm s = "C:" ++ s ++ ", libidris2_support, idris_term.h" diff --git a/support/c/idris_term.c b/support/c/idris_term.c index 80e826928..3b3887d40 100644 --- a/support/c/idris_term.c +++ b/support/c/idris_term.c @@ -36,13 +36,23 @@ void idris2_setupTerm() { int idris2_getTermCols() { struct winsize ts; - ioctl(0, TIOCGWINSZ, &ts); + int err = ioctl(0, TIOCGWINSZ, &ts); + if (err) { + err = ioctl(1, TIOCGWINSZ, &ts); + } + if (err) + return 0; return (int)ts.ws_col; } int idris2_getTermLines() { struct winsize ts; - ioctl(0, TIOCGWINSZ, &ts); + int err = ioctl(0, TIOCGWINSZ, &ts); + if (err) { + err = ioctl(1, TIOCGWINSZ, &ts); + } + if (err) + return 0; return (int)ts.ws_row; } diff --git a/tests/Main.idr b/tests/Main.idr index ae109ea3c..030fd22ea 100644 --- a/tests/Main.idr +++ b/tests/Main.idr @@ -204,7 +204,7 @@ idrisTestsAllSchemes : Requirement -> TestPool idrisTestsAllSchemes cg = MkTestPool ("Test across all scheme backends: " ++ show cg ++ " instance") [] (Just cg) - [ "scheme001" + [ "scheme001", "scheme002" , "channels001", "channels002", "channels003", "channels004", "channels005" , "channels006" ] diff --git a/tests/allschemes/scheme002/TermSize.idr b/tests/allschemes/scheme002/TermSize.idr new file mode 100644 index 000000000..fdba50480 --- /dev/null +++ b/tests/allschemes/scheme002/TermSize.idr @@ -0,0 +1,7 @@ +import System.Term + +main : IO () +main = do + width <- getTermCols + height <- getTermLines + printLn "Success \{show $ width > 0} \{show $ height > 0}" diff --git a/tests/allschemes/scheme002/expected b/tests/allschemes/scheme002/expected new file mode 100644 index 000000000..5dbe5530d --- /dev/null +++ b/tests/allschemes/scheme002/expected @@ -0,0 +1 @@ +"Success False False" diff --git a/tests/allschemes/scheme002/run b/tests/allschemes/scheme002/run new file mode 100755 index 000000000..ef7704711 --- /dev/null +++ b/tests/allschemes/scheme002/run @@ -0,0 +1,13 @@ +rm -rf build + +# observe that errors are correctly reported as zero. +$1 --exec main TermSize.idr