Minor changes

- Return of the newlines: Add all newlines once again
- Provide better comments
- Remove `sender`
- Consistently use set and get prefixed functions
This commit is contained in:
Sumit Sahrawat 2015-06-11 11:43:10 +05:30
parent 73bdfb0114
commit 6a1e912028
46 changed files with 77 additions and 73 deletions

View File

@ -16,4 +16,4 @@ instance IHaskellDisplay Value where
display renderable = return $ Display [plain json, html dom]
where
json = T.unpack $ E.decodeUtf8 $ LBS.toStrict $ encodePretty renderable
dom = [i|<div class="highlight-code" id="javascript">${json}</div>|]
dom = [i|<div class="highlight-code" id="javascript">${json}</div>|]

View File

@ -16,4 +16,4 @@ instance Show a => IHaskellDisplay (Maybe a) where
Nothing -> "<span style='color: red; font-weight: bold;'>Nothing</span>"
Just x -> printf
"<span style='color: green; font-weight: bold;'>Just</span><span style='font-family: monospace;'>%s</span>"
(show x)
(show x)

View File

@ -15,4 +15,4 @@ instance IHaskellDisplay (MarkupM a) where
where
str = renderMarkup (void val)
stringDisplay = plain str
htmlDisplay = html str
htmlDisplay = html str

View File

@ -46,4 +46,4 @@ chartData renderable format = do
mkFile opts filename renderable = renderableToFile opts filename renderable
#else
mkFile opts filename renderable = renderableToFile opts renderable filename
#endif
#endif

View File

@ -48,4 +48,4 @@ diagramData renderable format = do
-- Rendering hint.
diagram :: Diagram Cairo -> Diagram Cairo
diagram = id
diagram = id

View File

@ -53,4 +53,4 @@ animationData renderable = do
-- Rendering hint.
animation :: Animation Cairo V2 Double -> Animation Cairo V2 Double
animation = id
animation = id

View File

@ -12,4 +12,4 @@ instance IHaskellDisplay LaTeX where
display = display . IHaskell.Display.latex . T.unpack . render
instance (a ~ (), IO ~ io) => IHaskellDisplay (LaTeXT io a) where
display ma = display =<< execLaTeXT ma
display ma = display =<< execLaTeXT ma

View File

@ -91,4 +91,4 @@ imWidthHeight (ImageCMYK8 im) = imWH im
imWidthHeight (ImageCMYK16 im) = imWH im
imWH :: Image a -> (Int, Int)
imWH im = (imageWidth im, imageHeight im)
imWH im = (imageWidth im, imageHeight im)

View File

@ -71,4 +71,4 @@ data MagicClass = SVG
| HTML
| LaTeX
| Unknown
deriving Show
deriving Show

View File

@ -49,4 +49,4 @@ instance Show a => IHaskellWidget (Parser a) where
let key = "text" :: Text
Just (String text) = Map.lookup key dict
result = parse widget "<interactive>" $ T.unpack text
publisher $ toJSON result
publisher $ toJSON result

View File

@ -40,4 +40,4 @@ figureData figure format = do
where
extension SVG = "svg"
extension PNG = "png"
extension _ = ""
extension _ = ""

View File

@ -103,4 +103,4 @@ displayInteraction (KnitImage cap img) = do
(H.unsafeByteStringValue
-- assumes you use the default device which is png
(Char.pack "data:image/png;base64," <> encoded))
<> caption
<> caption

View File

@ -29,4 +29,4 @@ instance IHaskellDisplay Canvas where
display cnv = do
name <- getUniqueName
let script = buildScript' (width cnv) (height cnv) name (canvas cnv)
return $ Display [html $ unpack $ toLazyText script]
return $ Display [html $ unpack $ toLazyText script]

View File

@ -1,3 +1,3 @@
module IHaskell.Display.Widgets (module IHaskell.Display.Widgets.Button) where
import IHaskell.Display.Widgets.Button
import IHaskell.Display.Widgets.Button

View File

