Add JQLStyle class

This commit is contained in:
Roland Thomas Jr 2024-06-09 00:45:21 -04:00
parent bc2fad47e2
commit 50e0eb839e
Signed by: roland
GPG Key ID: 7C3C2B085A4C2872
1 changed files with 196 additions and 176 deletions

View File

@ -1,25 +1,19 @@
import json
from typing import Dict, List
from pygments.lexer import RegexLexer
from pygments.token import (
Text,
Keyword,
Operator,
Punctuation,
Name,
String,
Whitespace,
Error,
)
from prompt_toolkit import PromptSession
from prompt_toolkit.lexers import PygmentsLexer
from prompt_toolkit.styles import Style
from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.validation import Validator, ValidationError
from rich.console import Console
from rich.text import Text as RichText
from jira import JIRA
from jira.exceptions import JIRAError
from prompt_toolkit import PromptSession
from prompt_toolkit.completion import Completer, Completion
from prompt_toolkit.lexers import PygmentsLexer
from prompt_toolkit.styles import Style
from prompt_toolkit.validation import ValidationError, Validator
from prompt_toolkit.shortcuts import confirm
from pygments.lexer import RegexLexer
from pygments.token import Error, Keyword, Name, Operator, Punctuation, String, Text, Whitespace, _TokenType
from rich.console import Console
from rich.text import Text as RichText
class JQLLexer(RegexLexer):
@ -66,7 +60,8 @@ class JQLLexer(RegexLexer):
}
nord_style = Style.from_dict(
class JQLStyles:
nord: Style = Style.from_dict(
{
"pygments.whitespace": "#FFFFFF",
"pygments.keyword": "#81A1C1 bold",
@ -81,8 +76,7 @@ nord_style = Style.from_dict(
}
)
token_styles = {
token: Dict[_TokenType, str] = {
Whitespace: "#FFFFFF",
Keyword: "#81A1C1 bold",
Operator: "#EBCB8B bold",
@ -95,8 +89,7 @@ token_styles = {
Name.Other: "#D08770",
}
completion_styles = {
completion: Dict[str, str] = {
"Keywords": "#81A1C1 bold",
"Functions": "#A3BE8C",
"Attributes": "#B48EAD",
@ -105,7 +98,7 @@ completion_styles = {
}
completions ={
completions: Dict[str, List[str]] = {
"Keywords": [
"A",
"AND",
@ -137,7 +130,7 @@ completions ={
"THIS",
"TO",
"WILL",
"WITH"
"WITH",
],
"Functions": [
"issueHistory",
@ -157,7 +150,7 @@ completions ={
"startOfMonth",
"endOfMonth",
"startOfYear",
"endOfYear"
"endOfYear",
],
"Attributes": [
"assignee",
@ -186,7 +179,7 @@ completions ={
"text",
"timespent",
"voter",
"watcher"
"watcher",
],
"Operators": [
"=",
@ -204,7 +197,7 @@ completions ={
"WAS",
"WAS IN",
"WAS NOT IN",
"WAS NOT"
"WAS NOT",
],
"Projects": [
"QUANTUM",
@ -216,8 +209,8 @@ completions ={
"COSMIC",
"LUNAR",
"ASTRAL",
"PHOTON"
]
"PHOTON",
],
}
@ -232,7 +225,7 @@ class JQLPrinter:
tokens = list(JQLLexer().get_tokens(text))
rich_text = RichText()
for token_type, value in tokens:
style = token_styles.get(token_type, "white")
style = JQLStyles.token.get(token_type, "white")
rich_text.append(value, style=style)
return rich_text
@ -269,8 +262,8 @@ class JQLCompleter(Completer):
start_position=-len(text),
display=display_text,
display_meta=category,
style=f"fg: #D8DEE9 bg: {completion_styles.get(category, 'white')}",
selected_style=f"fg: {completion_styles.get(category, 'white')} bg: #D8DEE9",
style=f"fg: #D8DEE9 bg: {JQLStyles.completion.get(category, 'white')}",
selected_style=f"fg: {JQLStyles.completion.get(category, 'white')} bg: #D8DEE9",
)
@ -304,16 +297,28 @@ class JQLPrompt:
def create_jql_prompt_session(self):
completer = JQLCompleter(completions)
return PromptSession(
message=[("#B48EAD", "JQL \u276f ")],
lexer=PygmentsLexer(JQLLexer),
style=nord_style,
style=JQLStyles.nord,
completer=completer,
validator=JQLValidator(self.jira),
rprompt="[b] Back [exit] Exit",
rprompt=[
("#5E81AC bold", "[b] Back "),
("#BF616A bold", "[exit] Exit"),
],
bottom_toolbar=self.get_query_count,
validate_while_typing=False,
)
def get_input(self):
user_input = self.session.prompt("Enter JQL: ", validate_while_typing=False)
user_input = self.session.prompt()
if not user_input:
do_empty_query = confirm(
[("#EBCB8B bold", "[?] "), ("#D8DEE9 bold", "Do you want to perform an empty query?")],
suffix=[("#81A1C1 bold", " (Y/n) ")],
)
if not do_empty_query:
return
if user_input.lower() == "b":
return
if user_input.lower() == "exit":
@ -340,6 +345,21 @@ class JQLPrompt:
self.console.print("Goodbye!", style="#BF616A bold")
"""
[+] for additions
[-] for deletions
[~] for changes
[<] for incoming
[>] for outgoing
[] for success
[] for failure
[] for warnings
[?] for questions
[!] for errors
[] for information
"""
def main():
config = load_config()
console = Console(color_system="truecolor")