This repository was archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
highlight tvm script with pygment on vscode dark theme #185
Merged
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2c58187
feat: highlight tvm script with pygment on vscode dark theme
ganler c9b0d35
feat: add light theme (Jupyter style)
ganler e8fa6ea
try: add Pygment dependency to gen_requirements.py
ganler 5d100ea
fix: gen_requirement.py ordered by package name
ganler 7d29490
fix: Pygment -> Pygements
ganler 64ee682
Merge branch 'script-highlight' of github.com:ganler/relax into scrip…
ganler 38968f2
refact: refine highlighted printer API
ganler 924d2d4
fix(pylint): line too long
ganler a61644e
refact(doc): add Pygments style url and Pygment -> Pygements
ganler 4ff21e4
feat: add .show syntax sugar
ganler 49851d6
feat: enable .script and .show method for relax.Function
ganler 52395ae
fix: typo Pygements -> Pygment
ganler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,6 +72,7 @@ | |
| "scipy", | ||
| "synr", | ||
| "tornado", | ||
| "Pygments", | ||
| ], | ||
| ), | ||
| ), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| """Highlight printed TVM script. | ||
| """ | ||
| import os | ||
|
|
||
| from pygments import highlight as phl | ||
| from pygments.lexers import Python3Lexer | ||
| from pygments.formatters import Terminal256Formatter | ||
| from pygments.style import Style | ||
| from pygments.token import Keyword, Name, Comment, String, Number, Operator | ||
|
|
||
|
|
||
| _SCRIPT_THEME = os.getenv("TVM_SCRIPT_THEME", default="LIGHT") | ||
|
|
||
|
|
||
| class VSCDark(Style): | ||
| """A VSCode-Dark-like Pygment style configuration""" | ||
|
|
||
| styles = { | ||
| Keyword: "bold #c586c0", | ||
| Keyword.Namespace: "#4ec9b0", | ||
| Keyword.Type: "#82aaff", | ||
| Name: "#9cdcfe", | ||
| Name.Function: "bold #dcdcaa", | ||
| Name.Class: "bold #569cd6", | ||
| Name.Decorator: "italic #fe4ef3", | ||
| String: "#ce9178", | ||
| Number: "#b5cea8", | ||
| Operator: "#bbbbbb", | ||
| Operator.Word: "#569cd6", | ||
| Comment: "italic #6a9956", | ||
| } | ||
|
|
||
|
|
||
| class JupyterLight(Style): | ||
| """A Jupyter-Notebook-like Pygment style configuration""" | ||
|
|
||
| background_color = "#f8f8f8" | ||
| default_style = "" | ||
|
|
||
| styles = { | ||
| Keyword: "bold #008000", | ||
| Keyword.Type: "nobold #008000", | ||
| Name.Function: "#0000FF", | ||
| Name.Class: "bold #0000FF", | ||
| Name.Decorator: "#AA22FF", | ||
| String: "#BA2121", | ||
| Number: "#008000", | ||
| Operator: "bold #AA22FF", | ||
| Operator.Word: "bold #008000", | ||
| Comment: "italic #007979", | ||
| } | ||
|
|
||
|
|
||
| def highlight(text: str) -> str: | ||
| """ | ||
| Highlight given TVM script string with Pygment | ||
ganler marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Parameters | ||
| ---------- | ||
| text : str | ||
| String of the TVM script | ||
|
|
||
| Returns | ||
| ------- | ||
| str | ||
| The resulting string highlighted by ANSI escape code | ||
| """ | ||
| style = None | ||
| if _SCRIPT_THEME == "LIGHT": | ||
| style = JupyterLight | ||
| elif _SCRIPT_THEME == "DARK": | ||
| style = VSCDark | ||
| else: | ||
| raise ValueError( | ||
| f"Unknown theme environement variable: `{_SCRIPT_THEME}`." | ||
| " Set TVM_SCRIPT_THEME to LIGHT or DARK" | ||
| ) | ||
| return phl(text, Python3Lexer(), Terminal256Formatter(style=style)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.