Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added '?' command to check the current Pokemon as terminal background for iTerm #112

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pokemonterminal/adapter/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ def set_image_file_path(self, image_file_path):
"""
raise NotImplementedError()

def get_image_file_number(self):
"""
Get the number of the background image of the terminal.
:rtype str
"""
raise NotImplementedError()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, clever, was thinking of doing exactly like this. But instead of exception, print a error message and quit so you don't have to do all these stub methods in the other implementations.


def clear(self):
"""
Clear the terminal's background image.
Expand Down
19 changes: 18 additions & 1 deletion pokemonterminal/adapter/implementations/ITerm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
\tend tell
end tell"""

osa_script_get = """tell application "iTerm"
\ttell current session of current window
\t\treturn background image as text
\tend tell
end tell"""


class ITerm(TerminalAdapterInterface):
@staticmethod
Expand All @@ -20,13 +26,24 @@ def is_available():
def __run_osascript(self, stream):
p = subprocess.Popen(['osascript'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write(stream)
p.communicate()
path = p.communicate()
p.stdin.close()
return path

def set_image_file_path(self, image_file_path):
stdin = osa_script_fmt.format(image_file_path)
self.__run_osascript(str.encode(stdin))

def get_image_file_number(self):
stdin = osa_script_get.format()
path_tuple = self.__run_osascript(str.encode(stdin))
path_element = str(path_tuple[0])
# The easiest check to see if we have a Pokemon as a background is checking for the presence of '/'
if '/' in path_element:
return path_element.split('/')[-1].split('.')[0]
else:
return 0

def clear(self):
stdin = osa_script_fmt.format("")
self.__run_osascript(str.encode(stdin))
3 changes: 3 additions & 0 deletions pokemonterminal/adapter/implementations/NullAdapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ def is_available():
def set_image_file_path(self, image_file_path):
print(self.err)

def get_image_file_number(self):
print(self.err)

def clear(self):
print(self.err)
4 changes: 4 additions & 0 deletions pokemonterminal/adapter/implementations/Terminology.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ def is_available():
def set_image_file_path(self, image_file_path):
os.system('tybg "{}"'.format(image_file_path))

def get_image_file_number(self):
# TODO: Check the way to get the background image path in Terminology
print(self.err)

def clear(self):
os.system("tybg")
4 changes: 4 additions & 0 deletions pokemonterminal/adapter/implementations/Tilix.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def set_image_file_path(self, image_file_path):
self.setting_field,
image_file_path))

def get_image_file_number(self):
# TODO: Check the way to get the background image path in Tilinx
return 0

def clear(self):
command = 'gsettings set {} {}'
os.system(command.format(self.setting_key,
Expand Down
21 changes: 20 additions & 1 deletion pokemonterminal/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,23 @@ def change_terminal_background(db, arg): # arg is a pokemon_name
print_columns(suggestions[1:])


def get_terminal_background_name(db):
"""Gets the number of the Pokemon currently set as the terminal background"""
pokemon_number = scripter.get_terminal_background_number()
if isinstance(pokemon_number, (int, str)):
if isinstance(pokemon_number, int) or pokemon_number.isdigit():
if pokemon_number != 0:
print(db.get_pokemon(pokemon_number).get_name().capitalize())
else:
print("None")
elif isinstance(pokemon_number, str):
print(pokemon_number.replace('-', ' ').capitalize())
else:
print("None")
else:
print("Not yet implemented")


def change_wallpaper(db, arg): # arg is a pokemon_name
"""Change the wallpaper to the specified Pokemon, if it exists."""
if arg in db:
Expand Down Expand Up @@ -283,8 +300,10 @@ def single_argument_handler(arg, escape_code):
slideshow(db, 494, 650, rand=arg.startswith("rnd"))
elif arg.endswith("slideshow-kalos"):
slideshow(db, 650, 720, rand=arg.startswith("rnd"))
elif arg == "current":
get_terminal_background_name(db)
elif arg == "?":
print("This function is deprecated.")
get_terminal_background_name(db)
elif escape_code:
change_wallpaper(db, arg)
else:
Expand Down
7 changes: 7 additions & 0 deletions pokemonterminal/scripter.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def change_terminal(image_file_path):
adapter.set_image_file_path(image_file_path)


def get_terminal_background_number():
adapter = identify()
if adapter is None:
print("Terminal not supported")
return adapter.get_image_file_number()


def change_wallpaper(image_file_path):
if not isinstance(image_file_path, str):
print("A image path must be passed to the change wallpapper function.")
Expand Down
8 changes: 7 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ def test_all(capsys):
def test_question_mark(capsys):
main([__file__, '?'])
out, err = capsys.readouterr()
assert 'deprecated' in out
assert isinstance(out, str)


def test_current(capsys):
main([__file__, 'current'])
out, err = capsys.readouterr()
assert isinstance(out, str)


if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions tests/test_scripter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_adapter_methods():
assert callable(terminal.clear)
assert callable(terminal.is_available)
assert callable(terminal.set_image_file_path)
assert callable(terminal.get_image_file_number)


if __name__ == '__main__':
Expand Down