Skip to content

Commit

Permalink
Add userModule
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Jun 16, 2024
1 parent c953460 commit 944c3b9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import Prompt

main = do
run $
path
( segment Dull Blue currentDirectoryModule
<> ( gitBranchModule
>>= ( \branch ->
segment Dull Cyan (textModule ("\xe725 " ++ branch))
)
)
)
path $
segment Dull Magenta userModule
<> ( currentDirectoryModule
>>= \dir -> segment Dull Blue $ textModule $ " \xf07b " ++ dir
)
<> ( gitBranchModule
>>= \branch -> segment Dull Cyan $ textModule $ " \xe725 " ++ branch
)
```

### Cloning from source
Expand Down
13 changes: 11 additions & 2 deletions app/Prompt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Prompt
textColor,
currentDirectoryModule,
gitBranchModule,
userModule,
run,
Segment (..),
path,
Expand All @@ -23,6 +24,7 @@ import Data.Time (defaultTimeLocale, formatTime, getCurrentTime)
import GHC.IO.Exception (ExitCode)
import System.Console.ANSI.Codes (Color (..), ColorIntensity (..), ConsoleIntensity (BoldIntensity), ConsoleLayer (Background, Foreground), SGR (Reset, SetColor, SetConsoleIntensity), setSGRCode)
import System.Directory (getCurrentDirectory)
import System.Environment (getEnv)
import System.Exit (ExitCode (ExitSuccess))
import System.FilePath (takeFileName)
import System.Process (readProcessWithExitCode)
Expand Down Expand Up @@ -78,6 +80,15 @@ gitBranchModule =
else Nothing
in Prompt f

userModule :: Prompt String
userModule =
let f = do
result <- try (getEnv "USER") :: IO (Either SomeException String)
return $ case result of
Left _ -> Nothing
Right name -> Just name
in Prompt f

run :: Prompt String -> IO ()
run (Prompt f) = f >>= \s -> putStr (fromMaybe "" s)

Expand All @@ -101,7 +112,6 @@ timeModule =
return $ Just timeStr
in Prompt f


data Segment = Segment ColorIntensity Color (Prompt String)

segment :: ColorIntensity -> Color -> Prompt String -> Prompt [Segment]
Expand Down Expand Up @@ -129,7 +139,6 @@ pathSegment intensity color isFirst next m =
Just (nextIntensity, nextColor) -> backgroundColor nextIntensity nextColor x
Nothing -> x


safeHead :: [a] -> Maybe a
safeHead [] = Nothing
safeHead (x : _) = Just x

0 comments on commit 944c3b9

Please sign in to comment.