Skip to content

Commit

Permalink
input/output modes
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellwrosen committed Nov 12, 2023
1 parent 40d705b commit c8b40e9
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 28 deletions.
25 changes: 16 additions & 9 deletions termbox2-bindings-hs/src/Termbox2/Bindings/Hs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ module Termbox2.Bindings.Hs
TB_ERR_UNSUPPORTED_TERM
),
-- Tb_event (..),
Tb_input_mode
( Tb_input_mode,
TB_INPUT_ALT,
TB_INPUT_ESC,
TB_INPUT_MOUSE
),
Tb_key
( Tb_key,
TB_KEY_ARROW_DOWN,
Expand Down Expand Up @@ -170,6 +176,14 @@ module Termbox2.Bindings.Hs
TB_KEY_SPACE,
TB_KEY_TAB
),
Tb_output_mode
( Tb_output_mode,
TB_OUTPUT_216,
TB_OUTPUT_256,
TB_OUTPUT_GRAYSCALE,
TB_OUTPUT_NORMAL,
TB_OUTPUT_TRUECOLOR
),

-- ** Colors

Expand Down Expand Up @@ -218,15 +232,6 @@ module Termbox2.Bindings.Hs
-- _TB_INPUT_ESC,
-- _TB_INPUT_MOUSE,

-- ** Output modes

-- _TB_OUTPUT_CURRENT,
-- _TB_OUTPUT_216,
-- _TB_OUTPUT_256,
-- _TB_OUTPUT_GRAYSCALE,
-- _TB_OUTPUT_NORMAL,
-- _TB_OUTPUT_TRUECOLOR,

-- ** Function types
)
where
Expand Down Expand Up @@ -267,4 +272,6 @@ import Termbox2.Bindings.Hs.Internal.Functions
tb_strerror,
tb_width,
)
import Termbox2.Bindings.Hs.Internal.InputMode (Tb_input_mode (..))
import Termbox2.Bindings.Hs.Internal.Key (Tb_key (..))
import Termbox2.Bindings.Hs.Internal.OutputMode (Tb_output_mode (..))
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import Termbox2.Bindings.C qualified as Termbox
import Termbox2.Bindings.Hs.Internal.Attr (Tb_attr (Tb_attr))
import Termbox2.Bindings.Hs.Internal.Error (Tb_error (Tb_error))
import Termbox2.Bindings.Hs.Internal.InputMode (Tb_input_mode (Tb_input_mode))
import Termbox2.Bindings.Hs.Internal.OutputMode (Tb_output_mode (Tb_output_mode))
import Termbox2.Bindings.Hs.Internal.Prelude

-- | Clear the back buffer.
Expand Down Expand Up @@ -86,9 +87,13 @@ tb_get_input_mode = do
else Right (Tb_input_mode n)

-- | Get the output mode.
-- tb_get_output_mode :: IO (Either Tb_error Tb_output_mode)
tb_get_output_mode :: ()
tb_get_output_mode = ()
tb_get_output_mode :: IO (Either Tb_error Tb_output_mode)
tb_get_output_mode = do
n <- Termbox.tb_set_output_mode Termbox._TB_OUTPUT_CURRENT
pure
if n < 0
then Left (Tb_error n)
else Right (Tb_output_mode n)

-- | Get the terminal height.
tb_height :: IO (Either Tb_error Int)
Expand Down Expand Up @@ -225,10 +230,10 @@ tb_set_input_mode :: Tb_input_mode -> IO (Either Tb_error ())
tb_set_input_mode (Tb_input_mode mode) =
check (Termbox.tb_set_input_mode mode)

-- | Get or set the output mode.
-- tb_set_output_mode :: CInt -> IO CInt
tb_set_output_mode :: ()
tb_set_output_mode = ()
-- | Get the output mode.
tb_set_output_mode :: Tb_output_mode -> IO (Either Tb_error ())
tb_set_output_mode (Tb_output_mode mode) =
check (Termbox.tb_set_output_mode mode)

-- | Shutdown the @termbox@ library.
tb_shutdown :: IO (Either Tb_error ())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,17 @@ module Termbox2.Bindings.Hs.Internal.InputMode
where

import Data.Bits ((.|.))
import Data.Coerce (coerce)
import Foreign.C.Types (CInt)
import Termbox2.Bindings.C

