summaryrefslogtreecommitdiff
path: root/config/vim/plugin/pickachu
diff options
context:
space:
mode:
authordavidovski <david@davidovski.xyz>2021-10-09 22:20:41 +0100
committerdavidovski <david@davidovski.xyz>2021-10-09 22:20:41 +0100
commit01ced0b7ce47d279789efb2dc70d1cd009ac56ad (patch)
tree6ece604b8ae3476d2d70c9c9d42f86fe607990da /config/vim/plugin/pickachu
initial commit
Diffstat (limited to 'config/vim/plugin/pickachu')
-rw-r--r--config/vim/plugin/pickachu/__init__.py0
-rw-r--r--config/vim/plugin/pickachu/__pycache__/__init__.cpython-39.pycbin0 -> 142 bytes
-rw-r--r--config/vim/plugin/pickachu/__pycache__/apps.cpython-39.pycbin0 -> 1107 bytes
-rw-r--r--config/vim/plugin/pickachu/__pycache__/main.cpython-39.pycbin0 -> 719 bytes
-rw-r--r--config/vim/plugin/pickachu/__pycache__/processors.cpython-39.pycbin0 -> 1896 bytes
-rw-r--r--config/vim/plugin/pickachu/apps.py68
-rw-r--r--config/vim/plugin/pickachu/main.py36
-rw-r--r--config/vim/plugin/pickachu/processors.py80
8 files changed, 184 insertions, 0 deletions
diff --git a/config/vim/plugin/pickachu/__init__.py b/config/vim/plugin/pickachu/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/config/vim/plugin/pickachu/__init__.py
diff --git a/config/vim/plugin/pickachu/__pycache__/__init__.cpython-39.pyc b/config/vim/plugin/pickachu/__pycache__/__init__.cpython-39.pyc
new file mode 100644
index 0000000..c18ad9c
--- /dev/null
+++ b/config/vim/plugin/pickachu/__pycache__/__init__.cpython-39.pyc
Binary files differ
diff --git a/config/vim/plugin/pickachu/__pycache__/apps.cpython-39.pyc b/config/vim/plugin/pickachu/__pycache__/apps.cpython-39.pyc
new file mode 100644
index 0000000..c564bcc
--- /dev/null
+++ b/config/vim/plugin/pickachu/__pycache__/apps.cpython-39.pyc
Binary files differ
diff --git a/config/vim/plugin/pickachu/__pycache__/main.cpython-39.pyc b/config/vim/plugin/pickachu/__pycache__/main.cpython-39.pyc
new file mode 100644
index 0000000..c35190e
--- /dev/null
+++ b/config/vim/plugin/pickachu/__pycache__/main.cpython-39.pyc
Binary files differ
diff --git a/config/vim/plugin/pickachu/__pycache__/processors.cpython-39.pyc b/config/vim/plugin/pickachu/__pycache__/processors.cpython-39.pyc
new file mode 100644
index 0000000..1cf1153
--- /dev/null
+++ b/config/vim/plugin/pickachu/__pycache__/processors.cpython-39.pyc
Binary files differ
diff --git a/config/vim/plugin/pickachu/apps.py b/config/vim/plugin/pickachu/apps.py
new file mode 100644
index 0000000..00a6826
--- /dev/null
+++ b/config/vim/plugin/pickachu/apps.py
@@ -0,0 +1,68 @@
+import vim
+import subprocess
+from . import processors
+
+# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
+# name: apps.py
+# description: this function contains a dictionary object
+# where you can easily add new apps with processor
+# functions to handle their output. Note: a
+# processor is optional.
+# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
+
+ZENITY_COMMAND = vim.eval("g:pickachu_default_command")
+# Note: This is not the final date format that displays on
+# the users' buffer. This is the format we force
+# Zenity/Qarma to provide us.
+RETURNED_DATE_FORMAT = "%m/%d/%Y"
+if ZENITY_COMMAND == 'qarma':
+ RETURNED_DATE_FORMAT = "MM/dd/yy"
+
+apps = {
+ 'date': {
+ 'cmd': ZENITY_COMMAND,
+ 'processor': processors.dateProcessor,
+ 'options': [
+ '--calendar',
+ '--date-format=' + RETURNED_DATE_FORMAT
+ ]
+ },
+ 'file': {
+ 'cmd': ZENITY_COMMAND,
+ 'options': [
+ '--file-selection'
+ ]
+ },
+ 'color': {
+ 'cmd': ZENITY_COMMAND,
+ 'options': [
+ '--color-selection'
+ ],
+ 'processor': processors.colorProcessor
+ }
+}
+
+def runApp(choosenApp, format=None):
+ app = apps.get(choosenApp, None)
+ if app:
+ output = None
+ try:
+ command_array = [app['cmd']]
+ if app.get('options', False):
+ for option in app['options']:
+ command_array.append(option)
+ # Logging
+ command_array.append('2> /tmp/pickachu_log')
+ output = subprocess.check_output(command_array).decode('utf-8')
+ except:
+ return None
+
+ if app.get('processor', None):
+ if format:
+ return app['processor'](output.rstrip(), format)
+ else:
+ return app['processor'](output.rstrip())
+ else:
+ return output.rstrip()
+ else:
+ print("App does not exist.")
diff --git a/config/vim/plugin/pickachu/main.py b/config/vim/plugin/pickachu/main.py
new file mode 100644
index 0000000..d9a42b3
--- /dev/null
+++ b/config/vim/plugin/pickachu/main.py
@@ -0,0 +1,36 @@
+import vim
+from . import apps
+
+# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
+# name: main.py
+# description: This file evaluates the command arguments
+# provided by the user, calls the runApp function,
+# and inserts valid output to the user's buffer.
+# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
+
+DEFAULT_APP = vim.eval('g:pickachu_default_app')
+
+
+def MainFunction():
+ # This section is for getting the
+ # arguments from the user's Vim
+ # command.
+ arglength = int(vim.eval('a:0'))
+ CHOOSEN_APP = DEFAULT_APP
+ CHOOSEN_FORMAT = None
+ if arglength > 0:
+ CHOOSEN_APP = vim.eval('a:1')
+ if arglength > 1:
+ CHOOSEN_FORMAT = vim.eval('a:2')
+
+ # We run apps.py's runApp function to get an output.
+ output = apps.runApp(CHOOSEN_APP, CHOOSEN_FORMAT)
+
+ # Now, if runApp gave us an output, we can use the
+ # Vim API to print the output to the user's buffer.
+ if output:
+ pos_y, pos_x = vim.current.window.cursor
+ vim.current.line = vim.current.line[:pos_x+1] + output + vim.current.line[pos_x+1:]
+ vim.current.window.cursor = (pos_y, pos_x + len(output))
+ else:
+ print('Pickachu - Canceled')
diff --git a/config/vim/plugin/pickachu/processors.py b/config/vim/plugin/pickachu/processors.py
new file mode 100644
index 0000000..0fe04c1
--- /dev/null
+++ b/config/vim/plugin/pickachu/processors.py
@@ -0,0 +1,80 @@
+import vim
+from datetime import datetime
+
+# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
+# name: processors.py
+# description: this file contains functions that process data
+# from the runapp function (in app.py).
+# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
+
+DEFAULT_DATE_FORMAT = vim.eval("g:pickachu_default_date_format")
+DEFAULT_COLOR_FORMAT = vim.eval("g:pickachu_default_color_format")
+
+def dateProcessor(input, format=DEFAULT_DATE_FORMAT):
+ try:
+ dateObj = datetime.strptime(input, '%m/%d/%Y')
+ except(ValueError):
+ dateObj = datetime.strptime(input, '%m/%d/%y')
+ return dateObj.strftime(format)
+
+def colorProcessor(input, format=DEFAULT_COLOR_FORMAT):
+ # The system color picker returned an rgba value
+ if 'rgba' in input:
+ strip = input.strip('rgba)(')
+ array = strip.split(',')
+ # Round the alpha value to two decimal placed
+ array[3] = round(float(array[3]), 2)
+ rgba_string = "rgba("
+ values = ",".join(str(x) for x in array)
+ rgba_string += values + ")"
+ return rgba_string
+ # The system color picker returned an rgb value
+ elif 'rgb' in input:
+ # RGB as input
+ if format == 'rgb':
+ return input
+ else:
+ # Strip 'rgb' and parenthesis
+ strip = input.strip('rgb)(')
+ array = strip.split(',')
+
+ if format == 'hex':
+ hex = '#%02x%02x%02x' % (int(array[0]), int(array[1]), int(array[2]))
+ return hex.upper()
+ elif format == 'rgba':
+ rgba_string = "rgba("
+ array.append(1)
+ values = ",".join(str(x) for x in array)
+ rgba_string += values + ")"
+ return rgba_string
+ return array
+ # The system olor picker returned a hex
+ elif '#' in input:
+ # If there is a '#' in input,
+ # they are most likely using Qarma instead of Zenity
+ # or any other program that outputs hex
+ if format == 'hex':
+ return input
+ else:
+ hex = input.lstrip('#')
+ rgb_array = tuple(int(hex[i:i+2], 16) for i in (0, 2 ,4))
+
+ if format == 'rgb':
+ rgb_string = "rgb("
+ for i in range(0, len(rgb_array)):
+ rgb_string += str(rgb_array[i])
+ if i < len(rgb_array) - 1:
+ rgb_string += ", "
+ else:
+ rgb_string += ")"
+ return rgb_string
+ elif format == 'rgba':
+ rgba_string = "rgba("
+ for i in range(0, len(rgb_array)):
+ rgba_string += str(rgb_array[i])
+ if i < len(rgb_array) - 1:
+ rgba_string += ", "
+ else:
+ rgba_string += ", 1)"
+ return rgba_string
+ return None