summary refs log tree commit diff stats
path: root/code/color.rb
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-06-12 02:30:39 +0200
committerhut <hut@lavabit.com>2009-06-12 02:30:39 +0200
commit8f8b66aa27050f29b06ee66ed9c86308089fae4a (patch)
treeaedc4f63dc70a681696e213166d41f277891569b /code/color.rb
parent8a6f5eab0b884acd58ed86c00a30b57901442be6 (diff)
downloadranger-8f8b66aa27050f29b06ee66ed9c86308089fae4a.tar.gz
implementing colorschemes, work in progress
Diffstat (limited to 'code/color.rb')
-rw-r--r--code/color.rb141
1 files changed, 141 insertions, 0 deletions
diff --git a/code/color.rb b/code/color.rb
new file mode 100644
index 00000000..244ec127
--- /dev/null
+++ b/code/color.rb
@@ -0,0 +1,141 @@
+require 'ncurses'
+require 'code/debug'
+
+module Color
+	extend Color
+
+	COLORSCHEMEDIR = File.join(MYDIR, 'data', 'colorscheme')
+	def load_colorscheme(name)
+		## colorschemes are located in data/colorscheme/
+		fname = File.join(COLORSCHEMEDIR, "#{name}.rb")
+#		assert File.exists?(fname), "No such colorscheme: #{fname}"
+		
+		clear_all
+		load fname
+		::Console.write("Colorscheme #{name} loaded.")
+	end
+
+	def clear_all()
+		clear
+		for key, type in TYPES
+			type.clear
+		end
+	end
+
+	def clear
+		for var in instance_variables
+			instance_variable_set(var, nil)
+		end
+	end
+
+	def default() -1 end
+	def black()    0 end
+	def red()      1 end
+	def green()    2 end
+	def yellow()   3 end
+	def blue()     4 end
+	def magenta()  5 end
+	def cyan()     6 end
+	def white()    7 end
+
+	alias df default
+	alias brown yellow
+	alias orange yellow
+	alias purlpe magenta
+	alias pink magenta
+	alias teal cyan
+	alias gray white
+	alias grey white
+
+	def none()       Ncurses::A_NORMAL     end
+	def bold()       Ncurses::A_BOLD       end
+	def reverse()    Ncurses::A_REVERSE    end
+	def underline()  Ncurses::A_UNDERLINE  end
+
+	def standout()   Ncurses::A_STANDOUT   end
+	def blink()      Ncurses::A_BLINK      end
+	def dim()        Ncurses::A_DIM        end
+	def protect()    Ncurses::A_PROTECT    end
+	def invisible()  Ncurses::A_INVIS      end
+	def altcharset() Ncurses::A_ALTCHARSET end
+	def chartext()   Ncurses::A_CHARTEXT   end
+
+	alias reversed reverse
+	alias revert reverse
+	alias invis invisible
+
+	def default_color() return default, default, none end
+	alias dc default_color
+
+	## a shortcut.
+	##    use %w{txt type left_side}
+	## is equivalent to:
+	##    def txt() @txt || @type || @left_side || @base end
+	def self.use(arr)
+		arr << 'base' unless arr.last == 'base'
+		body = arr.map{|x| "@#{x}"}.join(' || ')
+		eval "def #{arr.first}() #{body} end"
+	end
+
+	use %w{base}
+	use %w{file}
+
+	use %w{link file}
+	use %w{badlink file}
+	use %w{goodlink file}
+	use %w{directory file}
+	use %w{forbidden directory file}
+
+
+	use %w{media file}
+	use %w{video media file}
+	use %w{sound media file}
+	use %w{image media file}
+	use %w{executable file}
+	use %w{script executable file}
+	use %w{binary executable file}
+
+	def content!() @content end
+
+	module Type
+		include Color
+
+		ATTRIBUTES = %w[
+			base file directory media executable
+			video sound image
+			script binary
+
+			terminal_cursor error info
+		]
+
+		for a in ATTRIBUTES
+			eval <<-DONE
+				def #{a}()
+					@#{a}_cache ||= @#{a} || super || Color.#{a} || default_color
+				end
+			DONE
+		end
+
+		## this is only meant to be used in Color
+		def clear_all() nil end
+	end
+
+	module Normal;   extend Type end
+	module Selected; extend Type end
+	module Marked;   extend Type end
+	module Console;  extend Type end
+
+	TYPES = {
+		:normal   => Normal,
+		:selected => Selected,
+		:marked   => Marked,
+		:console  => Console
+	}
+
+	def [](x)      TYPES[x] end
+	def selected() Selected end
+	def normal()   Normal   end
+	def marked()   Marked   end
+	def console()  Console  end
+end
+