Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a native readLine module #20

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Z-IO.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ library
Z.IO.Resource
Z.IO.StdStream
Z.IO.StdStream.Ansi
Z.IO.StdStream.ReadLine
Z.IO.Time
Z.IO.UV.Errno
Z.IO.UV.FFI
Expand All @@ -124,7 +125,7 @@ library
, time >=1.9 && <2.0
, unix-time >=0.4.7 && <0.5
, unordered-containers ^>=0.2
, Z-Data >=0.8.1 && <0.9
, Z-Data

default-language: Haskell2010
default-extensions:
Expand Down
8 changes: 4 additions & 4 deletions Z/IO/StdStream.hs
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,13 @@ withRawStdin = bracket_ (setStdinTTYMode TTY_MODE_RAW) (setStdinTTYMode TTY_MODE

-- | Get terminal's output window size in (width, height) format,
-- return (-1, -1) if stdout is not connected to TTY.
getStdoutWinSize :: HasCallStack => IO (CInt, CInt)
getStdoutWinSize :: HasCallStack => IO (Int, Int)
getStdoutWinSize = case stdout of
StdStream True hdl _ uvm ->
withUVManager' uvm $ do
(w, (h, ())) <- allocPrimUnsafe $ \ w ->
allocPrimUnsafe $ \ h -> throwUVIfMinus_ $ uv_tty_get_winsize hdl w h
return (w, h)
(w, (h, ())) <- allocPrimUnsafe @CInt $ \ w ->
allocPrimUnsafe @CInt $ \ h -> throwUVIfMinus_ $ uv_tty_get_winsize hdl w h
return (fromIntegral w, fromIntegral h)
_ -> return (-1, -1)

--------------------------------------------------------------------------------
Expand Down
44 changes: 22 additions & 22 deletions Z/IO/StdStream/Ansi.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module Z.IO.StdStream.Ansi
csi, sgr, colorToCode
) where

import Control.Monad
import qualified Z.Data.Builder as B
import qualified Z.Data.Parser as P
import qualified Z.Data.Text as T
Expand All @@ -69,26 +70,25 @@ cursorUp, cursorDown, cursorForward, cursorBackward
-> B.Builder ()
cursorDownLine, cursorUpLine :: Int -- ^ Number of lines to move
-> B.Builder ()
cursorUp n = csi [n] (B.char8 'A')
cursorDown n = csi [n] (B.char8 'B')
cursorForward n = csi [n] (B.char8 'C')
cursorBackward n = csi [n] (B.char8 'D')
cursorDownLine n = csi [n] (B.char8 'E')
cursorUpLine n = csi [n] (B.char8 'F')

getCursorPosition :: IO (Int, Int)
getCursorPosition = do
withRawStdin . withMVar stdinBuf $ \ i -> do
clearInputBuffer i
putStd (csi [] "6n")
readParser (do
P.word8 ESC
P.word8 BRACKET_LEFT
!n <- P.int
P.word8 SEMICOLON
!m <- P.int
P.word8 LETTER_R
return (m, n)) i
cursorUp n = when (n > 0) $ csi [n] (B.char8 'A')
cursorDown n = when (n > 0) $ csi [n] (B.char8 'B')
cursorForward n = when (n > 0) $ csi [n] (B.char8 'C')
cursorBackward n = when (n > 0) $ csi [n] (B.char8 'D')
cursorDownLine n = when (n > 0) $ csi [n] (B.char8 'E')
cursorUpLine n = when (n > 0) $ csi [n] (B.char8 'F')

getCursorPosition :: BufferedInput -> IO (Int, Int)
getCursorPosition i = do
clearInputBuffer i
putStd (csi [] "6n")
readParser (do
P.word8 ESC
P.word8 BRACKET_LEFT
!n <- P.int
P.word8 SEMICOLON
!m <- P.int
P.word8 LETTER_R
return (m, n)) i

-- | Code to move the cursor to the specified column. The column numbering is
-- 1-based (that is, the left-most column is numbered 1).
Expand Down Expand Up @@ -119,8 +119,8 @@ clearLine = csi [2] (B.char8 'K')

scrollPageUp, scrollPageDown :: Int -- ^ Number of lines to scroll by
-> B.Builder()
scrollPageUp n = csi [n] (B.char8 'S')
scrollPageDown n = csi [n] (B.char8 'T')
scrollPageUp n = when (n > 0) $ csi [n] (B.char8 'S')
scrollPageDown n = when (n > 0) $ csi [n] (B.char8 'T')

hideCursor, showCursor :: B.Builder ()
hideCursor = csi [] "?25l"
Expand Down
Loading