Skip to content

Commit

Permalink
Add a separate theme preference for terminal output
Browse files Browse the repository at this point in the history
This is useful because a lot of people seem to prefer a dark theme for
terminal output pastes, but a light theme for normal pastes.

This makes monokai the default for terminal output pastes, and keeps
"default" as the default for regular pastes. The theme preference is now
separated out: you can have a theme configured for regular pastes, and a
separate theme configured for terminal output pastes.
  • Loading branch information
chriskuehl committed May 24, 2019
1 parent 32e1ed5 commit 2ee9dd7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
8 changes: 7 additions & 1 deletion fluffy/component/highlighting.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,18 @@ class PygmentsHighlighter(namedtuple('PygmentsHighlighter', ('lexer',))):
def name(self):
return self.lexer.name

@property
def is_terminal_output(self):
return 'ansi-color' in self.lexer.aliases

def highlight(self, text):
text = _highlight(text, self.lexer)
return text


class DiffHighlighter(namedtuple('PygmentsHighlighter', ('lexer',))):
class DiffHighlighter(namedtuple('DiffHighlighter', ('lexer',))):

is_terminal_output = False

@property
def name(self):
Expand Down
2 changes: 1 addition & 1 deletion fluffy/templates/layouts/text.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% endblock %}

{% block content %}
<div id="highlightContainer" class="highlight-default">
<div id="highlightContainer" class="{% block highlight_class %}highlight-default{% endblock %}">
{% block highlight_start %}{% endblock %}
<div class="paste-toolbar">
<div class="info">
Expand Down
21 changes: 18 additions & 3 deletions fluffy/templates/paste.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
{% set page_name = 'paste' %}
{% extends 'layouts/text.html' %}

{#
Terminal output gets its own preferred theme setting, since many people
seem to prefer a dark background for terminal output, but a light
background for regular code.
#}
{% if highlighter.is_terminal_output %}
{% set preferredStyleVar = 'preferredStyleTerminal' %}
{% set defaultStyle = 'monokai' %}
{% else %}
{% set preferredStyleVar = 'preferredStyle' %}
{% set defaultStyle = 'default' %}
{% endif %}
{% block highlight_class %}highlight-{{defaultStyle}}{% endblock %}

{% block extra_head %}
<link rel="stylesheet" href="{{asset_url('pygments.css')}}" />
{% endblock %}
Expand All @@ -11,10 +25,11 @@

{% block highlight_start %}
<script>
var preferredStyleVar = {{preferredStyleVar|tojson}};
function changeStyleTo(style) {
document.getElementById('highlightContainer').className = 'highlight-' + style;
if (hasStorage) {
localStorage.setItem('preferredStyle', style);
localStorage.setItem(preferredStyleVar, style);
}
}

Expand All @@ -32,7 +47,7 @@

var preferredStyle = null;
if (hasStorage) {
preferredStyle = localStorage.getItem('preferredStyle');
preferredStyle = localStorage.getItem(preferredStyleVar);
if (preferredStyle !== null) {
changeStyleTo(preferredStyle);
}
Expand All @@ -58,7 +73,7 @@
{% for style in styles %}
<option
value="{{style.name}}"
{% if style.name == 'default' %}
{% if style.name == defaultStyle %}
selected="selected"
{% endif %}
>{{style.name}}</option>
Expand Down

0 comments on commit 2ee9dd7

Please sign in to comment.