Module: TTM

Defined in:
lib/tatum.rb

Overview

TTM

Defined Under Namespace

Modules: HR, Show Classes: File, Memory

Class Method Summary collapse

Class Method Details

.clearnil

Clears the output handle. If @io is STDOUT or STDERR, this method does nothing. If @io is a String, deletes the file at that path if it exists. If @io is any other object that responds to #clear then that method is called.

Returns:

  • (nil)


163
164
165
166
167
# File 'lib/tatum.rb', line 163

def self.clear
	if @io.respond_to?('clear')
		@io.clear
	end
end

.devexit(*opts) ⇒ nothing

Outputs “[devexit]” and exits. This method is handy is testing for when you want to exit the program, but make it clear that you did so in a developmentish way.

Returns:

  • (nothing)


509
510
511
512
513
514
# File 'lib/tatum.rb', line 509

def self.devexit(*opts)
	opts = opts_to_hash(*opts)
	opts = {'include'=>'devexit', 'stack'=>1}.merge(opts)
	self.line opts
	exit
end

.hr(opts = nil, &block) ⇒ Object

Outputs a horizontal rule.

TTM.hr

gives us this:

--------------------------------------------------

If you pass in a string, that string is embedded in the horizontal rule.

TTM.hr 'whatever'

gives us this:

--- whatever -------------------------------------

TTM.hr takes a block param. If a block is sent, then a horizontal rule is output before and after the block:

TTM.hr do
    puts 'do stuff'
end

gives us this:

--------------------------------------------------
do stuff
--------------------------------------------------

You can combine sending a string and a block:

TTM.hr('whatever') do
    puts 'do stuff'
end

output:

--- whatever -------------------------------------
do stuff
--------------------------------------------------

opts as hash

Options can be passed in as a hash. Behind the scenes, if you pass in a string then it is converted to a hash like this:

{'title'=>'whatever'}

So these two commands do exactly the same thing:

TTM.hr 'whatever'
TTM.hr 'title'=>'whatever'

dash

The dash option indicates what character to use for the dashes in the horizontal rule.

TTM.hr 'title'=>'whatever', 'dash'=>'='

output:

=== whatever =====================================


397
398
399
400
# File 'lib/tatum.rb', line 397

def self.hr(opts=nil, &block)
	@io or return
	return TTM::HR.puts opts, &block
end

.hr_widthInt

Returns width of horizontal rule.

Returns:

  • (Int)


44
45
46
# File 'lib/tatum.rb', line 44

def self.hr_width
	return @hr_width
end

.hr_width=(p_width) ⇒ Int

Sets width of horizontal rule.

Parameters:

  • p_width (Int)

Returns:

  • (Int)


51
52
53
# File 'lib/tatum.rb', line 51

def self.hr_width=(p_width)
	return @hr_width = p_width
end

.hrm(*opts) ⇒ Object

Outputs the name of the current method in a horizontal rule.

def mymethod
  TTM.hrm
end

mymethod()

outputs:

--- mymethod -------------------------------------

TTM.hrm accepts a do block:

def mymethod
   TTM.hrm do
      puts 'stuff about mymethod'
   end
end

outputs:

--- mymethod -------------------------------------
stuff about mymethod
--------------------------------------------------


436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/tatum.rb', line 436

def self.hrm(*opts)
	lbl = caller_locations(1,1)[0].label
	
	if opts[0]
		lbl += ': ' + opts[0].to_s
	end
	
	# block or straight
	if block_given?
		self.hr(lbl) do
			yield
		end
	else
		self.hr lbl
	end
end

.indent(*opts) ⇒ Object

Sets an indentation level. Output within the do block is indented. Takes an optional string.

TTM.indent('whatever') do
   TTM.puts 'dude'
end

outputs:

whatever
  dude


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/tatum.rb', line 189

def self.indent(*opts)
	opts = opts_to_hash(*opts)
	
	# output header if one was sent
	if opts['str']
		puts opts['str']
	end
	
	# if block given, temporarily set indent, then revert
	if block_given?
		hold_indent = @indent
		
		unless opts['skip']
			@indent += 1
		end
		
		begin
			yield
		ensure
			@indent = hold_indent
		end
		
	# else just set indent
	else
		@indent += 1
	end
end

.ioObject

Returns @io



17
18
19
# File 'lib/tatum.rb', line 17

def self.io
	return @io
end

.io=(p_io) ⇒ Object

Sets the output handle. Usually you’ll want to set the handle to STDOUT or STDERR. If the given io is a string, it’s assumed to be the path to a file to write to. To write to a string in memory, send TTM::Memory.

If a Class object is sent, then the class is instantiated with the p_io as the param for new(). That class will have to provide the #puts method.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tatum.rb', line 27

def self.io=(p_io)
	# special case: if p_io is a string, instantiate a TTM::File object.
	if p_io.is_a?(String)
		return @io = TTM::File.new(p_io)
		
	# if the given object is a class, instantiate it
	elsif p_io.is_a?(Class)
		return @io = p_io.new(p_io)
	
	# else just hold on to the handle
	else
		return @io = p_io
	end
