Skip to content

Commit 04466b6

Browse files
committed
fix windowsPrinter
1 parent 9d4b016 commit 04466b6

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

src/Pathy/Printer.purs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ module Pathy.Printer
1515
import Prelude
1616

1717
import Data.Foldable (fold)
18-
import Data.Maybe (Maybe, maybe)
18+
import Data.Maybe (Maybe(..), maybe)
1919
import Data.Monoid (class Monoid)
2020
import Data.Newtype (class Newtype, un, unwrap)
2121
import Data.String as Str
2222
import Data.String.NonEmpty (NonEmptyString)
2323
import Data.String.NonEmpty as NES
2424
import Partial.Unsafe (unsafePartial)
25+
import Pathy.Name (Name)
2526
import 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)
2728
import 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.
4647
posixPrinter :: Printer
4748
posixPrinter =
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.
130145
printSegment :: forall name. Newtype name NonEmptyString => Printer -> name -> String

test/Main.purs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Data.String as Str
1212
import Data.String.NonEmpty (NonEmptyString)
1313
import Data.String.NonEmpty as NES
1414
import Data.Symbol (SProxy(..))
15-
import Pathy (class IsDirOrFile, class IsRelOrAbs, Abs, Dir, Name(..), Path, Rel, alterExtension, canonicalize, currentDir, debugPrintPath, dir, extension, file, parentOf, parseAbsDir, parseAbsFile, parseRelDir, parseRelFile, posixParser, posixPrinter, printPath, relativeTo, rename, rootDir, sandbox, unsandbox, (<..>), (<.>), (</>), joinName, splitName)
15+
import Pathy (class IsDirOrFile, class IsRelOrAbs, Abs, Dir, Name(..), Path, Rel, alterExtension, canonicalize, currentDir, debugPrintPath, dir, extension, file, joinName, parentOf, parseAbsDir, parseAbsFile, parseRelDir, parseRelFile, posixParser, posixPrinter, printPath, relativeTo, rename, rootDir, sandbox, splitName, unsandbox, windowsPrinter, (<..>), (<.>), (</>))
1616
import Pathy.Gen as PG
1717
import Pathy.Name (reflectName)
1818
import Test.QuickCheck ((===))
@@ -146,6 +146,10 @@ main = do
146146
test' "(</>) - two directories"
147147
(dirFoo </> dirBar)
148148
"./foo/bar/"
149+
150+
test "windowsPrinter"
151+
(printWindowsPath $ rootDir </> dir (SProxy :: SProxy "C") </> dirBar)
152+
"C:\\bar\\"
149153

150154
test' "(</>) - file with two parents"
151155
(dirFoo
@@ -369,3 +373,6 @@ main = do
369373

370374
printTestPath :: forall a b. IsRelOrAbs a => IsDirOrFile b => Path a b -> String
371375
printTestPath p = debugPrintPath posixPrinter p
376+
377+
printWindowsPath :: forall a b. IsRelOrAbs a => IsDirOrFile b => Path a b -> String
378+
printWindowsPath p = debugPrintPath windowsPrinter p

0 commit comments

Comments
 (0)