<commit_before>\chapter{EventLoop module}

\begin{code}

module EventLoop ( eventLoop )
    where

import Shell ( Shell, getEnv, setEnv, getAllEnv )
import Prompt ( prompt )
import System.Directory ( getCurrentDirectory, setCurrentDirectory,
                          findExecutable, doesFileExist, doesDirectoryExist )
import System.IO ( hFlush, hIsEOF, stdin, stdout, stderr )
import System.IO.Unsafe ( unsafeInterleaveIO )
import System.Process ( runProcess, waitForProcess )
import Control.Monad.Trans ( liftIO )

-- we should STRIP SPACES before and after
process :: [String] -> Shell Bool -- do we quit or not?
process ["exit"] = return True
process (s:ss) | s == "cd" = chDir ss >> return False
               | otherwise = tryToRun s ss >> return False
process [] = return False

chDir :: [String] -> Shell ()
chDir ("-":_) = do Just dir <- getEnv "OLDPWD"
                   chDir' dir
chDir (dir:_) = do chDir' dir
chDir []      = do Just home <- getEnv "HOME"
                   chDir [home]

chDir' dir = do exists <- liftIO $ doesDirectoryExist dir
                if exists
                   then go
                   else err $ "cd: "++dir++": No such file or directory"
    where go = do olddir <- liftIO $ getCurrentDirectory<commit_msg>sketch in a few primitive functions.<commit_after>\chapter{EventLoop module}

\begin{code}

module EventLoop ( eventLoop )
    where

import Shell ( Shell, getEnv, setEnv, getAllEnv )
import Prompt ( prompt )
import System.Directory ( getCurrentDirectory, setCurrentDirectory,
                          getDirectoryContents,
                          findExecutable, doesFileExist, doesDirectoryExist )
import List ( sort )
import System.IO ( hFlush, hIsEOF, stdin, stdout, stderr )
import System.IO.Unsafe ( unsafeInterleaveIO )
import System.Process ( runProcess, waitForProcess )
import Control.Monad.Trans ( liftIO )

-- we should STRIP SPACES before and after
process :: [String] -> Shell Bool -- do we quit or not?
process ["exit"] = return True
process ["pwd"] = do liftIO getCurrentDirectory >>= liftIO . putStrLn
                     return False
process ["ls"] = do let unboring ('.':_) = False
                        unboring _ = True
                    liftIO (getDirectoryContents ".") >>=
                           liftIO . putStr . unlines . sort . filter unboring
                    return False
process (s:ss) | s == "cd" = chDir ss >> return False
               | otherwise = tryToRun s ss >> return False
process [] = return False

chDir :: [String] -> Shell ()
chDir ("-":_) = do Just dir <- getEnv "OLDPWD"
                   chDir' dir
chDir (dir:_) = do chDir' dir
chDir []      = do Just home <- getEnv "HOME"
                   chDir [home]

chDir' dir = do exists <- liftIO $ doesDirectoryExist dir
                if exists
                   then go
                   else err $ "cd: "++dir++": No such file or directory"
    where go = do olddir <- liftIO $ getCurrentDirectory
{'fraction_non_alphanumeric': 0.10175202156334232, 'fraction_numerical': 0.0, 'mean_word_length': 2.3397075365579303, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5644501', 'n_tokens_mistral': 845, 'n_tokens_neox': 818, 'n_words': 289}