Skip to content
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
18 changes: 2 additions & 16 deletions spec/lib/tmux_style_printer_spec.cr
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
require "spec"
require "../../src/tmux_style_printer"

class FakeShell < TmuxStylePrinter::Shell
def exec(cmd)
"$(#{cmd})"
end
end

describe TmuxStylePrinter do
it "transforms tmux status line format into escape sequences" do
printer = TmuxStylePrinter.new(shell = FakeShell.new)
result = printer.print("bg=red,fg=yellow,bold", reset_styles_after: true)
expected = "$(tput setab 1)$(tput setaf 3)$(tput bold)$(tput sgr0)"

result.should eq expected
end

it "transforms tmux status line format into escape sequences" do
printer = TmuxStylePrinter.new(shell = FakeShell.new)
printer = TmuxStylePrinter.new
result = printer.print("bg=red,fg=yellow,bold", reset_styles_after: true)
expected = "$(tput setab 1)$(tput setaf 3)$(tput bold)$(tput sgr0)"
expected = "\e[48;5;1m\e[38;5;3m\e[1m\e[0m"

result.should eq expected
end
Expand Down
49 changes: 15 additions & 34 deletions src/tmux_style_printer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ class TmuxStylePrinter
class InvalidFormat < Exception
end

abstract class Shell
abstract def exec(cmd)
end

STYLE_SEPARATOR = /[ ,]+/

COLOR_MAP = {
Expand All @@ -20,32 +16,25 @@ class TmuxStylePrinter
white: 7,
}

LAYER_MAP = {
bg: "setab",
fg: "setaf",
LAYER_CODE = {
"bg" => 48,
"fg" => 38,
}

STYLE_MAP = {
bright: "bold",
bold: "bold",
dim: "dim",
underscore: "smul",
reverse: "rev",
italics: "sitm",
"bright" => "\e[1m",
"bold" => "\e[1m",
"dim" => "\e[2m",
"underscore" => "\e[4m",
"reverse" => "\e[7m",
"italics" => "\e[3m",
}

class ShellExec < Shell
def exec(cmd)
`#{cmd}`.chomp
end
end
RESET = "\e[0m"

@shell : Shell
@applied_styles : Hash(String, String)
@reset_sequence : String | Nil

def initialize(shell = ShellExec.new)
@shell = shell
def initialize
@applied_styles = {} of String => String
end

Expand All @@ -58,7 +47,7 @@ class TmuxStylePrinter
output += parse_style_definition(style)
end

output += reset_sequence if reset_styles_after && !@applied_styles.empty?
output += RESET if reset_styles_after && !@applied_styles.empty?

output
end
Expand Down Expand Up @@ -89,7 +78,7 @@ class TmuxStylePrinter

raise InvalidFormat.new("Invalid color definition: #{style}") if color_to_apply.nil?

result = shell.exec("tput #{LAYER_MAP[layer]} #{color_to_apply}")
result = "\e[#{LAYER_CODE[layer]};5;#{color_to_apply}m"

@applied_styles[layer] = result

Expand All @@ -108,7 +97,7 @@ class TmuxStylePrinter

raise InvalidFormat.new("Invalid style definition: #{style}") if style_to_apply.nil?

result = style == "dim" ? "\033[2m" : shell.exec("tput #{STYLE_MAP[style]}")
result = style_to_apply

if should_remove_style
@applied_styles.delete(style)
Expand All @@ -121,14 +110,6 @@ class TmuxStylePrinter
end

private def reset_to_applied_styles!
[reset_sequence, @applied_styles.values].join
end

private def reset_sequence
@reset_sequence ||= shell.exec("tput sgr0").chomp
end

private def shell
@shell
[RESET, @applied_styles.values].join
end
end
Loading