about summary refs log tree commit diff stats
path: root/code
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-06-12 01:26:41 +0200
committerhut <hut@lavabit.com>2009-06-12 01:26:41 +0200
commit8a6f5eab0b884acd58ed86c00a30b57901442be6 (patch)
tree31672fce003ba464c922eb86188e854f7b7ccaa8 /code
parent9ed06dcd632e3bf6c19fc3943985e9841f1fed41 (diff)
downloadranger-8a6f5eab0b884acd58ed86c00a30b57901442be6.tar.gz
renamed Interface module to CLI, cleanups
Diffstat (limited to 'code')
-rw-r--r--code/cli.rb4
-rw-r--r--code/debug.rb147
-rw-r--r--code/draw.rb2
-rw-r--r--code/fm/fm.rb4
4 files changed, 97 insertions, 60 deletions
diff --git a/code/cli.rb b/code/cli.rb
index 3736f60c..0f915634 100644
--- a/code/cli.rb
+++ b/code/cli.rb
@@ -1,6 +1,6 @@
 require 'ncurses'
 
-module Interface
+module CLI
 	def self.keytable(key)
 		case key
 		when 12
@@ -71,7 +71,7 @@ module Interface
 	end
 
 	def geti
-		Interface::keytable(Ncurses.getch)
+		CLI.keytable(Ncurses.getch)
 	end
 
 	def set_title(x)
diff --git a/code/debug.rb b/code/debug.rb
index d3a97190..5c34b932 100644
--- a/code/debug.rb
+++ b/code/debug.rb
@@ -1,17 +1,32 @@
+## just a little module for easier debugging of ncurses CLI applications
+## where it's a bad idea to write debug info directly into the console.
+## use this with: include Debug
+
 require 'pp'
 
 module Debug
-	@@logfile = '/tmp/errorlog'
-	@@logstream = File.open(@@logfile, 'a')
+	def self.setup(name, stream=nil, level=nil)
+		if name.is_a? Hash
+			stream  = name[:stream]
+			level   = name[:level]
+			name    = name[:name]
+		end
+
+		@@name   = name   || 'debug'
+		@@stream = stream || STDOUT
+		@@level  = level  || 3
+		@@level  = 3
+
+		@@stream.sync = true
+	end
 
 	def self.write(str)
-		@@logstream.write(str)
-		@@logstream.flush
+		@@stream.write(str)
 		return str
 	end
+	
 	def self.puts(str)
-		@@logstream.puts(str)
-		@@logstream.flush
+		@@stream.puts(str)
 		return str
 	end
 
@@ -23,67 +38,89 @@ module Debug
 		end
 	end
 
-	if LOG_LEVEL > 0
-		def bm(descr="benchmark", &block)
-			# Benchmark
-			t1 = Time.now
+	## if you don't want your program to stop,
+	## but still want to retrieve the error information
+	def lograise(e=nil)
+		e ||= $!
+		log_err("#{e.class}: #{e.message}")
+		log_err(e.backtrace)
+	end
+
+	def bm(descr="benchmark", &block)
+		if @@level == 0
 			yield
-			dur = Time.now-t1
+			return
+		end
 
-			# substract the durtation of a "bm(..) do end"
-			dur -= bm_dummy(descr) do end
+		# Benchmark
+		t1 = Time.now
+		yield
+		dur = Time.now-t1
 
-			# Format the duration
-			dur *= 1000
-			dur = dur > 0 ? dur : 0
-			dur = '%0.3f' % dur
-			logerr("#{descr}: #{dur}ms")
-		end
+		# substract the durtation of a "bm(..) do end"
+		dur -= bm_dummy(descr) do end
 
-		def bm_dummy(descr="benchmark", &block)
-			t1 = Time.now
-			yield
-			return (Time.now-t1)
-		end
+		# Format the duration
+		dur *= 1000
+		dur = dur > 0 ? dur : 0
+		dur = '%0.3f' % dur
+		logerr("#{descr}: #{dur}ms")
+	end
 
