Colors for 256-color terminal

This commit is contained in:
Junegunn Choi 2013-10-29 00:05:43 +09:00
parent f6df1eaa71
commit f80e617147

59
fzf
View File

@ -10,7 +10,7 @@
# URL: https://github.com/junegunn/fzf # URL: https://github.com/junegunn/fzf
# Author: Junegunn Choi # Author: Junegunn Choi
# License: MIT # License: MIT
# Last update: October 28, 2013 # Last update: October 29, 2013
# #
# Copyright (c) 2013 Junegunn Choi # Copyright (c) 2013 Junegunn Choi
# #
@ -164,17 +164,18 @@ end
C = Curses C = Curses
def max_items; C.lines - 2; end def max_items; C.lines - 2; end
def cursor_y; C.lines - 1; end def cursor_y; C.lines - 1; end
def cprint str, col, flag = C::A_BOLD def cprint str, col
C.attron C.color_pair(col) | flag C.attron(col) do
C.addstr str C.addstr str
C.attroff C.color_pair(col) | flag end
end end
def print_input def print_input
C.setpos cursor_y, 0 C.setpos cursor_y, 0
C.clrtoeol C.clrtoeol
cprint '> ', 1 cprint '> ', color(:blue, true)
cprint @query, 2 cprint @query, color(:normal, true)
cprint ' ' * C.cols, color(:normal, true)
end end
def print_info progress = true, msg = nil def print_info progress = true, msg = nil
@ -184,13 +185,17 @@ def print_info progress = true, msg = nil
prefix = prefix =
if fan = @fan.shift if fan = @fan.shift
@fan.push fan @fan.push fan
cprint fan, 5, 0 cprint fan, color(:fan, true)
' ' ' '
else else
' ' ' '
end end
C.addstr "#{prefix}#{@matches.length}/#{@count}" if progress C.attron color(:info, false) do
progress &&= "#{prefix}#{@matches.length}/#{@count}"
C.addstr progress if progress
C.addstr msg if msg C.addstr msg if msg
C.addstr ' ' * (C.cols - (progress.to_s + msg.to_s).length)
end
end end
def refresh def refresh
@ -229,11 +234,30 @@ C.init_screen
C.start_color C.start_color
C.raw C.raw
C.noecho C.noecho
if C.can_change_color?
fg = ENV.fetch('FZF_FG', 252).to_i
bg = ENV.fetch('FZF_BG', 236).to_i
C.init_pair 1, 110, bg
C.init_pair 2, fg, bg
C.init_pair 3, 108, bg
C.init_pair 4, 220, bg - 1
C.init_pair 5, 151, bg - 1
C.init_pair 6, 148, bg + 1
C.init_pair 7, 144, bg + 1
else
C.init_pair 1, C::COLOR_BLUE, C::COLOR_BLACK C.init_pair 1, C::COLOR_BLUE, C::COLOR_BLACK
C.init_pair 2, C::COLOR_WHITE, C::COLOR_BLACK C.init_pair 2, C::COLOR_WHITE, C::COLOR_BLACK
C.init_pair 3, C::COLOR_YELLOW, C::COLOR_BLACK C.init_pair 3, C::COLOR_CYAN, C::COLOR_BLACK
C.init_pair 4, C::COLOR_RED, C::COLOR_BLACK C.init_pair 4, C::COLOR_YELLOW, C::COLOR_BLACK
C.init_pair 5, C::COLOR_CYAN, C::COLOR_BLACK C.init_pair 5, C::COLOR_CYAN, C::COLOR_BLACK
C.init_pair 6, C::COLOR_GREEN, C::COLOR_BLACK
C.init_pair 7, C::COLOR_WHITE, C::COLOR_BLACK
end
def color sym, bold = false
C.color_pair([:blue, :normal, :match, :chosen, :match!, :fan, :info].index(sym) + 1) |
(bold ? C::A_BOLD : 0)
end
@read = @read =
if $stdin.tty? if $stdin.tty?
@ -315,6 +339,7 @@ searcher = Thread.new {
begin begin
@smtx.synchronize do @smtx.synchronize do
print_info true, ' ..' print_info true, ' ..'
print_input
refresh refresh
end unless q.empty? end unless q.empty?
@ -377,7 +402,7 @@ searcher = Thread.new {
end end
end end
maxc = C.cols - 3 maxc = C.cols - 5
matches[0, max_items].each_with_index do |item, idx| matches[0, max_items].each_with_index do |item, idx|
next if !new_search && !((vcursor-1)..(vcursor+1)).include?(idx) next if !new_search && !((vcursor-1)..(vcursor+1)).include?(idx)
@ -389,21 +414,25 @@ searcher = Thread.new {
line = line[0, maxc] + '..' line = line[0, maxc] + '..'
end end
basic = chosen ? color(:chosen, true) : color(:normal)
C.setpos row, 0 C.setpos row, 0
C.clrtoeol C.clrtoeol
C.attron C.color_pair(3) | C::A_BOLD if chosen C.attron basic
C.addstr chosen ? '> ' : ' '
b, e = offset b, e = offset
e = [e, maxc].min e = [e, maxc].min
if b < maxc && b < e if b < maxc && b < e
C.addstr line[0, b] C.addstr line[0, b]
cprint line[b...e], chosen ? 4 : 1 cprint line[b...e], color(chosen ? :match! : :match, chosen)
C.attron C.color_pair(3) | C::A_BOLD if chosen C.attron basic
C.addstr line[e..-1] C.addstr line[e..-1]
else else
C.addstr line C.addstr line
end end
C.attroff C.color_pair(3) | C::A_BOLD if chosen C.addstr ' ' * [0, C.cols - line.length - 2].max
C.attroff basic
end end
print_info if !@lists.empty? || events[:loaded] print_info if !@lists.empty? || events[:loaded]