From ac0447b07e89faa3f3650a795396db8ecb8a32cc Mon Sep 17 00:00:00 2001 From: David Morris Date: Fri, 28 Feb 2025 07:19:04 +0000 Subject: [PATCH 1/4] Adding sqlite fts5 database --- .gitignore | 2 +- powerindex/app.py | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index f0523d7..88f5edc 100644 --- a/.gitignore +++ b/.gitignore @@ -149,7 +149,7 @@ environment.json shell-profile -hooks.db +*.db requirements-pwrhook.txt diff --git a/powerindex/app.py b/powerindex/app.py index ad38939..8a5e06c 100644 --- a/powerindex/app.py +++ b/powerindex/app.py @@ -3,6 +3,7 @@ import string import re import subprocess +import sqlite3 from flask import Flask, render_template, request, jsonify from pygments import highlight from pygments.lexers import BashLexer, JsonLexer @@ -35,6 +36,27 @@ def update_curl_flags(): # Run the update immediately update_curl_flags() +# Initialize the database +def init_db(): + db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'the-power.db') + conn = sqlite3.connect(db_path) + cursor = conn.cursor() + + # Create FTS5 table + cursor.execute(''' + CREATE VIRTUAL TABLE IF NOT EXISTS scripts_fts USING fts5( + script, + body + ) + ''') + + conn.commit() + conn.close() + print(f"Database initialized at {db_path}") + +# Add database initialization before Flask app creation +init_db() + app = Flask(__name__) # Add this at the top level for use in the template From 4556c2030ad5b401fde1639dbd3b456dc45ebec2 Mon Sep 17 00:00:00 2001 From: David Morris Date: Fri, 28 Feb 2025 07:28:33 +0000 Subject: [PATCH 2/4] Adding scripts and body into full text search table --- powerindex/app.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/powerindex/app.py b/powerindex/app.py index 8a5e06c..b561deb 100644 --- a/powerindex/app.py +++ b/powerindex/app.py @@ -50,6 +50,23 @@ def init_db(): ) ''') + # Clear existing entries + cursor.execute('DELETE FROM scripts_fts') + + # Load all scripts into the database + parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + shell_scripts = glob.glob(os.path.join(parent_dir, '*.sh')) + + for script_path in shell_scripts: + script_name = os.path.basename(script_path) + try: + with open(script_path, 'r') as file: + content = file.read() + cursor.execute('INSERT INTO scripts_fts (script, body) VALUES (?, ?)', + (script_name, content)) + except Exception as e: + print(f"Error loading script {script_name}: {str(e)}") + conn.commit() conn.close() print(f"Database initialized at {db_path}") From 18d2101b0ceadad68a07f68b7ffdbfe21d72e05b Mon Sep 17 00:00:00 2001 From: David Morris Date: Fri, 28 Feb 2025 08:09:00 +0000 Subject: [PATCH 3/4] Adding full text search --- powerindex/app.py | 24 ++++ powerindex/templates/base.html | 56 ++++++++ powerindex/templates/index.html | 228 +++++++++++++++---------------- powerindex/templates/search.html | 33 +++++ 4 files changed, 222 insertions(+), 119 deletions(-) create mode 100644 powerindex/templates/base.html create mode 100644 powerindex/templates/search.html diff --git a/powerindex/app.py b/powerindex/app.py index b561deb..2c76561 100644 --- a/powerindex/app.py +++ b/powerindex/app.py @@ -271,5 +271,29 @@ def highlight_search(text, search): pattern = re.compile(f'({re.escape(search)})', re.IGNORECASE) return pattern.sub(r'\1', text) +@app.route('/search', methods=['GET']) +def search(): + query = request.args.get('q', '') + results = [] + + if query: + # Use the same database path as init_db() + db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'the-power.db') + conn = sqlite3.connect(db_path) + cursor = conn.cursor() + + # Use FTS5 match to find results and highlight matching text + cursor.execute(''' + SELECT script, snippet(scripts_fts, 1, '', '', '...', 50) + FROM scripts_fts + WHERE body MATCH ? + ORDER BY rank + ''', (query,)) + + results = cursor.fetchall() + conn.close() + + return render_template('search.html', query=query, results=results) + if __name__ == '__main__': app.run(debug=False , host='localhost', port=8001) diff --git a/powerindex/templates/base.html b/powerindex/templates/base.html new file mode 100644 index 0000000..64f60a2 --- /dev/null +++ b/powerindex/templates/base.html @@ -0,0 +1,56 @@ + + + + + + {% block title %}Power Index{% endblock %} + + + + +
+
+
+ +
+
+ +
+ {% block content %} + {% endblock %} +
+
+ + {% block scripts %} + + {% endblock %} + + \ No newline at end of file diff --git a/powerindex/templates/index.html b/powerindex/templates/index.html index e4c8ed6..bba1aa5 100644 --- a/powerindex/templates/index.html +++ b/powerindex/templates/index.html @@ -1,135 +1,125 @@ - - - - - - Power Index - - - - -
-
- -

