Repository navigation
Expand file tree
/
Copy pathCommand.hs
More file actions
53 lines (44 loc) · 1.46 KB
/
Copy pathCommand.hs
File metadata and controls
53 lines (44 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveFunctor #-}
module Command where
import Data.Char
import qualified Data.Map as M
import Data.Maybe
import qualified Data.Text as T
import Reaction
import Safe
import Transport
data BuiltinCommand = BuiltinCommand
{ bcDescription :: T.Text
, bcGitHubLocation :: T.Text
, bcReaction :: Reaction Message T.Text
}
mkBuiltinCommand :: (T.Text, T.Text, Reaction Message T.Text) -> BuiltinCommand
mkBuiltinCommand (description, gitHubLocation, reaction) =
BuiltinCommand description gitHubLocation reaction
type CommandTable = M.Map T.Text BuiltinCommand
data Command a = Command
{ commandName :: T.Text
, commandArgs :: a
} deriving (Eq, Show, Functor)
renameCommand :: Command a -> T.Text -> Command a
renameCommand command name = command {commandName = name}
textAsCommand :: T.Text -> Maybe (Command T.Text)
textAsCommand (T.uncons -> Just ('!', restText)) =
Just
Command
{ commandName = T.takeWhile isAlphaNum restText
, commandArgs = T.dropWhile isSpace $ T.dropWhile isAlphaNum restText
}
textAsCommand _ = Nothing
textAsPipe :: T.Text -> [Command T.Text]
textAsPipe s =
fromMaybe [] $
mapM (textAsCommand . T.strip . T.replace placeholder "|") $
T.splitOn "|" $ T.replace "\\|" placeholder s
where
placeholder =
fromMaybe "\r" $
headMay $ dropWhile (`T.isInfixOf` s) $ iterate (T.cons '\r') "\r"