Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/Streamly/Coreutils/Basename.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- |
-- Module : Streamly.Coreutils.Basename
-- Copyright : (c) 2022 Composewell Technologies
-- License : BSD-3-Clause
-- Maintainer : streamly@composewell.com
-- Stability : experimental
-- Portability : GHC
--
-- Return pathe with any leading directory components removed.
-- If specified, also remove a trailing suffix.

module Streamly.Coreutils.Basename
( basename
, basenameWith

-- * Options
, Basename
, Suffix(..)
, suffix
)
where

import Data.List (stripPrefix)

data Suffix = None | Suffix [Char]

newtype Basename = Basename {removeSuffix :: Suffix}

suffix :: Suffix -> Basename -> Basename
suffix opt cfg = cfg {removeSuffix = opt}

defaultConfig :: Basename
defaultConfig = Basename None

basenameWith :: (Basename -> Basename) -> FilePath -> String
basenameWith f path =
let opt = f defaultConfig
base = reverse $ takeWhile (/= '/') $ reverse path
in case removeSuffix opt of
None -> base
Suffix x ->
let suf = reverse x
val0 = stripPrefix suf $ takeWhile (/= '/') $ reverse path
val = maybe base reverse val0
in val

basename :: FilePath -> String
basename = basenameWith id
1 change: 1 addition & 0 deletions streamly-coreutils.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ library
hs-source-dirs: src
exposed-modules:
Streamly.Coreutils
, Streamly.Coreutils.Basename
, Streamly.Coreutils.Common
, Streamly.Coreutils.Cp
, Streamly.Coreutils.Directory
Expand Down