packages feed

irc-fun-color (empty) → 0.1.0.0

raw patch · 10 files changed

+644/−0 lines, 10 filesdep +basedep +irc-fun-colorsetup-changed

Dependencies added: base, irc-fun-color

Files

+ AUTHORS view
@@ -0,0 +1,1 @@+fr33domlover <fr33domlover@riseup.net>
+ COPYING view
@@ -0,0 +1,121 @@+Creative Commons Legal Code++CC0 1.0 Universal++    CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE+    LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN+    ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS+    INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES+    REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS+    PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM+    THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED+    HEREUNDER.++Statement of Purpose++The laws of most jurisdictions throughout the world automatically confer+exclusive Copyright and Related Rights (defined below) upon the creator+and subsequent owner(s) (each and all, an "owner") of an original work of+authorship and/or a database (each, a "Work").++Certain owners wish to permanently relinquish those rights to a Work for+the purpose of contributing to a commons of creative, cultural and+scientific works ("Commons") that the public can reliably and without fear+of later claims of infringement build upon, modify, incorporate in other+works, reuse and redistribute as freely as possible in any form whatsoever+and for any purposes, including without limitation commercial purposes.+These owners may contribute to the Commons to promote the ideal of a free+culture and the further production of creative, cultural and scientific+works, or to gain reputation or greater distribution for their Work in+part through the use and efforts of others.++For these and/or other purposes and motivations, and without any+expectation of additional consideration or compensation, the person+associating CC0 with a Work (the "Affirmer"), to the extent that he or she+is an owner of Copyright and Related Rights in the Work, voluntarily+elects to apply CC0 to the Work and publicly distribute the Work under its+terms, with knowledge of his or her Copyright and Related Rights in the+Work and the meaning and intended legal effect of CC0 on those rights.++1. Copyright and Related Rights. A Work made available under CC0 may be+protected by copyright and related or neighboring rights ("Copyright and+Related Rights"). Copyright and Related Rights include, but are not+limited to, the following:++  i. the right to reproduce, adapt, distribute, perform, display,+     communicate, and translate a Work;+ ii. moral rights retained by the original author(s) and/or performer(s);+iii. publicity and privacy rights pertaining to a person's image or+     likeness depicted in a Work;+ iv. rights protecting against unfair competition in regards to a Work,+     subject to the limitations in paragraph 4(a), below;+  v. rights protecting the extraction, dissemination, use and reuse of data+     in a Work;+ vi. database rights (such as those arising under Directive 96/9/EC of the+     European Parliament and of the Council of 11 March 1996 on the legal+     protection of databases, and under any national implementation+     thereof, including any amended or successor version of such+     directive); and+vii. other similar, equivalent or corresponding rights throughout the+     world based on applicable law or treaty, and any national+     implementations thereof.++2. Waiver. To the greatest extent permitted by, but not in contravention+of, applicable law, Affirmer hereby overtly, fully, permanently,+irrevocably and unconditionally waives, abandons, and surrenders all of+Affirmer's Copyright and Related Rights and associated claims and causes+of action, whether now known or unknown (including existing as well as+future claims and causes of action), in the Work (i) in all territories+worldwide, (ii) for the maximum duration provided by applicable law or+treaty (including future time extensions), (iii) in any current or future+medium and for any number of copies, and (iv) for any purpose whatsoever,+including without limitation commercial, advertising or promotional+purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each+member of the public at large and to the detriment of Affirmer's heirs and+successors, fully intending that such Waiver shall not be subject to+revocation, rescission, cancellation, termination, or any other legal or+equitable action to disrupt the quiet enjoyment of the Work by the public+as contemplated by Affirmer's express Statement of Purpose.++3. Public License Fallback. Should any part of the Waiver for any reason+be judged legally invalid or ineffective under applicable law, then the+Waiver shall be preserved to the maximum extent permitted taking into+account Affirmer's express Statement of Purpose. In addition, to the+extent the Waiver is so judged Affirmer hereby grants to each affected+person a royalty-free, non transferable, non sublicensable, non exclusive,+irrevocable and unconditional license to exercise Affirmer's Copyright and+Related Rights in the Work (i) in all territories worldwide, (ii) for the+maximum duration provided by applicable law or treaty (including future+time extensions), (iii) in any current or future medium and for any number+of copies, and (iv) for any purpose whatsoever, including without+limitation commercial, advertising or promotional purposes (the+"License"). The License shall be deemed effective as of the date CC0 was+applied by Affirmer to the Work. Should any part of the License for any+reason be judged legally invalid or ineffective under applicable law, such+partial invalidity or ineffectiveness shall not invalidate the remainder+of the License, and in such case Affirmer hereby affirms that he or she+will not (i) exercise any of his or her remaining Copyright and Related+Rights in the Work or (ii) assert any associated claims and causes of+action with respect to the Work, in either case contrary to Affirmer's+express Statement of Purpose.++4. Limitations and Disclaimers.++ a. No trademark or patent rights held by Affirmer are waived, abandoned,+    surrendered, licensed or otherwise affected by this document.+ b. Affirmer offers the Work as-is and makes no representations or+    warranties of any kind concerning the Work, express, implied,+    statutory or otherwise, including without limitation warranties of+    title, merchantability, fitness for a particular purpose, non+    infringement, or the absence of latent or other defects, accuracy, or+    the present or absence of errors, whether or not discoverable, all to+    the greatest extent permissible under applicable law.+ c. Affirmer disclaims responsibility for clearing rights of other persons+    that may apply to the Work or any use thereof, including without+    limitation any person's Copyright and Related Rights in the Work.+    Further, Affirmer disclaims responsibility for obtaining any necessary+    consents, permissions or other rights required for any use of the+    Work.+ d. Affirmer understands and acknowledges that Creative Commons is not a+    party to this document and has no duty or obligation with respect to+    this CC0 or use of the Work.
+ ChangeLog view
@@ -0,0 +1,17 @@+The changes are recorded by the version control system, Darcs. To see a log+quickly from the terminal, run:++  $ darcs changes --repo http://darcs.rel4tion.org/repos/irc-fun-color++There is also a web interface at <http://darcs.rel4tion.org> which, among other+things, can display the history log.++To see the log in a local clone, first get a copy of the repository if you+haven't yet:++  $ darcs get http://darcs.rel4tion.org/repos/irc-fun-color++Then move into the newly created directory and run darcs:++  $ cd irc-fun-color+  $ darcs changes
+ INSTALL view
@@ -0,0 +1,13 @@+Install from Hackage:++    $ cabal install irc-fun-color++Install from unpacked release tarball or source repo:++    $ cd irc-fun-color+    $ cabal install++Just play with it without installing:++    $ cabal build+    $ cabal repl
+ NEWS view
@@ -0,0 +1,24 @@+This file lists the user-visible interesting changes between releases. For a+full list of changes to the source, see the ChangeLog.++++irc-fun-color 0.1.0.0 -- 2015-07-18+===================================++General, build and documentation changes:++* This library hasn't been tested much yet in actual IRC clients. If you find+  any issues, please report them and they'll be fixed :-)++New APIs, features and enhancements:++* (This is the first release, so everything is a new feature)++Bug fixes:++* (This is just the first release)++Dependency changes:++* (This is the first release)
+ README view
@@ -0,0 +1,14 @@+See the .cabal file for more info and link to project website the version+control.++The official download location is Hackage:++<http://hackage.haskell.org/package/irc-fun-color>++This library is free software, and is committed to software freedom. It is+released to the public domain using the CC0 Public Domain Dedication. For the+boring "legal" details see the file 'COPYING'.++See the file 'INSTALL' for hints on installation. The file 'ChangeLog' explains+how to see the history log of the changes done in the code. 'NEWS' provides a+friendly overview of the changes for each release.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ irc-fun-color.cabal view
@@ -0,0 +1,42 @@+name:                irc-fun-color+version:             0.1.0.0+synopsis:            Add color and style decorations to IRC messages.+description:+  An extension to IRC allows text formatting to be encoded into a message.+  Colors and decorations (e.g. bold) are available. This library can encode+  this formatting into a plain text message.+  .+  The \"fun\" part of the library name refers to the set of integrated+  irc-fun-* libraries. This is the first one to be released to Hackage, but all+  of them are already available as free software in Darcs repositories.+homepage:            http://rel4tion.org/projects/irc-fun-color/+bug-reports:         http://rel4tion.org/projects/irc-fun-color/tickets/+license:             PublicDomain+license-file:        COPYING+author:              fr33domlover+maintainer:          fr33domlover@riseup.net+copyright:           ♡ Copying is an act of love. Please copy, reuse and share.+category:            Network+build-type:          Simple+extra-source-files:  AUTHORS ChangeLog COPYING INSTALL NEWS README+cabal-version:       >=1.10++source-repository head+  type:                darcs+  location:            http://darcs.rel4tion.org/repos/irc-fun-color/++library+  exposed-modules:     Network.IRC.Fun.Color+  -- other-modules:       +  -- other-extensions:    +  build-depends:       base >=4.7 && <5+  hs-source-dirs:      src+  default-language:    Haskell2010++test-suite test+  default-language:    Haskell2010+  type:                exitcode-stdio-1.0+  hs-source-dirs:      test+  main-is:             test.hs+  build-depends:       base+                     , irc-fun-color
+ src/Network/IRC/Fun/Color.hs view
@@ -0,0 +1,326 @@+{- This file is part of irc-fun-color.+ -+ - Written in 2015 by fr33domlover <fr33domlover@rel4tion.org>.+ -+ - ♡ Copying is an act of love. Please copy, reuse and share.+ -+ - The author(s) have dedicated all copyright and related and neighboring+ - rights to this software to the public domain worldwide. This software is+ - distributed without any warranty.+ -+ - You should have received a copy of the CC0 Public Domain Dedication along+ - with this software. If not, see+ - <http://creativecommons.org/publicdomain/zero/1.0/>.+ -}++-- | This module allows you to add color and style to IRC text messages.+-- Decoding style-encoded messages isn't implemented currently.+--+-- Building styled strings is done in two steps:+--+-- (1) Construct the string using combinators+-- (2) Encode it into the IRC message styling format+--+-- The result you get is a style-encoded 'String' which you can send as an IRC+-- message (e.g. using PRIVMSG).+--+-- The combinators are:+--+-- * '(#>)', '(<#)' : Apply a style to a styled string+-- * '(<>)' : Styles strings are monoids, use '(<>)' to concatenate them+--+-- The tools for choosing styles for application are:+--+-- * 'Color' : Choose a (foreground) color+-- * 'Decoration' : Choose a decoration style, e.g. bold or underline+-- * 'fg' : Choose a foreground color, you can use the 'Color' itself directly+-- * 'bg' : Choose background color+-- * 'fgBg' : Choose both colors+--+-- Once you build the styled string, use 'encode' to obtain an encoded 'String'+-- for use in IRC.+--+-- Here are some examples. I assume here the @OverloadedStrings@ extension is+-- enabled. If you prefer not to use it, you'll need to directly apply+-- 'Pure' or 'fromString' to 'String's before styling (e.g. with '(#>)').+--+-- Green text:+--+-- > Green #> "hello beautiful world"+--+-- The same, but without the extension mentioned above:+--+-- > Green #> Pure "hello beautiful world"+--+-- Bold text:+--+-- > Bold #> "hello beautiful world"+--+-- Green text with some underlined text in the middle:+--+-- > Green #> ("hello " <> Underline #> "beautiful" <> " world")+--+-- Red text on gray background:+--+-- > Red `fgBg` Gray #> "hello beautiful world"+--+-- Text with a red underlined part in the middle, and the whole string with+-- blue background:+--+-- > ("hello " <> Red #> Underline #> "beaufitul" <> " world") <# bg Blue+--+-- Three letters. The first is lime-on-black and bold. The second is+-- black-on-line. The third is again lime-on-black, and italicized:+--+-- > Lime `fgBg` Black #> (Bold #> "A" <> Reverse #> "B" <> Italic #> "C")+--+-- Bold, underlined purple-on-white text:+--+-- > Bold #> Underline #> Purple `fgBg` White #> "hello beautiful world"+module Network.IRC.Fun.Color+    ( -- * Primary Toolkit+      Color (..)+    , Decoration (..)+    , (#>)+    , (<#)+    , fg+    , bg+    , fgBg+    , encode+      -- * Underlying Types+    , Style (..)+    , StyledString (..)+    , FgBg (..)+      -- * Utilities+    , RGB (..)+    , toIrcRGB+    , toTangoRGB+    , strip+    )+where++import Data.List (nub)+import Data.Monoid+import Data.String (IsString (..))++{-+TODO check out packages irc-colors, rainbow, colour, palette, color+-}++-------------------------------------------------------------------------------+-- Classes+-------------------------------------------------------------------------------++-- | A class for types which add style formatting to a string. This is what+-- makes '(#>)' and '(<#)' work.+class Style s where+    style :: s -> StyledString -> StyledString++-------------------------------------------------------------------------------+-- Types+-------------------------------------------------------------------------------++-- | One of the 16 available color codes.+data Color+    = White+    | Black+    | Navy+    | Green+    | Red+    | Maroon+    | Purple+    | Orange+    | Yellow+    | Lime+    | Teal+    | Cyan+    | Blue+    | Magenta+    | Gray+    | Silver+    deriving (Enum, Eq, Show)++-- | A color specified by its red, green and blue components.+data RGB a = RGB a a a deriving (Eq, Show)++-- | A text decoration style.+data Decoration+    = Bold+    | Italic+    | Underline+    | Reverse+--x    | Plain+    deriving (Eq, Show)++-- | A string tagged with style attributes.+data StyledString+    = Pure String+    | Colored (Maybe Color) (Maybe Color) StyledString+    | Decorated Decoration StyledString+    | Concat [StyledString]+    deriving Show++-- A string with all styling applied to it specified explicitly.+data StyledChunk = StyledChunk FgBg [Decoration] String deriving Show++-- | A color decoration, specifying text foreground and background colors.+data FgBg = FgBg (Maybe Color) (Maybe Color) deriving (Eq, Show)++-------------------------------------------------------------------------------+-- Instances+-------------------------------------------------------------------------------++mappend' :: StyledString -> StyledString -> StyledString+mappend' (Concat l) (Concat m) = Concat $ l ++ m+mappend' str (Concat l)        = Concat $ str : l+mappend' (Concat l) str        = Concat $ l ++ [str]+mappend' s t                   = Concat [s, t]++instance Monoid StyledString where+    mempty = Pure ""+    mappend = mappend'++instance IsString StyledString where+    fromString = Pure++instance Style Color where+    style color = Colored (Just color) Nothing++instance Style FgBg where+    style (FgBg f b) = Colored f b++instance Style Decoration where+    style = Decorated++-------------------------------------------------------------------------------+-- Functions+-------------------------------------------------------------------------------++-- | Apply a style to a given string.+(#>) :: Style s => s -> StyledString -> StyledString+(#>) = style+infixr 7 #>++-- | Apply a style to a given string.+(<#) :: Style s => StyledString -> s -> StyledString+(<#) = flip style+infixl 7 <#++-- | Create a foreground color style with a given color.+fg :: Color -> FgBg+fg color = FgBg (Just color) Nothing++-- | Create a background color style with a given color.+bg :: Color -> FgBg+bg color = FgBg Nothing (Just color)++-- | Create a color style with a given foreground and background colors.+fgBg :: Color -> Color -> FgBg+fgBg f b = FgBg (Just f) (Just b)++-- | The IRC color number (between 0 and 15 inclusive) of a given color.+colorNumber :: Color -> Int+colorNumber = fromEnum++-- | Return the default RGB values of IRC colors. Client often allow the user+-- to change the values, but these are the defaults.+toIrcRGB :: Num a => Color -> RGB a+toIrcRGB White   = RGB 0xff 0xff 0xff+toIrcRGB Black   = RGB 0x00 0x00 0x00+toIrcRGB Navy    = RGB 0x00 0x00 0x7f+toIrcRGB Green   = RGB 0x00 0x93 0x00+toIrcRGB Red     = RGB 0xff 0x00 0x00+toIrcRGB Maroon  = RGB 0x7f 0x00 0x00+toIrcRGB Purple  = RGB 0x9c 0x00 0x9c+toIrcRGB Orange  = RGB 0xfc 0x7f 0x00+toIrcRGB Yellow  = RGB 0xff 0xff 0x00+toIrcRGB Lime    = RGB 0x00 0xfc 0x00+toIrcRGB Teal    = RGB 0x00 0x93 0x93+toIrcRGB Cyan    = RGB 0x00 0xff 0xff+toIrcRGB Blue    = RGB 0x00 0x00 0xfc+toIrcRGB Magenta = RGB 0xff 0x00 0xff+toIrcRGB Gray    = RGB 0x7f 0x7f 0x7f+toIrcRGB Silver  = RGB 0xd2 0xd2 0xd2++-- | Return RGB values for color codes, using the Tango color scheme. It is a+-- rough mapping between IRC color names and the 16 terminal colors.+toTangoRGB :: Num a => Color -> RGB a+toTangoRGB White   = RGB 0xee 0xee 0xec+toTangoRGB Black   = RGB 0x00 0x00 0x00+toTangoRGB Navy    = RGB 0x34 0x65 0xa4+toTangoRGB Green   = RGB 0x4e 0x9a 0x06+toTangoRGB Red     = RGB 0xef 0x29 0x29+toTangoRGB Maroon  = RGB 0xcc 0x00 0x00+toTangoRGB Purple  = RGB 0x75 0x50 0x7b+toTangoRGB Orange  = RGB 0xc4 0xa0 0x00+toTangoRGB Yellow  = RGB 0xfc 0xe9 0x4f+toTangoRGB Lime    = RGB 0x8a 0xe2 0x34+toTangoRGB Teal    = RGB 0x06 0x98 0x9a+toTangoRGB Cyan    = RGB 0x34 0xe2 0xe2+toTangoRGB Blue    = RGB 0x73 0x9f 0xcf+toTangoRGB Magenta = RGB 0xad 0x7f 0xa8+toTangoRGB Gray    = RGB 0x55 0x57 0x53+toTangoRGB Silver  = RGB 0xd3 0xd7 0xcf++decoCode :: Decoration -> Char+decoCode Bold      = '\x02'+decoCode Italic    = '\x1d'+decoCode Underline = '\x1f'+decoCode Reverse   = '\x16'+--decorationCode Plain     = '\x0f'++colorChar = '\x03'++colorCode :: Maybe Color -> Maybe Color -> String+colorCode Nothing  Nothing  = ""+colorCode (Just f) Nothing  = colorChar : show (colorNumber f)+colorCode Nothing  (Just b) = colorChar : ',' : show (colorNumber b)+colorCode (Just f) (Just b) =+    colorChar : show (colorNumber f) ++ ',' : show (colorNumber b)++-- Apply color to a flat styled string+applyColor :: Maybe Color -> Maybe Color -> [StyledChunk] -> [StyledChunk]+applyColor f' b' = map h+    where+    g Nothing (Just c) = Just c+    g curr    _        = curr+    h (StyledChunk (FgBg f b) d s) = StyledChunk (FgBg (g f f') (g b b')) d s++-- Apply decoration to a flat styled string+applyDeco :: Decoration -> [StyledChunk] -> [StyledChunk]+applyDeco d = map h+    where+    g ds d = if {-Plain `elem` ds ||-} d `elem` ds then ds else d : ds+    h (StyledChunk c ds s) = StyledChunk c (g ds d) s++-- Tag the actual strings with all the hierarchically attached styles+flatten :: StyledString -> [StyledChunk]+flatten (Pure s)        = [StyledChunk (FgBg Nothing Nothing) [] s]+flatten (Colored f b s) = applyColor f b $ flatten s+flatten (Decorated d s) = applyDeco d $ flatten s+flatten (Concat ss)     = concatMap flatten ss++encodeColor :: Maybe Color -> Maybe Color -> String -> String+encodeColor Nothing Nothing s = s+encodeColor f       b       s = colorCode f b ++ s ++ [colorChar]++encodeDeco :: [Decoration] -> String -> String+encodeDeco ds s = foldl f s $ nub ds+    where+    f s' d = let c = decoCode d in c : s' ++ [c]++-- | Convert a styled string value into an IRC style-coded text message.+encode :: StyledString -> String+encode = concatMap f . flatten+    where+    f (StyledChunk (FgBg f b) ds s) = encodeDeco ds $ encodeColor f b s++-- | Remove style from a string, returning just the content.+strip :: StyledString -> String+strip (Pure s)        = s+strip (Colored _ _ s) = strip s+strip (Decorated _ s) = strip s+strip (Concat l)      = concatMap strip l++-- this is cool, but only do this if it doesn't take too much time...+--decode ::String -> StyledString
+ test/test.hs view
@@ -0,0 +1,84 @@+{- This file is part of irc-fun-color.+ -+ - Written in 2015 by fr33domlover <fr33domlover@rel4tion.org>.+ -+ - ♡ Copying is an act of love. Please copy, reuse and share.+ -+ - The author(s) have dedicated all copyright and related and neighboring+ - rights to this software to the public domain worldwide. This software is+ - distributed without any warranty.+ -+ - You should have received a copy of the CC0 Public Domain Dedication along+ - with this software. If not, see+ - <http://creativecommons.org/publicdomain/zero/1.0/>.+ -}++{-# LANGUAGE OverloadedStrings #-}++import Data.List (findIndex)+import Data.Monoid ((<>))+import Network.IRC.Fun.Color+import System.Exit (exitFailure)++cases :: [(StyledString, String, String)]+cases =+    [ ( Green #> "hello beautiful world"+      , "\ETX3hello beautiful world\ETX"+      , "hello beautiful world"+      )+    , ( Bold #> "hello beautiful world"+      , "\STXhello beautiful world\STX"+      , "hello beautiful world"+      )+    , ( Green #> ("hello " <> Underline #> "beautiful" <> " world")+      , "\ETX3hello \ETX\US\ETX3beautiful\ETX\US\ETX3 world\ETX"+      , "hello beautiful world"+      )+    , ( Red `fgBg` Gray #> "hello beautiful world"+      , "\ETX4,14hello beautiful world\ETX"+      , "hello beautiful world"+      )+    , ( ("hello " <> Red #> Underline #> "beautiful" <> " world") <# bg Blue+      , "\ETX,12hello \ETX\US\ETX4,12beautiful\ETX\US\ETX,12 world\ETX"+      , "hello beautiful world"+      )+    , ( Lime `fgBg` Black #> (Bold #> "A" <> Reverse #> "B" <> Italic #> "C")+      , "\STX\ETX9,1A\ETX\STX\SYN\ETX9,1B\ETX\SYN\GS\ETX9,1C\ETX\GS"+      , "ABC"+      )+    , ( Bold #> Underline #> Purple `fgBg` White #> "hello beautiful world"+      , "\US\STX\ETX6,0hello beautiful world\ETX\STX\US"+      , "hello beautiful world"+      )+    ]++results :: [(Bool, String, Bool, String)]+results = map f cases+    where+    f (styled, encoded, stripped) =+        let e = encode styled+            s = strip styled+        in  (e == encoded, e, s == stripped, s)++failure :: Maybe (Int, Bool, StyledString, String, String)+failure = do+    loc <- findIndex (\ (e, _, s, _) -> not (e && s)) results+    case (cases !! loc, results !! loc) of+        ((styled, encoded, _), (False, e, _, _)) ->+            Just (loc + 1, True, styled, encoded, e)+        ((styled, _, stripped), (_, _, False, s)) ->+            Just (loc + 1, False, styled, stripped, s)+        _ -> error "Test impl error"++putFailure :: Int -> Bool -> StyledString -> String -> String -> IO ()+putFailure n enc styled expected got = do+    putStrLn $ "TEST " ++ show n ++ " FAILED:"+    putStrLn $ (if enc then "encode " else "strip ") ++ show styled+    putStrLn $ "Expected: " ++ show expected+    putStrLn $ "Got:      " ++ show got++main :: IO ()+main =+    case failure of+        Nothing               -> putStrLn "ALL TESTS PASSED"+        Just (n, b, ss, e, s) -> putFailure n b ss e s >> exitFailure