packages feed

sixel 0.1.2.1 → 0.1.2.2

raw patch · 7 files changed

+196/−45 lines, 7 filesdep +base64-bytestringbinary-addedPVP: major bump suggested

API removals or changes: PVP suggests a major version bump

Dependencies added: base64-bytestring

API changes (from Hackage documentation)

- Data.Sixel: c_bufsize :: CInt -> CInt -> IO CInt
- Data.Sixel: c_img2sixel :: Ptr () -> Ptr () -> CInt -> CInt -> IO CInt
- Data.Sixel: instance Data.Sixel.ToSixel Data.Sixel.LatexStr
- Data.Sixel: instance GHC.Classes.Eq Data.Sixel.LatexStr
- Data.Sixel: instance GHC.Show.Show Data.Sixel.LatexStr
- Data.Sixel: latexStr :: String -> Float -> String
+ Data.OSC1337: Align :: String -> OSCCmd
+ Data.OSC1337: End :: OSCCmd
+ Data.OSC1337: FileName :: String -> OSCCmd
+ Data.OSC1337: Height :: Int -> OSCCmd
+ Data.OSC1337: ImageDat :: String -> OSCCmd
+ Data.OSC1337: Inline :: Int -> OSCCmd
+ Data.OSC1337: LatexStr :: String -> Float -> LatexStr
+ Data.OSC1337: MimeType :: String -> OSCCmd
+ Data.OSC1337: OSCImage :: String -> OSCImage
+ Data.OSC1337: PreserveAspectRatio :: Int -> OSCCmd
+ Data.OSC1337: Size :: Int -> OSCCmd
+ Data.OSC1337: Start :: OSCCmd
+ Data.OSC1337: Width :: Int -> OSCCmd
+ Data.OSC1337: [strSize] :: LatexStr -> Float
+ Data.OSC1337: [toLatexStr] :: LatexStr -> String
+ Data.OSC1337: [toOSCString] :: OSCImage -> String
+ Data.OSC1337: class ToOSC a
+ Data.OSC1337: data LatexStr
+ Data.OSC1337: data OSCCmd
+ Data.OSC1337: img2osc :: Image PixelRGB8 -> ByteString
+ Data.OSC1337: instance Data.OSC1337.ToOSC (Codec.Picture.Types.Image Codec.Picture.Types.PixelRGB8)
+ Data.OSC1337: instance Data.OSC1337.ToOSC Codec.Picture.Types.DynamicImage
+ Data.OSC1337: instance Data.OSC1337.ToOSC Data.Sixel.Internal.LatexStr
+ Data.OSC1337: instance GHC.Classes.Eq Data.OSC1337.OSCCmd
+ Data.OSC1337: instance GHC.Classes.Eq Data.OSC1337.OSCImage
+ Data.OSC1337: instance GHC.Show.Show Data.OSC1337.OSCCmd
+ Data.OSC1337: instance GHC.Show.Show Data.OSC1337.OSCImage
+ Data.OSC1337: instance GHC.Show.Show Data.Sixel.Internal.LatexStr
+ Data.OSC1337: instance GHC.Show.Show [Data.OSC1337.OSCCmd]
+ Data.OSC1337: instance GHC.Show.Show a => Data.OSC1337.ToOSC a
+ Data.OSC1337: latex :: String -> LatexStr
+ Data.OSC1337: math :: String -> LatexStr
+ Data.OSC1337: newtype OSCImage
+ Data.OSC1337: putImage :: FilePath -> IO ()
+ Data.OSC1337: putOSC :: ToOSC a => a -> IO ()
+ Data.OSC1337: toOSC :: ToOSC a => a -> OSCImage
+ Data.Sixel: instance Data.Sixel.ToSixel Data.Sixel.Internal.LatexStr
+ Data.Sixel: instance GHC.Show.Show Data.Sixel.Internal.LatexStr
+ Data.Sixel.Internal: LatexStr :: String -> Float -> LatexStr
+ Data.Sixel.Internal: [strSize] :: LatexStr -> Float
+ Data.Sixel.Internal: [toLatexStr] :: LatexStr -> String
+ Data.Sixel.Internal: c_bufsize :: CInt -> CInt -> IO CInt
+ Data.Sixel.Internal: c_img2sixel :: Ptr () -> Ptr () -> CInt -> CInt -> IO CInt
+ Data.Sixel.Internal: data LatexStr
+ Data.Sixel.Internal: instance GHC.Classes.Eq Data.Sixel.Internal.LatexStr
+ Data.Sixel.Internal: latex :: String -> LatexStr
+ Data.Sixel.Internal: latex2img :: LatexStr -> IO (Either String DynamicImage)
+ Data.Sixel.Internal: latexStr :: String -> Float -> String
+ Data.Sixel.Internal: math :: String -> LatexStr

