Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable advanced color modes in screen/put-string #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/lanterna/screen.clj
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,24 @@
Options can contain any of the following:

:fg - Foreground color.
Can be any one of (keys lanterna.constants/colors).
(default :default)
This can be either a keyword or a string.
If it's a keyword, it should be one of (keys lanterna.constants/colors).
Not all terminals support all colormodes, you can consult lanterna's
[TextColor documentaion](https://mabe02.github.io/lanterna/apidocs/3.1/com/googlecode/lanterna/TextColor.html)
to learn more.
If it is a string, it must be in one of the following formats.

* \"blue\", a string matching a color constant.
* \"#1 \", a string consisting of an int between 0 and 255.
This will use the indexed colormode, looking up the color in the terminal's own theme.
* \"#a1a1a1\", a hex color code in a string. This draws using the color provided in RGB mode..

As an escape hatch, directly providing a TextColor will use that to draw instead.
an unparseable input will draw with (lanterna.constants/colors :default) or error.


:bg - Background color.
Can be any one of (keys lanterna.constants/colors).
(default :default)
The same options for :fg, but will apply to the background instead.

:styles - Styles to apply to the text.
Can be a set containing some/none/all of (keys lanterna.constants/styles).
Expand All @@ -145,8 +157,8 @@
(into-array SGR (map c/styles styles))
col (int col)
row (int row)
fg ^TextColor (c/colors fg)
bg ^TextColor (c/colors bg)]
fg (t/parse-color fg)
bg (t/parse-color bg)]
(reduce (fn [acc ^java.lang.Character c]
(let [col (:col acc)
row (:row acc)
Expand Down Expand Up @@ -267,4 +279,4 @@
"
([^Screen screen] (get-key-blocking screen {}))
([^Screen screen {:keys [interval timeout] :as opts}]
(block-on get-key [screen] opts)))
(block-on get-key [screen] opts)))
24 changes: 23 additions & 1 deletion src/lanterna/terminal.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
com.googlecode.lanterna.terminal.Terminal
com.googlecode.lanterna.terminal.TerminalResizeListener
com.googlecode.lanterna.terminal.ansi.UnixTerminal
com.googlecode.lanterna.terminal.ansi.CygwinTerminal)
com.googlecode.lanterna.terminal.ansi.CygwinTerminal
com.googlecode.lanterna.TextColor)
(:require
[lanterna.common :refer [parse-key block-on]]
[lanterna.constants :as c]))
Expand Down Expand Up @@ -137,6 +138,27 @@
[^Terminal terminal]
(.flush terminal))

(defn parse-color ^TextColor
"Takes a keyword, a string, or a TextColor, and returns a TextColor.
For keyword, it looks it up in lanterna.constants/colors.
For a string, it parses it using the underlying java implementation.
For a text color, it returns it unchanged.

Returns (lanterna.constants/colors :default) or errors.
"
[inp-color]
(cond
(instance? TextColor inp-color)
inp-color

(keyword? inp-color)
(c/colors inp-color)

(string? inp-color)
(com.googlecode.lanterna.TextColor$Factory/fromString inp-color)

:else
(c/colors :default)))

(defn set-fg-color [^Terminal terminal color]
(.setForegroundColor terminal (c/colors color)))
Expand Down