Defer terminal detection to the adapter itself

This commit is contained in:
fisk 2017-06-21 17:30:28 +01:00
parent 2fea1581c1
commit e1ca2c8ca0
6 changed files with 48 additions and 16 deletions

View file

@ -1,10 +1,14 @@
import os
from adapter.implementations.ITerm import ITerm
from adapter.implementations.NullAdapter import NullAdapter
from adapter.implementations.Terminology import Terminology
from adapter.implementations.Tilix import Tilix
available_terminals = [
Terminology,
Tilix,
ITerm
]
def identify():
"""
@ -12,13 +16,8 @@ def identify():
:return: A terminal adapter interface or a NullAdapter.
:rtype: TerminalAdapterInterface
"""
if os.environ.get("TERMINOLOGY") == '1':
return Terminology()
if "TILIX_ID" in os.environ:
return Tilix()
if os.environ.get("ITERM_PROFILE"):
return ITerm()
for terminal in available_terminals:
if terminal.is_available():
return terminal()
return NullAdapter()

View file

@ -1,6 +1,22 @@
class TerminalAdapterInterface(object):
@staticmethod
def is_available():
"""
:return: True if the environment implies we are using this terminal.
:rtype bool
"""
raise NotImplementedError()
def set_pokemon(self, pokemon):
"""
Set the background image of the terminal.
:param pokemon: Information about a Pokémon.
:type pokemon: dict
"""
raise NotImplementedError()
def clear(self):
"""
Clear the terminal's background image.
"""
raise NotImplementedError()

View file

@ -1,9 +1,14 @@
import os
import subprocess
from adapter.base import TerminalAdapterInterface
class ITerm(TerminalAdapterInterface):
@staticmethod
def is_available():
return os.environ.get("ITERM_PROFILE")
def __generate_osascript(self, path):
# Create the content for script that will change the terminal background image.
content = "tell application \"iTerm\"\n"
@ -19,10 +24,10 @@ class ITerm(TerminalAdapterInterface):
p.communicate()
p.stdin.close()
def clear(self):
stdin = self.__generate_osascript("")
self.__run_osascript(str.encode(stdin))
def set_pokemon(self, pokemon):
stdin = self.__generate_osascript(pokemon.get_path())
self.__run_osascript(str.encode(stdin))
def clear(self):
stdin = self.__generate_osascript("")
self.__run_osascript(str.encode(stdin))

View file

@ -4,8 +4,12 @@ from adapter.base import TerminalAdapterInterface
class NullAdapter(TerminalAdapterInterface):
err = "This terminal emulator is not supported."
def clear(self):
print(self.err)
@staticmethod
def is_available():
return True
def set_pokemon(self, pokemon):
print(self.err)
def clear(self):
print(self.err)

View file

@ -4,6 +4,10 @@ from adapter.base import TerminalAdapterInterface
class Terminology(TerminalAdapterInterface):
@staticmethod
def is_available():
return os.environ.get("TERMINOLOGY") == '1'
def set_pokemon(self, pokemon):
os.system('tybg "{}"'.format(pokemon.get_path()))

View file

@ -7,6 +7,10 @@ class Tilix(TerminalAdapterInterface):
setting_key = "com.gexperts.Tilix.Settings"
setting_field = "background-image"
@staticmethod
def is_available():
return "TILIX_ID" in os.environ
def set_pokemon(self, pokemon):
command = 'gsettings set {} {} "{}"'
os.system(command.format(self.setting_key,