-
-
Save feekApp/3d29b035d839b2665b921ac84f08fc52 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -wU | |
require 'erb' | |
require 'fileutils' | |
require 'shellwords' | |
# extension to String to extract colours into rgb array | |
class String | |
# hex to array | |
def h2a | |
if self.length == 4 | |
self.scan(/[0-9A-Fa-f]/). | |
map {|i| (i*2).to_i(16) } | |
else | |
self.scan(/[0-9A-Fa-f]{2}/). | |
map {|i| i.to_i(16) } | |
end | |
end | |
# rgb to array | |
def rgb2a | |
self.gsub(/[\s\(\)rgba]/,''). | |
split(',')[0,3]. | |
map{|c| c.to_i} | |
end | |
end | |
# extension to Array to divide all elements by 255 | |
# to get decimal rgb value | |
class Array | |
def by255 | |
self.map{|i| (i/255.0).round(4).to_s } | |
end | |
end | |
# template for | |
def get_template() | |
end | |
class ColorBox | |
include ERB::Util | |
attr_accessor :color, :dir | |
Template = %{ | |
%!PS-Adobe-3.0 EPSF-3.0 | |
%%BoundingBox: 0 0 80 80 | |
gsave 0 0 80 80 rectclip gsave | |
<%= @color.join(' ') %> setrgbcolor | |
10 10 70 70 rectfill | |
grestore grestore | |
} | |
def initialize(dir) | |
Dir.mkdir(dir, 0700) unless Dir.exist?(dir) | |
@dir = dir | |
end | |
# set color | |
def set_color(color) | |
@color = get_rgb_array(color) | |
end | |
# get array from color string | |
def get_rgb_array(c) | |
if c[0] == '#' | |
return c.h2a.by255 | |
end | |
if c.match(/^rgb/) | |
return c.rgb2a.by255 | |
end | |
end | |
# render the template | |
def render() | |
ERB.new(Template).result(binding) | |
end | |
# save the file in the given dir unless it exists | |
def save() | |
file = File.join(@dir, @color.join('-')+'.eps') | |
unless File.exists?(file.to_s) | |
File.open(file, "w+") do |f| | |
f.write(render) | |
end | |
end | |
return file.to_s | |
end | |
end | |
def markup | |
dir = "#{ENV['HOME']}/Library/Caches/com.macromates.textmate.colors/" | |
args = [ "--clear-mark=#{dir.shellescape}", "--uuid=#{ENV['TM_DOCUMENT_UUID']}" ] | |
box = ColorBox.new(dir) | |
ARGF.each_with_index do |line, idx| | |
line.scan(/(#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})(?=[^0-9A-Fa-f])|rgba{0,1}\([0-9]{1,3}[, ]+?[0-9]{1,3}[, ]+?[0-9]{1,3}[, 0-9\.]*?\))/) { |c| | |
box.set_color(c[0]) | |
args << "--line=#{idx+1}" << "--set-mark=#{box.save.shellescape}" | |
} | |
end | |
system(ENV['TM_MATE'], *args) | |
end | |
# fork and redirect output to log | |
pid = fork do | |
FileUtils.mkdir_p("#{ENV['HOME']}/Library/Caches/com.macromates.textmate.colors") | |
STDOUT.reopen(open("#{ENV['HOME']}/Library/Caches/com.macromates.textmate.colors/log.txt", 'w')) | |
STDERR.reopen(open("#{ENV['HOME']}/Library/Caches/com.macromates.textmate.colors/err.txt", 'w')) | |
markup | |
end | |
Process.detach(pid) |
For me it is still working in Ventura. Do you have ruby installed?
And tbh, I did not writte this code. I found it somewhere on the old Macromates (Textmate) mailinglist.
hi @mugsuk ,
Now I remember Textmate relies on Ruby 1.8.7
Is the /Users/Mugsuk/Library/Application Support/TextMate/Ruby/1.8.7
directory present on your system? Maybe you can restore this directory from you backup?
Hi @mugsuk
sorry, I cannot help you more than this. Only this I can do, is share my settings:
under Scope Selector:
source.css, source.css.less
and maybe you should add source.css.scss
under Semantic Class (in order to activate script):
callback.document.did-save, callback.document.did-open, callback.document.did-change-scm-status
Last suggestion ask / search here on the mailinglist
Hi Feek
This has stopped working in macOS since venture; any idea why? Thanks
Murray