| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Shrun.Utils
Description
Provides utilities.
Synopsis
- breakStripPoint :: Text -> Text -> Tuple2 Text Text
- truncateIfNeeded :: Natural -> Text -> Text
- stripControlAll :: UnlinedText -> UnlinedText
- stripControlSmart :: UnlinedText -> UnlinedText
- escapeDoubleQuotes :: Text -> Text
- diffTime :: TimeSpec -> TimeSpec -> Natural
- timeSpecToRelTime :: TimeSpec -> RelativeTime
- foldMap1 :: (Foldable f, Semigroup s) => (a -> s) -> a -> f a -> s
- parseByteText :: Text -> Either Text (Bytes 'B Natural)
- whileM_ :: Monad m => m Bool -> m a -> m ()
- whenLeft :: Applicative f => Either a b -> (a -> f ()) -> f ()
- untilJust :: Monad m => m (Maybe b) -> m b
- unsafeListToNESeq :: HasCallStack => List a -> NESeq a
- (∸) :: (Ord a, Num a) => a -> a -> a
- readStripUnderscores :: (MonadFail m, Read a) => Text -> m a
Text Utils
breakStripPoint :: Text -> Text -> Tuple2 Text Text Source #
Wrapper for Text's breakOn that differs in that:
- If the
needleis found within thehaystack, we do not include it in the second part of the pair.
Examples
>>>-- Data.Text>>>T.breakOn "=" "HEY=LISTEN"("HEY","=LISTEN")
>>>-- Shrun.Utils.Text>>>breakStripPoint "=" "HEY=LISTEN"("HEY","LISTEN")
Other examples:
>>>breakStripPoint "=" "HEYLISTEN"("HEYLISTEN","")
>>>breakStripPoint "=" "=HEYLISTEN"("","HEYLISTEN")
>>>breakStripPoint "=" "HEYLISTEN="("HEYLISTEN","")
>>>breakStripPoint "=" "HEY==LISTEN"("HEY","=LISTEN")
truncateIfNeeded :: Natural -> Text -> Text Source #
For Natural \(n\) and Text \(t = t_0 t_1 \ldots t_m\), truncates
\(t\) if \(m > n\). In this case, \(t\) is truncated to \(n - 3\), and an
ellipsis ( \(\ldots\) ) is appended. We are left with a string with
length exactly \(n\):
\[ t_0 t_1 \ldots t_{n-3} \text{...} \quad \text{-- 3 literal } `\text{.' chars appended} \]
Examples
>>>truncateIfNeeded 7 "hi""hi"
>>>truncateIfNeeded 10 "This is 21 chars long""This is..."
stripControlAll :: UnlinedText -> UnlinedText Source #
Strips all control chars, including ansi escape sequences.
Examples
>>>stripControlAll "foo\ESC[0;3Abar \n baz""foobar baz"
stripControlSmart :: UnlinedText -> UnlinedText Source #
Strips control chars, including most ansi escape sequences. We leave behind SGR ansi escape sequences e.g. text coloring. See https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters.
Examples
>>>stripControlSmart "foo\ESC[0;3Abar \n baz""foobar baz"
>>>stripControlSmart "foo\ESC[0;3mbar \n baz""foo\ESC[0;3mbar baz"
escapeDoubleQuotes :: Text -> Text Source #
Escape double quotes in strings.
MonadTime Utils
diffTime :: TimeSpec -> TimeSpec -> Natural Source #
For given \(x, y\), returns the absolute difference \(|x - y|\) in seconds.
Examples
>>>:{let t1 = MkTimeSpec 5 0 -- 20 s + 1 billion ns = 21 s t2 = MkTimeSpec 20 1_000_000_000 in diffTime t1 t2 :} 16
timeSpecToRelTime :: TimeSpec -> RelativeTime Source #
Transforms a TimeSpec into a RelativeTime.
foldMap1 :: (Foldable f, Semigroup s) => (a -> s) -> a -> f a -> s Source #
Relaxes foldMap's Monoid constraint to Semigroup. Requires a
starting value. This will have to do until semigroupoids' Foldable1 is
in base.
Examples
>>>foldMap1 @List Sum 0 [1..4]Sum {getSum = 10}
>>>-- Silly, but demonstrates usage i.e. with non-monoid NonEmpty.>>>foldMap1 @List (:| []) 1 [2,3,4]1 :| [2,3,4]
Misc Utils
whileM_ :: Monad m => m Bool -> m a -> m () Source #
whileM_ mb ma executes ma as long as mb returns True.
whenLeft :: Applicative f => Either a b -> (a -> f ()) -> f () Source #
Runs the action when it is Left.
untilJust :: Monad m => m (Maybe b) -> m b Source #
Executes the monadic action until we receive a Just, returning the
value.
unsafeListToNESeq :: HasCallStack => List a -> NESeq a Source #