Move terminal logic to adapter pattern

Property identify iTerm instead of just checking if MacOS is being used

Remove .idea files from repository

These really shouldn't be in here
Remove the scripts from the repository, these don't need to be in here
This commit is contained in:
fisk 2017-06-21 13:27:27 +01:00
parent 489cd9ea4a
commit e1789d89d7
17 changed files with 138 additions and 144 deletions

23
.gitignore vendored
View file

@ -121,28 +121,7 @@ ENV/
### PyCharm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff:
.idea/workspace.xml
.idea/tasks.xml
# Sensitive or high-churn files:
.idea/dataSources/
.idea/dataSources.ids
.idea/dataSources.xml
.idea/dataSources.local.xml
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml
# Gradle:
.idea/gradle.xml
.idea/libraries
# Mongo Explorer plugin:
.idea/mongoSettings.xml
.idea/
## File-based project format:
*.iws

View file

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
</component>
</module>

11
.idea/misc.xml generated
View file

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.5.2 (/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5)" project-jdk-type="Python SDK" />
<component name="PyConsoleOptionsProvider">
<option name="myPythonConsoleState">
<console-settings sdk-home="/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5">
<option name="mySdkHome" value="/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5" />
</console-settings>
</option>
</component>
</project>

8
.idea/modules.xml generated
View file

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Pokemon-Terminal-Themes.iml" filepath="$PROJECT_DIR$/.idea/Pokemon-Terminal-Themes.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated
View file

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View file

@ -1,5 +0,0 @@
tell application "iTerm"
tell current session of current window
set background image to "/Users/Laki/Documents/GitHub/Pokemon-Terminal/./Images/Generation I - Kanto/151.png"
end tell
end tell

0
Scripts/empty Normal file
View file

View file

@ -1,2 +0,0 @@
#!/bin/bash
osascript /Users/Laki/Documents/GitHub/Pokemon-Terminal/./Scripts/background.scpt

View file

@ -1,5 +0,0 @@
tell application "System Events"
tell current desktop
set picture to "/Users/Laki/Documents/GitHub/Pokemon-Terminal/./Images/Extra/deoxys-attack.png"
end tell
end tell

26
adapter/__init__.py Normal file
View file

