imp: demo: hide the file names

This commit is contained in:
Simon Michael 2023-03-16 22:24:43 -10:00
parent 0aab8cbd9a
commit 2d496609bc

View File

@ -21,24 +21,24 @@ import System.Process (callProcess)
import System.IO.Error (catchIOError) import System.IO.Error (catchIOError)
import Safe (readMay, atMay, headMay) import Safe (readMay, atMay, headMay)
import Data.List (isPrefixOf, find, isInfixOf) import Data.List (isPrefixOf, find, isInfixOf)
import Data.Char (isDigit)
import Control.Applicative ((<|>)) import Control.Applicative ((<|>))
import Data.ByteString as B (ByteString) import Data.ByteString as B (ByteString)
import qualified Data.ByteString.Char8 as B import qualified Data.ByteString.Char8 as B
import System.IO.Temp (withSystemTempFile) import System.IO.Temp (withSystemTempFile)
import System.IO (hClose) import System.IO (hClose)
-- | An embedded asciinema cast, with some of the metadata separated out.
-- The original file name is not preserved.
data Demo = Demo { data Demo = Demo {
dfilename :: FilePath, -- file name dtitle :: String, -- asciinema title field
dtitle :: String, -- asciinema title field, effectively a description
_dcontent :: ByteString -- asciinema v2 content _dcontent :: ByteString -- asciinema v2 content
} }
demos :: [Demo] demos :: [Demo]
demos = map readDemo [ demos = map readDemo [
("install.cast", $(embedFileRelative "embeddedfiles/install.cast" )) $(embedFileRelative "embeddedfiles/install.cast" )
,("add.cast", $(embedFileRelative "embeddedfiles/add.cast" )) ,$(embedFileRelative "embeddedfiles/add.cast" )
,("reports.cast", $(embedFileRelative "embeddedfiles/reports.cast" )) ,$(embedFileRelative "embeddedfiles/reports.cast" )
] ]
-- | Command line options for this command. -- | Command line options for this command.
@ -56,7 +56,7 @@ demo CliOpts{rawopts_=rawopts, reportspec_=ReportSpec{_rsQuery=_query}} _j = do
let args = listofstringopt "args" rawopts let args = listofstringopt "args" rawopts
case args of case args of
[] -> do [] -> do
forM_ (zip [(1::Int)..] demos) $ \(i, Demo f t _) -> printf "%d) %-15s %s\n" i f t forM_ (zip [(1::Int)..] demos) $ \(i, Demo t _) -> printf "%d) %s\n" i t
exitSuccess exitSuccess
(a:as) -> (a:as) ->
@ -66,14 +66,14 @@ demo CliOpts{rawopts_=rawopts, reportspec_=ReportSpec{_rsQuery=_query}} _j = do
putStrLn "Usage: hledger-demo [NUM|NAME|STR], run with no arguments to see a list" putStrLn "Usage: hledger-demo [NUM|NAME|STR], run with no arguments to see a list"
exitFailure exitFailure
Just (Demo f t c) -> do Just (Demo t c) -> do
printf "playing (space to pause, . to step, ctrl-c to quit):\n %-15s %s\n" f t printf "playing (space to pause, . to step, ctrl-c to quit):\n %s\n" t
threadDelay 1000000 threadDelay 1000000
putStr "\n" putStr "\n"
runAsciinemaPlay c as runAsciinemaPlay c as
readDemo :: (FilePath, ByteString) -> Demo readDemo :: ByteString -> Demo
readDemo (name, content) = Demo name title content readDemo content = Demo title content
where where
title = maybe "" (readTitle . B.unpack) $ headMay $ B.lines content title = maybe "" (readTitle . B.unpack) $ headMay $ B.lines content
where where
@ -83,14 +83,12 @@ readDemo (name, content) = Demo name title content
| otherwise = readTitle $ tail s | otherwise = readTitle $ tail s
findDemo :: [Demo] -> String -> Maybe Demo findDemo :: [Demo] -> String -> Maybe Demo
findDemo ds s findDemo ds s =
| all isDigit s = readMay s >>= atMay ds . subtract 1 -- find by number (readMay s >>= atMay ds . subtract 1) -- try to find by number
| otherwise = <|> find ((sl `isPrefixOf`).lowercase.dtitle) ds -- or by title prefix (ignoring case)
find ((==sl).lowercase.dfilename) ds -- or by name, ignoring case <|> find ((sl `isInfixOf`) .lowercase.dtitle) ds -- or by title substring (ignoring case)
<|> find ((sl `isInfixOf`).lowercase.dfilename) ds -- or by name substring where
<|> find ((sl `isInfixOf`).lowercase.dtitle) ds -- or by title substring sl = lowercase s
where
sl = lowercase s
-- | Run asciinema play, passing content to its stdin. -- | Run asciinema play, passing content to its stdin.
runAsciinemaPlay :: ByteString -> [String] -> IO () runAsciinemaPlay :: ByteString -> [String] -> IO ()