@@ -15,15 +15,16 @@ module Pathy.Printer
1515import Prelude
1616
1717import Data.Foldable (fold )
18- import Data.Maybe (Maybe , maybe )
18+ import Data.Maybe (Maybe (..) , maybe )
1919import Data.Monoid (class Monoid )
2020import Data.Newtype (class Newtype , un , unwrap )
2121import Data.String as Str
2222import Data.String.NonEmpty (NonEmptyString )
2323import Data.String.NonEmpty as NES
2424import Partial.Unsafe (unsafePartial )
25+ import Pathy.Name (Name )
2526import Pathy.Path (Path , canonicalize , foldPath , (</>))
26- import Pathy.Phantom (class IsDirOrFile , class IsRelOrAbs , foldDirOrFile , foldRelOrAbs )
27+ import Pathy.Phantom (class IsDirOrFile , class IsRelOrAbs , Dir , foldDirOrFile , foldRelOrAbs , kind DirOrFile , kind RelOrAbs )
2728import Pathy.Sandboxed (SandboxedPath , sandboxRoot , unsandbox )
2829
2930-- | A `Printer` defines options for printing paths.
@@ -45,7 +46,7 @@ type Printer =
4546-- | A printer for POSIX paths.
4647posixPrinter :: Printer
4748posixPrinter =
48- { root: maybe " /" (\name -> " /" <> NES .toString name)
49+ { root: maybe " /" (\name -> " /" <> NES .toString (un Escaper posixEscaper name) )
4950 , current: NES .singleton ' .'
5051 , up: NES .singleton ' .' <> NES .singleton ' .'
5152 , sep: NES .singleton ' /'
@@ -114,17 +115,31 @@ printPathRep
114115 => Printer
115116 -> Path a b
116117 -> String
117- printPathRep printer p = go p
118+ printPathRep printer inputPath = go inputPath
118119 where
119120 go :: forall b' . IsDirOrFile b' => Path a b' -> String
120- go =
121- foldPath
122- (NES .toString (foldRelOrAbs (const (printer.current <> printer.sep)) (const printer.sep) p))
123- (\p' -> go p' <> NES .toString (printer.up <> printer.sep))
124- (\p' ->
125- foldDirOrFile
126- (\d -> go p' <> printSegment printer d <> NES .toString printer.sep)
127- (\f -> go p' <> printSegment printer f))
121+ go = foldPath caseCurrent caseParentOf caseIn
122+
123+ isAbs :: Boolean
124+ isAbs = foldRelOrAbs (const false ) (const true ) inputPath
125+
126+ caseCurrent :: String
127+ caseCurrent = if isAbs
128+ then printer.root Nothing
129+ else NES .toString $ printer.current <> printer.sep
130+
131+ caseParentOf :: Path a Dir -> String
132+ caseParentOf p = go p <> NES .toString (printer.up <> printer.sep)
133+
134+ caseIn :: forall b' . IsDirOrFile b' => Path a Dir -> Name b' -> String
135+ caseIn p name = name # foldDirOrFile
136+ (\dirName -> p # foldPath
137+ (if isAbs
138+ then printer.root (Just $ unwrap dirName) <> NES .toString printer.sep
139+ else caseCurrent <> printSegment printer dirName <> NES .toString printer.sep)
140+ (\p' -> caseParentOf p' <> printSegment printer dirName <> NES .toString printer.sep)
141+ (\p' n' -> caseIn p' n' <> printSegment printer dirName <> NES .toString printer.sep))
142+ (\fileName -> go p <> printSegment printer fileName)
128143
129144-- | Prints a name as a `String` using the escaper from the specified printer.
130145printSegment :: forall name . Newtype name NonEmptyString => Printer -> name -> String
0 commit comments