grep to suckless-script

This commit is contained in:
voidlizard 2025-02-01 18:31:08 +03:00
parent d126fcf375
commit 5a1e95b79c
1 changed files with 24 additions and 2 deletions

View File

@ -1180,6 +1180,14 @@ internalEntries = do
liftIO TIO.getContents liftIO TIO.getContents
<&> either (const nil) (mkList . fmap fixContext) . parseTop <&> either (const nil) (mkList . fmap fixContext) . parseTop
entry $ bindMatch "top:file" $ \case
[StringLike fn] -> do
liftIO $ TIO.readFile fn
<&> either (const nil) (mkList . fmap fixContext) . parseTop
_ -> throwIO (BadFormException @c nil)
brief "parses string as toplevel and produces a form" brief "parses string as toplevel and produces a form"
$ desc "parse:top:string SYMBOL STRING-LIKE" $ desc "parse:top:string SYMBOL STRING-LIKE"
$ entry $ bindMatch "parse:top:string" $ \case $ entry $ bindMatch "parse:top:string" $ \case
@ -1366,9 +1374,23 @@ internalEntries = do
[StringLike what] -> lift do [StringLike what] -> lift do
callProc what mempty mempty <&> mkList @c . fmap (fixContext) callProc what mempty mempty <&> mkList @c . fmap (fixContext)
StringLikeList (x:xs) -> lift do (StringLike x:xs) -> lift do
callProc x xs mempty <&> mkList @c . fmap (fixContext) callProc x (fmap (show.pretty) xs) mempty <&> mkList @c . fmap (fixContext)
_ -> throwIO (BadFormException @c nil) _ -> throwIO (BadFormException @c nil)
entry $ bindMatch "grep" \case
[TextLike needle, what ] | matchOne needle what
-> pure what
[TextLike needle, e@(ListVal xs) ] | any (matchOne needle) xs ->
pure $ mkList (filter (matchOne needle) xs)
_ -> pure nil
matchOne :: IsContext c => Text -> Syntax c -> Bool
matchOne what = \case
s@(TextLike x) | Text.isInfixOf what x -> True
e@(ListVal xs) -> or [ Text.isInfixOf what s | TextLike s <- xs ]
_ -> False