[TxMt] Insert Color Revised (help needed)

Brett Terpstra brett at circlesixdesign.com
Sat Oct 21 12:21:40 UTC 2006


I think this needed a minor revision to make it work if there was no  
text selected (no previous color) and you just wanted to insert a  
color.  Once again, probably not the most elegant way to handle it  
(but it works).  Let me know what you think.

#!/usr/bin/env ruby

string = STDIN.read
prefix, string = string.match(/(#?)(.*)/)[1,2]
string = $1 * 2 + $2 * 2 + $3 * 2 if string =~ /^(.)(.)(.)$/
if string == '' then
   prefix = '#'
   def_col = ''
else
   def_col = ' default color {' + string.scan(/../).map { |i| i.hex *  
257 }.join(",") + '}'
end
col = `osascript 2>/dev/null -e 'tell app "TextMate" to choose color# 
{def_col}'`
exit 200 if col == ""
col=col.scan(/\d+/).map { |i| "%02X" % (i.to_i / 257) }.join("")
print prefix
if col.match(/(.)\1(.)\2(.)\3/) then
   print $1 + $2 + $3
else
   print col
end


Brett

On Oct 21, 2006, at 12:19 AM, Charilaos Skiadas wrote:

> On Oct 20, 2006, at 10:42 PM, Brett Terpstra wrote:
>
>> It works, but I'd really like to see it cleaned up (I know Haris  
>> could do this in about 4 lines of code).  So if anyone has the  
>> time... It'd be great to learn a new algorithm to handle this.
>
> Well, since you asked for it ;) I'm sure JEG2 can shorten this  
> further, but here is my take on it. I've attached the command, and  
> appended the actual code.
>
> Perhaps we should add this to the CSS bundle?
>
> Haris
>
> #!/usr/bin/env ruby
> string = STDIN.read
> prefix, string = string.match(/(#?)(.*)/)[1,2]
> string = $1 * 2 + $2 * 2 + $3 * 2 if string =~ /^(.)(.)(.)$/
> def_col = ' default color {' + string.scan(/../).map { |i| i.hex *  
> 257 }.join(",") + '}'
> col = `osascript 2>/dev/null -e 'tell app "TextMate" to choose  
> color#{def_col}'`
> exit 200 if col == ""
> col=col.scan(/\d+/).map { |i| "%02X" % (i.to_i / 257) }.join("")
> print prefix
> if col.match(/(.)\1(.)\2(.)\3/) then
>   print $1 + $2 + $3
> else
>   print col
> end
>
> <Insert Color revised copy2.tmCommand>
>
> ______________________________________________________________________
> For new threads USE THIS: textmate at lists.macromates.com
> (threading gets destroyed and the universe will collapse if you don't)
> http://lists.macromates.com/mailman/listinfo/textmate

Brett Terpstra : Art Director
Circle Six Design, Inc.
111 Riverfront Dr, Suite 204
..................................................
p: 507.459.4398
     877.858.4332
f: 1.866.540.3063
e: brett at circlesixdesign.com
http://www.circlesixdesign.com
..................................................





More information about the textmate mailing list