Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions HyperNerd.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ executable HyperNerd
, Schedule
, Bot.Asciify
, Free
, OrgMode

-- LANGUAGE extensions used by modules in this package.
other-extensions: OverloadedStrings
Expand Down Expand Up @@ -224,6 +225,8 @@ test-suite HyperNerdTest
, Bot.FridayTest
, Bot.ExprTest
, Bot.GitHub
, OrgMode
, OrgModeTest
, Data.Maybe.Extra
, Data.Time.Extra
, Data.Time.ExtraTest
Expand Down
30 changes: 16 additions & 14 deletions src/Bot/Friday.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import Data.Time
import Effect
import Entity
import HyperNerd.Comonad
import OrgMode
import Property
import Reaction
import Regexp
Expand Down Expand Up @@ -240,20 +241,21 @@ videoCountCommand =

renderQueue :: [FridayVideo] -> T.Text
renderQueue queue@(FridayVideo {fridayVideoAuthor = user}:_) =
T.unlines $
([qmb|** {user}

Video Count {length queue}

|] :) $
map
(\video ->
let ytId = fromMaybe "dQw4w9WgXcQ" $ ytLinkId $ fridayVideoName video
in [qms||{fridayVideoDate video}
|{fridayVideoAuthor video}
|{fridayVideoName video}
|[[https://img.youtube.com/vi/{ytId}/default.jpg]]||])
queue
[qmb|** {user}

Video Count {length queue}\n\n
|] <>
renderTable
["Date", "Submitter", "Video", "Thumbnail"]
(map
(\video ->
let ytId = fromMaybe "dQw4w9WgXcQ" $ ytLinkId $ fridayVideoName video
in [ [qms|{fridayVideoDate video}|]
, [qms|{fridayVideoAuthor video}|]
, [qms|{fridayVideoName video}|]
, [qms|[[https://img.youtube.com/vi/{ytId}/default.jpg]]|]
])
queue)
renderQueue [] = ""

renderQueues :: Maybe T.Text -> VideoQueues -> T.Text
Expand Down
40 changes: 22 additions & 18 deletions src/Bot/Help.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Data.Proxy
import qualified Data.Text as T
import Effect
import Entity
import OrgMode
import Property
import Reaction
import Text.InterpolatedString.QM
Expand Down Expand Up @@ -73,28 +74,31 @@ refreshHelpGistId =
Reaction replyMessage

gistRenderCommandTable :: CommandTable -> T.Text
gistRenderCommandTable =
([qms|* Builtin Commands\n{header}\n|-\n|] <>) .
T.unlines . map renderRow . M.toList
gistRenderCommandTable commandTable = [qms|* Builtin Commands\n{table}\n|]
where
header :: T.Text
header = "|Name|Description|Location|"
renderRow :: (T.Text, BuiltinCommand) -> T.Text
renderRow (name, command) =
[qms||{name}|{bcDescription command}|{location}||]
where
location :: T.Text
location = [qms|[[{bcGitHubLocation command}][Source↗]]|]
table :: T.Text
table =
renderTable ["Name", "Description", "Location"] $
map
(\(name, command) ->
[ name
, bcDescription command
, [qms|[[{bcGitHubLocation command}][Source↗]]|]
]) $
M.toList commandTable

gistRenderCustomCommandsTable :: [Entity CustomCommand] -> T.Text
gistRenderCustomCommandsTable =
([qms|* Custom commands\n{header}\n|-\n|] <>) .
T.unlines . map (renderRow . entityPayload)
gistRenderCustomCommandsTable customCommands =
[qms|* Custom commands\n{table}\n|]
where
header :: T.Text
header = "|Name|Definition|%times|"
renderRow (CustomCommand name message times) =
[qms||{name}|{message}|{times}||]
table :: T.Text
table =
renderTable ["Name", "Definition", "%times"] $
map
((\(CustomCommand name message times) ->
[name, message, T.pack $ show times]) .
entityPayload)
customCommands

refreshHelpGist :: CommandTable -> GistId -> Effect ()
refreshHelpGist commandTable gistId = do
Expand Down
22 changes: 22 additions & 0 deletions src/OrgMode.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{-# LANGUAGE OverloadedStrings #-}

module OrgMode
( renderTable
) where

import Data.List
import qualified Data.Text as T

charEscapeList :: String
charEscapeList = "|"

renderTable :: [T.Text] -> [[T.Text]] -> T.Text
renderTable header rows =
T.unlines ([renderRow header, "|-"] <> map (renderRow . normalizeRow) rows)
where
normalizeRow row = take (length header) (row ++ repeat "")
renderRow :: [T.Text] -> T.Text
renderRow columns =
"|" <> T.concat (intersperse "|" $ map escapeColumn columns) <> "|"
escapeColumn :: T.Text -> T.Text
escapeColumn = T.filter $ not . (`elem` charEscapeList)
34 changes: 34 additions & 0 deletions test/OrgModeTest.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{-# LANGUAGE OverloadedStrings #-}

module OrgModeTest
( spec
) where

import OrgMode
import Test.HUnit

spec :: Test
spec =
TestLabel "Rendering OrgMode table" $
TestCase $
assertEqual
""
"|hello|world|foo|\n\
\|-\n\
\|1|2|3|\n\
\|1|2|3|\n\
\|1|2||\n\
\|1|||\n\
\||||\n\
\|1|2|3|\n\
\|~|||\n" $
renderTable
["hello", "world", "foo"]
[ ["1", "2", "3"]
, ["1", "2", "3"]
, ["1", "2"]
, ["1"]
, []
, ["1", "2", "3", "4", "5"]
, ["|~"]
]
2 changes: 2 additions & 0 deletions test/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import qualified Bot.PollTest
import qualified Bot.TwitchTest
import qualified CommandTest
import qualified Data.Time.ExtraTest
import qualified OrgModeTest
import qualified Sqlite.EntityPersistenceTest
import System.Exit
import Test.HUnit
Expand All @@ -36,6 +37,7 @@ main = do
, CommandTest.spec
, Sqlite.EntityPersistenceTest.spec
, Data.Time.ExtraTest.spec
, OrgModeTest.spec
]
if errors results + failures results == 0
then exitSuccess
Expand Down