@ -12,14 +12,13 @@ module IHaskell.Display.Widgets.Button (
setButtonStyle,
setButtonLabel,
setButtonTooltip,
disableButton,
enableButton,
setButtonStatus,
toggleButtonStatus,
-- * Get button properties
getButtonStyle,
getButtonLabel,
getButtonTooltip,
isDisabled,
getButtonStatus,
-- * Click handlers
setClickHandler,
getClickHandler,
@ -43,15 +42,15 @@ import IHaskell.Eval.Widgets
import qualified IHaskell.IPython.Message.UUID as U
import IHaskell.Types (WidgetMethod(..))
-- | ADT for a button
-- | A 'Button' represents a Button from IPython.html.widgets.
data Button =
Button
{ uuid :: U.UUID
, description :: IORef Text
, tooltip :: IORef Text
, disabled :: IORef Bool
, buttonStyle :: IORef ButtonStyle
, clickHandler :: IORef (Button -> IO ())
{ uuid :: U.UUID -- ^ The UUID for the comm
, description :: IORef Text -- ^ The label displayed on the button
, tooltip :: IORef Text -- ^ The tooltip shown on mouseover
, disabled :: IORef Bool -- ^ Whether the button is disabled
, buttonStyle :: IORef ButtonStyle -- ^ The button_style
, clickHandler :: IORef (Button -> IO ()) -- ^ Function executed when button is clicked
}
-- | Pre-defined button-styles
@ -67,15 +66,20 @@ data ButtonStyle = Primary
mkButton :: IO Button
mkButton = do
-- Default properties, with a random uuid
uuid <- U.random
sender <- newIORef Nothing
desc <- newIORef "label" -- Non-empty to get a display
commUUID <- U.random
desc <- newIORef "label" -- Non-empty to get a display
ttip <- newIORef ""
dis <- newIORef False
sty <- newIORef None
fun <- newIORef (\_ -> return ())
let b = Button uuid desc ttip dis sty fun
let b = Button { uuid = commUUID
, description = desc
, tooltip = ttip
, disabled = dis
, buttonStyle = sty
, clickHandler = fun
}
-- Open a comm for this widget, and store it in the kernel state
widgetSendOpen b (toJSON ButtonInitData) (toJSON b)
@ -83,9 +87,13 @@ mkButton = do
-- Return the button widget
return b
-- | Send an update msg for a button, with custom json. Make it easy
-- to update fragments of the state, by accepting a Pair instead of a
-- Value.
update :: Button -> [Pair] -> IO ()
update b v = widgetSendUpdate b . toJSON . object $ v
-- | Modify attributes of a button, stored inside it as IORefs
modify :: Button -> (Button -> IORef a) -> a -> IO ()
modify b attr val = writeIORef (attr b) val
@ -107,17 +115,12 @@ setButtonTooltip b txt = do
modify b tooltip txt
update b ["tooltip" .= txt]
-- | Disable the button
disableButton :: Button -> IO ()
disableButton b = do
modify b disabled True
update b ["disabled" .= True]
-- | Enable the button
enableButton :: Button -> IO ()
enableButton b = do
modify b disabled False
update b ["disabled" .= False]
-- | Set buttton status. True: Enabled, False: Disabled
setButtonStatus :: Button -> Bool -> IO ()
setButtonStatus b stat = do
let newStatus = not stat
modify b disabled newStatus
update b ["disabled" .= newStatus]
-- | Toggle the button
toggleButtonStatus :: Button -> IO ()
@ -139,9 +142,9 @@ getButtonLabel = readIORef . description
getButtonTooltip :: Button -> IO Text
getButtonTooltip = readIORef . tooltip
-- | Check whether the button is disabled
isDisabled :: Button -> IO Bool
isDisabled = readIORef . disabled
-- | Check whether the button is enabled / disabled
getButtonStatus :: Button -> IO Bool
getButtonStatus = not . readIORef . disabled
-- | Set a function to be activated on click
setClickHandler :: Button -> (Button -> IO ()) -> IO ()
@ -207,4 +210,4 @@ instance IHaskellWidget Button where
when (event == "click") $ triggerClick widget
str :: String -> String
str = id
str = id

View File

@ -249,4 +249,4 @@ main = do
putStrLn "Usage:"
putStrLn "simple-calc-example setup -- set up the profile"
putStrLn
"simple-calc-example kernel FILE -- run a kernel with FILE for communication with the frontend"
"simple-calc-example kernel FILE -- run a kernel with FILE for communication with the frontend"

View File

@ -242,4 +242,4 @@ dupHeader :: MonadIO m => MessageHeader -> MessageType -> m MessageHeader
dupHeader hdr mtype =
do
uuid <- liftIO UUID.random
return hdr { messageId = uuid, msgType = mtype }
return hdr { messageId = uuid, msgType = mtype }

View File

@ -6,4 +6,4 @@ import IHaskell.IPython.Types as X
import IHaskell.IPython.Message.Writer as X
import IHaskell.IPython.Message.Parser as X
import IHaskell.IPython.Message.UUID as X
import IHaskell.IPython.ZeroMQ as X
import IHaskell.IPython.ZeroMQ as X

View File

@ -173,4 +173,4 @@ commCloseParser :: LByteString -> Message
commCloseParser = requestParser $ \obj -> do
uuid <- obj .: "comm_id"
value <- obj .: "data"
return $ CommClose noHeader uuid value
return $ CommClose noHeader uuid value

View File

@ -37,4 +37,4 @@ instance FromJSON UUID where
instance ToJSON UUID where
-- Extract the string from the UUID.
toJSON (UUID str) = String $ pack str
toJSON (UUID str) = String $ pack str

View File

@ -129,4 +129,4 @@ ints :: [Int] -> [Int]
ints = id
string :: String -> String
string = id
string = id

View File

@ -477,4 +477,4 @@ instance Show MimeType where
show (MimeJpg _ _) = "image/jpeg"
show MimeSvg = "image/svg+xml"
show MimeLatex = "text/latex"
show MimeJavascript = "application/javascript"
show MimeJavascript = "application/javascript"

View File

@ -233,4 +233,4 @@ sendMessage debug hmacKey socket message = do
idents = identifiers head
metadata = "{}"
content = encodeStrict message
headStr = encodeStrict head
headStr = encodeStrict head

View File

@ -45,4 +45,4 @@ ident :: Parser String
ident = many (alphaNum <|> oneOf "-.")
dependency :: Parser String
dependency = string " dependency \"" *> ident <* string "\" doesn't exist\n"
dependency = string " dependency \"" *> ident <* string "\" doesn't exist\n"

View File

@ -41,4 +41,4 @@ failIfExists :: FilePath -> IO ()
failIfExists file = do
exists <- doesFileExist file
when exists $ fail $
printf "File %s already exists. To force supply --force." file
printf "File %s already exists. To force supply --force." file

View File

@ -107,4 +107,4 @@ fromExt s =
case map toLower (takeExtension s) of
".lhs" -> Just LhsMarkdown
".ipynb" -> Just IpynbFile
_ -> Nothing
_ -> Nothing

View File

@ -68,4 +68,4 @@ convOutputs sty array = do
getTexts :: LT.Text -> Value -> Maybe LT.Text
getTexts p (Object object)
| Just (Array text) <- lookup "text" object = concatWithPrefix p text
getTexts _ _ = Nothing
getTexts _ _ = Nothing

View File

@ -119,4 +119,4 @@ classifyLines sty@(LhsStyle c o _ _ _ _) (l:ls) =
then Just ""
else Nothing
dropSpace = LT.dropWhile isSpace
classifyLines _ [] = []
classifyLines _ [] = []

View File

@ -166,4 +166,4 @@ switchToTmpDir = void (try switchDir :: IO (Either SomeException ()))
where
switchDir =
getTemporaryDirectory >>=
setCurrentDirectory
setCurrentDirectory

View File

@ -339,4 +339,4 @@ completePathFilter includeFile includeDirectory left right = GhcMonad.liftIO $ d
else str
visible = filter (not . isHidden) suggestions
hidden = filter isHidden suggestions
return $ visible ++ hidden
return $ visible ++ hidden

View File

@ -1288,4 +1288,4 @@ displayError :: ErrMsg -> Display
displayError msg = Display [plain . typeCleaner $ msg, html $ formatError msg]
mono :: String -> String
mono = printf "<span class='mono'>%s</span>"
mono = printf "<span class='mono'>%s</span>"

View File

@ -253,4 +253,4 @@ span :: String -> String -> String
span = printf "<span class='%s'>%s</span>"
link :: String -> String -> String
link = printf "<a target='_blank' href='%s'>%s</a>"
link = printf "<a target='_blank' href='%s'>%s</a>"

View File

@ -23,4 +23,4 @@ info name = ghandle handler $ do
return $ typeCleaner $ showPpr dflags result
where
handler :: SomeException -> Interpreter String
handler _ = return ""
handler _ = return ""

View File

@ -53,4 +53,4 @@ inspect code pos = do
response <- ghandle handler (Just <$> getType identifier)
let prefix = identifier ++ " :: "
fmt str = Display [plain $ prefix ++ str]
return $ fmt <$> response
return $ fmt <$> response

View File

@ -224,4 +224,4 @@ showSuggestion = remove lintIdent . dropDo
-- Ignore other list elements - just proceed onwards.
clean (x:xs) = x : clean xs
clean [] = []
clean [] = []

View File

@ -64,4 +64,4 @@ shellWords = try (eof *> return []) <|> do
return $ x : xs
parseShell :: String -> Either ParseError [String]
parseShell string = parse shellWords "shell" (string ++ "\n")
parseShell string = parse shellWords "shell" (string ++ "\n")

View File

@ -295,4 +295,4 @@ getModuleName moduleSrc = do
case unLoc <$> hsmodName (unLoc mod) of
Nothing -> error "Module must have a name."
Just name -> return $ split "." $ moduleNameString name
otherwise -> error "getModuleName failed, output was neither Parsed nor Failure"
otherwise -> error "getModuleName failed, output was neither Parsed nor Failure"

View File

@ -393,4 +393,4 @@ getDescription str = do
showFixity thing fixity =
if fixity == GHC.defaultFixity
then O.empty
else O.ppr fixity O.<+> pprInfixName (getName thing)
else O.ppr fixity O.<+> pprInfixName (getName thing)

View File

@ -142,4 +142,4 @@ widgetHandler :: (Message -> IO ())
widgetHandler _ _ state [] = return state
widgetHandler sender header state (x:xs) = do
newState <- handleMessage sender header state x
widgetHandler sender header newState xs
widgetHandler sender header newState xs

View File

@ -175,4 +175,4 @@ ihaskellArgs =
noArgs = flagArg unexpected ""
where
unexpected a = error $ "Unexpected argument: " ++ a
unexpected a = error $ "Unexpected argument: " ++ a

View File

@ -262,4 +262,4 @@ getSandboxPackageConf = SH.shelly $ do
case confdirs of
[] -> return Nothing
dir:_ ->
return $ Just dir
return $ Just dir

View File

@ -117,4 +117,4 @@ recordParentHeader dir header =
recordKernelProfile :: String -> Profile -> IO ()
recordKernelProfile dir profile =
writeFile (dir ++ "/.kernel-profile") $ show profile
writeFile (dir ++ "/.kernel-profile") $ show profile

View File

@ -73,4 +73,4 @@ publishResult send replyHeader displayed updateNeeded pagerOutput usePager resul
prependCss (DisplayData MimeHtml html) =
DisplayData MimeHtml $ mconcat ["<style>", T.pack ihaskellCSS, "</style>", html]
prependCss x = x
prependCss x = x

View File

@ -229,4 +229,4 @@ dupHeader :: MessageHeader -> MessageType -> IO MessageHeader
dupHeader header messageType = do
uuid <- liftIO random
return header { messageId = uuid, msgType = messageType }
return header { messageId = uuid, msgType = messageType }

View File

@ -147,3 +147,4 @@ putChar = liftIO . P.putChar
print :: (MonadIO m, Show a) => a -> m ()
print = liftIO . P.print

View File

@ -23,4 +23,4 @@ replace needle replacement haystack =
T.unpack $ T.replace (T.pack needle) (T.pack replacement) (T.pack haystack)
split :: String -> String -> [String]
split delim = map T.unpack . T.splitOn (T.pack delim) . T.pack
split delim = map T.unpack . T.splitOn (T.pack delim) . T.pack