brick-skylighting 0.1 → 0.2
raw patch · 5 files changed
+112/−45 lines, 5 filesdep +skylighting-coredep −skylightingPVP ok
version bump matches the API change (PVP)
Dependencies added: skylighting-core
Dependencies removed: skylighting
API changes (from Hackage documentation)
- Brick.Widgets.Skylighting: simpleHighlight :: Text -> Text -> Widget n
+ Brick.Widgets.Skylighting: highlightFromMap :: SyntaxMap -> Text -> Text -> Widget n
Files
- CHANGELOG.md +28/−2
- README.md +17/−0
- brick-skylighting.cabal +3/−3
- programs/Demo.hs +40/−19
- src/Brick/Widgets/Skylighting.hs +24/−21
CHANGELOG.md view
@@ -1,5 +1,31 @@ # Revision history for brick-skylighting -## 0.1 -- YYYY-mm-dd+0.2+=== -* First version. Released on an unsuspecting world.+API changes:+ * Added `Brick.Widgets.Skylighting.highlightFromMap` to highlight text by+ looking up syntax names in a Skylighting `SyntaxMap.+ * Removed `Brick.Widgets.Skylighting.simpleHighlight` because we no+ longer have access to the bundled `SyntaxMap` of default entries in+ the `skylighting` package.++Package changes:+ * This now depends on `skylighting-core` (BSD), not `skylighting`+ (GPL), to avoid inadvertent compilation of a GPL depdendency:++ To ensure that users of this library do not inadvertently pull in a+ dependency on a GPL'd library (skylighting), this package uses and+ depends only on the new skylighting-core package, a BSD-compatible+ subset of the skylighting functionality. The main thing we give+ up in doing this is generated Haskell modules that provide syntax+ definitions embedded in the program. Consequently users of this+ library must load the syntax definitions themselves and provide them+ to this library's functions. (skylighting could be used for that+ if desired, but we no longer force users to do so.) Please see the+ README for notes on how to deal with this.++0.1+===++First version.
README.md view
@@ -2,6 +2,8 @@ brick-skylighting ================= ++ This package extends the [brick](https://github.com/jtdaugherty/brick) library with syntax highlighting support using the [Skylighting](https://github.com/jgm/skylighting) library.@@ -9,3 +11,18 @@ See the Haddock documentation for `Brick.Widgets.Skylighting` for API details, and see the demonstration program in `programs` for a complete working example.++Note: this library requires its users to provide syntax highlighting+definitions. For that, there are two choices:++ * Obtain XML specifications from e.g. the `skylighting-core` sources,+ then bundle these with your program files. Those files are+ GPL-licensed, so this option is preferable when you want to ship+ these GPL-licensed files with your program without the GPL infecting+ the license of your application.++ * Use the compiled-in syntax specifications in the `skylighting`+ package. This option is preferable if you are okay with a+ GPL-licensed dependency (i.e. if your application is also GPL'd) and+ you especially want to avoid having to bundle the XML files with your+ application or avoid the runtime loading of such files.
brick-skylighting.cabal view
@@ -1,5 +1,5 @@ name: brick-skylighting-version: 0.1+version: 0.2 synopsis: Show syntax-highlighted text in your Brick UI description: This package provides a module to use Skylighting to perform syntax highlighting and display the results in Brick-based@@ -37,7 +37,7 @@ build-depends: base <= 5, brick, vty,- skylighting >= 0.6,+ skylighting-core >= 0.7, text, containers @@ -52,5 +52,5 @@ brick, brick-skylighting, vty,- skylighting >= 0.6,+ skylighting-core >= 0.7, text
programs/Demo.hs view
@@ -2,16 +2,19 @@ module Main where import Control.Monad (void)+import Data.Maybe (fromJust) import Data.Monoid ((<>)) import qualified Data.Text as T import qualified Graphics.Vty as V-import qualified Skylighting as S+import qualified Skylighting.Core as S+import System.Environment (getArgs, getProgName)+import System.Exit (exitFailure) import Brick import Brick.Widgets.Center (hCenter) import Brick.Widgets.Border (borderWithLabel, hBorder) -import Brick.Widgets.Skylighting (simpleHighlight, attrMappingsForStyle)+import Brick.Widgets.Skylighting (highlight, attrMappingsForStyle) haskellProgram :: T.Text haskellProgram = T.unlines@@ -44,22 +47,15 @@ , "print_foo" ] -programs :: [(T.Text, T.Text)]-programs =- [ (haskellProgram, "Haskell")- , (pythonProgram, "Python")- , (bashProgram, "Bash")- ]--ui :: Int -> [Widget ()]-ui styleIndex =- [vBox $ usage : hBorder : header : progs]+ui :: [(T.Text, S.Syntax)] -> Int -> [Widget ()]+ui programs styleIndex =+ [vBox $ help : hBorder : header : progs] where- usage = hCenter $ txt "q/esc:quit up/down:change theme"+ help = hCenter $ txt "q/esc:quit up/down:change theme" header = hCenter $ txt $ "Theme: " <> (fst $ styles !! styleIndex) progs = showProg <$> programs- showProg (progSrc, langName) =- (borderWithLabel (txt langName) $ simpleHighlight langName progSrc)+ showProg (progSrc, syntax) =+ (borderWithLabel (txt $ S.sName syntax) $ highlight syntax progSrc) styles :: [(T.Text, S.Style)] styles =@@ -80,9 +76,9 @@ handleEvent i (VtyEvent (V.EvKey (V.KChar 'q') [])) = halt i handleEvent i _ = continue i -app :: App Int e ()-app =- App { appDraw = ui+app :: [(T.Text, S.Syntax)] -> App Int e ()+app programs =+ App { appDraw = ui programs , appAttrMap = \i -> attrMap V.defAttr $ attrMappingsForStyle $ snd $ styles !! i , appHandleEvent = handleEvent@@ -90,5 +86,30 @@ , appStartEvent = return } +usage :: IO ()+usage = do+ pn <- getProgName+ putStrLn $ pn <> " <path to XML syntax definintion directory>"+ main :: IO ()-main = void $ defaultMain app 0+main = do+ args <- getArgs+ path <- case args of+ [p] -> return p+ _ -> usage >> exitFailure++ syntaxMap <- do+ result <- S.loadSyntaxesFromDir path+ case result of+ Left e -> do+ putStrLn $ "Failed to load syntax map: " <> e+ exitFailure+ Right m -> return m++ let syntax = fromJust . S.syntaxByName syntaxMap+ programs = [ (haskellProgram, syntax "haskell")+ , (pythonProgram, syntax "python")+ , (bashProgram, syntax "bash")+ ]++ void $ defaultMain (app programs) 0
src/Brick/Widgets/Skylighting.hs view
@@ -11,7 +11,7 @@ -- -- To highlight some text in your user interface, use one of -- the (increasingly finer-grained) highlighting functions--- 'simpleHighlight', 'highlight', 'highlight'', or 'renderRawSource'.+-- 'highlight', 'highlight'', or 'renderRawSource'. -- -- To actually see pretty colors, you'll need to update your -- application's 'AttrMap' with name-to-color mappings. Those can be@@ -27,8 +27,8 @@ -- complete program that uses this module. module Brick.Widgets.Skylighting ( -- * Highlighting functions- simpleHighlight- , highlight+ highlight+ , highlightFromMap , highlight' , renderRawSource @@ -46,26 +46,29 @@ import Brick -import qualified Skylighting as Sky-import Skylighting (TokenType(..))+import qualified Skylighting.Core as Sky+import Skylighting.Types (TokenType(..)) --- | The simplest way to highlight some text. If the specified language--- does not have a corresponding Skylighting parser or if a parser is--- found but fails to parse the input, the text is rendered as-is and--- tab characters are converted to eight spaces.-simpleHighlight :: T.Text- -- ^ The Skylighting name of the language in which the- -- input text is written.- -> T.Text- -- ^ The text to be syntax-highlighted.- -> Widget n-simpleHighlight langName body =- case Sky.syntaxByName Sky.defaultSyntaxMap langName of- Nothing -> txt $ expandTabs body- Just syntax -> highlight syntax body+-- Hightlight the specified text by attempting to locate a syntax+-- highlighter for the specified language in a syntax map. If the+-- specified language does not have a corresponding Skylighting parser+-- or if a parser is found but fails to parse the input, the text is+-- rendered as-is and tab characters are converted to eight spaces.+highlightFromMap :: Sky.SyntaxMap+ -- ^ The syntax map to use to locate a syntax+ -- definition.+ -> T.Text+ -- ^ The Skylighting name of the language in which the+ -- input text is written.+ -> T.Text+ -- ^ The text to be syntax-highlighted.+ -> Widget n+highlightFromMap m name input =+ case Sky.syntaxByName m name of+ Nothing -> txt $ expandTabs input+ Just s -> highlight s input --- | If you already have a 'Syntax' handy, this provides more control--- than 'simpleHighlight'.+-- | Highlight the specified text using the provided syntax definition. highlight :: Sky.Syntax -- ^ The syntax to use to parse the input text. -> T.Text