Class: Perception::SeeSession
- Defined in:
- lib/perception/perception_main.rb,
lib/perception/logging.rb,
lib/perception/perception_private.rb
Overview
An instance of SeeSession corresponds to a session with the console.
Constant Summary collapse
- @@instance =
nil
Instance Attribute Summary collapse
Logging collapse
-
#log_status(message = '') ⇒ void
Prints a separator, the actual time and an optional message in the log file.
-
#logger(logdir = nil, filename = 'see.log') ⇒ Logger
Looks for a good place to save the log file, creates log directory & log file if necessary and instantiates Logger Object.
Automatic Indenting collapse
-
#indent! ⇒ Object
Turns automatic indention ON.
-
#left! ⇒ Object
Turns automatic indention OFF.
Slowing down collapse
-
#fast! ⇒ Object
Adjusts printing speed to full speed.
-
#slow!(speed = 1) ⇒ Object
Slows down output to readable speed.
Simple Benchmarking collapse
-
#bench ⇒ Float
The time in seconds since the last printout with see.
Progress Indicator collapse
-
#clear! ⇒ Object
Undos the last print.
-
#temp! ⇒ Object
Marks the last print as temporary.
Two layers of information in one line collapse
-
#flash!(alternativtext = ' ', how_many = 6) ⇒ Object
Shows two informations in one line.
Class Method Summary collapse
-
.instance ⇒ Object
Liefert die eine notwendige Instanz.
Instance Method Summary collapse
- #adjust_level ⇒ Object
- #clear_indent! ⇒ Object
- #init ⇒ Object
-
#initialize ⇒ SeeSession
constructor
A new instance of SeeSession.
- #printout(string, backward = nil) ⇒ Object
-
#process_newline ⇒ Object
Führt die aufgeschobenen Newlines aus.
-
#process_print(input, options = {}) ⇒ Object
Gibt das see aus.
- #wait!(string_akt) ⇒ Object
Constructor Details
#initialize ⇒ SeeSession
Returns a new instance of SeeSession.
37 38 39 |
# File 'lib/perception/perception_private.rb', line 37 def initialize init end |
Instance Attribute Details
#delayed_newlines ⇒ Object
21 22 23 |
# File 'lib/perception/perception_private.rb', line 21 def delayed_newlines @delayed_newlines end |
#indent ⇒ Object
21 22 23 |
# File 'lib/perception/perception_private.rb', line 21 def indent @indent end |
#level ⇒ Object
21 22 23 |
# File 'lib/perception/perception_private.rb', line 21 def level @level end |
#out ⇒ Object
21 22 23 |
# File 'lib/perception/perception_private.rb', line 21 def out @out end |
#time_last ⇒ Object
21 22 23 |
# File 'lib/perception/perception_private.rb', line 21 def time_last @time_last end |
Class Method Details
.instance ⇒ Object
Liefert die eine notwendige Instanz
29 30 31 32 33 |
# File 'lib/perception/perception_private.rb', line 29 def self.instance return @@instance if @@instance @@instance = SeeSession.new @@instance end |
Instance Method Details
#adjust_level ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/perception/perception_private.rb', line 91 def adjust_level @call_stack_now = caller.size call_stack_diff = @call_stack_now - @call_stack_last # puts "call_stack_diff=#{ call_stack_diff }" # puts if @indent if call_stack_diff > 0 && @call_stack_last != 0 @level += 1 elsif call_stack_diff < 0 && @level > 1 @level -= 1 end end nil end |
#bench ⇒ Float
Returns the time in seconds since the last printout with see.
88 89 90 91 |
# File 'lib/perception/perception_main.rb', line 88 def bench return 0 unless @time_last return (Time.now - @time_last) end |
#clear! ⇒ Object
Undos the last print. Note: Indention will get lost.
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/perception/perception_main.rb', line 99 def clear! return if @cursor_now <= SEE_TAB_WIDTH * @level return if @string_last.empty? return unless @out.include?(:console) self.wait!('') if @speed printout( ("\b" * @string_last.size) + (" " * @string_last.size) + ("\b" * @string_last.size), @string_last.size ) @string_last = '' @method_last = nil @time_last = Time.now @delayed_newlines = 0 @delayed_clear = false nil end |
#clear_indent! ⇒ Object
108 109 110 111 112 113 |
# File 'lib/perception/perception_private.rb', line 108 def clear_indent! @call_stack_last = 0 # wie direkt nach der Initialisierung @call_stack_now = 0 @level = SEE_LEVEL_START nil end |
#fast! ⇒ Object
Adjusts printing speed to full speed. This is used after slow!
80 81 82 83 |
# File 'lib/perception/perception_main.rb', line 80 def fast! @speed = nil nil end |
#flash!(alternativtext = ' ', how_many = 6) ⇒ Object
Shows two informations in one line. See the demo.
Via seee.flash! können zwei Informationen abwechselnd in der gleichen Zeile angezeigt werden. Das ist nützlich, wenn man bestimmte Werte anzeigen will ohne das Gesamtlayout zu zerstören.
Ohne Angabe eines Zweittextes wird der Ersttext einfach zum Blinken gebracht. Nachträglich, also wenn er schon ausgegeben ist. Das ist sehr nützlich, wenn man bestimme Werte verfolgen will.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/perception/perception_main.rb', line 135 def flash!(alternativtext=' ', how_many=6) string_bak = @string_last.dup method_bak = @method_last method_bak = :puts if method_bak == :pp speed_bak = @speed if speed_bak seee.slow!(speed_bak * 0.8) else seee.slow! end how_many.times do seee.clear! seee.temp! process_print( alternativtext, {:method => :print } ) seee.clear! process_print( string_bak, {:method => method_bak } ) end @speed = speed_bak return alternativtext end |
#indent! ⇒ Object
Turns automatic indention ON.
41 42 43 44 45 46 47 |
# File 'lib/perception/perception_main.rb', line 41 def indent! @indent = true @level = SEE_LEVEL_START @call_stack_last = 0 @call_stack_now = 0 nil end |
#init ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/perception/perception_private.rb', line 42 def init # früher @string_last = '' # enthält den letzten ausgegebenen String (damit man ihn löschen und wiederherstellen kann) @time_last = nil # enthält den Zeitpunkt des letzten Prints @method_last = nil # enthält die Methode, mit der der letzte String geprinted wurde @call_stack_last = 0 # Wie lang war der Stack beim letzten #see? # jetzt @speed = nil # nil = unverzögert, 1= lesbar, 2 = lesbar / doppelt so schnell, 0.5 = lesbar / halb so schnell @cursor_now = 0 # enthält die aktuelle Position des Cursors @method_now = nil # mit welcher Methode wird der aktuelle Print ausgeführt? @call_stack_now = 0 # Wie lang ist der Stack beim aktuellen #see? @indent = SEE_INDENT_START # sollen die Prints automatisch eingerückt werden? @level = SEE_LEVEL_START # aktueller Einrückungslevel # nächstes @delayed_newlines = 1 # Mit wie vielen Newline-Zeichen soll der nächste Print beginnen? (Newlines werden verzögert, damit man noch löschen kann) @delayed_clear = false # Soll der nächste Print den vorhergehenden überschreiben? @logger = nil # wird erst bei Bedarf erzeugt @out = Set.new # Wohin geht der Output? , :log @out << :console end |
#left! ⇒ Object
Turns automatic indention OFF.
52 53 54 55 56 57 58 |
# File 'lib/perception/perception_main.rb', line 52 def left! @indent = false @level = SEE_LEVEL_START @call_stack_last = 0 @call_stack_now = 0 nil end |
#log_status(message = '') ⇒ void
This method returns an undefined value.
Prints a separator, the actual time and an optional message in the log file. Usage:
seee.log_status "My Message"
=> (in log file)
------------------------------------------------------------------------------------------------------------------------
2012-10-17 Wednesday 10:47:05
My Message
100 101 102 103 104 105 |
# File 'lib/perception/logging.rb', line 100 def log_status(='') status = "\n\n\n# " + ('-'*120) + "\n" status += "# " + Time.now.inspect_see + "\n" status += "# " + + "\n" logger << status end |
#logger(logdir = nil, filename = 'see.log') ⇒ Logger
Looks for a good place to save the log file, creates log directory & log file if necessary and instantiates Logger Object.
Where is my logfile? Try
see seee.logger
=>
<Logger:0x2a57d20
@default_formatter = #<Logger::Formatter:0x2a57cc0 @datetime_format=nil>,
@formatter = nil,
@level = 0,
@logdev = #<Logger::LogDevice:0x2a57c60 @dev=#<File:C:/Ruby-Projekte/perception/log/see.log>, @filename='C:/Ruby-Projekte/perception/log/see.log', @mutex=#<<>>, @shift_age=0, @shift_size=1048576>,
@progname = nil>
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/perception/logging.rb', line 34 def logger( logdir=nil, filename='see.log' ) return @logger if @logger require 'logger' unless defined?(Logger) # Logfile festlegen unless logdir if Drumherum.host_os == :windows smart_init if Drumherum.directory_main.empty? logdir = File.(File.join(Drumherum.directory_main, 'log')) # puts "logdir= #{logdir} size=#{logdir.split('/').size}" end # if windows if ( Drumherum.host_os != :windows || logdir.nil? || logdir.empty? || logdir == 'test/log' || logdir.split('/').size <= 2 ) require 'tmpdir' logdir = Dir::tmpdir + '/log' end end # unless mycaller = CallerUtils.mycaller(:skip => ['perception', 'ruby\gems', 'ruby/gems', 'test/unit']) logfile = File.join(logdir, filename) #puts "mycaller=#{mycaller.inspect_pp}" #puts "logdir=#{logdir.inspect_pp}" #puts "logfile=#{logfile.inspect_pp}" # Erzeugen, wenn nötig begin # Dir erzeugen unless File.exist?(logdir) FileUtils.makedirs(logdir) puts "\n\nSeeSession#logger: Directory #{logdir} created\n\n" end # File erzeugen unless File.exist?(logfile) File.open(logfile,(File::WRONLY | File::APPEND | File::CREAT)) if (mycaller.nil? || mycaller.empty?) log_status else log_status("Logfile created by #{mycaller}") end end #Logger erzeugen @logger = Logger.new( logfile ) log_level = :debug @logger.level = Logger.const_get(log_level.to_s.upcase) @logger << "\n" # dem letzten Eintrag fehlt vielleicht das newline #rescue end return @logger end |
#printout(string, backward = nil) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/perception/perception_private.rb', line 180 def printout(string, backward=nil) if @out.include?(:console) spacer = (' '*SEE_TAB_WIDTH * @level) outputstring = string.gsub(/\n/, ("\n" + spacer) ) Kernel.print(spacer) if @cursor_now == 0 Kernel.print(outputstring) end if @out.include?(:log) # spacer = seee.bench.inspect_see + (' '*SEE_TAB_WIDTH * @level) + ' ' spacer = '' outputstring = string.gsub(/\n/, ("\n" + spacer) ) logger << (spacer) if @cursor_now == 0 logger << (outputstring) end # TODO: \n berücksichtigen unless backward @cursor_now += outputstring.size else @cursor_now -= backward end end |
#process_newline ⇒ Object
Führt die aufgeschobenen Newlines aus. Ohne Berücksichtigung der Zeit.
170 171 172 173 174 175 176 |
# File 'lib/perception/perception_private.rb', line 170 def process_newline result = "\n" * @delayed_newlines self.clear! if @delayed_clear printout(result) @delayed_newlines = 0 return result end |
#process_print(input, options = {}) ⇒ Object
Gibt das see aus
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/perception/perception_private.rb', line 121 def process_print( input, ={} ) # vorbereiten @method_now = [:method] || :puts self.clear! if @delayed_clear self.adjust_level newlines = self.process_newline if @delayed_newlines > 0 # ausgeben case @method_now when :puts result = input.inspect_or_to_s self.wait!(result) if @speed printout(result) @delayed_newlines += 1 # das newline wird erst später eingefügt when :pp result = input.inspect_see self.wait!(result) if @speed printout(result) @delayed_newlines += 1 # und das newline später eingefügt when :print result = input.inspect_or_to_s self.wait!(result) if @speed printout(result) when :multi result = '' result = PPP::pp(input, result, 200) # normal prettyprint result = PPP.beautify_multi(result) self.wait!(result) if @speed printout(result) @delayed_newlines += 1 # und das newline später eingefügt end # case # verwalten @string_last = result.dup @time_last = Time.now @method_last = @method_now @call_stack_last = @call_stack_now return (newlines ||'') + result end |
#slow!(speed = 1) ⇒ Object
Slows down output to readable speed. The speed depends on the length and the complexity of the printing. You can adjust the speed with the parameter:
-
slow! 1 –> same as slow!
-
slow! 2 –> slow, but double speed
-
slow! 0.5 –> even slower
-
slow! nil –> full speed (same as fast!)
71 72 73 74 |
# File 'lib/perception/perception_main.rb', line 71 def slow!(speed=1) @speed = speed nil end |
#temp! ⇒ Object
Marks the last print as temporary. The following print will overwrite it. Note: Indention will get lost.
118 119 120 121 |
# File 'lib/perception/perception_main.rb', line 118 def temp! @delayed_clear = true nil end |
#wait!(string_akt) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/perception/perception_private.rb', line 76 def wait!(string_akt) return unless @time_last verstrichene_zeit = Time.now - @time_last alter_string = @string_last.strip.chomp.scan(/\w+/) neuer_string = string_akt.strip.chomp.scan(/\w+/) differenz_information = Math.sqrt((alter_string - neuer_string).size + 0.3 ) time_for_perception = (differenz_information * SEE_PERCEPTION_TIME_PER_WORD * @speed ) / 1000 if verstrichene_zeit < time_for_perception sleep (time_for_perception - verstrichene_zeit) end nil end |