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

114 lines
2.0 KiB
Ruby
Raw Normal View History

2020-05-02 11:45:56 +02:00
class TmuxFormatPrinter
PATTERN_FORMAT = /#\[([^\]]*)\]/.freeze
COLOR_MAP = {
black: 0,
red: 1,
green: 2,
yellow: 3,
blue: 4,
magenta: 5,
cyan: 6,
white: 7
}.freeze
LAYER_MAP = {
bg: 'setab',
fg: 'setaf'
}.freeze
STYLE_MAP = {
bright: 'bold',
bold: 'bold',
dim: 'dim',
underscore: 'smul',
reverse: 'rev',
italics: 'sitm'
}.freeze
class ShellExec
def exec(cmd)
`#{cmd}`.chomp
end
end
def initialize(shell: ShellExec.new)
@shell = shell
end
def print(input)
@applied_styles = {}
output = input.gsub(PATTERN_FORMAT) do
match = $~
formats = match[1]
formats.split(',').map do |format|
parse_format(format)
end.join
end
output += reset_sequence unless @applied_styles.empty?
output
end
def parse_format(format)
if format.match(/^(bg|fg)=/)
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]
if match[:color] == 'default'
@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>.*)/)
should_remove_style = match[:remove] == 'no'
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
@reset_sequence ||= shell.exec('tput sgr0').chomp.freeze
end
private
attr_reader :shell
end