module Yi.UI.TabBar where
import Control.Applicative ((<$>))
import Control.Lens ((^.))
import qualified Data.List.PointedList.Circular as PL (PointedList (_focus), withFocus)
import qualified Data.Text as T (Text, pack, unpack)
import System.FilePath (isValid, splitPath)
import Yi.Buffer (shortIdentString)
import Yi.Editor (Editor (..), commonNamePrefix, findBufferWith, tabsA)
import Yi.Tab (tabWindowsA)
import Yi.Window (Window (bufkey))
data TabDescr = TabDescr
{ tabText :: T.Text
, tabInFocus :: Bool
} deriving (Show, Eq)
type TabBarDescr = PL.PointedList TabDescr
tabBarDescr :: Editor -> TabBarDescr
tabBarDescr editor = tabDescr <$> PL.withFocus (editor ^. tabsA)
where
prefix = commonNamePrefix editor
shorten = tabAbbrevTitle . shortIdentString (length prefix)
mkHintWith f = shorten $ findBufferWith f editor
hintForTab tab = mkHintWith (bufkey $ PL._focus (tab ^. tabWindowsA))
tabDescr (tab, True) = TabDescr (hintForTab tab) True
tabDescr (tab, False) = TabDescr (hintForTab tab) False
tabAbbrevTitle :: T.Text -> T.Text
tabAbbrevTitle title = if isValid fp
then T.pack $ concatMap abbrev (splitPath fp)
else title
where
fp = T.unpack title
abbrev "/" = "/"
abbrev path | head path == '.' && last path == '/' = take 2 path ++ "/"
| last path == '/' = head path : "/"
| otherwise = path