-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
wrap to terminal width #249
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ module Pretty (Doc, Pretty(..), string, text, viaShow, (<+>), (<>), align, hang, | |
import Control.Lens hiding (List) | ||
import Control.Monad.State | ||
import qualified Data.HashMap.Strict as HM | ||
import Data.Maybe (fromMaybe) | ||
import qualified Util.Set as Set | ||
import Prettyprinter hiding (Pretty(..), angles, parens) | ||
import qualified Prettyprinter as PP | ||
|
@@ -19,7 +20,10 @@ import Data.Sequence (Seq) | |
import Data.Text (Text) | ||
import qualified Data.Text as T | ||
import qualified Data.Foldable as F | ||
import System.Environment (lookupEnv) | ||
import System.FilePath (takeFileName) | ||
import System.IO.Unsafe (unsafePerformIO) | ||
import Text.Read (readMaybe) | ||
|
||
import Binding | ||
import Binding.Info | ||
|
@@ -58,8 +62,21 @@ angles doc = text "⟨" <> align (group doc) <> "⟩" | |
vec :: Doc ann -> Doc ann | ||
vec doc = text "[" <> align (group doc) <> "]" | ||
|
||
customLayoutOptions :: LayoutOptions | ||
customLayoutOptions = unsafePerformIO $ do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One concern is that the tests use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, that's because it doesn't work. |
||
columnsMaybeString <- lookupEnv "COLUMNS" | ||
let columnsMaybeInt :: Maybe Int | ||
columnsMaybeInt = do | ||
str <- columnsMaybeString | ||
readMaybe str | ||
let columns :: Int | ||
columns = fromMaybe 80 columnsMaybeInt | ||
|
||
pure $ defaultLayoutOptions | ||
{layoutPageWidth = AvailablePerLine columns 1.0} | ||
|
||
Comment on lines
+65
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider refactoring The function uses Consider refactoring the function to return customLayoutOptions :: IO LayoutOptions
customLayoutOptions = do
columnsMaybeString <- lookupEnv "COLUMNS"
let columnsMaybeInt = readMaybe =<< columnsMaybeString
let columns = fromMaybe 80 columnsMaybeInt
pure $ defaultLayoutOptions { layoutPageWidth = AvailablePerLine columns 1.0 } |
||
pretty :: Pretty ann a => a -> Text | ||
pretty x = renderStrict (layoutPretty defaultLayoutOptions (pp Env.empty x)) | ||
pretty x = renderStrict (layoutPretty customLayoutOptions (pp Env.empty x)) | ||
|
||
prettyPrint :: Pretty ann a => a -> IO () | ||
prettyPrint x = putDoc (pp Env.empty x) | ||
|
@@ -69,7 +86,7 @@ prettyPrintLn x = putDoc (pp Env.empty x) >> putStrLn "" | |
|
||
prettyEnv :: Pretty ann a => Env Var v -> a -> Text | ||
prettyEnv env x = | ||
renderStrict (layoutPretty defaultLayoutOptions (pp (fmap (const ()) env) x)) | ||
renderStrict (layoutPretty customLayoutOptions (pp (fmap (const ()) env) x)) | ||
|
||
prettyPrintEnv :: Pretty ann a => Env Var v -> a -> IO () | ||
prettyPrintEnv env x = | ||
|
@@ -201,7 +218,7 @@ class PrettyBinder ann a | a -> ann where | |
instance PrettyBinder VarInfo a => PrettyBinder VarInfo (TyF a) where | ||
ppBind env t = | ||
let subs = ppBind env <$> t | ||
in (pp env (fst <$> subs), foldMap snd subs) | ||
in (pp env (fst <$> subs), foldMap snd subs) | ||
|
||
newtype BinderPair = BinderPair (Ident, Var) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review the new imports for environment handling and unsafe IO operations.
The addition of
System.Environment
andSystem.IO.Unsafe
is necessary for the new functionality to dynamically fetch the terminal width. However, the use ofunsafePerformIO
should be carefully justified as it can lead to non-deterministic behavior in a purely functional setting.Consider alternatives to
unsafePerformIO
if possible, to maintain the purity of the functions.