-		def __log__(obj, level)
-			if level <= LOG_LEVEL
-				obj = obj.nil? ? "checkpoint at #{Time.now}" : obj
-				Debug.puts(obj)
-			end
+	def bm_dummy(descr="benchmark", &block)
+		t1 = Time.now
+		yield
+		return (Time.now-t1)
+	end
+
+	def __logwrite__(obj, level)
+		if level <= @@level
+			Debug.write(obj)
 		end
-		def __logpp__(obj, level)
-			if level <= LOG_LEVEL
-				$stdout = @@logstream
-				pp obj
-				$stdout.flush
-				$stdout = STDOUT
-			end
+		obj
+	end
+
+	def __log__(obj, level)
+		if level <= @@level
+			obj = obj.nil? ? "checkpoint at #{Time.now}" : obj
+			Debug.puts(obj)
 		end
+		obj
+	end
 
-		def logfatal(obj = nil) __log__(obj, 1) end
-		def logppfatal(obj = nil) __logpp__(obj, 1) end
+	def __logpp__(obj, level)
+		if level <= @@level
+			old_stdout = $stdout
+			$stdout    = @@stream
 
-		def logerr(obj = nil) __log__(obj, 2) end
-		def logpperr(obj = nil) __logpp__(obj, 2) end
+			pp(obj)
 
-		def log(obj = nil) __log__(obj, 3) end
-		def logpp(obj = nil) __logpp__(obj, 3) end
+			$stdout    = old_stdout
+		end
+		obj
+	end
 
-		def trace() __logpp__(caller, 3) end
-	else
-		def __log__(a, b) end
-		def __logpp__(a, b) end
+	## each send a different level to __logXYZ__
 
-		def logfatal(a=nil) end
-		def logppfatal(a=nil) end
+	def logfatal(      obj = nil ) __log__(     obj, 1)  end
+	def logppfatal(    obj = nil ) __logpp__(   obj, 1)  end
+	def logwritefatal( obj = nil ) __logwrite__(obj, 1)  end
 
-		def logerr(a=nil) end
-		def logpperr(a=nil) end
+	def logerr(        obj = nil ) __log__(     obj, 2)  end
+	def logpperr(      obj = nil ) __logpp__(   obj, 2)  end
+	def logwriteerr(   obj = nil ) __logwrite__(obj, 2)  end
 
-		def log(a=nil) end
-		def logpp(a=nil) end
-		def trace() end
-		def bm(*args, &block) yield end
-	end
+	def log(           obj = nil ) __log__(     obj, 3)  end
+	def logpp(         obj = nil ) __logpp__(   obj, 3)  end
+	def logwrite(      obj = nil ) __logwrite__(obj, 3)  end
+
+	def trace(         n = 1     ) __log__(caller(n), 3) end
+
+	alias log_fatal logfatal
+	alias logpp_fatal logppfatal
+	alias logwrite_fatal logwritefatal
+	alias log_err logerr
+	alias logpp_err logpperr
+	alias logwrite_err logwriteerr
 end
+
diff --git a/code/draw.rb b/code/draw.rb
index 8e032199..36688642 100644
--- a/code/draw.rb
+++ b/code/draw.rb
@@ -169,7 +169,7 @@ module Fm
 	end
 
 	def self.get_boundaries(column)
-		cols = Interface.cols # to cache
+		cols = CLI.cols # to cache
 		case column
 		when 0
 			return 0, cols / 8
diff --git a/code/fm/fm.rb b/code/fm/fm.rb
index 81b868ca..8e60286b 100644
--- a/code/fm/fm.rb
+++ b/code/fm/fm.rb
@@ -58,7 +58,7 @@ module Fm
 		@memory['0'] = pwd
 
 		# Give me some way to redraw screen while waiting for
-		# input from Interface.geti
+		# input from CLI.geti
 
 #		for i in 1..20
 #			eval "Signal.trap(#{i}) do
@@ -98,7 +98,7 @@ module Fm
 	end
 
 	def lines
-		Interface::lines - @bars.size
+		CLI::lines - @bars.size
 	end
 
 	def dump