Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Tag listing.

  • Loading branch information...
commit 1cb9bc51a804a8f60a721c76d0b70e1c69adb24f 1 parent 69f6eaa
@bjornbm authored
Showing with 22 additions and 12 deletions.
  1. +4 −4 NNUtil.hs
  2. +18 −8 nn.hs
View
8 NNUtil.hs
@@ -33,10 +33,10 @@ filePattern = obsP ++ idP ++ tagP ++ restP
filePattern' = obsP ++ idP ++ tagP -- Allows backup files to match.
filePattern0 = "^" ++ idP ++ tagP -- Don't match obsolete files.
--- | Extract tags from file names.
-tags :: [FilePath] -> [(String, Int)]
-tags = f . group . sort . map (takeWhile (/='-') . tail . dropWhile (/='-'))
- where f xs = zip (map head xs) (map length xs)
+-- | Extract tags from file names and count the number of uses of each tag.
+countTags :: [FilePath] -> [(Int, String)]
+countTags = f . group . sort . map (takeWhile (/='-') . tail . dropWhile (/='-'))
+ where f xs = zip (map length xs) (map head xs)
-- | Create an ID for a new file. Specifically a time stamp
-- based on the current local time with minute precision.
View
26 nn.hs
@@ -9,6 +9,7 @@ import NNUtil
import System.Console.CmdArgs
import System.Directory
import Prelude hiding (all)
+import Text.Printf
import Text.Regex.TDFA
@@ -19,31 +20,29 @@ import Text.Regex.TDFA
data NN = List { all :: Bool, exec :: Maybe String, terms :: [String] }
| Cat { iD :: String }
+ | Tags { popularity :: Bool }
| Junk
deriving (Show, Data, Typeable)
main = do
- mode <- cmdArgs (modes [listMode &= auto, catMode])
+ mode <- cmdArgs (modes [listMode &= auto, catMode, tagsMode])
dir <- getEnv "NN_HOME"
setCurrentDirectory dir
case mode of
List _ _ _ -> list mode
Cat _ -> print mode
+ Tags _ -> tags mode
otherwise -> list mode
+
listMode = List { exec = def &= help "Pass files as arguments to COMMAND" &= typ "COMMAND"
, all = def &= help "Include obsoleted files in search"
, terms = def &= args &= typ "SEARCH TERMS"
}
catMode = Cat { iD = def &= args &= typ "FILE ID" }
-
-getFiles [] = do
- processFiles <$> mdlist
-
-getFiles terms = do
- processFiles <$> mdfind terms
+tagsMode = Tags { popularity = def &= help "Show and sort by the popularity of tags" }
-- List the names of files matching the terms.
list (List _ Nothing terms) = putStr . unlines =<< getFiles terms
@@ -57,5 +56,16 @@ list (List _ (Just exec) terms) = do
ExitSuccess -> return ()
otherwise -> print code
+tags (Tags pop) = do
+ ts <- countTags <$> getFiles []
+ if pop then mapM_ (uncurry (printf "%3d %s\n")) $ reverse $ sort ts
+ else putStr $ unlines $ map snd ts
+
+
+getFiles [] = do
+ processFiles <$> mdlist
+
+getFiles terms = do
+ processFiles <$> mdfind terms
-processFiles = id -- sort . filter (=~ filePattern0) . fmap takeFileName
+processFiles = sort . filter (=~ filePattern0) . fmap takeFileName
Please sign in to comment.
Something went wrong with that request. Please try again.