-- Tn.Main - main module for Tn. -- Copyright 2014 Peter Harpending. -- -- This program is free software: you can redistribute it and/or -- modify it under the terms of the GNU General Public License as -- published by the Free Software Foundation, either version 3 of the -- License, or (at your option) any later version. -- -- This program is distributed in the hope that it will be useful, but -- WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -- General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with this program. If not, see -- . module Main where import System.Directory import System.Environment import Tn.IO import Tn.Journal -- |The main function. Runs the program. main :: IO () main = getArgs >>= parseArgs -- |Print help. usage :: String -> IO () usage = putStrLn . documentation -- |Get documentation for a command. documentation :: String -> String documentation "main" = "Usage: tn (list-entries|list-journals|new-entry|new-journal)\nSee `tn usage` for more specific usage." documentation "list-entries" = "Usage: tn list-entries JOURNAL" documentation "list-journals" = "Usage: tn list-journals" documentation "new-journal" = "Usage: tn new-journal NAME [force]" documentation "new-entry" = "Usage: tn new-entry JOURNAL" -- |Parses the command line arguments parseArgs :: [String] -> IO () parseArgs (cmd:rest) | "list-entries" == cmd = listEntries rest | "le" == cmd = listEntries rest | "list-journals" == cmd = listJournals rest | "lj" == cmd = listJournals rest | "new-journal" == cmd = newJournal rest | "nj" == cmd = newJournal rest | "new-entry" == cmd = newEntry rest | "ne" == cmd = newEntry rest | otherwise = usage "main" parseArgs _ = usage "main" -- |List the entries -- listEntires :: [String] -> IO () listEntries ("usage":_) = usage "list-entries" listEntries ("--usage":_) = usage "list-entries" listEntries ("help":_) = usage "list-entries" listEntries ("--help":_) = usage "list-entries" listEntries (journalNom:_) = do jpath <- getJournalPathFromName journalNom journal <- readJournalFromFile jpath let entriesl = (entries journal) entriess = map show entriesl mapM_ putStrLn entriess listEntries _ = usage "list-entries" -- |List the journals listJournals :: [String] -> IO () listJournals ("usage":_) = usage "list-journals" listJournals ("--usage":_) = usage "list-journals" listJournals ("help":_) = usage "list-journals" listJournals ("--help":_) = usage "list-journals" listJournals _ = do home <- getHomeDirectory let fullpath = home ++ "/.tn.d/journals/" dirContents <- (getDirectoryContents fullpath) mapM_ putStrLn $ map (init . init . init) $ drop 2 dirContents -- |Make a new entry (impure). newEntry :: [String] -> IO () newEntry ("usage":_) = usage "new-entry" newEntry ("--usage":_) = usage "new-entry" newEntry ("help":_) = usage "new-entry" newEntry ("--help":_) = usage "new-entry" newEntry (journalNom:entryPath:_) = do jpath <- getJournalPathFromName journalNom journal <- readJournalFromFile jpath newJournal <- getNewEntry journal entryPath writeJournal newJournal newEntry _ = usage "new-entry" -- |Make a new journal (impure). newJournal :: [String] -> IO () newJournal ("usage":_) = usage "new-journal" newJournal ("--usage":_) = usage "new-journal" newJournal ("help":_) = usage "new-journal" newJournal ("--help":_) = usage "new-journal" newJournal (nom:"force":_) = writeNewJournal nom True newJournal (nom:"--force":_) = writeNewJournal nom True newJournal (nom:_) = writeNewJournal nom False