@ -0,0 +1,26 @@
import os
import sys
from adapter.implementations.ITerm import ITerm
from adapter.implementations.NullAdapter import NullAdapter
from adapter.implementations.Terminology import Terminology
from adapter.implementations.Tilix import Tilix
def identify():
"""
Identify the terminal we are using based on env vars.
: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()
return NullAdapter()

6
adapter/base.py Normal file
View file

@ -0,0 +1,6 @@
class TerminalAdapterInterface(object):
def set_pokemon(self, pokemon):
raise NotImplementedError()
def clear(self):
raise NotImplementedError()

View file

@ -0,0 +1,54 @@
import os
from adapter.base import TerminalAdapterInterface
# todo: broken probably
# do we really need to use applescript? would be nice to
# find a way to do this without.
cwd = os.path.dirname(os.path.realpath(__file__))
class ITerm(TerminalAdapterInterface):
def __terminal_script(self, path):
# Create the content for script that will change the terminal background image.
content = "tell application \"iTerm\"\n"
content += "\ttell current session of current window\n"
content += "\t\tset background image to \"" + path + "\"\n"
content += "\tend tell\n"
content += "end tell"
return content
def __iterm2_clear_script(self):
# Create and save the script for clearing the terminal background image.
content = self.__terminal_script("")
file = open(cwd + "/./Scripts/background.scpt", "wb")
file.write(bytes(content, 'UTF-8'))
file.close()
def __darwin_create_terminal_bash(self):
# Create and save the run.sh that will execute the AppleScript if the correct run.sh doesn't already exist.
content = "#!/bin/bash\n" + "osascript " + cwd + "/./Scripts/background.scpt"
if open(cwd + "/./Scripts/run.sh", 'r').read() == content:
return
file = open(cwd + "/./Scripts/run.sh", 'wb')
file.write(bytes(content, 'UTF-8'))
file.close()
def __iterm2_create_terminal_script(self, pokemon):
# Create and save the script for changing the terminal background image.
content = self.__terminal_script(pokemon.get_path())
file = open(cwd + "/./Scripts/background.scpt", "wb")
file.write(bytes(content, 'UTF-8'))
file.close()
def clear(self):
self.__iterm2_clear_script()
self.__darwin_create_terminal_bash()
os.system(cwd + "/./Scripts/run.sh")
def set_pokemon(self, pokemon):
# Create, save and run the bash script to change the terminal background.
self.__iterm2_create_terminal_script(pokemon)
self.__darwin_create_terminal_bash()
os.system(cwd + "/./Scripts/run.sh")

View file

@ -0,0 +1,11 @@
from adapter.base import TerminalAdapterInterface
class NullAdapter(TerminalAdapterInterface):
err = "This terminal emulator is not supported."
def clear(self):
print(self.err)
def set_pokemon(self, pokemon):
print(self.err)

View file

@ -0,0 +1,11 @@
import os
from adapter.base import TerminalAdapterInterface
class Terminology(TerminalAdapterInterface):
def set_pokemon(self, pokemon):
os.system('tybg "%s"'.format(pokemon.get_path()))
def clear(self):
os.system("tybg")

View file

@ -0,0 +1,19 @@
import os
from adapter.base import TerminalAdapterInterface
class Tilix(TerminalAdapterInterface):
setting_key = "com.gexperts.Tilix.Settings"
setting_field = "background-image"
def set_pokemon(self, pokemon):
command = 'gsettings set %s %s "%s"'
os.system(command.format(self.setting_key,
self.setting_field,
pokemon.get_path()))
def clear(self):
command = 'gsettings set %s %s'
os.system(command.format(self.setting_key,
self.setting_field))

View file

View file

@ -3,17 +3,19 @@
import os
import sys
from adapter import identify
cwd = os.path.dirname(os.path.realpath(__file__))
def __terminal_script(path):
# Create the content for script that will change the terminal background image.
content = "tell application \"iTerm\"\n"
content += "\ttell current session of current window\n"
content += "\t\tset background image to \"" + path + "\"\n"
content += "\tend tell\n"
content += "end tell"
return content
def clear_terminal():
adapter = identify()
adapter.clear()
def change_terminal(pokemon):
adapter = identify()
adapter.set_pokemon(pokemon)
def __wallpaper_script(pokemon):
@ -26,22 +28,6 @@ def __wallpaper_script(pokemon):
return content
def __iterm2_create_terminal_script(pokemon):
# Create and save the script for changing the terminal background image.
content = __terminal_script(pokemon.get_path())
file = open(cwd + "/./Scripts/background.scpt", "wb")
file.write(bytes(content, 'UTF-8'))
file.close()
def __iterm2_clear_script():
# Create and save the script for clearing the terminal background image.
content = __terminal_script("")
file = open(cwd + "/./Scripts/background.scpt", "wb")
file.write(bytes(content, 'UTF-8'))
file.close()
def __darwin_create_wallpaper_script(pokemon):
# Create and save the script for changing the wallpaper.
content = __wallpaper_script(pokemon)
@ -50,16 +36,6 @@ def __darwin_create_wallpaper_script(pokemon):
file.close()
def __darwin_create_terminal_bash():
# Create and save the run.sh that will execute the AppleScript if the correct run.sh doesn't already exist.
content = "#!/bin/bash\n" + "osascript " + cwd + "/./Scripts/background.scpt"
if open(cwd + "/./Scripts/run.sh", 'r').read() == content:
return
file = open(cwd + "/./Scripts/run.sh", 'wb')
file.write(bytes(content, 'UTF-8'))
file.close()
# Create and save the run.sh that will execute the AppleScript if the correct run.sh
# doesn't already exist.
def __darwin_create_wallpaper_bash():
@ -71,37 +47,6 @@ def __darwin_create_wallpaper_bash():
file.close()
def change_terminal(pokemon):
if sys.platform == "darwin":
# Create, save and run the bash script to change the terminal background.
__iterm2_create_terminal_script(pokemon)
__darwin_create_terminal_bash()
os.system(cwd + "/./Scripts/run.sh")
if sys.platform == "linux":
os.system(__linux_create_terminal(pokemon))
def __linux_create_terminal(pokemon):
if os.environ.get("TERMINOLOGY") == '1':
return "tybg \"" + pokemon.get_path() + "\""
elif "TILIX_ID" in os.environ:
return "gsettings set com.gexperts.Tilix.Settings background-image " + \
"\""+ pokemon.get_path()+"\""
else:
print("Terminal emulator not supported")
exit(1)
def __linux_clear_terminal():
if os.environ.get("TERMINOLOGY") == '1':
return "tybg"
elif "TILIX_ID" in os.environ:
return "gsettings reset com.gexperts.Tilix.Settings background-image"
else:
print("Terminal emulator not supported")
exit(1)
def change_wallpaper(pokemon):
if sys.platform == "darwin":
# Create, save and run the bash script to change the wallpaper.
@ -112,15 +57,6 @@ def change_wallpaper(pokemon):
os.system(__linux_create_wallpaper_script(pokemon))
def clear_terminal():
if sys.platform == "darwin":
__iterm2_clear_script()
__darwin_create_terminal_bash()
os.system(cwd + "/./Scripts/run.sh")
if sys.platform == "linux":
os.system(__linux_clear_terminal())
def __linux_create_wallpaper_script(pokemon):
# If its gnome... aka GDMSESSION=gnome-xorg, etc.
if os.environ.get("GDMSESSION").find("gnome") >= 0: