module Sound.SC3.UGen.Help where
import Control.Exception
import Control.Monad
import Data.List.Split
import System.IO.Error
import System.Cmd
import System.Directory
import System.Environment
import System.FilePath
get_env_default :: String -> String -> IO String
get_env_default e k = do
r <- tryJust (guard . isDoesNotExistError) (getEnv e)
case r of
Right v -> return v
_ -> return k
sc3HelpDirectory :: IO String
sc3HelpDirectory = do
h <- getEnv "HOME"
let d = h </> ".local/share/SuperCollider/Help"
get_env_default "SC3_HELP" d
sc3HelpClassFile :: FilePath -> String -> IO (Maybe FilePath)
sc3HelpClassFile d c = do
let f = d </> "Classes" </> c <.> "html"
e <- doesFileExist f
return (if e then Just f else Nothing)
sc3HelpOperatorEntry :: FilePath -> String -> FilePath
sc3HelpOperatorEntry d o = d </> "Overviews/Operators.html#." ++ o
sc3HelpMethod :: FilePath -> Char -> (String,String) -> FilePath
sc3HelpMethod d z (c,m) = d </> "Classes" </> c <.> "html#" ++ [z] ++ m
sc3HelpClassMethod :: FilePath -> (String,String) -> FilePath
sc3HelpClassMethod d = sc3HelpMethod d '*'
sc3HelpInstanceMethod :: FilePath -> (String,String) -> FilePath
sc3HelpInstanceMethod d = sc3HelpMethod d '-'
ugenSC3HelpFile :: String -> IO FilePath
ugenSC3HelpFile x = do
let s = filter (`notElem` "@") x
d <- sc3HelpDirectory
cf <- sc3HelpClassFile d s
case splitOn "." s of
["Operator",m] -> return (sc3HelpOperatorEntry d m)
[c,'*':m] -> return (sc3HelpClassMethod d (c,m))
[c,m] -> return (sc3HelpInstanceMethod d (c,m))
_ -> case cf of
Just cf' -> return cf'
Nothing -> error (show ("ugenSC3HelpFile",d,cf,x,s))
viewSC3Help :: String -> IO ()
viewSC3Help u = do
nm <- ugenSC3HelpFile u
br <- get_env_default "BROWSER" "x-www-browser"
void (rawSystem br ["file://" ++ nm])