mirror of
https://github.com/ilyakooo0/vty.git
synced 2024-12-01 14:36:27 +03:00
Use GHC Handles instead of POSIX Fds for output
Ignore-this: bc16788a651ce4c7b5229b80e6cefe83 darcs-hash:20100907180247-f0a0d-05cdfbb6ae1d7b976a7bd75b4265d5596e659163.gz
This commit is contained in:
parent
fddf5c977a
commit
d5c51d264f
@ -26,6 +26,8 @@ import Foreign.Marshal.Array ( peekArray )
|
||||
import Foreign.Ptr ( plusPtr )
|
||||
import Foreign.Storable ( poke )
|
||||
|
||||
import System.IO
|
||||
|
||||
import Unsafe.Coerce
|
||||
|
||||
-- | The debug display terminal produces a string representation of the requested picture. There is
|
||||
@ -62,6 +64,8 @@ instance Terminal DebugTerminal where
|
||||
>>= return . UTF8.fromRep . BSCore.pack
|
||||
>>= writeIORef (debug_terminal_last_output t)
|
||||
|
||||
output_handle t = return stdout
|
||||
|
||||
data DebugDisplay = DebugDisplay
|
||||
{ debug_display_bounds :: DisplayRegion
|
||||
}
|
||||
|
@ -26,6 +26,8 @@ import Data.Foldable
|
||||
import Data.IORef
|
||||
import Data.String.UTF8 hiding ( foldl )
|
||||
|
||||
import System.IO
|
||||
|
||||
data TerminalHandle where
|
||||
TerminalHandle :: Terminal t => t -> IORef TerminalState -> TerminalHandle
|
||||
|
||||
@ -82,6 +84,9 @@ class Terminal t where
|
||||
-- end_ptr - start_ptr
|
||||
output_byte_buffer :: t -> OutputBuffer -> Word -> IO ()
|
||||
|
||||
-- | Handle of output device
|
||||
output_handle :: t -> IO Handle
|
||||
|
||||
instance Terminal TerminalHandle where
|
||||
terminal_ID (TerminalHandle t _) = terminal_ID t
|
||||
release_terminal (TerminalHandle t _) = release_terminal t
|
||||
@ -90,6 +95,7 @@ instance Terminal TerminalHandle where
|
||||
display_bounds (TerminalHandle t _) = display_bounds t
|
||||
display_terminal_instance (TerminalHandle t _) = display_terminal_instance t
|
||||
output_byte_buffer (TerminalHandle t _) = output_byte_buffer t
|
||||
output_handle (TerminalHandle t _) = output_handle t
|
||||
|
||||
data DisplayHandle where
|
||||
DisplayHandle :: DisplayTerminal d => d -> TerminalHandle -> DisplayState -> DisplayHandle
|
||||
|
@ -70,6 +70,8 @@ instance Terminal Term where
|
||||
|
||||
output_byte_buffer t = output_byte_buffer (super_term t)
|
||||
|
||||
output_handle t = output_handle (super_term t)
|
||||
|
||||
data DisplayContext = DisplayContext
|
||||
{ super_display :: DisplayHandle
|
||||
}
|
||||
|
@ -26,10 +26,10 @@ import Data.Word
|
||||
import Foreign.Ptr
|
||||
import Foreign.C.Types ( CLong, CSize, CInt )
|
||||
|
||||
import GHC.Handle
|
||||
|
||||
import qualified System.Console.Terminfo as Terminfo
|
||||
import System.IO
|
||||
import System.Posix.IO
|
||||
import System.Posix.Types ( Fd(..) )
|
||||
|
||||
data Term = Term
|
||||
{ term_info_ID :: String
|
||||
@ -44,7 +44,7 @@ data Term = Term
|
||||
, set_default_attr :: CapExpression
|
||||
, clear_screen :: CapExpression
|
||||
, display_attr_caps :: DisplayAttrCaps
|
||||
, term_fd :: Fd
|
||||
, term_handle :: Handle
|
||||
}
|
||||
|
||||
data DisplayAttrCaps = DisplayAttrCaps
|
||||
@ -93,7 +93,7 @@ terminal_instance in_ID = do
|
||||
case parse_result of
|
||||
Left e -> fail $ show e
|
||||
Right cap -> return $ Just cap
|
||||
the_fd <- liftIO $ dup stdOutput
|
||||
the_handle <- liftIO $ hDuplicate stdout
|
||||
pure Term
|
||||
<*> pure in_ID
|
||||
<*> pure ti
|
||||
@ -107,7 +107,7 @@ terminal_instance in_ID = do
|
||||
<*> require_cap "sgr0"
|
||||
<*> require_cap "clear"
|
||||
<*> current_display_attr_caps ti
|
||||
<*> pure the_fd
|
||||
<*> pure the_handle
|
||||
|
||||
current_display_attr_caps :: ( Applicative m, MonadIO m )
|
||||
=> Terminfo.Terminal
|
||||
@ -137,7 +137,7 @@ instance Terminal Term where
|
||||
release_terminal t = do
|
||||
liftIO $ marshall_cap_to_terminal t set_default_attr []
|
||||
liftIO $ marshall_cap_to_terminal t cnorm []
|
||||
liftIO $ closeFd $ term_fd t
|
||||
liftIO $ hClose $ term_handle t
|
||||
return ()
|
||||
|
||||
reserve_display t = do
|
||||
@ -177,13 +177,12 @@ instance Terminal Term where
|
||||
-- flush is required *before* the c_output_byte_buffer call
|
||||
-- otherwise there may still be data in GHC's internal stdout buffer.
|
||||
-- _ <- handleToFd stdout
|
||||
let Fd c_fd = term_fd t
|
||||
if out_byte_count /= 0
|
||||
then c_output_byte_buffer c_fd out_ptr ( toEnum $! fromEnum out_byte_count )
|
||||
else return ()
|
||||
hPutBuf (term_handle t) out_ptr (fromEnum out_byte_count)
|
||||
|
||||
foreign import ccall unsafe "output_buffer.h stdout_output_buffer" c_output_byte_buffer
|
||||
:: CInt -> Ptr Word8 -> CSize -> IO ()
|
||||
output_handle t = return (term_handle t)
|
||||
|
||||
-- foreign import ccall unsafe "output_buffer.h stdout_output_buffer" c_output_byte_buffer
|
||||
-- :: CInt -> Ptr Word8 -> CSize -> IO ()
|
||||
foreign import ccall "gwinsz.h c_get_window_size" c_get_window_size
|
||||
:: IO CLong
|
||||
-- foreign import ccall "fdatasync" c_fdatasync
|
||||
|
@ -63,6 +63,8 @@ instance Terminal XTermColor where
|
||||
|
||||
output_byte_buffer t = output_byte_buffer (super_term t)
|
||||
|
||||
output_handle t = output_handle (super_term t)
|
||||
|
||||
data DisplayContext = DisplayContext
|
||||
{ super_display :: DisplayHandle
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user