| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Shrun.Utils
Description
Provides utilities.
Synopsis
- breakStripPoint :: Text -> Text -> (Text, Text)
- truncateIfNeeded :: Int -> Text -> Text
- stripControlAll :: Text -> Text
- stripControlSmart :: Text -> Text
- escapeDoubleQuotes :: Text -> Text
- diffTime :: TimeSpec -> TimeSpec -> Natural
- timeSpecToRelTime :: TimeSpec -> RelativeTime
- hWithHidden :: forall (p :: HandleMode) m a. (CanRead p, CanWrite p, MonadMask m, MonadHandleReader m, MonadHandleWriter m) => Handle p -> m a -> m a
- withHiddenInput :: (MonadMask m, MonadHandleReader m, MonadHandleWriter m) => m a -> m a
- hHide :: forall (p :: HandleMode) m. (CanWrite p, MonadHandleWriter m) => Handle p -> m ()
- drainStdin :: (MonadCatch m, MonadHandleReader m) => m ()
- inverseMap :: (Bounded a, Enum a, Ord k) => (a -> k) -> k -> Maybe a
- inverseMapFail :: (Bounded a, Enum a, MonadFail m) => (a -> Text) -> Text -> (Bool, [Text]) -> Text -> m a
- inversePretty :: (Bounded a, Enum a, Pretty a) => Text -> Maybe a
- inversePrettyFail :: (Bounded a, Enum a, MonadFail m, Pretty a) => Text -> (Bool, [Text]) -> Text -> m a
- atomicReadWrite :: (HasCallStack, MonadAtomic m, MonadMask m) => TBQueue a -> (a -> m b) -> m ()
- fmtUnrecognizedError :: (IsString a, Monoid a) => a -> (Bool, [a]) -> a -> a
- mkMetaStr :: (IsString a, Monoid a) => (Bool, [a]) -> a
- parseByteText :: Text -> Either Text (Bytes 'B Natural)
- readIncCounter :: TVar Word16 -> STM Word16
- surroundJust :: forall k m l (ks :: IxList) u v a b. (JoinKinds k A_Prism m, JoinKinds A_Prism l k) => Optic l ks u v (Maybe a) (Maybe b) -> Optic m ks (Maybe u) (Maybe v) a b
- 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
- (∸) :: (Ord a, Num a) => a -> a -> a
- readStripUnderscores :: (MonadFail m, Read a) => Text -> m a
- indexPos :: NESeq a -> NESeq (Positive Int, a)
Text Utils
breakStripPoint :: Text -> Text -> (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 :: Int -> 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 :: Text -> Text Source #
Strips all control chars, including ansi escape sequences.
Examples
>>>stripControlAll "foo\ESC[0;3Abar \n baz""foobar baz"
stripControlSmart :: Text -> Text 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.
Terminal input
hWithHidden :: forall (p :: HandleMode) m a. (CanRead p, CanWrite p, MonadMask m, MonadHandleReader m, MonadHandleWriter m) => Handle p -> m a -> m a Source #
withHiddenInput :: (MonadMask m, MonadHandleReader m, MonadHandleWriter m) => m a -> m a Source #
Hides stdin input. Some caveats:
Tragically, this does not prevent sudo from hijacking stdin e.g.
shrun "sudo ls && sleep 10"
will launch the prompt which will overwrite the terminal. Oh well.
- It does not swallow stdin e.g. read stdin and throw it away. For example, if the user types in a bunch of stuff, it will be buffered in memory then printed / executed (e.g. enter key) after shrun finishes.
For the main console, this is handled by drainStdin.
hHide :: forall (p :: HandleMode) m. (CanWrite p, MonadHandleWriter m) => Handle p -> m () Source #
drainStdin :: (MonadCatch m, MonadHandleReader m) => m () Source #
Drains stdin.
Text parsing
Parses a finite type from an injective function.
Arguments
| :: (Bounded a, Enum a, MonadFail m) | |
| => (a -> Text) | Text injection. |
| -> Text | Field name. |
| -> (Bool, [Text]) | Field metavar. |
| -> Text | Key. |
| -> m a |
inverseMap with a Text injection that fails via MonadFail and
fmtUnrecognizedError. Intended for parsing config values.
inverseMap with text keys given by a Pretty instance.
Arguments
| :: (Bounded a, Enum a, MonadFail m, Pretty a) | |
| => Text | Field name. |
| -> (Bool, [Text]) | Field metavar. |
| -> Text | Key. |
| -> m a |
inverseMapFail with Pretty instance.
Misc Utils
Arguments
| :: (HasCallStack, MonadAtomic m, MonadMask m) | |
| => TBQueue a | Queue from which to read. |
| -> (a -> m b) | Function to apply. |
| -> m () |
Reads from a queue and applies the function, if we receive a value. Atomic in the sense that if a read is successful, then we will apply the given function, even if an async exception is raised.
Arguments
| :: (IsString a, Monoid a) | |
| => a | Field name. |
| -> (Bool, [a]) | (Include off?, Valid values). |
| -> a | Bad unrecognized value or error message. |
| -> a | Error message. |
Provides a standard format for "unrecognized param" failures.
surroundJust :: forall k m l (ks :: IxList) u v a b. (JoinKinds k A_Prism m, JoinKinds A_Prism l k) => Optic l ks u v (Maybe a) (Maybe b) -> Optic m ks (Maybe u) (Maybe v) a b Source #
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.