Commits

paradoxiology  committed 30e6bb9 Merge

Merged haskeline auto command/subreddit complete to the default branch

  • Participants
  • Parent commits 54a66f6, 4fd50d2

Comments (0)

Files changed (3)

File hreddit.cabal

                        time == 1.1.4,
                        old-locale == 1.0.0.2
                        regex-posix == 0.94.4
+                       haskeline == 0.6.4.3
 
   Hs-source-dirs:      src
   

File src/Formatter.hs

                   parseRedditLinks,
                   between,
                   isBetween,
+                  splitOn,
                   glob) where
 
 import Data.List

File src/Navigation.hs

 import RedditActions
 import Formatter(isBetween, between, glob)
 
-import Data.List (intercalate)
+import Data.List (intercalate, isPrefixOf)
 import Data.Char (isSpace, isDigit)
 import Data.Maybe (fromMaybe, listToMaybe)
 
 import System.IO (stdout, hFlush)
 import System.Process (system)
 import System.Console.ANSI
+import System.Console.Haskeline
 import Text.PrettyPrint.ANSI.Leijen (putDoc)
 
 import Control.Monad.State
 
                                    apply cmd index = maybe (throwError "Invalid article index") (return . cmd) (maybeRead index)  
          
+commandList = ["open", "upvote", "downvote", "unvote", "back", "forward", "search", "login", "exit"]
 
+
+-- Popular subreddits
+subredditList = ["3DS", "4chan", "adviceanimals", "android", "apple", "art", "askreddit", "askscience", "atheism", "aww", "beer", "bestof", "blog", "books", "business", "canada", "circlejerk", "coding", "cogsci", "comics", "conspiracy", "cooking", "creepy", "design", "diy", "doesanybodyelse", "earthporn", "economics", "entertainment", "environment", "explainlikeimfive", "fffffffuuuuuuuuuuuu", "firstworldproblems", "fitness", "food", "frugal", "funny", "gadgets", "gaming", "geek", "gifs", "gonewild", "guns", "happy", "haskell", "health", "history", "howto", "humor", "iama", "itookapicture", "kindle", "lgbt", "libertarian", "linux", "listentothis", "lolcats", "loseit", "malefashionadvice", "math", "minecraft", "movies", "music", "netsec", "news", "nsfw", "offbeat", "philosophy", "photography", "physics", "pics", "politics", "programming", "psychology", "reddit.com", "science", "scifi", "seduction", "self", "sex", "shutupandtakemymoney", "skeptic", "space", "sports", "starcraft", "technology", "tf2", "tldr", "todayilearned", "trees", "truereddit", "twoxchromosomes", "videos", "vim", "web_design", "webgames", "wikipedia", "woahdude", "worldnews", "worldpolitics", "wtf", "youshouldknow"]
+
+
+autocompleteLine prev cur
+                | prev == ""   = complete cur commandList
+                | prev == " r" = complete cur subredditList
+                | otherwise    = []
+                    where complete str = (map simpleCompletion) . filter (str `isPrefixOf`)
+
+
+inputSettings :: MonadIO m => Settings m
+inputSettings = Settings { historyFile = Nothing,
+                           complete = completeWordWithPrev Nothing " \t" (\ prev cur -> return $ autocompleteLine prev cur),
+                           autoAddHistory = True }
+
+
+waitForInput :: (MonadIO m, MonadException m) => ReadingState -> InputT m String
+waitForInput rState = do
+                          liftIO $ setCursorColumn 0
+                          cmdStr <- getInputLine $ cmdprompt rState
+                          return $ fromMaybe "" cmdStr
+                          where cmdprompt rState = case rState of
+                                                       ReadingListing _ (Subreddit "")  -> defaultPrompt
+                                                       ReadingListing _ (Subreddit sub) -> sub ++ ">"
+                                                       ReadingListing _ (SearchResults _) -> "results.search>"
+                                                       ReadingComments  _ _ a -> "comments." ++ articelSub a ++ ">"
+                                                       NoState                -> defaultPrompt
+                                                       where defaultPrompt = "reddit>"
+
+
+runInput :: ReadingState -> IO String
+runInput readingState = runInputT inputSettings $ waitForInput readingState
+
+						       
 waitForCommand :: R Command
 waitForCommand = do
                      liftIO $ setCursorColumn 0
 runCommand Exit    = return Exit;
 runCommand command = do
                         execCommand command
-                        command' <- waitForCommand
-                        return command'
+                        readingState <- gets curRState
+                        cmdStr <- liftIO $ runInput readingState
+                        command' <- parseCommand cmdStr
                         `catchError` ( \ err -> do
                                                     liftIO $ putStrLn err
                                                     runCommand NoCommand)
                                
 
+
+                     
+                    
 execCommand :: Command -> R ()
 execCommand (GotoSubreddit subreddit) = do
                                             articleList <- downloadSubreddit subreddit ""