{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
module Brick.Widgets.FileBrowser
(
FileBrowser
, FileInfo(..)
, FileStatus(..)
, FileType(..)
, newFileBrowser
, selectNonDirectories
, selectDirectories
, setWorkingDirectory
, getWorkingDirectory
, updateFileBrowserSearch
, setFileBrowserEntryFilter
, actionFileBrowserBeginSearch
, actionFileBrowserSelectEnter
, actionFileBrowserSelectCurrent
, actionFileBrowserListPageUp
, actionFileBrowserListPageDown
, actionFileBrowserListHalfPageUp
, actionFileBrowserListHalfPageDown
, actionFileBrowserListTop
, actionFileBrowserListBottom
, actionFileBrowserListNext
, actionFileBrowserListPrev
, handleFileBrowserEvent
, maybeSelectCurrentEntry
, renderFileBrowser
, fileBrowserCursor
, fileBrowserIsSearching
, fileBrowserSelection
, fileBrowserException
, fileBrowserSelectable
, fileInfoFileType
, fileBrowserAttr
, fileBrowserCurrentDirectoryAttr
, fileBrowserSelectionInfoAttr
, fileBrowserSelectedAttr
, fileBrowserDirectoryAttr
, fileBrowserBlockDeviceAttr
, fileBrowserRegularFileAttr
, fileBrowserCharacterDeviceAttr
, fileBrowserNamedPipeAttr
, fileBrowserSymbolicLinkAttr
, fileBrowserUnixSocketAttr
, fileTypeMatch
, fileExtensionMatch
, fileBrowserEntryFilterL
, fileBrowserSelectableL
, fileInfoFilenameL
, fileInfoSanitizedFilenameL
, fileInfoFilePathL
, fileInfoFileStatusL
, fileInfoLinkTargetTypeL
, fileStatusSizeL
, fileStatusFileTypeL
, prettyFileSize
, entriesForDirectory
, getFileInfo
)
where
import qualified Control.Exception as E
import Control.Monad (forM)
import Control.Monad.IO.Class (liftIO)
import Data.Char (toLower, isPrint)
import Data.Maybe (fromMaybe, isJust, fromJust)
import qualified Data.Foldable as F
import qualified Data.Text as T
#if !(MIN_VERSION_base(4,11,0))
import Data.Monoid
#endif
import Data.Int (Int64)
import Data.List (sortBy, isSuffixOf)
import qualified Data.Set as Set
import qualified Data.Vector as V
import Lens.Micro
import qualified Graphics.Vty as Vty
import qualified System.Directory as D
import qualified System.Posix.Files as U
import qualified System.Posix.Types as U
import qualified System.FilePath as FP
import Text.Printf (printf)
import Brick.Types
import Brick.AttrMap (AttrName)
import Brick.Widgets.Core
import Brick.Widgets.List
data FileBrowser n =
FileBrowser { FileBrowser n -> FilePath
fileBrowserWorkingDirectory :: FilePath
, FileBrowser n -> List n FileInfo
fileBrowserEntries :: List n FileInfo
, FileBrowser n -> [FileInfo]
fileBrowserLatestResults :: [FileInfo]
, FileBrowser n -> Set FilePath
fileBrowserSelectedFiles :: Set.Set String
, FileBrowser n -> n
fileBrowserName :: n
, FileBrowser n -> Maybe (FileInfo -> Bool)
fileBrowserEntryFilter :: Maybe (FileInfo -> Bool)
, FileBrowser n -> Maybe Text
fileBrowserSearchString :: Maybe T.Text
, FileBrowser n -> Maybe IOException
fileBrowserException :: Maybe E.IOException
, FileBrowser n -> FileInfo -> Bool
fileBrowserSelectable :: FileInfo -> Bool
}
data FileStatus =
FileStatus { FileStatus -> Int64
fileStatusSize :: Int64
, FileStatus -> Maybe FileType
fileStatusFileType :: Maybe FileType
}
deriving (Int -> FileStatus -> ShowS
[FileStatus] -> ShowS
FileStatus -> FilePath
(Int -> FileStatus -> ShowS)
-> (FileStatus -> FilePath)
-> ([FileStatus] -> ShowS)
-> Show FileStatus
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [FileStatus] -> ShowS
$cshowList :: [FileStatus] -> ShowS
show :: FileStatus -> FilePath
$cshow :: FileStatus -> FilePath
showsPrec :: Int -> FileStatus -> ShowS
$cshowsPrec :: Int -> FileStatus -> ShowS
Show, FileStatus -> FileStatus -> Bool
(FileStatus -> FileStatus -> Bool)
-> (FileStatus -> FileStatus -> Bool) -> Eq FileStatus
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FileStatus -> FileStatus -> Bool
$c/= :: FileStatus -> FileStatus -> Bool
== :: FileStatus -> FileStatus -> Bool
$c== :: FileStatus -> FileStatus -> Bool
Eq)
data FileInfo =
FileInfo { FileInfo -> FilePath
fileInfoFilename :: String
, FileInfo -> FilePath
fileInfoSanitizedFilename :: String
, FileInfo -> FilePath
fileInfoFilePath :: FilePath
, FileInfo -> Either IOException FileStatus
fileInfoFileStatus :: Either E.IOException FileStatus
, FileInfo -> Maybe FileType
fileInfoLinkTargetType :: Maybe FileType
}
deriving (Int -> FileInfo -> ShowS
[FileInfo] -> ShowS
FileInfo -> FilePath
(Int -> FileInfo -> ShowS)
-> (FileInfo -> FilePath) -> ([FileInfo] -> ShowS) -> Show FileInfo
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [FileInfo] -> ShowS
$cshowList :: [FileInfo] -> ShowS
show :: FileInfo -> FilePath
$cshow :: FileInfo -> FilePath
showsPrec :: Int -> FileInfo -> ShowS
$cshowsPrec :: Int -> FileInfo -> ShowS
Show, FileInfo -> FileInfo -> Bool
(FileInfo -> FileInfo -> Bool)
-> (FileInfo -> FileInfo -> Bool) -> Eq FileInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FileInfo -> FileInfo -> Bool
$c/= :: FileInfo -> FileInfo -> Bool
== :: FileInfo -> FileInfo -> Bool
$c== :: FileInfo -> FileInfo -> Bool
Eq)
data FileType =
RegularFile
| BlockDevice
| CharacterDevice
| NamedPipe
| Directory
| SymbolicLink
| UnixSocket
deriving (ReadPrec [FileType]
ReadPrec FileType
Int -> ReadS FileType
ReadS [FileType]
(Int -> ReadS FileType)
-> ReadS [FileType]
-> ReadPrec FileType
-> ReadPrec [FileType]
-> Read FileType
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [FileType]
$creadListPrec :: ReadPrec [FileType]
readPrec :: ReadPrec FileType
$creadPrec :: ReadPrec FileType
readList :: ReadS [FileType]
$creadList :: ReadS [FileType]
readsPrec :: Int -> ReadS FileType
$creadsPrec :: Int -> ReadS FileType
Read, Int -> FileType -> ShowS
[FileType] -> ShowS
FileType -> FilePath
(Int -> FileType -> ShowS)
-> (FileType -> FilePath) -> ([FileType] -> ShowS) -> Show FileType
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
showList :: [FileType] -> ShowS
$cshowList :: [FileType] -> ShowS
show :: FileType -> FilePath
$cshow :: FileType -> FilePath
showsPrec :: Int -> FileType -> ShowS
$cshowsPrec :: Int -> FileType -> ShowS
Show, FileType -> FileType -> Bool
(FileType -> FileType -> Bool)
-> (FileType -> FileType -> Bool) -> Eq FileType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FileType -> FileType -> Bool
$c/= :: FileType -> FileType -> Bool
== :: FileType -> FileType -> Bool
$c== :: FileType -> FileType -> Bool
Eq)
suffixLenses ''FileBrowser
suffixLenses ''FileInfo
suffixLenses ''FileStatus
newFileBrowser :: (FileInfo -> Bool)
-> n
-> Maybe FilePath
-> IO (FileBrowser n)
newFileBrowser :: (FileInfo -> Bool) -> n -> Maybe FilePath -> IO (FileBrowser n)
newFileBrowser FileInfo -> Bool
selPredicate n
name Maybe FilePath
mCwd = do
FilePath
initialCwd <- case Maybe FilePath
mCwd of
Just FilePath
path -> FilePath -> IO FilePath
forall (m :: * -> *) a. Monad m => a -> m a
return FilePath
path
Maybe FilePath
Nothing -> IO FilePath
D.getCurrentDirectory
let b :: FileBrowser n
b = FileBrowser :: forall n.
FilePath
-> List n FileInfo
-> [FileInfo]
-> Set FilePath
-> n
-> Maybe (FileInfo -> Bool)
-> Maybe Text
-> Maybe IOException
-> (FileInfo -> Bool)
-> FileBrowser n
FileBrowser { fileBrowserWorkingDirectory :: FilePath
fileBrowserWorkingDirectory = FilePath
initialCwd
, fileBrowserEntries :: List n FileInfo
fileBrowserEntries = n -> Vector FileInfo -> Int -> List n FileInfo
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list n
name Vector FileInfo
forall a. Monoid a => a
mempty Int
1
, fileBrowserLatestResults :: [FileInfo]
fileBrowserLatestResults = [FileInfo]
forall a. Monoid a => a
mempty
, fileBrowserSelectedFiles :: Set FilePath
fileBrowserSelectedFiles = Set FilePath
forall a. Monoid a => a
mempty
, fileBrowserName :: n
fileBrowserName = n
name
, fileBrowserEntryFilter :: Maybe (FileInfo -> Bool)
fileBrowserEntryFilter = Maybe (FileInfo -> Bool)
forall a. Maybe a
Nothing
, fileBrowserSearchString :: Maybe Text
fileBrowserSearchString = Maybe Text
forall a. Maybe a
Nothing
, fileBrowserException :: Maybe IOException
fileBrowserException = Maybe IOException
forall a. Maybe a
Nothing
, fileBrowserSelectable :: FileInfo -> Bool
fileBrowserSelectable = FileInfo -> Bool
selPredicate
}
FilePath -> FileBrowser n -> IO (FileBrowser n)
forall n. FilePath -> FileBrowser n -> IO (FileBrowser n)
setWorkingDirectory FilePath
initialCwd FileBrowser n
b
selectNonDirectories :: FileInfo -> Bool
selectNonDirectories :: FileInfo -> Bool
selectNonDirectories FileInfo
i =
case FileInfo -> Maybe FileType
fileInfoFileType FileInfo
i of
Just FileType
Directory -> Bool
False
Just FileType
SymbolicLink ->
case FileInfo -> Maybe FileType
fileInfoLinkTargetType FileInfo
i of
Just FileType
Directory -> Bool
False
Maybe FileType
_ -> Bool
True
Maybe FileType
_ -> Bool
True
selectDirectories :: FileInfo -> Bool
selectDirectories :: FileInfo -> Bool
selectDirectories FileInfo
i =
case FileInfo -> Maybe FileType
fileInfoFileType FileInfo
i of
Just FileType
Directory -> Bool
True
Just FileType
SymbolicLink ->
case FileInfo -> Maybe FileType
fileInfoLinkTargetType FileInfo
i of
Just FileType
Directory -> Bool
True
Maybe FileType
_ -> Bool
False
Maybe FileType
_ -> Bool
False
setFileBrowserEntryFilter :: Maybe (FileInfo -> Bool) -> FileBrowser n -> FileBrowser n
setFileBrowserEntryFilter :: Maybe (FileInfo -> Bool) -> FileBrowser n -> FileBrowser n
setFileBrowserEntryFilter Maybe (FileInfo -> Bool)
f FileBrowser n
b =
FileBrowser n -> FileBrowser n
forall n. FileBrowser n -> FileBrowser n
applyFilterAndSearch (FileBrowser n -> FileBrowser n) -> FileBrowser n -> FileBrowser n
forall a b. (a -> b) -> a -> b
$ FileBrowser n
b FileBrowser n -> (FileBrowser n -> FileBrowser n) -> FileBrowser n
forall a b. a -> (a -> b) -> b
& (Maybe (FileInfo -> Bool) -> Identity (Maybe (FileInfo -> Bool)))
-> FileBrowser n -> Identity (FileBrowser n)
forall n. Lens' (FileBrowser n) (Maybe (FileInfo -> Bool))
fileBrowserEntryFilterL ((Maybe (FileInfo -> Bool) -> Identity (Maybe (FileInfo -> Bool)))
-> FileBrowser n -> Identity (FileBrowser n))
-> Maybe (FileInfo -> Bool) -> FileBrowser n -> FileBrowser n
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Maybe (FileInfo -> Bool)
f
setWorkingDirectory :: FilePath -> FileBrowser n -> IO (FileBrowser n)
setWorkingDirectory :: FilePath -> FileBrowser n -> IO (FileBrowser n)
setWorkingDirectory FilePath
path FileBrowser n
b = do
Either IOException [FileInfo]
entriesResult <- IO [FileInfo] -> IO (Either IOException [FileInfo])
forall e a. Exception e => IO a -> IO (Either e a)
E.try (IO [FileInfo] -> IO (Either IOException [FileInfo]))
-> IO [FileInfo] -> IO (Either IOException [FileInfo])
forall a b. (a -> b) -> a -> b
$ FilePath -> IO [FileInfo]
entriesForDirectory FilePath
path
let ([FileInfo]
entries, Maybe IOException
exc) = case Either IOException [FileInfo]
entriesResult of
Left (e::E.IOException) -> ([], IOException -> Maybe IOException
forall a. a -> Maybe a
Just IOException
e)
Right [FileInfo]
es -> ([FileInfo]
es, Maybe IOException
forall a. Maybe a
Nothing)
[FileInfo]
allEntries <- if FilePath
path FilePath -> FilePath -> Bool
forall a. Eq a => a -> a -> Bool
== FilePath
"/" then [FileInfo] -> IO [FileInfo]
forall (m :: * -> *) a. Monad m => a -> m a
return [FileInfo]
entries else do
Either IOException FileInfo
parentResult <- IO FileInfo -> IO (Either IOException FileInfo)
forall e a. Exception e => IO a -> IO (Either e a)
E.try (IO FileInfo -> IO (Either IOException FileInfo))
-> IO FileInfo -> IO (Either IOException FileInfo)
forall a b. (a -> b) -> a -> b
$ FilePath -> IO FileInfo
parentOf FilePath
path
[FileInfo] -> IO [FileInfo]
forall (m :: * -> *) a. Monad m => a -> m a
return ([FileInfo] -> IO [FileInfo]) -> [FileInfo] -> IO [FileInfo]
forall a b. (a -> b) -> a -> b
$ case Either IOException FileInfo
parentResult of
Left (IOException
_::E.IOException) -> [FileInfo]
entries
Right FileInfo
parent -> FileInfo
parent FileInfo -> [FileInfo] -> [FileInfo]
forall a. a -> [a] -> [a]
: [FileInfo]
entries
let b' :: FileBrowser n
b' = [FileInfo] -> FileBrowser n -> FileBrowser n
forall n. [FileInfo] -> FileBrowser n -> FileBrowser n
setEntries [FileInfo]
allEntries FileBrowser n
b
FileBrowser n -> IO (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return (FileBrowser n -> IO (FileBrowser n))
-> FileBrowser n -> IO (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ FileBrowser n
b' FileBrowser n -> (FileBrowser n -> FileBrowser n) -> FileBrowser n
forall a b. a -> (a -> b) -> b
& (FilePath -> Identity FilePath)
-> FileBrowser n -> Identity (FileBrowser n)
forall n. Lens' (FileBrowser n) FilePath
fileBrowserWorkingDirectoryL ((FilePath -> Identity FilePath)
-> FileBrowser n -> Identity (FileBrowser n))
-> FilePath -> FileBrowser n -> FileBrowser n
forall s t a b. ASetter s t a b -> b -> s -> t
.~ FilePath
path
FileBrowser n -> (FileBrowser n -> FileBrowser n) -> FileBrowser n
forall a b. a -> (a -> b) -> b
& (Maybe IOException -> Identity (Maybe IOException))
-> FileBrowser n -> Identity (FileBrowser n)
forall n. Lens' (FileBrowser n) (Maybe IOException)
fileBrowserExceptionL ((Maybe IOException -> Identity (Maybe IOException))
-> FileBrowser n -> Identity (FileBrowser n))
-> Maybe IOException -> FileBrowser n -> FileBrowser n
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Maybe IOException
exc
FileBrowser n -> (FileBrowser n -> FileBrowser n) -> FileBrowser n
forall a b. a -> (a -> b) -> b
& (Set FilePath -> Identity (Set FilePath))
-> FileBrowser n -> Identity (FileBrowser n)
forall n. Lens' (FileBrowser n) (Set FilePath)
fileBrowserSelectedFilesL ((Set FilePath -> Identity (Set FilePath))
-> FileBrowser n -> Identity (FileBrowser n))
-> Set FilePath -> FileBrowser n -> FileBrowser n
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Set FilePath
forall a. Monoid a => a
mempty
parentOf :: FilePath -> IO FileInfo
parentOf :: FilePath -> IO FileInfo
parentOf FilePath
path = FilePath -> FilePath -> IO FileInfo
getFileInfo FilePath
".." (FilePath -> IO FileInfo) -> FilePath -> IO FileInfo
forall a b. (a -> b) -> a -> b
$ ShowS
FP.takeDirectory FilePath
path
getFileInfo :: String
-> FilePath
-> IO FileInfo
getFileInfo :: FilePath -> FilePath -> IO FileInfo
getFileInfo FilePath
name = [FilePath] -> FilePath -> IO FileInfo
go []
where
go :: [FilePath] -> FilePath -> IO FileInfo
go [FilePath]
history FilePath
fullPath = do
FilePath
filePath <- FilePath -> IO FilePath
D.makeAbsolute FilePath
fullPath
Either IOException FileStatus
statusResult <- IO FileStatus -> IO (Either IOException FileStatus)
forall e a. Exception e => IO a -> IO (Either e a)
E.try (IO FileStatus -> IO (Either IOException FileStatus))
-> IO FileStatus -> IO (Either IOException FileStatus)
forall a b. (a -> b) -> a -> b
$ FilePath -> IO FileStatus
U.getSymbolicLinkStatus FilePath
filePath
let stat :: Either IOException FileStatus
stat = do
FileStatus
status <- Either IOException FileStatus
statusResult
let U.COff Int64
sz = FileStatus -> COff
U.fileSize FileStatus
status
FileStatus -> Either IOException FileStatus
forall (m :: * -> *) a. Monad m => a -> m a
return FileStatus :: Int64 -> Maybe FileType -> FileStatus
FileStatus { fileStatusFileType :: Maybe FileType
fileStatusFileType = FileStatus -> Maybe FileType
fileTypeFromStatus FileStatus
status
, fileStatusSize :: Int64
fileStatusSize = Int64
sz
}
Maybe FileType
targetTy <- case FileStatus -> Maybe FileType
fileStatusFileType (FileStatus -> Maybe FileType)
-> Either IOException FileStatus
-> Either IOException (Maybe FileType)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Either IOException FileStatus
stat of
Right (Just FileType
SymbolicLink) -> do
Either SomeException FilePath
targetPathResult <- IO FilePath -> IO (Either SomeException FilePath)
forall e a. Exception e => IO a -> IO (Either e a)
E.try (IO FilePath -> IO (Either SomeException FilePath))
-> IO FilePath -> IO (Either SomeException FilePath)
forall a b. (a -> b) -> a -> b
$ FilePath -> IO FilePath
U.readSymbolicLink FilePath
filePath
case Either SomeException FilePath
targetPathResult of
Left (SomeException
_::E.SomeException) -> Maybe FileType -> IO (Maybe FileType)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe FileType
forall a. Maybe a
Nothing
Right FilePath
targetPath ->
if FilePath
targetPath FilePath -> [FilePath] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [FilePath]
history
then Maybe FileType -> IO (Maybe FileType)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe FileType
forall a. Maybe a
Nothing
else do
FileInfo
targetInfo <- IO FileInfo -> IO FileInfo
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FileInfo -> IO FileInfo) -> IO FileInfo -> IO FileInfo
forall a b. (a -> b) -> a -> b
$ [FilePath] -> FilePath -> IO FileInfo
go (FilePath
fullPath FilePath -> [FilePath] -> [FilePath]
forall a. a -> [a] -> [a]
: [FilePath]
history) FilePath
targetPath
case FileInfo -> Either IOException FileStatus
fileInfoFileStatus FileInfo
targetInfo of
Right (FileStatus Int64
_ Maybe FileType
targetTy) -> Maybe FileType -> IO (Maybe FileType)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe FileType
targetTy
Either IOException FileStatus
_ -> Maybe FileType -> IO (Maybe FileType)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe FileType
forall a. Maybe a
Nothing
Either IOException (Maybe FileType)
_ -> Maybe FileType -> IO (Maybe FileType)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe FileType
forall a. Maybe a
Nothing
FileInfo -> IO FileInfo
forall (m :: * -> *) a. Monad m => a -> m a
return FileInfo :: FilePath
-> FilePath
-> FilePath
-> Either IOException FileStatus
-> Maybe FileType
-> FileInfo
FileInfo { fileInfoFilename :: FilePath
fileInfoFilename = FilePath
name
, fileInfoFilePath :: FilePath
fileInfoFilePath = FilePath
filePath
, fileInfoSanitizedFilename :: FilePath
fileInfoSanitizedFilename = ShowS
sanitizeFilename FilePath
name
, fileInfoFileStatus :: Either IOException FileStatus
fileInfoFileStatus = Either IOException FileStatus
stat
, fileInfoLinkTargetType :: Maybe FileType
fileInfoLinkTargetType = Maybe FileType
targetTy
}
fileInfoFileType :: FileInfo -> Maybe FileType
fileInfoFileType :: FileInfo -> Maybe FileType
fileInfoFileType FileInfo
i =
case FileInfo -> Either IOException FileStatus
fileInfoFileStatus FileInfo
i of
Left IOException
_ -> Maybe FileType
forall a. Maybe a
Nothing
Right FileStatus
stat -> FileStatus -> Maybe FileType
fileStatusFileType FileStatus
stat
getWorkingDirectory :: FileBrowser n -> FilePath
getWorkingDirectory :: FileBrowser n -> FilePath
getWorkingDirectory = FileBrowser n -> FilePath
forall n. FileBrowser n -> FilePath
fileBrowserWorkingDirectory
setEntries :: [FileInfo] -> FileBrowser n -> FileBrowser n
setEntries :: [FileInfo] -> FileBrowser n -> FileBrowser n
setEntries [FileInfo]
es FileBrowser n
b =
FileBrowser n -> FileBrowser n
forall n. FileBrowser n -> FileBrowser n
applyFilterAndSearch (FileBrowser n -> FileBrowser n) -> FileBrowser n -> FileBrowser n
forall a b. (a -> b) -> a -> b
$ FileBrowser n
b FileBrowser n -> (FileBrowser n -> FileBrowser n) -> FileBrowser n
forall a b. a -> (a -> b) -> b
& ([FileInfo] -> Identity [FileInfo])
-> FileBrowser n -> Identity (FileBrowser n)
forall n. Lens' (FileBrowser n) [FileInfo]
fileBrowserLatestResultsL (([FileInfo] -> Identity [FileInfo])
-> FileBrowser n -> Identity (FileBrowser n))
-> [FileInfo] -> FileBrowser n -> FileBrowser n
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [FileInfo]
es
fileBrowserIsSearching :: FileBrowser n -> Bool
fileBrowserIsSearching :: FileBrowser n -> Bool
fileBrowserIsSearching FileBrowser n
b = Maybe Text -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Text -> Bool) -> Maybe Text -> Bool
forall a b. (a -> b) -> a -> b
$ FileBrowser n
bFileBrowser n
-> Getting (Maybe Text) (FileBrowser n) (Maybe Text) -> Maybe Text
forall s a. s -> Getting a s a -> a
^.Getting (Maybe Text) (FileBrowser n) (Maybe Text)
forall n. Lens' (FileBrowser n) (Maybe Text)
fileBrowserSearchStringL
fileBrowserSelection :: FileBrowser n -> [FileInfo]
fileBrowserSelection :: FileBrowser n -> [FileInfo]
fileBrowserSelection FileBrowser n
b =
let getEntry :: FilePath -> FileInfo
getEntry FilePath
filename = Maybe FileInfo -> FileInfo
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe FileInfo -> FileInfo) -> Maybe FileInfo -> FileInfo
forall a b. (a -> b) -> a -> b
$ (FileInfo -> Bool) -> [FileInfo] -> Maybe FileInfo
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
F.find ((FilePath -> FilePath -> Bool
forall a. Eq a => a -> a -> Bool
== FilePath
filename) (FilePath -> Bool) -> (FileInfo -> FilePath) -> FileInfo -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FileInfo -> FilePath
fileInfoFilename) ([FileInfo] -> Maybe FileInfo) -> [FileInfo] -> Maybe FileInfo
forall a b. (a -> b) -> a -> b
$ FileBrowser n
bFileBrowser n
-> Getting [FileInfo] (FileBrowser n) [FileInfo] -> [FileInfo]
forall s a. s -> Getting a s a -> a
^.Getting [FileInfo] (FileBrowser n) [FileInfo]
forall n. Lens' (FileBrowser n) [FileInfo]
fileBrowserLatestResultsL
in (FilePath -> FileInfo) -> [FilePath] -> [FileInfo]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FilePath -> FileInfo
getEntry ([FilePath] -> [FileInfo]) -> [FilePath] -> [FileInfo]
forall a b. (a -> b) -> a -> b
$ Set FilePath -> [FilePath]
forall (t :: * -> *) a. Foldable t => t a -> [a]
F.toList (Set FilePath -> [FilePath]) -> Set FilePath -> [FilePath]
forall a b. (a -> b) -> a -> b
$ FileBrowser n
bFileBrowser n
-> Getting (Set FilePath) (FileBrowser n) (Set FilePath)
-> Set FilePath
forall s a. s -> Getting a s a -> a
^.Getting (Set FilePath) (FileBrowser n) (Set FilePath)
forall n. Lens' (FileBrowser n) (Set FilePath)
fileBrowserSelectedFilesL
updateFileBrowserSearch :: (Maybe T.Text -> Maybe T.Text)
-> FileBrowser n
-> FileBrowser n
updateFileBrowserSearch :: (Maybe Text -> Maybe Text) -> FileBrowser n -> FileBrowser n
updateFileBrowserSearch Maybe Text -> Maybe Text
f FileBrowser n
b =
let old :: Maybe Text
old = FileBrowser n
bFileBrowser n
-> Getting (Maybe Text) (FileBrowser n) (Maybe Text) -> Maybe Text
forall s a. s -> Getting a s a -> a
^.Getting (Maybe Text) (FileBrowser n) (Maybe Text)
forall n. Lens' (FileBrowser n) (Maybe Text)
fileBrowserSearchStringL
new :: Maybe Text
new = Maybe Text -> Maybe Text
f (Maybe Text -> Maybe Text) -> Maybe Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ FileBrowser n
bFileBrowser n
-> Getting (Maybe Text) (FileBrowser n) (Maybe Text) -> Maybe Text
forall s a. s -> Getting a s a -> a
^.Getting (Maybe Text) (FileBrowser n) (Maybe Text)
forall n. Lens' (FileBrowser n) (Maybe Text)
fileBrowserSearchStringL
oldLen :: Int
oldLen = Int -> (Text -> Int) -> Maybe Text -> Int
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
0 Text -> Int
T.length Maybe Text
old
newLen :: Int
newLen = Int -> (Text -> Int) -> Maybe Text -> Int
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
0 Text -> Int
T.length Maybe Text
new
in if Maybe Text
old Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe Text
new
then FileBrowser n
b
else if Int
oldLen Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
newLen
then FileBrowser n
b FileBrowser n -> (FileBrowser n -> FileBrowser n) -> FileBrowser n
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text))
-> FileBrowser n -> Identity (FileBrowser n)
forall n. Lens' (FileBrowser n) (Maybe Text)
fileBrowserSearchStringL ((Maybe Text -> Identity (Maybe Text))
-> FileBrowser n -> Identity (FileBrowser n))
-> Maybe Text -> FileBrowser n -> FileBrowser n
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Maybe Text
new
else FileBrowser n -> FileBrowser n
forall n. FileBrowser n -> FileBrowser n
applyFilterAndSearch (FileBrowser n -> FileBrowser n) -> FileBrowser n -> FileBrowser n
forall a b. (a -> b) -> a -> b
$ FileBrowser n
b FileBrowser n -> (FileBrowser n -> FileBrowser n) -> FileBrowser n
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text))
-> FileBrowser n -> Identity (FileBrowser n)
forall n. Lens' (FileBrowser n) (Maybe Text)
fileBrowserSearchStringL ((Maybe Text -> Identity (Maybe Text))
-> FileBrowser n -> Identity (FileBrowser n))
-> Maybe Text -> FileBrowser n -> FileBrowser n
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Maybe Text
new
applyFilterAndSearch :: FileBrowser n -> FileBrowser n
applyFilterAndSearch :: FileBrowser n -> FileBrowser n
applyFilterAndSearch FileBrowser n
b =
let filterMatch :: FileInfo -> Bool
filterMatch = (FileInfo -> Bool) -> Maybe (FileInfo -> Bool) -> FileInfo -> Bool
forall a. a -> Maybe a -> a
fromMaybe (Bool -> FileInfo -> Bool
forall a b. a -> b -> a
const Bool
True) (FileBrowser n
bFileBrowser n
-> Getting
(Maybe (FileInfo -> Bool))
(FileBrowser n)
(Maybe (FileInfo -> Bool))
-> Maybe (FileInfo -> Bool)
forall s a. s -> Getting a s a -> a
^.Getting
(Maybe (FileInfo -> Bool))
(FileBrowser n)
(Maybe (FileInfo -> Bool))
forall n. Lens' (FileBrowser n) (Maybe (FileInfo -> Bool))
fileBrowserEntryFilterL)
searchMatch :: FileInfo -> Bool
searchMatch = (FileInfo -> Bool)
-> (Text -> FileInfo -> Bool) -> Maybe Text -> FileInfo -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Bool -> FileInfo -> Bool
forall a b. a -> b -> a
const Bool
True)
(\Text
search FileInfo
i -> (Text -> Text
T.toLower Text
search Text -> Text -> Bool
`T.isInfixOf` (FilePath -> Text
T.pack (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ Char -> Char
toLower (Char -> Char) -> ShowS
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FileInfo -> FilePath
fileInfoSanitizedFilename FileInfo
i)))
(FileBrowser n
bFileBrowser n
-> Getting (Maybe Text) (FileBrowser n) (Maybe Text) -> Maybe Text
forall s a. s -> Getting a s a -> a
^.Getting (Maybe Text) (FileBrowser n) (Maybe Text)
forall n. Lens' (FileBrowser n) (Maybe Text)
fileBrowserSearchStringL)
match :: FileInfo -> Bool
match FileInfo
i = FileInfo -> Bool
filterMatch FileInfo
i Bool -> Bool -> Bool
&& FileInfo -> Bool
searchMatch FileInfo
i
matching :: [FileInfo]
matching = (FileInfo -> Bool) -> [FileInfo] -> [FileInfo]
forall a. (a -> Bool) -> [a] -> [a]
filter FileInfo -> Bool
match ([FileInfo] -> [FileInfo]) -> [FileInfo] -> [FileInfo]
forall a b. (a -> b) -> a -> b
$ FileBrowser n
bFileBrowser n
-> Getting [FileInfo] (FileBrowser n) [FileInfo] -> [FileInfo]
forall s a. s -> Getting a s a -> a
^.Getting [FileInfo] (FileBrowser n) [FileInfo]
forall n. Lens' (FileBrowser n) [FileInfo]
fileBrowserLatestResultsL
in FileBrowser n
b { fileBrowserEntries :: List n FileInfo
fileBrowserEntries = n -> Vector FileInfo -> Int -> List n FileInfo
forall (t :: * -> *) n e.
Foldable t =>
n -> t e -> Int -> GenericList n t e
list (FileBrowser n
bFileBrowser n -> Getting n (FileBrowser n) n -> n
forall s a. s -> Getting a s a -> a
^.Getting n (FileBrowser n) n
forall n. Lens' (FileBrowser n) n
fileBrowserNameL) ([FileInfo] -> Vector FileInfo
forall a. [a] -> Vector a
V.fromList [FileInfo]
matching) Int
1 }
prettyFileSize :: Int64
-> T.Text
prettyFileSize :: Int64 -> Text
prettyFileSize Int64
i
| Int64
i Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Int64
2 Int64 -> Int64 -> Int64
forall a b. (Num a, Integral b) => a -> b -> a
^ (Int64
40::Int64) = FilePath -> Text
T.pack (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ Double -> FilePath
format (Int64
i Int64 -> Double -> Double
`divBy` (Double
2 Double -> Double -> Double
forall a. Floating a => a -> a -> a
** Double
40)) FilePath -> ShowS
forall a. Semigroup a => a -> a -> a
<> FilePath
"T"
| Int64
i Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Int64
2 Int64 -> Int64 -> Int64
forall a b. (Num a, Integral b) => a -> b -> a
^ (Int64
30::Int64) = FilePath -> Text
T.pack (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ Double -> FilePath
format (Int64
i Int64 -> Double -> Double
`divBy` (Double
2 Double -> Double -> Double
forall a. Floating a => a -> a -> a
** Double
30)) FilePath -> ShowS
forall a. Semigroup a => a -> a -> a
<> FilePath
"G"
| Int64
i Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Int64
2 Int64 -> Int64 -> Int64
forall a b. (Num a, Integral b) => a -> b -> a
^ (Int64
20::Int64) = FilePath -> Text
T.pack (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ Double -> FilePath
format (Int64
i Int64 -> Double -> Double
`divBy` (Double
2 Double -> Double -> Double
forall a. Floating a => a -> a -> a
** Double
20)) FilePath -> ShowS
forall a. Semigroup a => a -> a -> a
<> FilePath
"M"
| Int64
i Int64 -> Int64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Int64
2 Int64 -> Int64 -> Int64
forall a b. (Num a, Integral b) => a -> b -> a
^ (Int64
10::Int64) = FilePath -> Text
T.pack (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ Double -> FilePath
format (Int64
i Int64 -> Double -> Double
`divBy` (Double
2 Double -> Double -> Double
forall a. Floating a => a -> a -> a
** Double
10)) FilePath -> ShowS
forall a. Semigroup a => a -> a -> a
<> FilePath
"K"
| Bool
otherwise = FilePath -> Text
T.pack (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ Int64 -> FilePath
forall a. Show a => a -> FilePath
show Int64
i FilePath -> ShowS
forall a. Semigroup a => a -> a -> a
<> FilePath
" bytes"
where
format :: Double -> FilePath
format = FilePath -> Double -> FilePath
forall r. PrintfType r => FilePath -> r
printf FilePath
"%0.1f"
divBy :: Int64 -> Double -> Double
divBy :: Int64 -> Double -> Double
divBy Int64
a Double
b = ((Int64 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
a) :: Double) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Double
b
entriesForDirectory :: FilePath -> IO [FileInfo]
entriesForDirectory :: FilePath -> IO [FileInfo]
entriesForDirectory FilePath
rawPath = do
FilePath
path <- FilePath -> IO FilePath
D.makeAbsolute FilePath
rawPath
[FilePath]
dirContents <- FilePath -> IO [FilePath]
D.listDirectory FilePath
path
[FileInfo]
infos <- [FilePath] -> (FilePath -> IO FileInfo) -> IO [FileInfo]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [FilePath]
dirContents ((FilePath -> IO FileInfo) -> IO [FileInfo])
-> (FilePath -> IO FileInfo) -> IO [FileInfo]
forall a b. (a -> b) -> a -> b
$ \FilePath
f -> do
FilePath -> FilePath -> IO FileInfo
getFileInfo FilePath
f (FilePath
path FilePath -> ShowS
FP.</> FilePath
f)
let dirsFirst :: FileInfo -> FileInfo -> Ordering
dirsFirst FileInfo
a FileInfo
b = if FileInfo -> Maybe FileType
fileInfoFileType FileInfo
a Maybe FileType -> Maybe FileType -> Bool
forall a. Eq a => a -> a -> Bool
== FileType -> Maybe FileType
forall a. a -> Maybe a
Just FileType
Directory Bool -> Bool -> Bool
&&
FileInfo -> Maybe FileType
fileInfoFileType FileInfo
b Maybe FileType -> Maybe FileType -> Bool
forall a. Eq a => a -> a -> Bool
== FileType -> Maybe FileType
forall a. a -> Maybe a
Just FileType
Directory
then FilePath -> FilePath -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Char -> Char
toLower (Char -> Char) -> ShowS
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FileInfo -> FilePath
fileInfoFilename FileInfo
a)
(Char -> Char
toLower (Char -> Char) -> ShowS
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FileInfo -> FilePath
fileInfoFilename FileInfo
b)
else if FileInfo -> Maybe FileType
fileInfoFileType FileInfo
a Maybe FileType -> Maybe FileType -> Bool
forall a. Eq a => a -> a -> Bool
== FileType -> Maybe FileType
forall a. a -> Maybe a
Just FileType
Directory Bool -> Bool -> Bool
&&
FileInfo -> Maybe FileType
fileInfoFileType FileInfo
b Maybe FileType -> Maybe FileType -> Bool
forall a. Eq a => a -> a -> Bool
/= FileType -> Maybe FileType
forall a. a -> Maybe a
Just FileType
Directory
then Ordering
LT
else if FileInfo -> Maybe FileType
fileInfoFileType FileInfo
b Maybe FileType -> Maybe FileType -> Bool
forall a. Eq a => a -> a -> Bool
== FileType -> Maybe FileType
forall a. a -> Maybe a
Just FileType
Directory Bool -> Bool -> Bool
&&
FileInfo -> Maybe FileType
fileInfoFileType FileInfo
a Maybe FileType -> Maybe FileType -> Bool
forall a. Eq a => a -> a -> Bool
/= FileType -> Maybe FileType
forall a. a -> Maybe a
Just FileType
Directory
then Ordering
GT
else FilePath -> FilePath -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Char -> Char
toLower (Char -> Char) -> ShowS
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FileInfo -> FilePath
fileInfoFilename FileInfo
a)
(Char -> Char
toLower (Char -> Char) -> ShowS
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FileInfo -> FilePath
fileInfoFilename FileInfo
b)
allEntries :: [FileInfo]
allEntries = (FileInfo -> FileInfo -> Ordering) -> [FileInfo] -> [FileInfo]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy FileInfo -> FileInfo -> Ordering
dirsFirst [FileInfo]
infos
[FileInfo] -> IO [FileInfo]
forall (m :: * -> *) a. Monad m => a -> m a
return [FileInfo]
allEntries
fileTypeFromStatus :: U.FileStatus -> Maybe FileType
fileTypeFromStatus :: FileStatus -> Maybe FileType
fileTypeFromStatus FileStatus
s =
if | FileStatus -> Bool
U.isBlockDevice FileStatus
s -> FileType -> Maybe FileType
forall a. a -> Maybe a
Just FileType
BlockDevice
| FileStatus -> Bool
U.isCharacterDevice FileStatus
s -> FileType -> Maybe FileType
forall a. a -> Maybe a
Just FileType
CharacterDevice
| FileStatus -> Bool
U.isNamedPipe FileStatus
s -> FileType -> Maybe FileType
forall a. a -> Maybe a
Just FileType
NamedPipe
| FileStatus -> Bool
U.isRegularFile FileStatus
s -> FileType -> Maybe FileType
forall a. a -> Maybe a
Just FileType
RegularFile
| FileStatus -> Bool
U.isDirectory FileStatus
s -> FileType -> Maybe FileType
forall a. a -> Maybe a
Just FileType
Directory
| FileStatus -> Bool
U.isSocket FileStatus
s -> FileType -> Maybe FileType
forall a. a -> Maybe a
Just FileType
UnixSocket
| FileStatus -> Bool
U.isSymbolicLink FileStatus
s -> FileType -> Maybe FileType
forall a. a -> Maybe a
Just FileType
SymbolicLink
| Bool
otherwise -> Maybe FileType
forall a. Maybe a
Nothing
fileBrowserCursor :: FileBrowser n -> Maybe FileInfo
fileBrowserCursor :: FileBrowser n -> Maybe FileInfo
fileBrowserCursor FileBrowser n
b = (Int, FileInfo) -> FileInfo
forall a b. (a, b) -> b
snd ((Int, FileInfo) -> FileInfo)
-> Maybe (Int, FileInfo) -> Maybe FileInfo
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GenericList n Vector FileInfo -> Maybe (Int, FileInfo)
forall (t :: * -> *) n e.
(Splittable t, Foldable t) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement (FileBrowser n
bFileBrowser n
-> Getting
(GenericList n Vector FileInfo)
(FileBrowser n)
(GenericList n Vector FileInfo)
-> GenericList n Vector FileInfo
forall s a. s -> Getting a s a -> a
^.Getting
(GenericList n Vector FileInfo)
(FileBrowser n)
(GenericList n Vector FileInfo)
forall n. Lens' (FileBrowser n) (List n FileInfo)
fileBrowserEntriesL)
actionFileBrowserBeginSearch :: FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserBeginSearch :: FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserBeginSearch FileBrowser n
b =
FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return (FileBrowser n -> EventM n (FileBrowser n))
-> FileBrowser n -> EventM n (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ (Maybe Text -> Maybe Text) -> FileBrowser n -> FileBrowser n
forall n.
(Maybe Text -> Maybe Text) -> FileBrowser n -> FileBrowser n
updateFileBrowserSearch (Maybe Text -> Maybe Text -> Maybe Text
forall a b. a -> b -> a
const (Maybe Text -> Maybe Text -> Maybe Text)
-> Maybe Text -> Maybe Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"") FileBrowser n
b
actionFileBrowserSelectEnter :: FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserSelectEnter :: FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserSelectEnter FileBrowser n
b =
FileBrowser n -> EventM n (FileBrowser n)
forall n. FileBrowser n -> EventM n (FileBrowser n)
maybeSelectCurrentEntry FileBrowser n
b
actionFileBrowserSelectCurrent :: FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserSelectCurrent :: FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserSelectCurrent FileBrowser n
b =
FileBrowser n -> EventM n (FileBrowser n)
forall n. FileBrowser n -> EventM n (FileBrowser n)
selectCurrentEntry FileBrowser n
b
actionFileBrowserListPageUp :: Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListPageUp :: FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListPageUp FileBrowser n
b = do
let old :: List n FileInfo
old = FileBrowser n
b FileBrowser n
-> Getting (List n FileInfo) (FileBrowser n) (List n FileInfo)
-> List n FileInfo
forall s a. s -> Getting a s a -> a
^. Getting (List n FileInfo) (FileBrowser n) (List n FileInfo)
forall n. Lens' (FileBrowser n) (List n FileInfo)
fileBrowserEntriesL
List n FileInfo
new <- List n FileInfo -> EventM n (List n FileInfo)
forall (t :: * -> *) n e.
(Foldable t, Splittable t, Ord n) =>
GenericList n t e -> EventM n (GenericList n t e)
listMovePageUp List n FileInfo
old
FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return (FileBrowser n -> EventM n (FileBrowser n))
-> FileBrowser n -> EventM n (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ FileBrowser n
b FileBrowser n -> (FileBrowser n -> FileBrowser n) -> FileBrowser n
forall a b. a -> (a -> b) -> b
& (List n FileInfo -> Identity (List n FileInfo))
-> FileBrowser n -> Identity (FileBrowser n)
forall n. Lens' (FileBrowser n) (List n FileInfo)
fileBrowserEntriesL ((List n FileInfo -> Identity (List n FileInfo))
-> FileBrowser n -> Identity (FileBrowser n))
-> List n FileInfo -> FileBrowser n -> FileBrowser n
forall s t a b. ASetter s t a b -> b -> s -> t
.~ List n FileInfo
new
actionFileBrowserListPageDown :: Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListPageDown :: FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListPageDown FileBrowser n
b = do
let old :: List n FileInfo
old = FileBrowser n
b FileBrowser n
-> Getting (List n FileInfo) (FileBrowser n) (List n FileInfo)
-> List n FileInfo
forall s a. s -> Getting a s a -> a
^. Getting (List n FileInfo) (FileBrowser n) (List n FileInfo)
forall n. Lens' (FileBrowser n) (List n FileInfo)
fileBrowserEntriesL
List n FileInfo
new <- List n FileInfo -> EventM n (List n FileInfo)
forall (t :: * -> *) n e.
(Foldable t, Splittable t, Ord n) =>
GenericList n t e -> EventM n (GenericList n t e)
listMovePageDown List n FileInfo
old
FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return (FileBrowser n -> EventM n (FileBrowser n))
-> FileBrowser n -> EventM n (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ FileBrowser n
b FileBrowser n -> (FileBrowser n -> FileBrowser n) -> FileBrowser n
forall a b. a -> (a -> b) -> b
& (List n FileInfo -> Identity (List n FileInfo))
-> FileBrowser n -> Identity (FileBrowser n)
forall n. Lens' (FileBrowser n) (List n FileInfo)
fileBrowserEntriesL ((List n FileInfo -> Identity (List n FileInfo))
-> FileBrowser n -> Identity (FileBrowser n))
-> List n FileInfo -> FileBrowser n -> FileBrowser n
forall s t a b. ASetter s t a b -> b -> s -> t
.~ List n FileInfo
new
actionFileBrowserListHalfPageUp :: Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListHalfPageUp :: FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListHalfPageUp FileBrowser n
b = do
let old :: List n FileInfo
old = FileBrowser n
b FileBrowser n
-> Getting (List n FileInfo) (FileBrowser n) (List n FileInfo)
-> List n FileInfo
forall s a. s -> Getting a s a -> a
^. Getting (List n FileInfo) (FileBrowser n) (List n FileInfo)
forall n. Lens' (FileBrowser n) (List n FileInfo)
fileBrowserEntriesL
List n FileInfo
new <- Double -> List n FileInfo -> EventM n (List n FileInfo)
forall (t :: * -> *) n m e.
(Foldable t, Splittable t, Ord n, RealFrac m) =>
m -> GenericList n t e -> EventM n (GenericList n t e)
listMoveByPages (-Double
0.5::Double) List n FileInfo
old
FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return (FileBrowser n -> EventM n (FileBrowser n))
-> FileBrowser n -> EventM n (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ FileBrowser n
b FileBrowser n -> (FileBrowser n -> FileBrowser n) -> FileBrowser n
forall a b. a -> (a -> b) -> b
& (List n FileInfo -> Identity (List n FileInfo))
-> FileBrowser n -> Identity (FileBrowser n)
forall n. Lens' (FileBrowser n) (List n FileInfo)
fileBrowserEntriesL ((List n FileInfo -> Identity (List n FileInfo))
-> FileBrowser n -> Identity (FileBrowser n))
-> List n FileInfo -> FileBrowser n -> FileBrowser n
forall s t a b. ASetter s t a b -> b -> s -> t
.~ List n FileInfo
new
actionFileBrowserListHalfPageDown :: Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListHalfPageDown :: FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListHalfPageDown FileBrowser n
b = do
let old :: List n FileInfo
old = FileBrowser n
b FileBrowser n
-> Getting (List n FileInfo) (FileBrowser n) (List n FileInfo)
-> List n FileInfo
forall s a. s -> Getting a s a -> a
^. Getting (List n FileInfo) (FileBrowser n) (List n FileInfo)
forall n. Lens' (FileBrowser n) (List n FileInfo)
fileBrowserEntriesL
List n FileInfo
new <- Double -> List n FileInfo -> EventM n (List n FileInfo)
forall (t :: * -> *) n m e.
(Foldable t, Splittable t, Ord n, RealFrac m) =>
m -> GenericList n t e -> EventM n (GenericList n t e)
listMoveByPages (Double
0.5::Double) List n FileInfo
old
FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return (FileBrowser n -> EventM n (FileBrowser n))
-> FileBrowser n -> EventM n (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ FileBrowser n
b FileBrowser n -> (FileBrowser n -> FileBrowser n) -> FileBrowser n
forall a b. a -> (a -> b) -> b
& (List n FileInfo -> Identity (List n FileInfo))
-> FileBrowser n -> Identity (FileBrowser n)
forall n. Lens' (FileBrowser n) (List n FileInfo)
fileBrowserEntriesL ((List n FileInfo -> Identity (List n FileInfo))
-> FileBrowser n -> Identity (FileBrowser n))
-> List n FileInfo -> FileBrowser n -> FileBrowser n
forall s t a b. ASetter s t a b -> b -> s -> t
.~ List n FileInfo
new
actionFileBrowserListTop :: Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListTop :: FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListTop FileBrowser n
b =
FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return (FileBrowser n -> EventM n (FileBrowser n))
-> FileBrowser n -> EventM n (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ FileBrowser n
b FileBrowser n -> (FileBrowser n -> FileBrowser n) -> FileBrowser n
forall a b. a -> (a -> b) -> b
& (List n FileInfo -> Identity (List n FileInfo))
-> FileBrowser n -> Identity (FileBrowser n)
forall n. Lens' (FileBrowser n) (List n FileInfo)
fileBrowserEntriesL ((List n FileInfo -> Identity (List n FileInfo))
-> FileBrowser n -> Identity (FileBrowser n))
-> (List n FileInfo -> List n FileInfo)
-> FileBrowser n
-> FileBrowser n
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Int -> List n FileInfo -> List n FileInfo
forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
Int -> GenericList n t e -> GenericList n t e
listMoveTo Int
0
actionFileBrowserListBottom :: Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListBottom :: FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListBottom FileBrowser n
b = do
let sz :: Int
sz = Vector FileInfo -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (GenericList n Vector FileInfo -> Vector FileInfo
forall n (t :: * -> *) e. GenericList n t e -> t e
listElements (GenericList n Vector FileInfo -> Vector FileInfo)
-> GenericList n Vector FileInfo -> Vector FileInfo
forall a b. (a -> b) -> a -> b
$ FileBrowser n
bFileBrowser n
-> Getting
(GenericList n Vector FileInfo)
(FileBrowser n)
(GenericList n Vector FileInfo)
-> GenericList n Vector FileInfo
forall s a. s -> Getting a s a -> a
^.Getting
(GenericList n Vector FileInfo)
(FileBrowser n)
(GenericList n Vector FileInfo)
forall n. Lens' (FileBrowser n) (List n FileInfo)
fileBrowserEntriesL)
FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return (FileBrowser n -> EventM n (FileBrowser n))
-> FileBrowser n -> EventM n (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ FileBrowser n
b FileBrowser n -> (FileBrowser n -> FileBrowser n) -> FileBrowser n
forall a b. a -> (a -> b) -> b
& (GenericList n Vector FileInfo
-> Identity (GenericList n Vector FileInfo))
-> FileBrowser n -> Identity (FileBrowser n)
forall n. Lens' (FileBrowser n) (List n FileInfo)
fileBrowserEntriesL ((GenericList n Vector FileInfo
-> Identity (GenericList n Vector FileInfo))
-> FileBrowser n -> Identity (FileBrowser n))
-> (GenericList n Vector FileInfo -> GenericList n Vector FileInfo)
-> FileBrowser n
-> FileBrowser n
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Int
-> GenericList n Vector FileInfo -> GenericList n Vector FileInfo
forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
Int -> GenericList n t e -> GenericList n t e
listMoveTo (Int
sz Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
actionFileBrowserListNext :: Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListNext :: FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListNext FileBrowser n
b =
FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return (FileBrowser n -> EventM n (FileBrowser n))
-> FileBrowser n -> EventM n (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ FileBrowser n
b FileBrowser n -> (FileBrowser n -> FileBrowser n) -> FileBrowser n
forall a b. a -> (a -> b) -> b
& (List n FileInfo -> Identity (List n FileInfo))
-> FileBrowser n -> Identity (FileBrowser n)
forall n. Lens' (FileBrowser n) (List n FileInfo)
fileBrowserEntriesL ((List n FileInfo -> Identity (List n FileInfo))
-> FileBrowser n -> Identity (FileBrowser n))
-> (List n FileInfo -> List n FileInfo)
-> FileBrowser n
-> FileBrowser n
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Int -> List n FileInfo -> List n FileInfo
forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
Int -> GenericList n t e -> GenericList n t e
listMoveBy Int
1
actionFileBrowserListPrev :: Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListPrev :: FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListPrev FileBrowser n
b =
FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return (FileBrowser n -> EventM n (FileBrowser n))
-> FileBrowser n -> EventM n (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ FileBrowser n
b FileBrowser n -> (FileBrowser n -> FileBrowser n) -> FileBrowser n
forall a b. a -> (a -> b) -> b
& (List n FileInfo -> Identity (List n FileInfo))
-> FileBrowser n -> Identity (FileBrowser n)
forall n. Lens' (FileBrowser n) (List n FileInfo)
fileBrowserEntriesL ((List n FileInfo -> Identity (List n FileInfo))
-> FileBrowser n -> Identity (FileBrowser n))
-> (List n FileInfo -> List n FileInfo)
-> FileBrowser n
-> FileBrowser n
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Int -> List n FileInfo -> List n FileInfo
forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
Int -> GenericList n t e -> GenericList n t e
listMoveBy (-Int
1)
handleFileBrowserEvent :: (Ord n) => Vty.Event -> FileBrowser n -> EventM n (FileBrowser n)
handleFileBrowserEvent :: Event -> FileBrowser n -> EventM n (FileBrowser n)
handleFileBrowserEvent Event
e FileBrowser n
b =
if FileBrowser n -> Bool
forall n. FileBrowser n -> Bool
fileBrowserIsSearching FileBrowser n
b
then Event -> FileBrowser n -> EventM n (FileBrowser n)
forall n.
Ord n =>
Event -> FileBrowser n -> EventM n (FileBrowser n)
handleFileBrowserEventSearching Event
e FileBrowser n
b
else Event -> FileBrowser n -> EventM n (FileBrowser n)
forall n.
Ord n =>
Event -> FileBrowser n -> EventM n (FileBrowser n)
handleFileBrowserEventNormal Event
e FileBrowser n
b
safeInit :: T.Text -> T.Text
safeInit :: Text -> Text
safeInit Text
t | Text -> Int
T.length Text
t Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = Text
t
| Bool
otherwise = Text -> Text
T.init Text
t
handleFileBrowserEventSearching :: (Ord n) => Vty.Event -> FileBrowser n -> EventM n (FileBrowser n)
handleFileBrowserEventSearching :: Event -> FileBrowser n -> EventM n (FileBrowser n)
handleFileBrowserEventSearching Event
e FileBrowser n
b =
case Event
e of
Vty.EvKey (Vty.KChar Char
'c') [Modifier
Vty.MCtrl] ->
FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return (FileBrowser n -> EventM n (FileBrowser n))
-> FileBrowser n -> EventM n (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ (Maybe Text -> Maybe Text) -> FileBrowser n -> FileBrowser n
forall n.
(Maybe Text -> Maybe Text) -> FileBrowser n -> FileBrowser n
updateFileBrowserSearch (Maybe Text -> Maybe Text -> Maybe Text
forall a b. a -> b -> a
const Maybe Text
forall a. Maybe a
Nothing) FileBrowser n
b
Vty.EvKey Key
Vty.KEsc [] ->
FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return (FileBrowser n -> EventM n (FileBrowser n))
-> FileBrowser n -> EventM n (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ (Maybe Text -> Maybe Text) -> FileBrowser n -> FileBrowser n
forall n.
(Maybe Text -> Maybe Text) -> FileBrowser n -> FileBrowser n
updateFileBrowserSearch (Maybe Text -> Maybe Text -> Maybe Text
forall a b. a -> b -> a
const Maybe Text
forall a. Maybe a
Nothing) FileBrowser n
b
Vty.EvKey Key
Vty.KBS [] ->
FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return (FileBrowser n -> EventM n (FileBrowser n))
-> FileBrowser n -> EventM n (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ (Maybe Text -> Maybe Text) -> FileBrowser n -> FileBrowser n
forall n.
(Maybe Text -> Maybe Text) -> FileBrowser n -> FileBrowser n
updateFileBrowserSearch ((Text -> Text) -> Maybe Text -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Text
safeInit) FileBrowser n
b
Vty.EvKey Key
Vty.KEnter [] ->
(Maybe Text -> Maybe Text) -> FileBrowser n -> FileBrowser n
forall n.
(Maybe Text -> Maybe Text) -> FileBrowser n -> FileBrowser n
updateFileBrowserSearch (Maybe Text -> Maybe Text -> Maybe Text
forall a b. a -> b -> a
const Maybe Text
forall a. Maybe a
Nothing) (FileBrowser n -> FileBrowser n)
-> EventM n (FileBrowser n) -> EventM n (FileBrowser n)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
FileBrowser n -> EventM n (FileBrowser n)
forall n. FileBrowser n -> EventM n (FileBrowser n)
maybeSelectCurrentEntry FileBrowser n
b
Vty.EvKey (Vty.KChar Char
c) [] ->
FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return (FileBrowser n -> EventM n (FileBrowser n))
-> FileBrowser n -> EventM n (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ (Maybe Text -> Maybe Text) -> FileBrowser n -> FileBrowser n
forall n.
(Maybe Text -> Maybe Text) -> FileBrowser n -> FileBrowser n
updateFileBrowserSearch ((Text -> Text) -> Maybe Text -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Text -> Char -> Text) -> Char -> Text -> Text
forall a b c. (a -> b -> c) -> b -> a -> c
flip Text -> Char -> Text
T.snoc Char
c)) FileBrowser n
b
Event
_ ->
Event -> FileBrowser n -> EventM n (FileBrowser n)
forall n.
Ord n =>
Event -> FileBrowser n -> EventM n (FileBrowser n)
handleFileBrowserEventCommon Event
e FileBrowser n
b
handleFileBrowserEventNormal :: (Ord n) => Vty.Event -> FileBrowser n -> EventM n (FileBrowser n)
handleFileBrowserEventNormal :: Event -> FileBrowser n -> EventM n (FileBrowser n)
handleFileBrowserEventNormal Event
e FileBrowser n
b =
case Event
e of
Vty.EvKey (Vty.KChar Char
'/') [] ->
FileBrowser n -> EventM n (FileBrowser n)
forall n. FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserBeginSearch FileBrowser n
b
Vty.EvKey Key
Vty.KEnter [] ->
FileBrowser n -> EventM n (FileBrowser n)
forall n. FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserSelectEnter FileBrowser n
b
Vty.EvKey (Vty.KChar Char
' ') [] ->
FileBrowser n -> EventM n (FileBrowser n)
forall n. FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserSelectCurrent FileBrowser n
b
Event
_ ->
Event -> FileBrowser n -> EventM n (FileBrowser n)
forall n.
Ord n =>
Event -> FileBrowser n -> EventM n (FileBrowser n)
handleFileBrowserEventCommon Event
e FileBrowser n
b
handleFileBrowserEventCommon :: (Ord n) => Vty.Event -> FileBrowser n -> EventM n (FileBrowser n)
handleFileBrowserEventCommon :: Event -> FileBrowser n -> EventM n (FileBrowser n)
handleFileBrowserEventCommon Event
e FileBrowser n
b =
case Event
e of
Vty.EvKey (Vty.KChar Char
'b') [Modifier
Vty.MCtrl] ->
FileBrowser n -> EventM n (FileBrowser n)
forall n. Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListPageUp FileBrowser n
b
Vty.EvKey (Vty.KChar Char
'f') [Modifier
Vty.MCtrl] ->
FileBrowser n -> EventM n (FileBrowser n)
forall n. Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListPageDown FileBrowser n
b
Vty.EvKey (Vty.KChar Char
'd') [Modifier
Vty.MCtrl] ->
FileBrowser n -> EventM n (FileBrowser n)
forall n. Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListHalfPageDown FileBrowser n
b
Vty.EvKey (Vty.KChar Char
'u') [Modifier
Vty.MCtrl] ->
FileBrowser n -> EventM n (FileBrowser n)
forall n. Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListHalfPageUp FileBrowser n
b
Vty.EvKey (Vty.KChar Char
'g') [] ->
FileBrowser n -> EventM n (FileBrowser n)
forall n. Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListTop FileBrowser n
b
Vty.EvKey (Vty.KChar Char
'G') [] ->
FileBrowser n -> EventM n (FileBrowser n)
forall n. Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListBottom FileBrowser n
b
Vty.EvKey (Vty.KChar Char
'j') [] ->
FileBrowser n -> EventM n (FileBrowser n)
forall n. Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListNext FileBrowser n
b
Vty.EvKey (Vty.KChar Char
'k') [] ->
FileBrowser n -> EventM n (FileBrowser n)
forall n. Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListPrev FileBrowser n
b
Vty.EvKey (Vty.KChar Char
'n') [Modifier
Vty.MCtrl] ->
FileBrowser n -> EventM n (FileBrowser n)
forall n. Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListNext FileBrowser n
b
Vty.EvKey (Vty.KChar Char
'p') [Modifier
Vty.MCtrl] ->
FileBrowser n -> EventM n (FileBrowser n)
forall n. Ord n => FileBrowser n -> EventM n (FileBrowser n)
actionFileBrowserListPrev FileBrowser n
b
Event
_ ->
FileBrowser n
-> Lens' (FileBrowser n) (GenericList n Vector FileInfo)
-> (Event
-> GenericList n Vector FileInfo
-> EventM n (GenericList n Vector FileInfo))
-> Event
-> EventM n (FileBrowser n)
forall a b e n.
a -> Lens' a b -> (e -> b -> EventM n b) -> e -> EventM n a
handleEventLensed FileBrowser n
b forall n. Lens' (FileBrowser n) (List n FileInfo)
Lens' (FileBrowser n) (GenericList n Vector FileInfo)
fileBrowserEntriesL Event
-> GenericList n Vector FileInfo
-> EventM n (GenericList n Vector FileInfo)
forall (t :: * -> *) n e.
(Foldable t, Splittable t, Ord n) =>
Event -> GenericList n t e -> EventM n (GenericList n t e)
handleListEvent Event
e
maybeSelectCurrentEntry :: FileBrowser n -> EventM n (FileBrowser n)
maybeSelectCurrentEntry :: FileBrowser n -> EventM n (FileBrowser n)
maybeSelectCurrentEntry FileBrowser n
b =
case FileBrowser n -> Maybe FileInfo
forall n. FileBrowser n -> Maybe FileInfo
fileBrowserCursor FileBrowser n
b of
Maybe FileInfo
Nothing -> FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return FileBrowser n
b
Just FileInfo
entry ->
if FileBrowser n -> FileInfo -> Bool
forall n. FileBrowser n -> FileInfo -> Bool
fileBrowserSelectable FileBrowser n
b FileInfo
entry
then FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return (FileBrowser n -> EventM n (FileBrowser n))
-> FileBrowser n -> EventM n (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ FileBrowser n
b FileBrowser n -> (FileBrowser n -> FileBrowser n) -> FileBrowser n
forall a b. a -> (a -> b) -> b
& (Set FilePath -> Identity (Set FilePath))
-> FileBrowser n -> Identity (FileBrowser n)
forall n. Lens' (FileBrowser n) (Set FilePath)
fileBrowserSelectedFilesL ((Set FilePath -> Identity (Set FilePath))
-> FileBrowser n -> Identity (FileBrowser n))
-> (Set FilePath -> Set FilePath) -> FileBrowser n -> FileBrowser n
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ FilePath -> Set FilePath -> Set FilePath
forall a. Ord a => a -> Set a -> Set a
Set.insert (FileInfo -> FilePath
fileInfoFilename FileInfo
entry)
else case FileInfo -> Maybe FileType
fileInfoFileType FileInfo
entry of
Just FileType
Directory ->
IO (FileBrowser n) -> EventM n (FileBrowser n)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (FileBrowser n) -> EventM n (FileBrowser n))
-> IO (FileBrowser n) -> EventM n (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ FilePath -> FileBrowser n -> IO (FileBrowser n)
forall n. FilePath -> FileBrowser n -> IO (FileBrowser n)
setWorkingDirectory (FileInfo -> FilePath
fileInfoFilePath FileInfo
entry) FileBrowser n
b
Just FileType
SymbolicLink ->
case FileInfo -> Maybe FileType
fileInfoLinkTargetType FileInfo
entry of
Just FileType
Directory -> do
IO (FileBrowser n) -> EventM n (FileBrowser n)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (FileBrowser n) -> EventM n (FileBrowser n))
-> IO (FileBrowser n) -> EventM n (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ FilePath -> FileBrowser n -> IO (FileBrowser n)
forall n. FilePath -> FileBrowser n -> IO (FileBrowser n)
setWorkingDirectory (FileInfo -> FilePath
fileInfoFilePath FileInfo
entry) FileBrowser n
b
Maybe FileType
_ ->
FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return FileBrowser n
b
Maybe FileType
_ ->
FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return FileBrowser n
b
selectCurrentEntry :: FileBrowser n -> EventM n (FileBrowser n)
selectCurrentEntry :: FileBrowser n -> EventM n (FileBrowser n)
selectCurrentEntry FileBrowser n
b =
case FileBrowser n -> Maybe FileInfo
forall n. FileBrowser n -> Maybe FileInfo
fileBrowserCursor FileBrowser n
b of
Maybe FileInfo
Nothing -> FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return FileBrowser n
b
Just FileInfo
e -> FileBrowser n -> EventM n (FileBrowser n)
forall (m :: * -> *) a. Monad m => a -> m a
return (FileBrowser n -> EventM n (FileBrowser n))
-> FileBrowser n -> EventM n (FileBrowser n)
forall a b. (a -> b) -> a -> b
$ FileBrowser n
b FileBrowser n -> (FileBrowser n -> FileBrowser n) -> FileBrowser n
forall a b. a -> (a -> b) -> b
& (Set FilePath -> Identity (Set FilePath))
-> FileBrowser n -> Identity (FileBrowser n)
forall n. Lens' (FileBrowser n) (Set FilePath)
fileBrowserSelectedFilesL ((Set FilePath -> Identity (Set FilePath))
-> FileBrowser n -> Identity (FileBrowser n))
-> (Set FilePath -> Set FilePath) -> FileBrowser n -> FileBrowser n
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ FilePath -> Set FilePath -> Set FilePath
forall a. Ord a => a -> Set a -> Set a
Set.insert (FileInfo -> FilePath
fileInfoFilename FileInfo
e)
renderFileBrowser :: (Show n, Ord n)
=> Bool
-> FileBrowser n
-> Widget n
renderFileBrowser :: Bool -> FileBrowser n -> Widget n
renderFileBrowser Bool
foc FileBrowser n
b =
let maxFilenameLength :: Int
maxFilenameLength = GenericList n Vector Int -> Int
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (GenericList n Vector Int -> Int)
-> GenericList n Vector Int -> Int
forall a b. (a -> b) -> a -> b
$ (FilePath -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length (FilePath -> Int) -> (FileInfo -> FilePath) -> FileInfo -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FileInfo -> FilePath
fileInfoFilename) (FileInfo -> Int)
-> GenericList n Vector FileInfo -> GenericList n Vector Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (FileBrowser n
bFileBrowser n
-> Getting
(GenericList n Vector FileInfo)
(FileBrowser n)
(GenericList n Vector FileInfo)
-> GenericList n Vector FileInfo
forall s a. s -> Getting a s a -> a
^.Getting
(GenericList n Vector FileInfo)
(FileBrowser n)
(GenericList n Vector FileInfo)
forall n. Lens' (FileBrowser n) (List n FileInfo)
fileBrowserEntriesL)
cwdHeader :: Widget n
cwdHeader = Padding -> Widget n -> Widget n
forall n. Padding -> Widget n -> Widget n
padRight Padding
Max (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$
FilePath -> Widget n
forall n. FilePath -> Widget n
str (FilePath -> Widget n) -> FilePath -> Widget n
forall a b. (a -> b) -> a -> b
$ ShowS
sanitizeFilename ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$ FileBrowser n -> FilePath
forall n. FileBrowser n -> FilePath
fileBrowserWorkingDirectory FileBrowser n
b
selInfo :: Widget n
selInfo = case GenericList n Vector FileInfo -> Maybe (Int, FileInfo)
forall (t :: * -> *) n e.
(Splittable t, Foldable t) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement (FileBrowser n
bFileBrowser n
-> Getting
(GenericList n Vector FileInfo)
(FileBrowser n)
(GenericList n Vector FileInfo)
-> GenericList n Vector FileInfo
forall s a. s -> Getting a s a -> a
^.Getting
(GenericList n Vector FileInfo)
(FileBrowser n)
(GenericList n Vector FileInfo)
forall n. Lens' (FileBrowser n) (List n FileInfo)
fileBrowserEntriesL) of
Maybe (Int, FileInfo)
Nothing -> Int -> Widget n -> Widget n
forall n. Int -> Widget n -> Widget n
vLimit Int
1 (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$ Char -> Widget n
forall n. Char -> Widget n
fill Char
' '
Just (Int
_, FileInfo
i) -> Padding -> Widget n -> Widget n
forall n. Padding -> Widget n -> Widget n
padRight Padding
Max (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$ FileInfo -> Widget n
forall n. FileInfo -> Widget n
selInfoFor FileInfo
i
fileTypeLabel :: Maybe FileType -> p
fileTypeLabel Maybe FileType
Nothing = p
"unknown"
fileTypeLabel (Just FileType
t) =
case FileType
t of
FileType
RegularFile -> p
"file"
FileType
BlockDevice -> p
"block device"
FileType
CharacterDevice -> p
"character device"
FileType
NamedPipe -> p
"pipe"
FileType
Directory -> p
"directory"
FileType
SymbolicLink -> p
"symbolic link"
FileType
UnixSocket -> p
"socket"
selInfoFor :: FileInfo -> Widget n
selInfoFor FileInfo
i =
let label :: Text
label = case FileInfo -> Either IOException FileStatus
fileInfoFileStatus FileInfo
i of
Left IOException
_ -> Text
"unknown"
Right FileStatus
stat ->
let maybeSize :: Text
maybeSize = if FileStatus -> Maybe FileType
fileStatusFileType FileStatus
stat Maybe FileType -> Maybe FileType -> Bool
forall a. Eq a => a -> a -> Bool
== FileType -> Maybe FileType
forall a. a -> Maybe a
Just FileType
RegularFile
then Text
", " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int64 -> Text
prettyFileSize (FileStatus -> Int64
fileStatusSize FileStatus
stat)
else Text
""
in Maybe FileType -> Text
forall p. IsString p => Maybe FileType -> p
fileTypeLabel (FileStatus -> Maybe FileType
fileStatusFileType FileStatus
stat) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
maybeSize
in Text -> Widget n
forall n. Text -> Widget n
txt (Text -> Widget n) -> Text -> Widget n
forall a b. (a -> b) -> a -> b
$ (FilePath -> Text
T.pack (FilePath -> Text) -> FilePath -> Text
forall a b. (a -> b) -> a -> b
$ FileInfo -> FilePath
fileInfoSanitizedFilename FileInfo
i) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
": " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
label
maybeSearchInfo :: Widget n
maybeSearchInfo = case FileBrowser n
bFileBrowser n
-> Getting (Maybe Text) (FileBrowser n) (Maybe Text) -> Maybe Text
forall s a. s -> Getting a s a -> a
^.Getting (Maybe Text) (FileBrowser n) (Maybe Text)
forall n. Lens' (FileBrowser n) (Maybe Text)
fileBrowserSearchStringL of
Maybe Text
Nothing -> Widget n
forall n. Widget n
emptyWidget
Just Text
s -> Padding -> Widget n -> Widget n
forall n. Padding -> Widget n -> Widget n
padRight Padding
Max (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$
Text -> Widget n
forall n. Text -> Widget n
txt Text
"Search: " Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+>
n -> Location -> Widget n -> Widget n
forall n. n -> Location -> Widget n -> Widget n
showCursor (FileBrowser n
bFileBrowser n -> Getting n (FileBrowser n) n -> n
forall s a. s -> Getting a s a -> a
^.Getting n (FileBrowser n) n
forall n. Lens' (FileBrowser n) n
fileBrowserNameL) ((Int, Int) -> Location
Location (Text -> Int
T.length Text
s, Int
0)) (Text -> Widget n
forall n. Text -> Widget n
txt Text
s)
in AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withDefAttr AttrName
fileBrowserAttr (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$
[Widget n] -> Widget n
forall n. [Widget n] -> Widget n
vBox [ AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withDefAttr AttrName
fileBrowserCurrentDirectoryAttr Widget n
forall n. Widget n
cwdHeader
, (Bool -> FileInfo -> Widget n)
-> Bool -> GenericList n Vector FileInfo -> Widget n
forall (t :: * -> *) n e.
(Traversable t, Splittable t, Ord n, Show n) =>
(Bool -> e -> Widget n) -> Bool -> GenericList n t e -> Widget n
renderList (Bool -> Int -> Set FilePath -> n -> Bool -> FileInfo -> Widget n
forall n.
Bool -> Int -> Set FilePath -> n -> Bool -> FileInfo -> Widget n
renderFileInfo Bool
foc Int
maxFilenameLength (FileBrowser n
bFileBrowser n
-> Getting (Set FilePath) (FileBrowser n) (Set FilePath)
-> Set FilePath
forall s a. s -> Getting a s a -> a
^.Getting (Set FilePath) (FileBrowser n) (Set FilePath)
forall n. Lens' (FileBrowser n) (Set FilePath)
fileBrowserSelectedFilesL) (FileBrowser n
bFileBrowser n -> Getting n (FileBrowser n) n -> n
forall s a. s -> Getting a s a -> a
^.Getting n (FileBrowser n) n
forall n. Lens' (FileBrowser n) n
fileBrowserNameL))
Bool
foc (FileBrowser n
bFileBrowser n
-> Getting
(GenericList n Vector FileInfo)
(FileBrowser n)
(GenericList n Vector FileInfo)
-> GenericList n Vector FileInfo
forall s a. s -> Getting a s a -> a
^.Getting
(GenericList n Vector FileInfo)
(FileBrowser n)
(GenericList n Vector FileInfo)
forall n. Lens' (FileBrowser n) (List n FileInfo)
fileBrowserEntriesL)
, Widget n
maybeSearchInfo
, AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withDefAttr AttrName
fileBrowserSelectionInfoAttr Widget n
forall n. Widget n
selInfo
]
renderFileInfo :: Bool -> Int -> Set.Set String -> n -> Bool -> FileInfo -> Widget n
renderFileInfo :: Bool -> Int -> Set FilePath -> n -> Bool -> FileInfo -> Widget n
renderFileInfo Bool
foc Int
maxLen Set FilePath
selFiles n
n Bool
listSel FileInfo
info =
(if Bool
foc
then (if Bool
listSel then AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
forceAttr AttrName
listSelectedFocusedAttr
else if Bool
sel then AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
forceAttr AttrName
fileBrowserSelectedAttr else Widget n -> Widget n
forall a. a -> a
id)
else (if Bool
listSel then AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
forceAttr AttrName
listSelectedAttr
else if Bool
sel then AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
forceAttr AttrName
fileBrowserSelectedAttr else Widget n -> Widget n
forall a. a -> a
id)) (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$
Padding -> Widget n -> Widget n
forall n. Padding -> Widget n -> Widget n
padRight Padding
Max Widget n
body
where
sel :: Bool
sel = FileInfo -> FilePath
fileInfoFilename FileInfo
info FilePath -> Set FilePath -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set FilePath
selFiles
addAttr :: Widget n -> Widget n
addAttr = (Widget n -> Widget n)
-> (FileType -> Widget n -> Widget n)
-> Maybe FileType
-> Widget n
-> Widget n
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Widget n -> Widget n
forall a. a -> a
id (AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withDefAttr (AttrName -> Widget n -> Widget n)
-> (FileType -> AttrName) -> FileType -> Widget n -> Widget n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FileType -> AttrName
attrForFileType) (FileInfo -> Maybe FileType
fileInfoFileType FileInfo
info)
body :: Widget n
body = Widget n -> Widget n
forall n. Widget n -> Widget n
addAttr (Int -> Widget n -> Widget n
forall n. Int -> Widget n -> Widget n
hLimit (Int
maxLen Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$
Padding -> Widget n -> Widget n
forall n. Padding -> Widget n -> Widget n
padRight Padding
Max (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$
(if Bool
foc Bool -> Bool -> Bool
&& Bool
listSel then n -> Location -> Widget n -> Widget n
forall n. n -> Location -> Widget n -> Widget n
showCursor n
n ((Int, Int) -> Location
Location (Int
0,Int
0)) else Widget n -> Widget n
forall a. a -> a
id) (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$
FilePath -> Widget n
forall n. FilePath -> Widget n
str (FilePath -> Widget n) -> FilePath -> Widget n
forall a b. (a -> b) -> a -> b
$ FileInfo -> FilePath
fileInfoSanitizedFilename FileInfo
info FilePath -> ShowS
forall a. Semigroup a => a -> a -> a
<> FilePath
suffix)
suffix :: FilePath
suffix = (if FileInfo -> Maybe FileType
fileInfoFileType FileInfo
info Maybe FileType -> Maybe FileType -> Bool
forall a. Eq a => a -> a -> Bool
== FileType -> Maybe FileType
forall a. a -> Maybe a
Just FileType
Directory then FilePath
"/" else FilePath
"") FilePath -> ShowS
forall a. Semigroup a => a -> a -> a
<>
(if Bool
sel then FilePath
"*" else FilePath
"")
sanitizeFilename :: String -> String
sanitizeFilename :: ShowS
sanitizeFilename = (Char -> Char) -> ShowS
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Char -> Char
toPrint
where
toPrint :: Char -> Char
toPrint Char
c | Char -> Bool
isPrint Char
c = Char
c
| Bool
otherwise = Char
'?'
attrForFileType :: FileType -> AttrName
attrForFileType :: FileType -> AttrName
attrForFileType FileType
RegularFile = AttrName
fileBrowserRegularFileAttr
attrForFileType FileType
BlockDevice = AttrName
fileBrowserBlockDeviceAttr
attrForFileType FileType
CharacterDevice = AttrName
fileBrowserCharacterDeviceAttr
attrForFileType FileType
NamedPipe = AttrName
fileBrowserNamedPipeAttr
attrForFileType FileType
Directory = AttrName
fileBrowserDirectoryAttr
attrForFileType FileType
SymbolicLink = AttrName
fileBrowserSymbolicLinkAttr
attrForFileType FileType
UnixSocket = AttrName
fileBrowserUnixSocketAttr
fileBrowserAttr :: AttrName
fileBrowserAttr :: AttrName
fileBrowserAttr = AttrName
"fileBrowser"
fileBrowserCurrentDirectoryAttr :: AttrName
fileBrowserCurrentDirectoryAttr :: AttrName
fileBrowserCurrentDirectoryAttr = AttrName
fileBrowserAttr AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> AttrName
"currentDirectory"
fileBrowserSelectionInfoAttr :: AttrName
fileBrowserSelectionInfoAttr :: AttrName
fileBrowserSelectionInfoAttr = AttrName
fileBrowserAttr AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> AttrName
"selectionInfo"
fileBrowserDirectoryAttr :: AttrName
fileBrowserDirectoryAttr :: AttrName
fileBrowserDirectoryAttr = AttrName
fileBrowserAttr AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> AttrName
"directory"
fileBrowserBlockDeviceAttr :: AttrName
fileBrowserBlockDeviceAttr :: AttrName
fileBrowserBlockDeviceAttr = AttrName
fileBrowserAttr AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> AttrName
"block"
fileBrowserRegularFileAttr :: AttrName
fileBrowserRegularFileAttr :: AttrName
fileBrowserRegularFileAttr = AttrName
fileBrowserAttr AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> AttrName
"regular"
fileBrowserCharacterDeviceAttr :: AttrName
fileBrowserCharacterDeviceAttr :: AttrName
fileBrowserCharacterDeviceAttr = AttrName
fileBrowserAttr AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> AttrName
"char"
fileBrowserNamedPipeAttr :: AttrName
fileBrowserNamedPipeAttr :: AttrName
fileBrowserNamedPipeAttr = AttrName
fileBrowserAttr AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> AttrName
"pipe"
fileBrowserSymbolicLinkAttr :: AttrName
fileBrowserSymbolicLinkAttr :: AttrName
fileBrowserSymbolicLinkAttr = AttrName
fileBrowserAttr AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> AttrName
"symlink"
fileBrowserUnixSocketAttr :: AttrName
fileBrowserUnixSocketAttr :: AttrName
fileBrowserUnixSocketAttr = AttrName
fileBrowserAttr AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> AttrName
"unixSocket"
fileBrowserSelectedAttr :: AttrName
fileBrowserSelectedAttr :: AttrName
fileBrowserSelectedAttr = AttrName
fileBrowserAttr AttrName -> AttrName -> AttrName
forall a. Semigroup a => a -> a -> a
<> AttrName
"selected"
fileTypeMatch :: [FileType] -> FileInfo -> Bool
fileTypeMatch :: [FileType] -> FileInfo -> Bool
fileTypeMatch [FileType]
tys FileInfo
i = Bool -> (FileType -> Bool) -> Maybe FileType -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (FileType -> [FileType] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [FileType]
tys) (Maybe FileType -> Bool) -> Maybe FileType -> Bool
forall a b. (a -> b) -> a -> b
$ FileInfo -> Maybe FileType
fileInfoFileType FileInfo
i
fileExtensionMatch :: String -> FileInfo -> Bool
fileExtensionMatch :: FilePath -> FileInfo -> Bool
fileExtensionMatch FilePath
ext FileInfo
i = case FileInfo -> Maybe FileType
fileInfoFileType FileInfo
i of
Just FileType
Directory -> Bool
True
Just FileType
RegularFile -> (Char
'.' Char -> ShowS
forall a. a -> [a] -> [a]
: (Char -> Char
toLower (Char -> Char) -> ShowS
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath
ext)) FilePath -> FilePath -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isSuffixOf` (Char -> Char
toLower (Char -> Char) -> ShowS
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FileInfo -> FilePath
fileInfoFilename FileInfo
i)
Just FileType
SymbolicLink -> case FileInfo -> Maybe FileType
fileInfoLinkTargetType FileInfo
i of
Just FileType
Directory -> Bool
True
Maybe FileType
_ -> Bool
False
Maybe FileType
_ -> Bool
False