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

Add some windows hack for getChar #9

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions patat.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Executable patat
Data.Aeson.Extended
Data.Aeson.TH.Extended
Data.Data.Extended
Patat.GetKey
Patat.Presentation
Patat.Presentation.Display
Patat.Presentation.Display.Table
Expand Down
3 changes: 2 additions & 1 deletion src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Control.Monad (forever, unless, when)
import Data.Monoid ((<>))
import Data.Version (showVersion)
import qualified Options.Applicative as OA
import qualified Patat.GetKey as GetKey
import Patat.Presentation
import qualified Paths_patat
import qualified System.Console.ANSI as Ansi
Expand Down Expand Up @@ -108,7 +109,7 @@ main = do

where
interactiveLoop options pres0 = do
IO.hSetBuffering IO.stdin IO.NoBuffering
GetKey.initialize
commandChan <- Chan.newChan

_ <- forkIO $ forever $
Expand Down
61 changes: 61 additions & 0 deletions src/Patat/GetKey.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
--------------------------------------------------------------------------------
{-# LANGUAGE CPP #-}


--------------------------------------------------------------------------------
#if defined(mingw32_HOST_OS) || defined(__MINGW32__)
{-# LANGUAGE ForeignFunctionInterface #-}
#endif


--------------------------------------------------------------------------------
module Patat.GetKey
( initialize
, getKey
) where


--------------------------------------------------------------------------------
import qualified System.IO as IO


--------------------------------------------------------------------------------
#if defined(mingw32_HOST_OS) || defined(__MINGW32__)
import Data.Char (chr)
import Foreign.C.Types (CInt)
#endif


--------------------------------------------------------------------------------
initialize :: IO ()
initialize = IO.hSetBuffering IO.stdin IO.NoBuffering


--------------------------------------------------------------------------------
#if defined(mingw32_HOST_OS) || defined(__MINGW32__)
foreign import ccall unsafe "conio.h getch" win_getch :: IO CInt
#endif


--------------------------------------------------------------------------------
getKeyCode :: IO Char
#if defined(mingw32_HOST_OS) || defined(__MINGW32__)
getKeyCode = fmap (chr . fromEnum) win_getch
#else
getKeyCode = getChar
#endif


--------------------------------------------------------------------------------
getKey :: IO String
getKey = do
c0 <- getKeyCode
case c0 of
'\ESC' -> do
c1 <- getKeyCode
case c1 of
'[' -> do
c2 <- getKeyCode
return [c0, c1, c2]
_ -> return [c0, c1]
_ -> return [c0]
16 changes: 2 additions & 14 deletions src/Patat/Presentation/Interactive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Patat.Presentation.Interactive


--------------------------------------------------------------------------------
import qualified Patat.GetKey as GetKey
import Patat.Presentation.Internal
import Patat.Presentation.Read

Expand All @@ -32,7 +33,7 @@ data PresentationCommand
--------------------------------------------------------------------------------
readPresentationCommand :: IO PresentationCommand
readPresentationCommand = do
k <- readKey
k <- GetKey.getKey
case k of
"q" -> return Exit
"\n" -> return Forward
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried applying these changes over the most recent version in hopes of getting it to work on Windows; testing this out on the first 01.md, it didn't appear to do anything differently. Advancing worked fine, but it was impossible to move backwards on slides.

However, commenting out this particular line and freeing \n seems to resolve some kind of fight between the windows console and getChar, allowing you to write a command and then flush the buffer with enter, making patat usable for me on W10 1809.

Expand All @@ -49,19 +50,6 @@ readPresentationCommand = do
"G" -> return Last
"r" -> return Reload
_ -> readPresentationCommand
where
readKey :: IO String
readKey = do
c0 <- getChar
case c0 of
'\ESC' -> do
c1 <- getChar
case c1 of
'[' -> do
c2 <- getChar
return [c0, c1, c2]
_ -> return [c0, c1]
_ -> return [c0]


--------------------------------------------------------------------------------
Expand Down