Browse Source

Reorder the code

It's getting slightly unorganized and large...
tags/3.0.0
Fabian Winter 4 months ago
parent
commit
a5e44f0042
1 changed files with 108 additions and 103 deletions
  1. 108
    103
      rofimoji.py

+ 108
- 103
rofimoji.py View File

@@ -1751,6 +1751,100 @@ fitzpatrick_modifiers_reversed = {" ".join(name.split()[:-1]): modifier for modi
fitzpatrick_modifiers.items() if name != "neutral"}


def main():
args = parse_arguments()
active_window = get_active_window()

returncode, stdout = open_main_rofi_window(args)

if returncode == 1:
sys.exit()
else:
emojis = compile_chosen_emojis(stdout.splitlines(), args.skin_tone, args.rofi_args)

if returncode == 0:
insert_emojis(emojis, active_window, args.use_clipboard)
elif returncode == 10:
copy_emojis_to_clipboard(emojis)
elif returncode == 11:
type_emojis(emojis, active_window)
elif returncode == 12:
copy_paste_emojis(emojis, active_window)


def parse_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser(description='Select, insert or copy Unicode emoji')
parser.add_argument(
'--use-clipboard',
'-c',
dest='use_clipboard',
action='store_true',
help='Do not type the emoji directly, but copy it to the clipboard, insert it from there and then restore the clipboard\'s original value'
)
parser.add_argument(
'--skin-tone',
'-s',
dest='skin_tone',
action='store',
choices=['neutral', 'light', 'medium-light', 'moderate', 'dark brown', 'black', 'ask'],
default='ask',
help='Decide on a skin-tone for all supported emojis. If nor set (or set to "ask"), you will be asked for each one'
)
parser.add_argument(
'--rofi-args',
dest='rofi_args',
action='store',
default='',
help='A string of arguments to give to rofi'
)
parsed_args = parser.parse_args()
parsed_args.rofi_args = parsed_args.rofi_args.split()

return parsed_args


def get_active_window() -> str:
xdotool = Popen(args=['xdotool', 'getactivewindow'], stdout=PIPE)
return xdotool.communicate()[0].decode("utf-8")[:-1]


def open_main_rofi_window(args):
rofi = Popen(
args=[
'rofi',
'-dmenu',
'-i',
'-multi-select',
'-p',
' 😀 ',
'-kb-custom-1',
'Alt+c',
'-kb-custom-2',
'Alt+t',
'-kb-custom-3',
'Alt+p',
*args.rofi_args
],
stdin=PIPE,
stdout=PIPE
)
(stdout, _) = rofi.communicate(input=emoji_list.encode('utf-8'))
return rofi.returncode, stdout


def compile_chosen_emojis(chosen_emojis: List[bytes], skin_tone: str, rofi_args: List[str]) -> str:
emojis = ""
for line in chosen_emojis:
emoji = line.decode('utf-8').split()[0]

if emoji in skin_tone_selectable_emojis:
emoji = select_skin_tone(emoji, skin_tone, rofi_args)

emojis += emoji

return emojis


def select_skin_tone(selected_emoji: chr, skin_tone: str, rofi_args: List[str]):
if skin_tone == 'neutral':
return selected_emoji
@@ -1767,11 +1861,8 @@ def select_skin_tone(selected_emoji: chr, skin_tone: str, rofi_args: List[str]):
'rofi',
'-dmenu',
'-i',
'-multi-select',
'-p',
selected_emoji + ' ',
'-kb-custom-1',
'Alt+c',
*rofi_args
],
stdin=PIPE,
@@ -1793,19 +1884,6 @@ def insert_emojis(emojis: str, active_window: str, use_clipboard: bool = False):
type_emojis(emojis, active_window)


def type_emojis(emojis: str, active_window: str):
Popen(
args=[
'xdotool',
'type',
'--clearmodifiers',
'--window',
active_window,
emojis
]
)


def copy_paste_emojis(emojis: str, active_window: str):
xsel = Popen(args=['xsel', '-o', '-b'], stdout=PIPE)
old_clipboard_content = xsel.communicate()[0].decode("utf-8")
@@ -1826,6 +1904,19 @@ def copy_paste_emojis(emojis: str, active_window: str):
xsel.communicate(input=old_primary_content.encode('utf-8'))


def type_emojis(emojis: str, active_window: str):
Popen(
args=[
'xdotool',
'type',
'--clearmodifiers',
'--window',
active_window,
emojis
]
)


def copy_emojis_to_clipboard(emojis: str):
xsel = Popen(
args=[
@@ -1838,91 +1929,5 @@ def copy_emojis_to_clipboard(emojis: str):
xsel.communicate(input=emojis.encode('utf-8'))


def parse_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser(description='Select, insert or copy Unicode emoji')
parser.add_argument(
'--use-clipboard',
'-c',
dest='use_clipboard',
action='store_true',
help='Do not type the emoji directly, but copy it to the clipboard, insert it from there and then restore the clipboard\'s original value'
)
parser.add_argument(
'--skin-tone',
'-s',
dest='skin_tone',
action='store',
choices=['neutral', 'light', 'medium-light', 'moderate', 'dark brown', 'black', 'ask'],
default='ask',
help='Decide on a skin-tone for all supported emojis. If nor set (or set to "ask"), you will be asked for each one'
)
parser.add_argument(
'--rofi-args',
dest='rofi_args',
action='store',
default='',
help='A string of arguments to give to rofi'
)
parsed_args = parser.parse_args()
parsed_args.rofi_args = parsed_args.rofi_args.split()

return parsed_args


def get_active_window() -> str:
xdotool = Popen(args=['xdotool', 'getactivewindow'], stdout=PIPE)
return xdotool.communicate()[0].decode("utf-8")[:-1]


def compile_chosen_emojis(chosen_emojis: List[bytes], skin_tone: str, rofi_args: List[str]) -> str:
emojis = ""
for line in chosen_emojis:
emoji = line.decode('utf-8').split()[0]

if emoji in skin_tone_selectable_emojis:
emoji = select_skin_tone(emoji, skin_tone, rofi_args)

emojis += emoji

return emojis


if __name__ == "__main__":
args = parse_arguments()

active_window = get_active_window()

rofi = Popen(
args=[
'rofi',
'-dmenu',
'-i',
'-multi-select',
'-p',
' 😀 ',
'-kb-custom-1',
'Alt+c',
'-kb-custom-2',
'Alt+t',
'-kb-custom-3',
'Alt+p',
*args.rofi_args
],
stdin=PIPE,
stdout=PIPE
)
(stdout, stderr) = rofi.communicate(input=emoji_list.encode('utf-8'))

if rofi.returncode == 1:
sys.exit()
else:
emojis = compile_chosen_emojis(stdout.splitlines(), args.skin_tone, args.rofi_args)

if rofi.returncode == 0:
insert_emojis(emojis, active_window, args.use_clipboard)
elif rofi.returncode == 10:
copy_emojis_to_clipboard(emojis)
elif rofi.returncode == 11:
type_emojis(emojis, active_window)
elif rofi.returncode == 12:
copy_paste_emojis(emojis, active_window)
main()

Loading…
Cancel
Save