From 8d7a8d3e56ffc1fb3ea7d5ed0ef274ef3c0e3205 Mon Sep 17 00:00:00 2001 From: staticvoid Date: Wed, 29 Jun 2022 14:31:45 +0300 Subject: [PATCH] Add `--max-entries` option to `ts` subcommand --- README.md | 7 +++---- app/Cli.hs | 14 ++++++++++---- app/Main.hs | 4 ++-- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 03a7e3c..8087d8d 100644 --- a/README.md +++ b/README.md @@ -31,11 +31,10 @@ After building (that took ~40min on 8-core machine) you'll have a shell with `4r ``` > 4r --help -Usage: 4r [-N|--offline] [-A|--no-ansi] [-c|--columns COLUMNS_NUMBER] +Usage: 4r [-A|--no-ansi] [-c|--columns COLUMNS_NUMBER] (COMMAND | COMMAND | COMMAND) Available options: - -N,--offline only access cache -A,--no-ansi disable ANSI codes -c,--columns COLUMNS_NUMBER number of columns @@ -49,12 +48,12 @@ Available commands: Usual workflow: -- Print 10 last updated topics: `4r ts | head -20` +- Print 10 last updated topics: `4r ts -m 10` Example output: ``` - > 4r ts | head -20 + > 4r ts -m 10 Переезжаю: @abzyk at 2022-06-27 21:01:10 #31 Пожелания: @nick at 2022-06-27 20:43:34 #20 diff --git a/app/Cli.hs b/app/Cli.hs index db369db..2cd4d4b 100644 --- a/app/Cli.hs +++ b/app/Cli.hs @@ -24,13 +24,16 @@ data Options = Options optsParser :: Parser Options optsParser = Options <$> switch (long "no-ansi" <> short 'A' <> help "disable ANSI codes") - <*> (Just <$> option auto (metavar "COLUMNS_NUMBER" <> long "columns" <> short 'c' <> help "number of columns") + <*> (Just <$> option auto (metavar "COLUMNS_NUMBER" + <> long "columns" + <> short 'c' + <> help "number of columns") <|> pure Nothing) <*> choiceParser data Choice = GetSections - | GetTopics - | GetTopic {topic_id :: Integer} + | GetTopics { maxEntries :: Maybe Int } + | GetTopic { topic_id :: Integer } deriving stock (Show) choiceParser :: Parser Choice @@ -41,7 +44,10 @@ choiceParser = foldl1 (<|>) (progDesc "Request list of sections"))) , subparser (command "ts" (info - (pure GetTopics) + (GetTopics <$> (Just <$> option auto (metavar "MAX_ENTRIES_NUMBER" + <> long "max-entries" + <> short 'm' + <> help "max number of entries"))) (progDesc "Request list of topics"))) , subparser (command "t" (info diff --git a/app/Main.hs b/app/Main.hs index cfd0887..39594b2 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -45,11 +45,11 @@ program GetSections = do sections <- Api.getSections withSgr [SetColor Foreground Vivid White] do sequenceA_ $ intersperse (putTextLnO "") (printColor <$> sections) -program GetTopics = do +program GetTopics{maxEntries} = do topics <- Api.getTopics withSgr [SetColor Foreground Vivid White] do sequenceA_ $ intersperse (putTextLnO "") - (fmap printColor . reverse $ sortBy (comparing (.last_activity_date)) topics) + (fmap printColor . maybe id take maxEntries . reverse $ sortBy (comparing (.last_activity_date)) topics) program GetTopic{topic_id} = do topic <- Api.getTopic topic_id withSgr [SetColor Foreground Vivid White] do