@@ -66,19 +66,19 @@ watchDirectory dir allow = do
6666 await
6767 pure await
6868
69- watcher :: FilePath -> Int -> IO ()
70- watcher dir port = do
69+ watcher :: Maybe FilePath -> FilePath -> Int -> IO ()
70+ watcher initialFile dir port = do
7171 sock <- socket AF_INET Stream 0
7272 setSocketOption sock ReuseAddr 1
7373 let bindLoop port =
7474 (port <$ bind sock (SockAddrInet (fromIntegral port) iNADDR_ANY))
7575 <|> bindLoop (port + 1 ) -- try the next port if that fails
7676 chosenPort <- bindLoop port
7777 listen sock 2
78- serverLoop dir sock chosenPort
78+ serverLoop initialFile dir sock chosenPort
7979
80- serverLoop :: FilePath -> Socket -> Int -> IO ()
81- serverLoop dir sock port = do
80+ serverLoop :: Maybe FilePath -> FilePath -> Socket -> Int -> IO ()
81+ serverLoop initialFile dir sock port = do
8282 Console. setTitle " Unison"
8383 Console. clearScreen
8484 Console. setCursorPosition 0 0
@@ -93,37 +93,45 @@ serverLoop dir sock port = do
9393 (_input, output) <- N. socketToStreams socket
9494 d <- watchDirectory dir (" .u" `isSuffixOf` )
9595 n <- newIORef (0 :: Int )
96- (`finally` P. terminateProcess ph) . forever $ do
97- (sourceFile, source0) <- d
98- let source = Text. unpack source0
99- Console. clearScreen
100- Console. setCursorPosition 0 0
101- marker <- do
102- n0 <- readIORef n
103- writeIORef n (n0 + 1 )
104- pure [" 🌻🌸🌵🌺🌴" !! (n0 `mod` 5 )]
105- -- pure ["🕐🕑🕒🕓🕔🕕🕖🕗🕘🕙🕚🕛" !! (n0 `mod` 12)]
106- Console. setTitle " Unison"
107- putStrLn " "
108- putStrLn $ marker ++ " " ++ sourceFile ++ " has changed, reloading...\n "
109- parseResult <- Parsers. readAndParseFile @ Symbol Parser. penv0 sourceFile
110- case parseResult of
111- Left parseError -> do
112- Console. setTitle " Unison \128721"
113- putStrLn $ parseErrorToAnsiString source parseError
114- Right (env0, unisonFile) -> do
115- let (Result notes' r) = FileParsers. serializeUnisonFile unisonFile
116- showNote notes =
117- intercalateMap " \n\n " (printNoteWithSourceAsAnsi env0 source) notes
118- putStrLn . showNote . toList $ notes'
119- case r of
120- Nothing -> do
96+ let go sourceFile source0 = do
97+ let source = Text. unpack source0
98+ Console. clearScreen
99+ Console. setCursorPosition 0 0
100+ marker <- do
101+ n0 <- readIORef n
102+ writeIORef n (n0 + 1 )
103+ pure [" 🌻🌸🌵🌺🌴" !! (n0 `mod` 5 )]
104+ -- pure ["🕐🕑🕒🕓🕔🕕🕖🕗🕘🕙🕚🕛" !! (n0 `mod` 12)]
105+ Console. setTitle " Unison"
106+ putStrLn " "
107+ putStrLn $ marker ++ " " ++ sourceFile ++ " has changed, reloading...\n "
108+ parseResult <- Parsers. readAndParseFile @ Symbol Parser. penv0 sourceFile
109+ case parseResult of
110+ Left parseError -> do
121111 Console. setTitle " Unison \128721"
122- pure () -- just await next change
123- Just (_unisonFile', _typ, bs) -> do
124- Console. setTitle " Unison ✅"
125- putStrLn " ✅ Your program typechecks!"
126- putStrLn " Any watch expressions (lines starting with `>`) are shown below.\n "
127- Streams. write (Just bs) output
128- -- todo: read from input to get the response and then show that
129- -- for this we need a deserializer for Unison terms, mirroring what is in Unison.Codecs.hs
112+ putStrLn $ parseErrorToAnsiString source parseError
113+ Right (env0, unisonFile) -> do
114+ let (Result notes' r) = FileParsers. serializeUnisonFile unisonFile
115+ showNote notes =
116+ intercalateMap " \n\n " (printNoteWithSourceAsAnsi env0 source) notes
117+ putStrLn . showNote . toList $ notes'
118+ case r of
119+ Nothing -> do
120+ Console. setTitle " Unison \128721"
121+ pure () -- just await next change
122+ Just (_unisonFile', _typ, bs) -> do
123+ Console. setTitle " Unison ✅"
124+ putStrLn " ✅ Your program typechecks!"
125+ putStrLn " Any watch expressions (lines starting with `>`) are shown below.\n "
126+ Streams. write (Just bs) output
127+ -- todo: read from input to get the response and then show that
128+ -- for this we need a deserializer for Unison terms, mirroring what is in Unison.Codecs.hs
129+ (`finally` P. terminateProcess ph) $ do
130+ case initialFile of
131+ Just sourceFile -> do
132+ contents <- Data.Text.IO. readFile sourceFile
133+ go sourceFile contents
134+ Nothing -> pure ()
135+ forever $ do
136+ (sourceFile, contents) <- d
137+ go sourceFile contents
0 commit comments