Last active
July 21, 2020 05:57
-
-
Save soupi/09db4a67afa96f356d7676e438f161c2 to your computer and use it in GitHub Desktop.
Fancily write to file which song is playing in cmus (can be read from obs using textmonitor script)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env stack | |
-- stack --resolver lts-15.7 script --package text --package process | |
{-# LANGUAGE OverloadedStrings #-} | |
import Control.Monad | |
import qualified Data.Text as T | |
import qualified Data.Text.IO as T | |
import System.Process | |
import Control.Exception | |
import Control.Concurrent | |
main :: IO () | |
main = forever $ do | |
run `catch` \(SomeException e) -> pure () | |
threadDelay 2000000 | |
run :: IO () | |
run = do | |
current <- readTextFile | |
text <- getSong | |
unless (current == text) $ do | |
deleteFromFile current | |
threadDelay delay | |
writeToFile "" =<< getSong | |
deleteFromFile :: T.Text -> IO () | |
deleteFromFile current = do | |
unless (T.null current) $ do | |
let | |
temp | |
| T.drop (T.length current - 2) current == " " = | |
T.take (T.length current - 2) current | |
| otherwise = | |
T.take (T.length current - 1) current | |
T.writeFile file (prefix <> temp) | |
threadDelay delay | |
deleteFromFile temp | |
writeToFile :: T.Text -> T.Text -> IO () | |
writeToFile current full = do | |
unless (current == full) $ do | |
let | |
temp | |
| T.drop (T.length current) full == " " = | |
T.take (T.length current + 2) full | |
| otherwise = | |
T.take (T.length current + 1) full | |
T.writeFile file (prefix <> temp) | |
threadDelay delay | |
writeToFile temp full | |
getSong :: IO T.Text | |
getSong = | |
fmap | |
(T.unwords . T.lines . T.pack) | |
(readCreateProcess (shell "cmus-remote -C \"format_print %A - %t\"") "") | |
readTextFile :: IO T.Text | |
readTextFile = | |
catch | |
(T.drop (T.length prefix) <$> T.readFile file) | |
(\(SomeException _) -> pure "") | |
file :: FilePath | |
file = "/tmp/cmus_playing.txt" | |
prefix :: T.Text | |
prefix = "Now Playing: " | |
delay = 95000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I modified this script https://obsproject.com/forum/resources/text-monitor.825/ with:
This in the update function:
and modified the checkFile function like this:
And the intervals:
So that now I can update text faster than the default which is 1 second.