Skip to content

Commit

Permalink
[ base ] Add getTermCols and getTermLines to base library and fix pri… (
Browse files Browse the repository at this point in the history
  • Loading branch information
dunhamsteve authored Jul 18, 2023
1 parent 6be16a3 commit 8d7791b
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions libs/base/System/Term.idr
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion libs/base/base.ipkg
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,5 @@ modules = Control.App,
System.File.Virtual,
System.Info,
System.REPL,
System.Signal
System.Signal,
System.Term
2 changes: 2 additions & 0 deletions src/Libraries/Utils/Term.idr
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
14 changes: 12 additions & 2 deletions support/c/idris_term.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Main.idr
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down
7 changes: 7 additions & 0 deletions tests/allschemes/scheme002/TermSize.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import System.Term

main : IO ()
main = do
width <- getTermCols
height <- getTermLines
printLn "Success \{show $ width > 0} \{show $ height > 0}"
1 change: 1 addition & 0 deletions tests/allschemes/scheme002/expected
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"Success False False"
13 changes: 13 additions & 0 deletions tests/allschemes/scheme002/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
rm -rf build

# observe that errors are correctly reported as zero.
$1 --exec main TermSize.idr </dev/null

# The following should report True True, but the ci scripts don't
# provide a terminal
# $1 --exec main TermSize.idr

# The following should also report True True if the output is a terminal,
# but the testing framework redirects output.

# idris2 --exec main tests/allschemes/scheme002/TermSize.idr </dev/null

0 comments on commit 8d7791b

Please sign in to comment.