1
0
mirror of https://github.com/Morantron/tmux-fingers.git synced 2024-06-24 07:26:44 +02:00
tmux-fingers/lib/tmux_format_printer.rb

111 lines
2.0 KiB
Ruby
Raw Permalink Normal View History

2020-05-02 11:45:56 +02:00
class TmuxFormatPrinter
2022-07-06 16:46:12 +02:00
FORMAT_SEPARATOR = /[ ,]+/
2020-05-02 11:45:56 +02:00
COLOR_MAP = {
black: 0,
red: 1,
green: 2,
yellow: 3,
blue: 4,
magenta: 5,
cyan: 6,
white: 7
}.freeze
LAYER_MAP = {
2022-07-06 16:46:12 +02:00
bg: "setab",
fg: "setaf"
2020-05-02 11:45:56 +02:00
}.freeze
STYLE_MAP = {
2022-07-06 16:46:12 +02:00
bright: "bold",
bold: "bold",
dim: "dim",
underscore: "smul",
reverse: "rev",
italics: "sitm"
2020-05-02 11:45:56 +02:00
}.freeze
class ShellExec
def exec(cmd)
`#{cmd}`.chomp
end
end
def initialize(shell: ShellExec.new)
@shell = shell
end
def print(input, reset_styles_after: false)
2020-05-02 11:45:56 +02:00
@applied_styles = {}
2022-07-06 16:46:12 +02:00
output = ""
2020-05-02 11:45:56 +02:00
input.split(FORMAT_SEPARATOR).each do |format|
output += parse_format(format)
2020-05-02 11:45:56 +02:00
end
output += reset_sequence if reset_styles_after && !@applied_styles.empty?
2020-05-02 11:45:56 +02:00
output
end
def parse_format(format)
2022-07-06 16:46:12 +02:00
if /^(bg|fg)=/.match?(format)
2020-05-02 11:45:56 +02:00
parse_color(format)
else
parse_style(format)
end
end
def parse_color(format)
match = format.match(/(?<layer>bg|fg)=(?<color>(colou?r(?<color_code>[0-9]+)|.*))/)
layer = match[:layer].to_sym
color = match[:color].to_sym
color_code = match[:color_code]
2022-07-06 16:46:12 +02:00
if match[:color] == "default"
2020-05-02 11:45:56 +02:00
@applied_styles.delete(layer)
return reset_to_applied_styles!
end
color_to_apply = color_code || COLOR_MAP[color]
result = shell.exec("tput #{LAYER_MAP[layer]} #{color_to_apply}")
@applied_styles[layer] = result
result
end
def parse_style(format)
match = format.match(/(?<remove>no)?(?<style>.*)/)
2022-07-06 16:46:12 +02:00
should_remove_style = match[:remove] == "no"
2020-05-02 11:45:56 +02:00
style = match[:style].to_sym
result = shell.exec("tput #{STYLE_MAP[style]}")
if should_remove_style
@applied_styles.delete(style)
return reset_to_applied_styles!
end
@applied_styles[style] = result
result
end
def reset_to_applied_styles!
[reset_sequence, @applied_styles.values].join
end
def reset_sequence
2022-07-06 16:46:12 +02:00
@reset_sequence ||= shell.exec("tput sgr0").chomp.freeze
2020-05-02 11:45:56 +02:00
end
private
attr_reader :shell
end