Skip to content

Commit

Permalink
rework Tb_output_mode a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellwrosen committed Nov 15, 2023
1 parent c3a6222 commit 7d51d35
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 30 deletions.
1 change: 1 addition & 0 deletions termbox-bindings-hs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Rework `Tb_attr` and `Tb_color` into `Tb_color_and_attrs`
- Rework `Tb_select_input_mode` and `Tb_input_mode`
- Rework `Tb_select_output_mode` and `Tb_output_mode`
- Make `mod` field of `Tb_event` a `Maybe Tb_event_mod` rather than a `Tb_event_mod`
- Make `Show` instance of `Tb_key` not call `error` on unknown keys
- Hide representation of `Tb_event_mod`
Expand Down
11 changes: 8 additions & 3 deletions termbox-bindings-hs/src/Termbox/Bindings/Hs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ module Termbox.Bindings.Hs
tb_init_file,
tb_shutdown,

-- ** Get or set input or output mode
-- ** Get or set input mode
tb_get_input_mode,
tb_select_input_mode,

-- ** Get or set output mode
tb_get_output_mode,
tb_select_output_mode,

-- ** Get terminal dimensions
Expand Down Expand Up @@ -148,8 +152,7 @@ module Termbox.Bindings.Hs
TB_KEY_TAB
),
Tb_output_mode
( TB_OUTPUT_CURRENT,
TB_OUTPUT_216,
( TB_OUTPUT_216,
TB_OUTPUT_256,
TB_OUTPUT_GRAYSCALE,
TB_OUTPUT_NORMAL
Expand Down Expand Up @@ -179,6 +182,8 @@ import Termbox.Bindings.Hs.Internal.EventMod (Tb_event_mod (..))
import Termbox.Bindings.Hs.Internal.EventType (Tb_event_type (..))
import Termbox.Bindings.Hs.Internal.Functions
( tb_change_cell,
tb_get_input_mode,
tb_get_output_mode,
tb_height,
tb_init,
tb_init_fd,
Expand Down
18 changes: 12 additions & 6 deletions termbox-bindings-hs/src/Termbox/Bindings/Hs/Internal/Functions.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Termbox.Bindings.Hs.Internal.Functions
( tb_change_cell,
tb_get_input_mode,
tb_get_output_mode,
tb_height,
tb_init,
tb_init_fd,
Expand Down Expand Up @@ -53,6 +54,11 @@ tb_get_input_mode :: IO Tb_input_mode
tb_get_input_mode =
coerce Termbox.tb_select_input_mode Termbox._TB_INPUT_CURRENT

-- | Get the output mode.
tb_get_output_mode :: IO Tb_output_mode
tb_get_output_mode =
coerce Termbox.tb_select_output_mode Termbox._TB_OUTPUT_CURRENT

-- | Get the terminal height.
tb_height :: IO Int
tb_height =
Expand Down Expand Up @@ -128,13 +134,13 @@ tb_put_cell cx cy cell =

-- | Set the input mode.
tb_select_input_mode :: Tb_input_mode -> IO ()
tb_select_input_mode =
void . coerce @(CInt -> IO CInt) @(Tb_input_mode -> IO Tb_input_mode) Termbox.tb_select_input_mode
tb_select_input_mode (Tb_input_mode mode) =
void (Termbox.tb_select_input_mode mode)

-- | Get or set the output mode.
tb_select_output_mode :: Tb_output_mode -> IO Tb_output_mode
tb_select_output_mode =
coerce Termbox.tb_select_output_mode
-- | Set the output mode.
tb_select_output_mode :: Tb_output_mode -> IO ()
tb_select_output_mode (Tb_output_mode mode) =
void (Termbox.tb_select_output_mode mode)

-- | Set the foreground and background attributes that 'tb_clear' clears the back buffer with.
tb_set_clear_attributes ::
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import Termbox.Bindings.C (_TB_EFAILED_TO_OPEN_TTY, _TB_EPIPE_TRAP_ERROR, _TB_EU
-- | A 'tb_init' error.
newtype Tb_init_error
= Tb_init_error CInt
deriving stock (Eq)
deriving anyclass (Exception)
deriving stock (Eq)

instance Show Tb_init_error where
show = \case
Expand Down
34 changes: 16 additions & 18 deletions termbox-bindings-hs/src/Termbox/Bindings/Hs/Internal/OutputMode.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Termbox.Bindings.Hs.Internal.OutputMode
( Tb_output_mode
( Tb_output_mode,
TB_OUTPUT_CURRENT,
TB_OUTPUT_216,
TB_OUTPUT_256,
TB_OUTPUT_GRAYSCALE,
Expand All @@ -11,7 +10,12 @@ module Termbox.Bindings.Hs.Internal.OutputMode
where

import Foreign.C.Types (CInt)
import qualified Termbox.Bindings.C
import Termbox.Bindings.C
( _TB_OUTPUT_216,
_TB_OUTPUT_256,
_TB_OUTPUT_GRAYSCALE,
_TB_OUTPUT_NORMAL,
)

-- | The output mode.
newtype Tb_output_mode
Expand All @@ -20,40 +24,34 @@ newtype Tb_output_mode

instance Show Tb_output_mode where
show = \case
TB_OUTPUT_CURRENT -> "TB_OUTPUT_CURRENT"
TB_OUTPUT_216 -> "TB_OUTPUT_216"
TB_OUTPUT_256 -> "TB_OUTPUT_256"
TB_OUTPUT_GRAYSCALE -> "TB_OUTPUT_GRAYSCALE"
TB_OUTPUT_NORMAL -> "TB_OUTPUT_NORMAL"

pattern TB_OUTPUT_CURRENT :: Tb_output_mode
pattern TB_OUTPUT_CURRENT <-
((== Tb_output_mode Termbox.Bindings.C._TB_OUTPUT_CURRENT) -> True)
where
TB_OUTPUT_CURRENT = Tb_output_mode Termbox.Bindings.C._TB_OUTPUT_CURRENT

pattern TB_OUTPUT_216 :: Tb_output_mode
pattern TB_OUTPUT_216 <-
((== Tb_output_mode Termbox.Bindings.C._TB_OUTPUT_216) -> True)
((== Tb_output_mode _TB_OUTPUT_216) -> True)
where
TB_OUTPUT_216 = Tb_output_mode Termbox.Bindings.C._TB_OUTPUT_216
TB_OUTPUT_216 = Tb_output_mode _TB_OUTPUT_216

pattern TB_OUTPUT_256 :: Tb_output_mode
pattern TB_OUTPUT_256 <-
((== Tb_output_mode Termbox.Bindings.C._TB_OUTPUT_256) -> True)
((== Tb_output_mode _TB_OUTPUT_256) -> True)
where
TB_OUTPUT_256 = Tb_output_mode Termbox.Bindings.C._TB_OUTPUT_256
TB_OUTPUT_256 = Tb_output_mode _TB_OUTPUT_256

pattern TB_OUTPUT_GRAYSCALE :: Tb_output_mode
pattern TB_OUTPUT_GRAYSCALE <-
((== Tb_output_mode Termbox.Bindings.C._TB_OUTPUT_GRAYSCALE) -> True)
((== Tb_output_mode _TB_OUTPUT_GRAYSCALE) -> True)
where
TB_OUTPUT_GRAYSCALE = Tb_output_mode Termbox.Bindings.C._TB_OUTPUT_GRAYSCALE
TB_OUTPUT_GRAYSCALE = Tb_output_mode _TB_OUTPUT_GRAYSCALE

pattern TB_OUTPUT_NORMAL :: Tb_output_mode
pattern TB_OUTPUT_NORMAL <-
((== Tb_output_mode Termbox.Bindings.C._TB_OUTPUT_NORMAL) -> True)
((== Tb_output_mode _TB_OUTPUT_NORMAL) -> True)
where
TB_OUTPUT_NORMAL = Tb_output_mode Termbox.Bindings.C._TB_OUTPUT_NORMAL
TB_OUTPUT_NORMAL = Tb_output_mode _TB_OUTPUT_NORMAL

{-# COMPLETE TB_OUTPUT_CURRENT, TB_OUTPUT_216, TB_OUTPUT_256, TB_OUTPUT_GRAYSCALE, TB_OUTPUT_NORMAL #-}
-- N.B. This requires Tb_output_mode to remain abstract
{-# COMPLETE TB_OUTPUT_216, TB_OUTPUT_256, TB_OUTPUT_GRAYSCALE, TB_OUTPUT_NORMAL #-}
4 changes: 2 additions & 2 deletions termbox/src/Termbox/Internal/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ initialize =
TB_EUNSUPPORTED_TERMINAL -> UnsupportedTerminal
Right () -> do
tb_select_input_mode _TB_INPUT_MOUSE
_ <- tb_select_output_mode TB_OUTPUT_256
tb_select_output_mode TB_OUTPUT_256
pure (Right ())

-- | Shut down a @termbox@ program.
finalize :: IO ()
finalize = do
_ <- tb_select_output_mode TB_OUTPUT_NORMAL
tb_select_output_mode TB_OUTPUT_NORMAL
tb_shutdown

0 comments on commit 7d51d35

Please sign in to comment.