Skip to content

Commit 431c708

Browse files
authored
Merge pull request #7 from haskell-works/newhoggy/new-ToString-typeclass
Adopt some useful modules from `hw-polysemy`
2 parents 138d821 + 7f42415 commit 431c708

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

hw-prelude.cabal

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,15 @@ library
9494
HaskellWorks.Error.Types
9595
HaskellWorks.Error.Types.GenericError
9696
HaskellWorks.Error.Types.TimedOut
97+
HaskellWorks.FilePath
9798
HaskellWorks.IO.Network.NamedPipe
9899
HaskellWorks.IO.Network.Port
99100
HaskellWorks.IO.Network.Socket
100101
HaskellWorks.IO.Process
102+
HaskellWorks.OS
101103
HaskellWorks.Prelude
104+
HaskellWorks.Stack
105+
HaskellWorks.String
102106
HaskellWorks.ToText
103107
HaskellWorks.Unsafe
104108
hs-source-dirs: src

src/HaskellWorks/FilePath.hs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module HaskellWorks.FilePath
2+
( exeSuffix,
3+
addExeSuffix,
4+
) where
5+
6+
import qualified HaskellWorks.OS as OS
7+
8+
import qualified Data.List as L
9+
import Data.Monoid
10+
import Data.String
11+
12+
exeSuffix :: String
13+
exeSuffix = if OS.isWin32 then ".exe" else ""
14+
15+
addExeSuffix :: String -> String
16+
addExeSuffix s = if ".exe" `L.isSuffixOf` s
17+
then s
18+
else s <> exeSuffix

src/HaskellWorks/OS.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module HaskellWorks.OS
2+
( isWin32,
3+
) where
4+
5+
import Data.Bool
6+
import Data.Eq
7+
import System.Info
8+
9+
-- | Determine if the operating system is Windows.
10+
isWin32 :: Bool
11+
isWin32 = os == "mingw32"
12+
{-# INLINE isWin32 #-}

src/HaskellWorks/Stack.hs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module HaskellWorks.Stack
2+
( callerModuleName,
3+
) where
4+
5+
import Data.Function
6+
import Data.Maybe (listToMaybe, maybe)
7+
import Data.String
8+
import Data.Tuple
9+
import GHC.Stack (HasCallStack, callStack, getCallStack,
10+
srcLocModule)
11+
12+
-- | Get the module name of the caller.
13+
callerModuleName :: HasCallStack => String
14+
callerModuleName = maybe "<no-module>" (srcLocModule . snd) (listToMaybe (getCallStack callStack))

src/HaskellWorks/String.hs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module HaskellWorks.String
2+
( ToString(..),
3+
) where
4+
5+
import Data.Function
6+
import Data.String
7+
import Data.Text (Text)
8+
import qualified Data.Text as T
9+
import qualified Data.Text.Lazy as LT
10+
11+
class ToString a where
12+
toString :: a -> String
13+
14+
instance ToString String where
15+
toString = id
16+
17+
instance ToString Text where
18+
toString = T.unpack
19+
20+
instance ToString LT.Text where
21+
toString = LT.unpack

0 commit comments

Comments
 (0)