File: VirtualTerminal.hs

package info (click to toggle)
debug-me 1.20200820-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 708 kB
  • sloc: haskell: 3,140; sh: 115; makefile: 80
file content (46 lines) | stat: -rw-r--r-- 1,299 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{- Copyright 2017 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU AGPL version 3 or higher.
 -}

module VirtualTerminal where

import System.FilePath
import System.Directory
import System.Process
import System.Environment

-- | Finds a virtual terminal program that looks like it will work
-- to run a command with some parameters.
--
-- Note that the parameters are exposed to the shell by some virtual 
-- terminals, but not by others.
runInVirtualTerminal :: String -> String -> [String] -> IO (Maybe CreateProcess)
runInVirtualTerminal title cmd params = do
	path <- getSearchPath
	mdisplay <- lookupEnv "DISPLAY"
	possibles <- case mdisplay of
		Just _ -> return $ do
			p <- path
			c <- xtermcmds
			return (p, c)
		Nothing -> return []
	find possibles
  where
	find [] = return Nothing
	find ((d, (c, ps)):rest) = do
		exists <- doesFileExist (d </> c)
		if exists
			then return $ Just $ proc (d </> c) ps
			else find rest
	
	-- Ordered list; generally xfce user may have gnome stuff
	-- installed, and only fall back to the older terminals when
	-- nothing else is available.
	xtermcmds =
		[ ("xfce4-terminal", std)
		, ("gnome-terminal", ["-e", unwords (cmd:params)])
		, ("xterm", std)
		, ("rxvt", ["-T", title, "-e", cmd])
		]
	std = ["-T", title, "-e", unwords (cmd:params)]