@@ -8,7 +8,6 @@ module Stack.List
88 ) where
99
1010import Pantry ( loadSnapshot )
11- import RIO.List ( intercalate )
1211import qualified RIO.Map as Map
1312import RIO.Process ( HasProcessContext )
1413import Stack.Config ( makeConcreteResolver )
@@ -19,14 +18,17 @@ import Stack.Types.Runner ( Runner, globalOptsL )
1918
2019-- | Type representing exceptions thrown by functions exported by the
2120-- "Stack.List" module.
22- newtype ListException
23- = CouldNotParsePackageSelectors [String ]
21+ newtype ListPrettyException
22+ = CouldNotParsePackageSelectors [StyleDoc ]
2423 deriving (Show , Typeable )
2524
26- instance Exception ListException where
27- displayException (CouldNotParsePackageSelectors strs) = unlines $
28- " Error: [S-4926]"
29- : map (" - " ++ ) strs
25+ instance Pretty ListPrettyException where
26+ pretty (CouldNotParsePackageSelectors errs) =
27+ " [S-4926]"
28+ <> line
29+ <> bulletedList errs
30+
31+ instance Exception ListPrettyException
3032
3133-- | Function underlying the @stack list@ command. List packages.
3234listCmd :: [String ] -> RIO Runner ()
@@ -53,13 +55,13 @@ listPackages mSnapshot input = do
5355 (errs2, locs) <- partitionEithers <$> traverse toLoc names
5456 case errs1 ++ errs2 of
5557 [] -> pure ()
56- errs -> throwM $ CouldNotParsePackageSelectors errs
58+ errs -> prettyThrowM $ CouldNotParsePackageSelectors errs
5759 mapM_ (prettyInfo . fromString . packageIdentifierString) locs
5860 where
5961 toLoc | Just snapshot <- mSnapshot = toLocSnapshot snapshot
6062 | otherwise = toLocNoSnapshot
6163
62- toLocNoSnapshot :: PackageName -> RIO env (Either String PackageIdentifier )
64+ toLocNoSnapshot :: PackageName -> RIO env (Either StyleDoc PackageIdentifier )
6365 toLocNoSnapshot name = do
6466 mloc1 <-
6567 getLatestHackageLocation YesRequireHackageIndex name UsePreferredVersions
@@ -82,31 +84,38 @@ listPackages mSnapshot input = do
8284 case mloc of
8385 Nothing -> do
8486 candidates <- getHackageTypoCorrections name
85- pure $ Left $ concat
86- [ " Could not find package "
87- , packageNameString name
88- , " on Hackage"
87+ pure $ Left $ fillSep
88+ [ flow " Could not find package"
89+ , style Current (fromString $ packageNameString name)
90+ , flow " on Hackage. "
8991 , if null candidates
90- then " "
91- else " . Perhaps you meant: " ++
92- intercalate " , " (map packageNameString candidates)
92+ then mempty
93+ else fillSep $
94+ flow " Perhaps you meant one of:"
95+ : mkNarrativeList (Just Good ) False
96+ (map (fromString . packageNameString) candidates :: [StyleDoc ])
9397 ]
9498 Just loc -> pure $ Right (packageLocationIdent loc)
9599
96100 toLocSnapshot ::
97101 RawSnapshot
98102 -> PackageName
99- -> RIO env (Either String PackageIdentifier )
103+ -> RIO env (Either StyleDoc PackageIdentifier )
100104 toLocSnapshot snapshot name =
101105 case Map. lookup name (rsPackages snapshot) of
102106 Nothing ->
103- pure $ Left $
104- " Package does not appear in snapshot: " ++ packageNameString name
107+ pure $ Left $ fillSep
108+ [ flow " Package does not appear in snapshot:"
109+ , style Current (fromString $ packageNameString name) <> " ."
110+ ]
105111 Just sp -> do
106112 loc <- cplComplete <$> completePackageLocation (rspLocation sp)
107113 pure $ Right (packageLocationIdent loc)
108114
109115 parse s =
110116 case parsePackageName s of
111117 Just x -> Right x
112- Nothing -> Left $ " Could not parse as package name or identifier: " ++ s
118+ Nothing -> Left $ fillSep
119+ [ flow " Could not parse as package name or identifier:"
120+ , style Current (fromString s) <> " ."
121+ ]
0 commit comments