The Power Viewer

-
- -
- - -
-
- - {% if script_content %} -
-

- - - {{ selected_script }} -

-
-
-
-

Original Script

-
-
- {{ highlighted_content | safe }} -
+{% extends "base.html" %} + +{% block title %}The Power Viewer{% endblock %} + +{% block header %}The Power Viewer{% endblock %} + +{% block content %} +
+ + +
+
+ + {% if script_content %} +
+

+ + + {{ selected_script }} +

+
+
+
+

Original Script

+
+
+ {{ highlighted_content | safe }}
-
-
-
-

Rendered Script

- -
-
- {{ rendered_content | safe }} -
+
+
+
+
+

Rendered Script

+ +
+
+ {{ rendered_content | safe }}
-
-
-
-
-

Standard Output

- -
- +
+
+
+
+
+

Standard Output

+
-

+                        
                     
-
-

Standard Error

- -
-
-
{{ stderr }}
-
+

+                
+
+

Standard Error

+ +
+
+
{{ stderr }}
-
-

Headers

- - -
-
-
{{ headers }}
-
+
+
+

Headers

+ + +
+
+
{{ headers }}
- -

Configuration

- - - - - - - - - - {% for key, value in config.items() %} - - - - - {% endfor %} - -
KeyValue
{{ key }}{{ value }}
- {% endif %} + +

Configuration

+ + + + + + + + + + {% for key, value in config.items() %} + + + + + {% endfor %} + +
KeyValue
{{ key }}{{ value }}
+ {% endif %}
+{% endblock %} +{% block scripts %} + {{ super() }} - - +{% endblock %} diff --git a/powerindex/templates/search.html b/powerindex/templates/search.html new file mode 100644 index 0000000..f28ced4 --- /dev/null +++ b/powerindex/templates/search.html @@ -0,0 +1,33 @@ +{% extends "base.html" %} + +{% block title %}Power Index - Full Text Search{% endblock %} + +{% block header %}Full Text Search{% endblock %} + +{% block content %} +
+
+ + +
+
+ +{% if query %} +
+

Results for "{{ query }}"

+ {% if results %} + {% for script, snippet in results %} +
+

{{ script }}

+
{{ snippet | safe }}
+ +
+ {% endfor %} + {% else %} +

No results found.

+ {% endif %} +
+{% endif %} +{% endblock %} \ No newline at end of file From df896a23c0c9f44b9bef52736a2b5cf1adeb7243 Mon Sep 17 00:00:00 2001 From: David Morris Date: Fri, 28 Feb 2025 08:15:03 +0000 Subject: [PATCH 4/4] adding navbar --- powerindex/templates/base.html | 1 + powerindex/templates/navbar.html | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100644 powerindex/templates/navbar.html diff --git a/powerindex/templates/base.html b/powerindex/templates/base.html index 64f60a2..6e5b7da 100644 --- a/powerindex/templates/base.html +++ b/powerindex/templates/base.html @@ -25,6 +25,7 @@

{% block header %}Power Index{% endblock %}

+ {% include 'navbar.html' %}
diff --git a/powerindex/templates/navbar.html b/powerindex/templates/navbar.html new file mode 100644 index 0000000..484239d --- /dev/null +++ b/powerindex/templates/navbar.html @@ -0,0 +1,6 @@ +