end

.json(obj, opts = {}) ⇒ Object

Outputs a given structure in JSON format;

hsh = {
   'whatever' => 'dude',
   'people' => [
      'Joe',
      'Mekonnan',
      'AJ'
   ]
};

TTM.json hsh

outputs

--------------------------------------------------
{
  "whatever": "dude",
  "people": [
    "Joe",
    "Mekonnan",
    "AJ"
  ]
}
--------------------------------------------------


550
551
552
553
554
555
556
557
# File 'lib/tatum.rb', line 550

def self.json(obj, opts={})
	@io or return
	require 'json'
	
	hr do
		return self.puts(::JSON.pretty_generate(obj))
	end
end

.line(*opts) ⇒ Object

Outputs the number and file name of the line this method is called from. Also outputs a given string if one is sent.

TTM.line 'whatever'

outputs:

[6 myfile.rb] whatever


469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/tatum.rb', line 469

def self.line(*opts)
	@io or return
	opts = opts_to_hash(*opts)
	opts = {'stack'=>0}.merge(opts)
	
	# get caller location
	loc = caller_locations()[opts['stack']]
	
	# initialize output string
	out = "[#{loc.lineno} #{::File.basename(loc.path)}"
	
	# add included string if sent
	if opts['include']
		out += ' ' + opts['include']
	end
	
	# close brackets
	out += ']'
	
	# add str if sent
	if opts['str']
		out += ' ' + opts['str']
	end
	
	# output
	return self.puts(out)
end

.puts(*opts) ⇒ Object

Outputs a stringification of the given object, indented if necessary.

TTM.puts '[a]'

TTM.indent do
   line = "[b]\n[c]\n[d]"
   TTM.puts line
end

TTM.puts '[e]'

outputs:

[a]
  [b]
  [c]
  [d]
[e]

Some objects are represented by strings in brackets instead of their to_s values. This is to make it easier to see what typew of objects they are.

TTM.puts nil
TTM.puts ''
TTM.puts '   '

outputs:

[nil]
[empty-string]
[no-content-string]


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/tatum.rb', line 126

def self.puts(*opts)
	@io or return
	lead = @tab * @indent
	vals = []
	
	# build string values
	opts.each do |opt|
		vals.push object_display_string(opt)
	end
	
	# loop through strings
	vals.join('').lines.each do |line|
		line = lead + line
		@io.puts line
	end

	
	# Always return nil to avoid confusion when this method is the last line
	# in a function and the return value of the function is used for
	# something.
	return nil
end

.show(obj) ⇒ Object

TTM.show outputs a visual representation of a hash or an array.

hsh = {
   'whatever' => 'dude',
   'people' => [
      'Joe',
      'Mekonnan',
      ''
   ],

   'status' => nil
};

TTM.show hsh

arr = %w{a b c}
TTM.show arr

outputs:

+----------+---------------------+
| people   | [Joe | Mekonnan | ] |
| status   | [nil]               |
| whatever | dude                |
+----------+---------------------+
--------------------------------------------------
a
b
c
--------------------------------------------------


325
326
327
328
# File 'lib/tatum.rb', line 325

def self.show(obj)
	@io or return
	return TTM::Show.puts obj
end

.silent(&block) ⇒ Object

Temporarily turns off output.

TTM.puts '[a]'

TTM.silent do
   TTM.puts '[b]'
end

TTM.puts '[c]'

outputs:

[a]
[c]


282
283
284
# File 'lib/tatum.rb', line 282

def self.silent(&block)
	tmp_io nil, &block
end

.tabString

Returns the string used for indentation.

Returns:

  • (String)


58
59
60
# File 'lib/tatum.rb', line 58

def self.tab
	return @tab
end

.tab=(p_tab) ⇒ String

Sets the tab used for indentation

TTM.tab = '         '

TTM.indent('[a]') do
   TTM.puts '[b]'
end

TTM.puts '[c]'

outputs:

[a]
         [b]
[c]

Parameters:

  • p_tab (String)

Returns:

  • (String)


80
81
82
# File 'lib/tatum.rb', line 80

def self.tab=(p_tab)
	return @tab = p_tab
end

.tmp_io(p_io, &block) ⇒ Object

Temporarily sends output to a different output target in the do block.

TTM.indent('[a]') do
   TTM.puts '[stuff sent to STDOUT]'

   TTM.tmp_io(STDERR) do
      TTM.puts '[stuff sent to STDERR]'
   end

   TTM.puts '[more stuff sent to STDOUT]'
end

This code outputs this to STDOUT:

[a]
  [stuff sent to STDOUT]
  [more stuff sent to STDOUT]

And this to STDERR. Notice that indentation is reset to 0 during the temporary redirect.

[stuff sent to STDERR]


248
249
250
251
252
253
254
255
256
257
# File 'lib/tatum.rb', line 248

def self.tmp_io(p_io, &block)
	io_hold = @io
	@io = p_io
	hold_indent = @indent
	@indent = 0
	yield
ensure
	@indent = hold_indent
	@io = io_hold
end