More refactoring
[xyz.git] / app / Main.hs
index 4ddb724..07f31d8 100644 (file)
@@ -3,12 +3,12 @@ module Main where
 import Data.List (intercalate)
 import Data.String hiding (unlines)
 import Data.Text hiding (map, init, length, splitAt)
-import Data.Text.IO (putStrLn, putStr)
+import Data.Text.IO (putStrLn, putStr, writeFile)
 import Data.Text.Lazy (toStrict)
 import Data.Text.Template
 import Data.Time.Clock.POSIX
 import Lib
-import Prelude hiding (putStrLn, putStr, init, unlines)
+import Prelude hiding (putStrLn, putStr, init, unlines, writeFile)
 import Prompts
 import System.Directory
 import System.Directory (doesFileExist)
@@ -39,7 +39,7 @@ init = do
             themes <- allFilesIn $ home ++ "/.xyz/themes"
             projectSpec <- initPrompt (map pack themes)
             createDirectory "entries"
-            writeFile "xyz.json" . encode . toJSObject $ projectSpec
+            writeFile "xyz.json" . pack . encode . toJSObject $ projectSpec
             putStrLn "\n\nInitialised empty project"
 
 build :: IO ()
@@ -58,40 +58,28 @@ entry = do
   homeDir <- getHomeDirectory
   entrySpec <- entryPrompt
   entryNum <- fmap (show . (+1) . length) $ allFilesIn "entries"
-  stamp <- fmap (show . round) getPOSIXTime
+  stamp <- fmap (pack . show . round) getPOSIXTime
   videoTemplate <- readFile (homeDir ++ "/.xyz/entry-video.txt") >>= return . template . fromString
   normalTemplate <- readFile (homeDir ++ "/.xyz/entry.txt") >>= return . template . fromString
-  --let filename = "entries/" ++ (show entryNum) ++ "-" ++ (unpack . (toLower. replace " " "-") $ pack entryName) ++ ".txt"
 
-  case entrySpec of
-    Just (title, videoSpec) -> do
-      let filename = "entries/" ++ entryNum ++ "-" ++ (unpack . slugify $ title) ++ ".txt"
-      case videoSpec of
-        Just (videoId, videoFilename) -> putStrLn "oh jimmy"
-        Nothing -> putStrLn "oh bimmy"
+  let rawEntry = case entrySpec of
+        Just (title, videoSpec) -> do
+          let baseContext = [("timestamp", stamp), ("title", title)]
+          case videoSpec of
+            Just (videoId, videoFilename) -> (title, toStrict . render videoTemplate $ context $ baseContext ++ [("videoid", videoId), ("videofilename", videoFilename)])
+            Nothing -> (title, toStrict . render normalTemplate $ context baseContext)
 
+  let fileName = "entries/" ++ entryNum ++ "-" ++ (unpack . slugify $ fst rawEntry) ++ ".txt"
+  writeFile fileName $ snd rawEntry
 
-  putStrLn "lel"
-  -- case video of
-  --    Just (videoId, videoFilename) -> do
-  --      let tmplContext = context $ [("timestamp", fromString . show $ (stamp::Integer)), ("title", pack entryName), ("videofilename", pack $ videoFilename), ("videoid", pack videoId)]
-  --      let rawEntry = unpack . toStrict $ render videoTemplate tmplContext
-  --      writeFile filename rawEntry
-  --      callCommand $ "youtube-dl -f 'webm[height<1080]+bestaudio' \"https://www.youtube.com/watch?v=" ++ videoId ++ "\" -o 'build/videos/" ++ videoFilename ++ "'"
-  --    Nothing -> do
-  --      let tmplContext = context $ [("timestamp", fromString . show $ (stamp::Integer)), ("title", pack entryName)]
-  --      let rawEntry = unpack . toStrict $ render normalTemplate tmplContext
-  --      writeFile filename rawEntry
+  callCommand $ "nano " ++ fileName
 
-  -- (putStrLn $ ("Created " ++ filename)) >> build
+  push <- pushPrompt
+  case push of
+    True -> do callCommand . unpack $ "git add . && git commit -m \"" +++ (fst rawEntry) +++ "\" && git push"
+    False -> return ()
 
-  -- push <- yornPrompt "Do you want to push this entry?"
-
-  -- case push of
-  --   True -> do callCommand $ "git add . && git commit -m \"" ++ entryName ++ "\" && git push"
-  --   False -> return ()
-
-  -- putStrLn "Thanks for using xyz!"
+  putStrLn "Thanks for using xyz!"
 
 usage :: IO ()
 usage =  putStr . ununlines $ [