Browse Source

Disable output buffering, add arg to enable it.

Josh Bicking 6 years ago
parent
commit
150d9e162a
2 changed files with 15 additions and 9 deletions
  1. 1 1
      README.md
  2. 14 8
      src/Parsing.hs

+ 1 - 1
README.md

@@ -21,4 +21,4 @@ Run `stack exec pi-digits-exe`, or execute the binary found in `./.stack-work/in
 - [X] Separate into different files. Possibly parsing and logic files.
 - [X] Separate into different files. Possibly parsing and logic files.
 - [X] ~~Find a better way to transfer "globals", like `delim` and `printFun`.~~ `parseIndex` handles all parsing and calling logic now.
 - [X] ~~Find a better way to transfer "globals", like `delim` and `printFun`.~~ `parseIndex` handles all parsing and calling logic now.
 - [X] Implement a faster mod operation, to allow for larger numbers (like 12345678901234567890). ~~It will likely be implemented with the algorithm explained in the paper.~~ Used a [slightly faster, less iterative way](https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/fast-modular-exponentiation), implemented with [this gist](https://gist.github.com/trevordixon/6788535).
 - [X] Implement a faster mod operation, to allow for larger numbers (like 12345678901234567890). ~~It will likely be implemented with the algorithm explained in the paper.~~ Used a [slightly faster, less iterative way](https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/fast-modular-exponentiation), implemented with [this gist](https://gist.github.com/trevordixon/6788535).
-- [ ] Customize print behavior and frequency. Flush output every N digits, or something similar.
+- [X] Customize print behavior and frequency. ~~Flush output every N digits, or something similar.~~ Output is flushed as it's written. This can be disabled with `--buffer`.

+ 14 - 8
src/Parsing.hs

@@ -5,7 +5,7 @@ import Numeric (showHex, showIntAtBase)
 import Data.List (isInfixOf, intercalate, genericTake, genericDrop)
 import Data.List (isInfixOf, intercalate, genericTake, genericDrop)
 import Data.List.Split (splitOn)
 import Data.List.Split (splitOn)
 import Text.Read (readMaybe)
 import Text.Read (readMaybe)
-import System.IO (hFlush, stdout)
+import System.IO (stdout, hFlush, hSetBuffering, BufferMode(NoBuffering))
 
 
 import Logic (hexDigits)
 import Logic (hexDigits)
 
 
@@ -17,7 +17,7 @@ import Data.Semigroup ((<>))
 prompt :: (Integer -> String) -> String -> IO ()
 prompt :: (Integer -> String) -> String -> IO ()
 prompt printFun delim = do
 prompt printFun delim = do
   putStr ">> "
   putStr ">> "
-  hFlush stdout
+  hFlush stdout  -- In case buffering is enabled.
   response <- getLine
   response <- getLine
   putStrLn $ parseIndex printFun delim response
   putStrLn $ parseIndex printFun delim response
   prompt printFun delim
   prompt printFun delim
@@ -31,10 +31,10 @@ parseIndex printFun delim response =
       if (isInfixOf ".." response) then
       if (isInfixOf ".." response) then
         let
         let
           range = splitOn ".." response
           range = splitOn ".." response
-          jlow = readMaybe $ range !! 0 :: Maybe Integer
-          jhigh = readMaybe $ range !! 1 :: Maybe Integer
+          jLow = readMaybe $ range !! 0 :: Maybe Integer
+          jHigh = readMaybe $ range !! 1 :: Maybe Integer
         in
         in
-          case (jlow, jhigh) of
+          case (jLow, jHigh) of
             (Just low, Just high) -> getDigitsFrom low high
             (Just low, Just high) -> getDigitsFrom low high
             _ -> []
             _ -> []
       else
       else
@@ -55,7 +55,6 @@ getDigitsFrom low high
   | otherwise = genericDrop low . genericTake high $ hexDigits
   | otherwise = genericDrop low . genericTake high $ hexDigits
 
 
 
 
-
 -- Complain about argument type.
 -- Complain about argument type.
 printErr :: String
 printErr :: String
 printErr = "Error: Please give a positive Integer (ex: 3) or a valid range of positive Integers (ex: 3..5)."
 printErr = "Error: Please give a positive Integer (ex: 3) or a valid range of positive Integers (ex: 3..5)."
@@ -65,7 +64,8 @@ printErr = "Error: Please give a positive Integer (ex: 3) or a valid range of po
 data Arguments = Arguments
 data Arguments = Arguments
   { eval  :: Maybe String
   { eval  :: Maybe String
   , print :: Maybe PrintFunc
   , print :: Maybe PrintFunc
-  , delimiter :: String}
+  , delimiter :: String
+  , buffer :: Bool}
 
 
 arguments :: Parser Arguments
 arguments :: Parser Arguments
 arguments = Arguments
 arguments = Arguments
@@ -77,6 +77,8 @@ arguments = Arguments
                         <> metavar "delim"
                         <> metavar "delim"
                         <> value ""
                         <> value ""
                         <> help "Delimiter to separate printed values.")
                         <> help "Delimiter to separate printed values.")
+      <*> switch (long "buffer"
+               <> help "Don't print output as it's calculated.")
 
 
 
 
 
 
@@ -99,7 +101,7 @@ binPrint = flag' BinPrint ( long "binary"
 
 
 -- Handle args, either prompt or eval & quit.
 -- Handle args, either prompt or eval & quit.
 argHandle :: Arguments -> IO ()
 argHandle :: Arguments -> IO ()
-argHandle (Arguments toEval outputType delim) = do
+argHandle (Arguments toEval outputType delim buff) = do
   let
   let
     printFunIO =
     printFunIO =
       case outputType of
       case outputType of
@@ -114,6 +116,10 @@ argHandle (Arguments toEval outputType delim) = do
           return (\n -> showHex n "")
           return (\n -> showHex n "")
     in do
     in do
     printFun <- printFunIO
     printFun <- printFunIO
+    if buff then
+      return ()
+    else
+      hSetBuffering stdout NoBuffering
     case toEval of
     case toEval of
       Just s -> putStrLn $ parseIndex printFun delim s
       Just s -> putStrLn $ parseIndex printFun delim s
       _ -> do
       _ -> do