-- | The input mode.
newtype Tb_input_mode
= Tb_input_mode CInt
deriving stock (Eq)

instance Show Tb_input_mode where
show mode
| mode == TB_INPUT_ALT = "TB_INPUT_ALT"
| mode == TB_INPUT_ESC = "TB_INPUT_ESC"
| mode == TB_INPUT_MOUSE = "TB_INPUT_MOUSE"
| mode == TB_INPUT_ALT <> TB_INPUT_MOUSE = "TB_INPUT_ALT <> TB_INPUT_MOUSE"
| mode == TB_INPUT_ESC <> TB_INPUT_MOUSE = "TB_INPUT_ESC <> TB_INPUT_MOUSE"
| otherwise = "Tb_input_mode " ++ show mode
deriving stock (Eq, Show)

instance Semigroup Tb_input_mode where
Tb_input_mode x <> Tb_input_mode y =
Tb_input_mode (x .|. y)
(<>) = coerce ((.|.) :: CInt -> CInt -> CInt)

pattern TB_INPUT_ALT :: Tb_input_mode
pattern TB_INPUT_ALT <-
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module Termbox2.Bindings.Hs.Internal.OutputMode
( Tb_output_mode
( Tb_output_mode,
TB_OUTPUT_216,
TB_OUTPUT_256,
TB_OUTPUT_GRAYSCALE,
TB_OUTPUT_NORMAL,
TB_OUTPUT_TRUECOLOR
),
)
where

import Foreign.C.Types (CInt)
import Termbox2.Bindings.C

-- | The output mode.
newtype Tb_output_mode
= Tb_output_mode CInt
deriving stock (Eq)

instance Show Tb_output_mode where
show = \case
TB_OUTPUT_216 -> "TB_OUTPUT_216"
TB_OUTPUT_256 -> "TB_OUTPUT_256"
TB_OUTPUT_GRAYSCALE -> "TB_OUTPUT_GRAYSCALE"
TB_OUTPUT_NORMAL -> "TB_OUTPUT_NORMAL"
TB_OUTPUT_TRUECOLOR -> "TB_OUTPUT_TRUECOLOR"
Tb_output_mode mode -> show ("Tb_output_mode " ++ show mode)

pattern TB_OUTPUT_216 :: Tb_output_mode
pattern TB_OUTPUT_216 <-
((== Tb_output_mode _TB_OUTPUT_216) -> True)
where
TB_OUTPUT_216 = Tb_output_mode _TB_OUTPUT_216

pattern TB_OUTPUT_256 :: Tb_output_mode
pattern TB_OUTPUT_256 <-
((== Tb_output_mode _TB_OUTPUT_256) -> True)
where
TB_OUTPUT_256 = Tb_output_mode _TB_OUTPUT_256

pattern TB_OUTPUT_GRAYSCALE :: Tb_output_mode
pattern TB_OUTPUT_GRAYSCALE <-
((== Tb_output_mode _TB_OUTPUT_GRAYSCALE) -> True)
where
TB_OUTPUT_GRAYSCALE = Tb_output_mode _TB_OUTPUT_GRAYSCALE

pattern TB_OUTPUT_NORMAL :: Tb_output_mode
pattern TB_OUTPUT_NORMAL <-
((== Tb_output_mode _TB_OUTPUT_NORMAL) -> True)
where
TB_OUTPUT_NORMAL = Tb_output_mode _TB_OUTPUT_NORMAL

pattern TB_OUTPUT_TRUECOLOR :: Tb_output_mode
pattern TB_OUTPUT_TRUECOLOR <-
((== Tb_output_mode _TB_OUTPUT_TRUECOLOR) -> True)
where
TB_OUTPUT_TRUECOLOR = Tb_output_mode _TB_OUTPUT_TRUECOLOR
1 change: 1 addition & 0 deletions termbox2-bindings-hs/termbox2-bindings-hs.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ library
Termbox2.Bindings.Hs.Internal.Functions
Termbox2.Bindings.Hs.Internal.InputMode
Termbox2.Bindings.Hs.Internal.Key
Termbox2.Bindings.Hs.Internal.OutputMode
Termbox2.Bindings.Hs.Internal.Prelude

-- executable termbox2-bindings-hs-example-demo
Expand Down

0 comments on commit c8b40e9

Please sign in to comment.