lib/ppcurses/form/input_element.rb in ppcurses-0.1.1 vs lib/ppcurses/form/input_element.rb in ppcurses-0.1.2
- old
+ new
@@ -21,23 +21,39 @@
def initialize(label, size )
@label = label
@size = size
@selected = false
- @value = ''
- @cursor_location = 0
@filter = nil
+ self.clear
end
# Creates an InputElement that only allows integer input
#
def InputElement.new_integer_only( label, size)
i_only = PPCurses::InputElement.new(label, size)
- i_only.filter =PPCurses::NumberFilter.new
+ i_only.filter = PPCurses::IntegerFilter.new
i_only
end
+ # Creates an InputElement that only allows a number
+ # but allows a decimal point. I.E. 10.2
+ #
+ def InputElement.new_decimal_only( label, size)
+ i_only = PPCurses::InputElement.new(label, size)
+ i_only.filter = PPCurses::DecimalFilter.new
+ i_only
+ end
+
+ # Creates an InputElement that only allows time data
+ # E.G. 20:10.20 == 20 hours, 10 minutes and 20 seconds
+ #
+ def InputElement.new_time_only( label, size)
+ i_only = PPCurses::InputElement.new(label, size)
+ i_only.filter = PPCurses::TimeFilter.new
+ i_only
+ end
def show(screen)
print_label( screen )
@value_start_point = screen.cur_point
@@ -75,14 +91,23 @@
if @value.length >= @size
# Ignore input
return
end
- unless passes_filter(key)
+ #
+ # Get a temporary version of the current value
+ # with the new character added so that we can
+ # test it against the filter.
+ temp_val = value_with(key)
+
+ unless passes_filter(temp_val)
return
end
+ #
+ # Actually add the new character if the filter passes
+ #
add_character(key)
end
@@ -105,10 +130,16 @@
def height
1
end
+ def clear
+ @value = ''
+ @cursor_location = 0
+ end
+
+
# --------------------------------------------------------------------------------
protected
def print_label( screen )
@@ -158,32 +189,78 @@
@cursor_location -= 1
end
- def add_character ( char )
+
+ #
+ # Returns what the value of the input element would be, if
+ # the given character was added. Does not modify the internal
+ # state.
+ #
+ def value_with (char)
+ temp_val = @value
+
if @cursor_location == @value.length
- @value += char
+ temp_val = @value + char
else
- @value = @value.slice(0..@cursor_location-1) + char + @value.slice(@cursor_location..@value.length-1)
+ temp_val = @value.slice(0..@cursor_location-1) + char + @value.slice(@cursor_location..@value.length-1)
end
+
+ temp_val
+ end
+
+ def add_character ( char )
+ @value = value_with(char)
@cursor_location += 1
end
end
+# ============================================================
+# ------------ Filters
+# ============================================================
+ class IntegerFilter
- class NumberFilter
+ def passes_filter( value )
+ if value =~ /^\d+$/
+ return true
+ end
- def passes_filter( key )
- if key =~ /^\d+$/
+ false
+ end
+
+ end
+
+
+ class DecimalFilter
+
+ def passes_filter( value )
+ if value =~ /^\d*\.?\d*$/
return true
end
false
end
end
+
+ # Used for hours minute second input.
+ # Valid input includes:
+ # 3:33.20 == 3 hours 33 minutes and 20 seconds
+ # 33.20 == 33 minutes and 20 seconds
+ # 33 == 33 minutes
+ # 0.20 == 20 seconds
+ class TimeFilter
+ def passes_filter( value )
+ if value =~ /^\d*\:?\d*\.?\d*$/
+ return true
+ end
+
+ false
+ end
+
+ end
end
\ No newline at end of file