Files

CHANGELOG.md view
@@ -11,3 +11,7 @@ ## 0.1.2.1 -- 2020-05-20  * Fix overflow of parsing number++## 0.1.2.2 -- 2020-05-22++* Support for OSC1337
README.md view
@@ -17,9 +17,19 @@  ![demo](https://raw.githubusercontent.com/junjihashimoto/sixel/master/demo.png) +# Terminal requirements +For using sixel, see terminal requirements of [libsixel](https://saitoha.github.io/libsixel).++This library supports OSC-1337, too.+OSC-1337 is available for iterm2 and [hterm of crostini](https://chromium.googlesource.com/apps/libapps/+/master/hterm).++![demo](https://raw.githubusercontent.com/junjihashimoto/sixel/master/demo-osc1337.png)+ # References  * https://en.wikipedia.org/wiki/Sixel * https://saitoha.github.io/libsixel+* https://chromium.googlesource.com/apps/libapps/+/master/hterm/doc/ControlSequences.md#OSC-1337+* https://www.iterm2.com/documentation-escape-codes.html 
+ demo-osc1337.png view

binary file changed (absent → 57202 bytes)

sixel.cabal view
@@ -1,6 +1,6 @@ cabal-version:       >=1.10 name:                sixel-version:             0.1.2.1+version:             0.1.2.2 synopsis:            Sixel library to show images in a terminal emulator description:         Sixel can show graphics on a terminal emulator. This library is developed to showing images on ghci. license:             BSD3@@ -13,10 +13,13 @@ build-type:          Simple extra-source-files:  CHANGELOG.md                    , demo.png+                   , demo-osc1337.png                    , README.md  library   exposed-modules:     Data.Sixel+                     , Data.Sixel.Internal+                     , Data.OSC1337   hs-source-dirs:      src   default-language:    Haskell2010   build-depends:       base >= 4.7 && < 5@@ -25,6 +28,7 @@                      , bytestring                      , process                      , temporary+                     , base64-bytestring   c-sources:           csrc/sixel.c  executable sixel-exe
+ src/Data/OSC1337.hs view
@@ -0,0 +1,105 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE LambdaCase #-}+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE UndecidableInstances #-}++module Data.OSC1337+  ( module Data.OSC1337,+    LatexStr (..),+    latex,+    math,+  )+where++import Codec.Picture+import Codec.Picture.Png (encodePng)+import qualified Data.ByteString as B+import Data.ByteString (ByteString)+import Data.ByteString.Base64 (encode)+import qualified Data.ByteString.Char8 as BC+import Data.ByteString.Lazy (toStrict)+import Data.Sixel.Internal+import System.IO.Unsafe (unsafePerformIO)++data OSCCmd+  = Start+  | End+  | FileName String+  | Size Int+  | Width Int+  | Height Int+  | PreserveAspectRatio Int+  | Inline Int+  | Align String+  | MimeType String+  | ImageDat String+  deriving (Eq)++instance Show OSCCmd where+  show = \case+    Start -> "\ESC]1337;"+    End -> "\a"+    Size s -> "size=" ++ show s ++ ";"+    Width s -> "width=" ++ show s ++ ";"+    Height s -> "height=" ++ show s ++ ";"+    PreserveAspectRatio s -> "preserveAspectRatio=" ++ show s ++ ";"+    Inline s -> "inline=" ++ show s ++ ";"+    Align s -> "align=" ++ s ++ ";"+    MimeType s -> "type=" ++ s ++ ";"+    ImageDat s -> BC.unpack $ encode $ BC.pack s++instance {-# OVERLAPS #-} Show [OSCCmd] where+  show xs = concat $ map show xs++newtype OSCImage = OSCImage {toOSCString :: String} deriving (Eq)++instance Show OSCImage where+  show (OSCImage img) = img++-- | See https://chromium.googlesource.com/apps/libapps/+/master/hterm/doc/ControlSequences.md#OSC-1337+class ToOSC a where+  toOSC :: a -> OSCImage+  putOSC :: a -> IO ()++instance {-# OVERLAPS #-} (Show a) => ToOSC a where+  toOSC xs = OSCImage $ show xs+  putOSC xs = putStrLn $ show xs++img2osc :: Image PixelRGB8 -> ByteString+img2osc img =+  let (Image w h _) = img+      dat = toStrict $ encodePng img+   in B.concat+        [ "\ESC]1337;File=name=",+          encode "display.png",+          ";",+          "width=" <> BC.pack (show w) <> "px;",+          "height=" <> BC.pack (show h) <> "px;",+          "inline=1;:",+          encode dat,+          "\a"+        ]++instance {-# OVERLAPS #-} ToOSC DynamicImage where+  toOSC dimg = toOSC $ convertRGB8 dimg+  putOSC img = putOSC $ convertRGB8 img++instance {-# OVERLAPS #-} ToOSC (Image PixelRGB8) where+  toOSC img = OSCImage (BC.unpack $ img2osc img)+  putOSC img = B.putStr $ img2osc img++putImage :: FilePath -> IO ()+putImage file = do+  readImage file >>= \case+    Left err -> print err+    Right img -> putOSC img++instance Show LatexStr where+  show str = show $ toOSC str++instance ToOSC LatexStr where+  toOSC str = unsafePerformIO $ do+    latex2img str >>= \case+      Left err -> error err+      Right img -> return $ toOSC img+  putOSC img = putStr $ show $ toOSC img
src/Data/Sixel.hs view
@@ -1,14 +1,21 @@ {-# LANGUAGE FlexibleInstances #-}-{-# LANGUAGE UndecidableInstances #-} {-# LANGUAGE LambdaCase #-}+{-# LANGUAGE UndecidableInstances #-} -module Data.Sixel where+module Data.Sixel+  ( module Data.Sixel,+    LatexStr (..),+    latex,+    math,+  )+where  import Codec.Picture import Data.ByteString (ByteString) import qualified Data.ByteString.Char8 as BC import qualified Data.ByteString.Internal as B import Data.Char (chr)+import Data.Sixel.Internal import qualified Data.Vector.Storable as V import Data.Word (Word8) import Foreign.C.String@@ -19,25 +26,8 @@ import System.IO.Unsafe (unsafePerformIO) import System.Process (readProcessWithExitCode) -foreign import ccall "bufsize" c_bufsize :: CInt -> CInt -> IO CInt--foreign import ccall "img2sixel" c_img2sixel :: Ptr () -> Ptr () -> CInt -> CInt -> IO CInt- newtype SixelImage = SixelImage {toSixelString :: String} deriving (Eq) -data LatexStr-  = LatexStr-      { toLatexStr :: String,-        strSize :: Float-      }-  deriving (Eq)--latex :: String -> LatexStr-latex str = LatexStr str 2.5--math :: String -> LatexStr-math str = LatexStr ("$"++str++"$") 2.5- instance Show SixelImage where   show (SixelImage img) = img @@ -95,7 +85,7 @@  instance {-# OVERLAPS #-} ToSixel DynamicImage where   toSixel dimg = toSixel $ convertRGB8 dimg-  putSixel img = BC.putStr $ img2sixel $ convertRGB8 img+  putSixel img = putSixel $ convertRGB8 img  instance {-# OVERLAPS #-} ToSixel (Image PixelRGB8) where   toSixel img = SixelImage (BC.unpack $ img2sixel img)@@ -105,31 +95,11 @@   toSixel = id   putSixel img = putStr $ show img -latexStr :: String -> Float -> String-latexStr str size =-  "\\documentclass[border=2pt]{standalone}"-    ++ "\\usepackage{amsmath}"-    ++ "\\usepackage{graphicx}"-    ++ "\\usepackage{varwidth}"-    ++ "\\begin{document}"-    ++ "\\begin{varwidth}{\\linewidth}"-    ++ "\\scalebox{"-    ++ show size-    ++ "}{"-    ++ str-    ++ "}"-    ++ "\\end{varwidth}"-    ++ "\\end{document}"- instance ToSixel LatexStr where-  toSixel (LatexStr str size) = unsafePerformIO $ do-    withSystemTempDirectory "sixel" $ \dir -> do-      writeFile (dir ++ "/sixel.tex") (latexStr str size)-      (_,outlog,errlog) <- readProcessWithExitCode "pdflatex" ["-output-directory="++dir ,dir ++ "/sixel.tex"] ""-      readProcessWithExitCode "convert" [dir ++ "/sixel.pdf", "-quality", "90", dir ++ "/sixel.png"] ""-      readImage (dir ++ "/sixel.png") >>= \case-        Left err -> error $ "can not read sixel.png. // " ++ errlog ++ " // " ++ outlog-        Right img -> return $ toSixel img+  toSixel str = unsafePerformIO $ do+    latex2img str >>= \case+      Left err -> error err+      Right img -> return $ toSixel img   putSixel img = putStr $ show $ toSixel img  --  toSixel img = SixelImage (show (toSixelCmds img))@@ -190,7 +160,9 @@       return (fromIntegral len)  -- | Display sixel image via ByteString+--  -- putStr of String is really slow on ghci. (Compiled version is not so slow.)+--  -- To improve perfomance of rendering on ghci, this function uses putStr of ByteString. putImage :: FilePath -> IO () putImage file = do
+ src/Data/Sixel/Internal.hs view
@@ -0,0 +1,56 @@+{-# LANGUAGE FlexibleInstances #-}+{-# LANGUAGE LambdaCase #-}+{-# LANGUAGE UndecidableInstances #-}++module Data.Sixel.Internal where++import Codec.Picture+import Foreign.C.String+import Foreign.C.Types+import Foreign.ForeignPtr+import Foreign.Ptr+import System.IO.Temp (withSystemTempDirectory)+import System.Process (readProcessWithExitCode)++foreign import ccall "bufsize" c_bufsize :: CInt -> CInt -> IO CInt++foreign import ccall "img2sixel" c_img2sixel :: Ptr () -> Ptr () -> CInt -> CInt -> IO CInt++data LatexStr+  = LatexStr+      { toLatexStr :: String,+        strSize :: Float+      }+  deriving (Eq)++latex :: String -> LatexStr+latex str = LatexStr str 2.5++math :: String -> LatexStr+math str = LatexStr ("$" ++ str ++ "$") 2.5++latexStr :: String -> Float -> String+latexStr str size =+  "\\documentclass[border=2pt]{standalone}"+    ++ "\\usepackage{amsmath}"+    ++ "\\usepackage{graphicx}"+    ++ "\\usepackage{varwidth}"+    ++ "\\begin{document}"+    ++ "\\begin{varwidth}{\\linewidth}"+    ++ "\\scalebox{"+    ++ show size+    ++ "}{"+    ++ str+    ++ "}"+    ++ "\\end{varwidth}"+    ++ "\\end{document}"++latex2img :: LatexStr -> IO (Either String DynamicImage)+latex2img (LatexStr str size) =+  withSystemTempDirectory "sixel" $ \dir -> do+    writeFile (dir ++ "/sixel.tex") (latexStr str size)+    (_, outlog, errlog) <- readProcessWithExitCode "pdflatex" ["-output-directory=" ++ dir, dir ++ "/sixel.tex"] ""+    readProcessWithExitCode "convert" [dir ++ "/sixel.pdf", "-quality", "90", dir ++ "/sixel.png"] ""+    readImage (dir ++ "/sixel.png") >>= \case+      Left err -> return $ Left $ "can not read sixel.png. \n" ++ err ++ "\n" ++ errlog ++ "\n" ++ outlog+      Right img -> return $ Right img