imp:demo: add an easy -s/--speed option, play at 2x by default

This commit is contained in:
Simon Michael 2023-06-01 12:15:16 -10:00
parent c9f39db363
commit 1b0be2b65f
2 changed files with 41 additions and 19 deletions

View File

@ -46,13 +46,14 @@ import Control.Concurrent (threadDelay)
import System.Process (callProcess) 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, findIndex, isInfixOf) import Data.List (isPrefixOf, find, findIndex, isInfixOf, dropWhileEnd)
import Control.Applicative ((<|>)) import Control.Applicative ((<|>))
import Data.ByteString as B (ByteString) import Data.ByteString as B (ByteString)
import Data.Maybe import Data.Maybe
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)
import System.Console.CmdArgs.Explicit (flagReq)
demos :: [Demo] demos :: [Demo]
demos = map readDemo [ demos = map readDemo [
@ -75,7 +76,10 @@ data Demo = Demo {
-- | Command line options for this command. -- | Command line options for this command.
demomode = hledgerCommandMode demomode = hledgerCommandMode
$(embedFileRelative "Hledger/Cli/Commands/Demo.txt") $(embedFileRelative "Hledger/Cli/Commands/Demo.txt")
[] [
flagReq ["speed","s"] (\s opts -> Right $ setopt "speed" s opts) "SPEED"
("playback speed (1 is original speed, .5 is half, 2 is double, etc (default: 2))")
]
[generalflagsgroup3] [generalflagsgroup3]
[] []
([], Just $ argsFlag optsstr) ([], Just $ argsFlag optsstr)
@ -87,8 +91,7 @@ usagestr = "Usage: hledger demo " <> optsstr
demo :: CliOpts -> Journal -> IO () demo :: CliOpts -> Journal -> IO ()
demo CliOpts{rawopts_=rawopts, reportspec_=ReportSpec{_rsQuery=_query}} _j = do demo CliOpts{rawopts_=rawopts, reportspec_=ReportSpec{_rsQuery=_query}} _j = do
-- demos <- getCurrentDirectory >>= readDemos -- demos <- getCurrentDirectory >>= readDemos
let args = listofstringopt "args" rawopts case listofstringopt "args" rawopts of
case args of
[] -> putStrLn usagestr >> printDemos [] -> putStrLn usagestr >> printDemos
(a:as) -> (a:as) ->
case findDemo demos a of case findDemo demos a of
@ -98,14 +101,23 @@ demo CliOpts{rawopts_=rawopts, reportspec_=ReportSpec{_rsQuery=_query}} _j = do
printDemos printDemos
exitFailure exitFailure
Just (Demo t c) -> do Just (Demo t c) -> do
let i = maybe 0 (1+) $ findIndex (\(Demo t2 _) -> t2 == t) demos -- should succeed let
-- try to preserve the original pauses a bit while also moving things along
defidlelimit = 10
defspeed = 2
speed =
case maybestringopt "speed" rawopts of
Nothing -> defspeed
Just s -> fromMaybe err $ readMay s
where err = error' $ "could not parse --speed " <> s <> ", numeric argument expected"
idx = maybe 0 (1+) $ findIndex (\(Demo t2 _) -> t2 == t) demos -- should succeed
mw <- getTerminalWidth mw <- getTerminalWidth
let line = red' $ replicate w '.' where w = fromMaybe (length t) mw let line = red' $ replicate w '.' where w = fromMaybe (length t) mw
printf "playing: %d) %s\nspace to pause, . to step, ctrl-c to quit\n" i (bold' t) printf "playing: %d) %s\nspace to pause, . to step, ctrl-c to quit\n" idx (bold' t)
putStrLn line putStrLn line
putStrLn "" putStrLn ""
threadDelay 1000000 threadDelay 1000000
runAsciinemaPlay c as runAsciinemaPlay speed defidlelimit c as
putStrLn "" putStrLn ""
putStrLn line putStrLn line
@ -133,13 +145,21 @@ printDemos = putStrLn $ unlines $
-- "" : -- "" :
[show i <> ") " <> bold' t | (i, Demo t _) <- zip [(1::Int)..] demos] [show i <> ") " <> bold' t | (i, Demo t _) <- zip [(1::Int)..] demos]
-- | Run asciinema play, passing content to its stdin. -- | Run asciinema play with the given speed and idle limit, passing the given content to its stdin.
runAsciinemaPlay :: ByteString -> [String] -> IO () runAsciinemaPlay :: Float -> Float -> ByteString -> [String] -> IO ()
runAsciinemaPlay content args = runAsciinemaPlay speed idlelimit content args =
withSystemTempFile "hledger-cast" $ \f h -> do -- try piping to stdin also withSystemTempFile "hledger-cast" $ \f h -> do -- try piping to stdin also
B.hPutStrLn h content >> hClose h B.hPutStrLn h content >> hClose h
callProcess "asciinema" ("play" : f : args) callProcess "asciinema" (dbg8With (("asciinema: "++).unwords) $ concat [
["play"]
,["-s"<> showwithouttrailingzero speed]
,if idlelimit == 0 then [] else ["-i"<>showwithouttrailingzero idlelimit]
,[f]
,args
])
`catchIOError` \err -> do `catchIOError` \err -> do
putStrLn $ "There was a problem. Is asciinema installed ?\n" <> show err -- (or PowerSession on Windows) putStrLn $ "There was a problem. Is asciinema installed ?\n" <> show err -- (or PowerSession on Windows)
exitFailure exitFailure
where
showwithouttrailingzero = dropWhileEnd (=='.') . dropWhileEnd (=='0') . show

View File

@ -10,18 +10,20 @@ Tips:
Make your terminal window large enough to see the demo clearly. Make your terminal window large enough to see the demo clearly.
Use the -s/--speed SPEED option to set your preferred playback speed,
eg `-s4` to play at 4x original speed or `-s.5` to play at half speed.
The default speed is 2x.
Other asciinema options can be added following a double dash,
eg `-- -i.1` to limit pauses or `-- -h` to list asciinema's other options.
During playback, several keys are available: During playback, several keys are available:
SPACE to pause/unpause, . to step forward (while paused), SPACE to pause/unpause, . to step forward (while paused),
CTRL-c quit. CTRL-c quit.
asciinema options can be added following a double dash, such as
`-s N` to adjust speed and `-i SECS` to limit pauses.
Run `asciinema -h` to list these options.
Examples: Examples:
```shell ```shell
$ hledger demo # list available demos $ hledger demo # list available demos
$ hledger demo 1 # play the first demo $ hledger demo 1 # play the first demo at default speed (2x)
$ hledger demo install -- -s5 -i.5 # play the install demo at 5x speed, $ hledger demo install -s4 # play the "install" demo at 4x speed
# with pauses limited to half a second
``` ```