242 lines
9.1 KiB
Haskell
Raw Normal View History

2018-05-29 21:03:19 -07:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE DataKinds #-}
2017-08-23 12:43:25 -07:00
module Main where
import Control.Exception.Base
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Trans.Class
import Control.Monad.Trans.Reader
import qualified Data.ByteString.Char8 as BS
import Data.List
import Data.List.Split
import qualified Data.Map as M
import Data.Maybe
2021-07-04 01:32:53 -06:00
import qualified Data.Text
import Data.Time
import qualified GI.Gtk as Gtk
import qualified GI.Gtk.Objects.Overlay as Gtk
import Network.HostName
import StatusNotifier.Tray
import System.Directory
import System.Environment
2021-07-04 01:32:53 -06:00
import System.Environment.XDG.BaseDir
import System.FilePath.Posix
import System.IO
import System.Log.Handler.Simple
import System.Log.Logger
import System.Process
import System.Taffybar
import System.Taffybar.Auth
import System.Taffybar.Context (appendHook)
import System.Taffybar.DBus
import System.Taffybar.DBus.Toggle
import System.Taffybar.Hooks
import System.Taffybar.Information.CPU
import System.Taffybar.Information.EWMHDesktopInfo
import System.Taffybar.Information.Memory
import System.Taffybar.Information.X11DesktopInfo
import System.Taffybar.SimpleConfig
import System.Taffybar.Util
import System.Taffybar.Widget
import System.Taffybar.Widget.Crypto
import System.Taffybar.Widget.Generic.Icon
import System.Taffybar.Widget.Generic.PollingGraph
import System.Taffybar.Widget.Generic.PollingLabel
import System.Taffybar.Widget.Util
import System.Taffybar.Widget.Workspaces
import Text.Printf
import Text.Read hiding (lift)
setClassAndBoundingBoxes :: MonadIO m => Data.Text.Text -> Gtk.Widget -> m Gtk.Widget
setClassAndBoundingBoxes klass = buildContentsBox >=> flip widgetSetClassGI klass
deocrateWithSetClassAndBoxes :: MonadIO m => Data.Text.Text -> m Gtk.Widget -> m Gtk.Widget
deocrateWithSetClassAndBoxes klass builder = builder >>= setClassAndBoundingBoxes klass
2021-07-04 01:32:53 -06:00
mkRGBA (r, g, b, a) = (r/256, g/256, b/256, a/256)
blue = mkRGBA (42, 99, 140, 256)
yellow1 = mkRGBA (242, 163, 54, 256)
yellow2 = mkRGBA (254, 204, 83, 256)
yellow3 = mkRGBA (227, 134, 18, 256)
red = mkRGBA (210, 77, 37, 256)
2017-10-26 00:32:31 -07:00
myGraphConfig =
2017-05-24 21:56:34 -07:00
defaultGraphConfig
{ graphPadding = 0
2017-10-26 00:32:31 -07:00
, graphBorderWidth = 0
, graphWidth = 75
2018-05-21 12:44:12 -07:00
, graphBackgroundColor = (0.0, 0.0, 0.0, 0.0)
}
2018-05-16 11:56:22 -07:00
netCfg = myGraphConfig
{ graphDataColors = [yellow1, yellow2]
, graphLabel = Just "net"
2017-10-26 00:32:31 -07:00
}
2018-05-16 11:56:22 -07:00
memCfg = myGraphConfig
2018-04-27 17:36:32 -07:00
{ graphDataColors = [(0.129, 0.588, 0.953, 1)]
, graphLabel = Just "mem"
}
2017-05-24 21:56:34 -07:00
2018-05-16 11:56:22 -07:00
cpuCfg = myGraphConfig
{ graphDataColors = [red, (1, 0, 1, 0.5)]
2018-05-16 11:56:22 -07:00
, graphLabel = Just "cpu"
}
memCallback :: IO [Double]
memCallback = do
2016-09-16 14:11:52 -07:00
mi <- parseMeminfo
return [memoryUsedRatio mi]
2018-05-16 11:56:22 -07:00
cpuCallback = do
(_, systemLoad, totalLoad) <- cpuLoad
return [totalLoad, systemLoad]
2019-06-06 00:31:10 -07:00
getFullWorkspaceNames :: X11Property [(WorkspaceId, String)]
getFullWorkspaceNames = go <$> readAsListOfString Nothing "_NET_DESKTOP_FULL_NAMES"
2019-06-06 00:31:10 -07:00
where go = zip [WorkspaceId i | i <- [0..]]
2018-03-23 21:24:38 -07:00
workspaceNamesLabelSetter workspace =
2021-08-20 15:37:49 -06:00
remapNSP . fromMaybe "" . lookup (workspaceIdx workspace) <$>
2018-03-23 21:24:38 -07:00
liftX11Def [] getFullWorkspaceNames
2021-08-20 15:37:49 -06:00
where remapNSP "NSP" = "S"
remapNSP n = n
enableLogger logger level = do
logger <- getLogger logger
saveGlobalLogger $ setLevel level logger
logDebug = do
2019-06-05 00:35:47 -07:00
global <- getLogger ""
saveGlobalLogger $ setLevel DEBUG global
logger3 <- getLogger "System.Taffybar"
saveGlobalLogger $ setLevel DEBUG logger3
2018-05-21 12:44:12 -07:00
logger <- getLogger "System.Taffybar.Widget.Generic.AutoSizeImage"
saveGlobalLogger $ setLevel DEBUG logger
2018-05-21 12:44:12 -07:00
logger2 <- getLogger "StatusNotifier.Tray"
saveGlobalLogger $ setLevel DEBUG logger2
2019-06-06 00:31:10 -07:00
-- workspacesLogger <- getLogger "System.Taffybar.Widget.Workspaces"
-- saveGlobalLogger $ setLevel WARNING workspacesLogger
-- logDebug
-- logM "What" WARNING "Why"
-- enableLogger "System.Taffybar.Widget.Util" DEBUG
-- enableLogger "System.Taffybar.Information.XDG.DesktopEntry" DEBUG
-- enableLogger "System.Taffybar.WindowIcon" DEBUG
-- enableLogger "System.Taffybar.Widget.Generic.PollingLabel" DEBUG
2021-08-14 02:14:25 -06:00
cssFilesByHostname =
[ ("uber-loaner", ["uber-loaner.css"])
, ("imalison-home", ["taffybar.css"])
, ("ivanm-dfinity-razer", ["taffybar.css"])
, ("ryzen-shine", ["taffybar.css"])
, ("stevie-nixos", ["taffybar.css"])
]
2016-09-16 14:11:52 -07:00
main = do
enableLogger "Graphics.UI.GIGtkStrut" DEBUG
hostName <- getHostName
2017-03-05 18:30:55 -08:00
homeDirectory <- getHomeDirectory
2021-08-17 23:42:28 -06:00
let relativeFiles = fromMaybe ["taffybar.css"] $ lookup hostName cssFilesByHostname
2021-08-14 04:24:25 -06:00
cssFiles <- mapM (getUserConfigFile "taffybar") relativeFiles
2021-08-14 02:14:25 -06:00
let myCPU = deocrateWithSetClassAndBoxes "cpu" $
pollingGraphNew cpuCfg 5 cpuCallback
myMem = deocrateWithSetClassAndBoxes "mem" $
pollingGraphNew memCfg 5 memCallback
myNet = deocrateWithSetClassAndBoxes "net" $
networkGraphNew netCfg Nothing
myLayout = deocrateWithSetClassAndBoxes "layout" $
layoutNew defaultLayoutConfig
myWindows = deocrateWithSetClassAndBoxes "windows" $
windowsNew defaultWindowsConfig
myWorkspaces =
flip widgetSetClassGI "workspaces" =<<
workspacesNew defaultWorkspacesConfig
{ minIcons = 1
, getWindowIconPixbuf =
scaledWindowIconPixbufGetter $
getWindowIconPixbufFromChrome <|||>
unscaledDefaultGetWindowIconPixbuf <|||>
(\size _ -> lift $ loadPixbufByName size "application-default-icon")
, widgetGap = 0
, showWorkspaceFn = hideEmpty
, updateRateLimitMicroseconds = 100000
, labelSetter = workspaceNamesLabelSetter
, widgetBuilder = buildLabelOverlayController
}
myClock = deocrateWithSetClassAndBoxes "clock" $
textClockNewWith
defaultClockConfig
{ clockUpdateStrategy = RoundedTargetInterval 60 0.0
2021-08-09 03:34:43 -06:00
, clockFormatString = "%a %b %_d, 🕑%I:%M %p"
}
myICP = deocrateWithSetClassAndBoxes "icp" $ cryptoPriceLabelWithIcon @"ICP-USD"
myBTC = deocrateWithSetClassAndBoxes "btc" $ cryptoPriceLabelWithIcon @"BTC-USD"
myETH = deocrateWithSetClassAndBoxes "eth" $ cryptoPriceLabelWithIcon @"ETH-USD"
myTray = deocrateWithSetClassAndBoxes "tray" $
sniTrayNewFromParams defaultTrayParams { trayLeftClickAction = PopupMenu
, trayRightClickAction = Activate
}
2021-08-09 03:34:43 -06:00
myMpris = mpris2NewWithConfig
MPRIS2Config { mprisWidgetWrapper = deocrateWithSetClassAndBoxes "mpris" . return
, updatePlayerWidget =
simplePlayerWidget
defaultPlayerConfig
{ setNowPlayingLabel = playingText 20 20 }
2021-08-09 03:34:43 -06:00
}
myBatteryIcon = deocrateWithSetClassAndBoxes "battery-icon" batteryIconNew
myBatteryText =
deocrateWithSetClassAndBoxes "battery-text" $ textBatteryNew "$percentage$%"
batteryWidgets = [myBatteryIcon, myBatteryText]
baseEndWidgets =
[ myTray
, myICP
, myBTC
, myETH
, myMpris
]
fullEndWidgets = baseEndWidgets ++ [ myCPU, myMem, myNet, myMpris ]
laptopEndWidgets = batteryWidgets ++ baseEndWidgets
baseConfig =
defaultSimpleTaffyConfig
{ startWidgets = [ myWorkspaces, myLayout, myWindows ]
, endWidgets = baseEndWidgets
2021-09-10 12:13:41 -06:00
, barPosition = Top
2021-08-09 03:34:43 -06:00
, widgetSpacing = 0
, barPadding = 0
, barHeight = ScreenRatio $ 1/27
2021-08-14 02:14:25 -06:00
, cssPaths = cssFiles
, startupHook = void $ setCMCAPIKey "f9e66366-9d42-4c6e-8d40-4194a0aaa329"
, centerWidgets = [ myClock ]
2017-03-12 21:25:59 -07:00
}
selectedConfig =
fromMaybe baseConfig $ lookup hostName
[ ( "uber-loaner"
, baseConfig { endWidgets = laptopEndWidgets }
)
, ( "adele"
, baseConfig { endWidgets = laptopEndWidgets }
)
, ( "stevie-nixos"
, baseConfig { endWidgets = laptopEndWidgets
, startWidgets = [myWorkspaces, myLayout]
}
)
, ( "jay-lenovo"
, baseConfig { endWidgets = laptopEndWidgets }
)
]
simpleTaffyConfig = selectedConfig
{ centerWidgets = [ myClock ]
-- , endWidgets = []
-- , startWidgets = []
2019-01-05 18:42:16 -08:00
}
startTaffybar $
appendHook (void $ getTrayHost False) $
withLogServer $
withToggleServer $
toTaffyConfig simpleTaffyConfig