From 522c294ca813630240fd9dc8278d714a23105c45 Mon Sep 17 00:00:00 2001 From: mbays Date: Sat, 25 Dec 2021 00:00:00 +0000 Subject: [PATCH] add referring to end of list with $ --- Command.hs | 6 ++++-- CommandLine.hs | 10 ++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Command.hs b/Command.hs index 034f0b9..3ef4a4c 100644 --- a/Command.hs +++ b/Command.hs @@ -189,8 +189,10 @@ helpOn s = , " Specify first match of pattern with \"^pattern^\"," , " or all matches with \"^^pattern^\"." , " Specify multiple items or ranges by separating with \",\"." - , " Examples: \"1,3-5,^pattern^-\" refers to links 1,3,4,5 and all links from the" - , " the first match of pattern onwards, and \"~-\" refers to the whole queue." + , " Last item is denoted by \"$\", and nth from last by \"$n\"." + , " Examples: \"1,3-5,^pattern^-$2\" refers to links 1,3,4,5 and all links from" + , " the first match of pattern to the penultimate item;" + , " \"~-\" refers to the whole queue." , "" , " Patterns are (extended) regular expressions (see `man 7 regex`)." , " Matching is case-insensitive unless pattern contains an uppercase character." diff --git a/CommandLine.hs b/CommandLine.hs index a4d639c..e60f3e3 100644 --- a/CommandLine.hs +++ b/CommandLine.hs @@ -32,6 +32,7 @@ import Text.Parsec.String (Parser) data ElemSpec = ESNum Int + | ESNumFromEnd Int | ESSearch String deriving (Eq,Ord,Show) @@ -64,6 +65,7 @@ resolveElemsSpecs typeStr match as ess = resolveES :: ElemSpec -> Either String Int resolveES (ESNum n) = return $ n - 1 + resolveES (ESNumFromEnd n) = return $ length as - n resolveES (ESSearch s) = maybe (Left $ "No " <> typeStr <> " matches pattern: " ++ s) Right $ headMay . map fst . filter snd . zip [0..] $ (s `match`) <$> as @@ -142,6 +144,10 @@ patt = escapedWhile (noneOf "^ ") <* optional (char '^') elemSpec :: Parser ElemSpec elemSpec = choice [ ESNum <$> nat + , char '$' >> ESNumFromEnd <$> choice + [ nat + , (+ 1) <$> countMany (string "$") + ] , char '^' >> ESSearch <$> patt ] elemsSpec :: Parser ElemsSpec @@ -170,9 +176,9 @@ ref = escapedArgStartingWith . oneOf baseTarget :: Parser PTarget baseTarget = choice - [ PTargetLinks False PTargetCurr <$> elemsSpecs + [ PTargetLog <$> elemsSpecsBy (string "$") + , PTargetLinks False PTargetCurr <$> elemsSpecs , PTargetRef PTargetCurr <$> ref "./?" - , PTargetLog <$> elemsSpecsBy (string "$") , PTargetQueue <$> elemsSpecsBy (string "~") , char '\'' >> choice [ char '\'' >> return PTargetJumpBack -- 2.11.4.GIT