Class: RubyCurses::Progress

Inherits:
Widget show all
Defined in:
lib/rbcurse/core/widgets/rprogress.rb

Overview

TODO user may want to print a label on progress: like not started or complete.

Instance Attribute Summary

Attributes inherited from Widget

#_object_created, #col_offset, #cols_panned, #config, #curpos, #focussed, #form, #id, #key_label, #parent_component, #row_offset, #rows_panned, #state

Instance Method Summary collapse

Methods inherited from Widget

#action_manager, #changed, #click, #color_pair, #command, #destroy, #enter, #event_list, #focus, #get_preferred_size, #getvalue_for_paint, #handle_key, #height, #height=, #hide, #init_vars, #leave, #modified?, #move, #on_enter, #on_leave, #override_graphic, #process_key, #remove, #repaint_all, #repaint_required, #rowcol, #set_buffer_modified, #set_buffering, #set_form, #set_form_col, #set_form_row, #set_modified, #setformrowcol, #setrowcol, #show, #text_variable, #unbind_key, #width, #width=

Methods included from Io

#__create_footer_window, #clear_this, #get_file, #print_this, #rb_getchar, #rb_gets, #rbgetstr, #warn

Methods included from Utils

#OLDdefine_key, #_process_key, #bind_key, #bind_keys, #clean_string!, #define_key, #define_prefix_command, #display_app_help, #get_attrib, #get_color, #keycode_tos, #last_line, #one_line_window, #parse_formatted_text, #print_key_bindings, #repeatm, #run_command, #shell_out, #shell_output, #suspend, #view, #wrap_text

Methods included from ConfigSetup

#cget, #config_setup, #configure, #variable_set

Methods included from EventHandler

#bind, #fire_handler, #fire_property_change

Constructor Details

#initialize(form, config = {}, &block) ⇒ Progress

Returns a new instance of Progress.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rbcurse/core/widgets/rprogress.rb', line 19

def initialize form, config={}, &block

  @row = config.fetch("row",-1) 
  @col = config.fetch("col",-1) 
  @bgcolor = config.fetch("bgcolor", $def_bg_color)
  @color = config.fetch("color", $def_fg_color)
  @name = config.fetch("name", "pbar")
  @editable = false
  @focusable = false
  super
  @surround_chars ||= "[]" # for :old style
  @repaint_required = true
end

Instance Method Details

#getvalueObject



32
33
34
# File 'lib/rbcurse/core/widgets/rprogress.rb', line 32

def getvalue
  @fraction || 0.0
end

#repaintObject



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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rbcurse/core/widgets/rprogress.rb', line 38

def repaint
  return unless @repaint_required
  $log.debug " XXXX PBAR inside repaint #{@color} , #{@fraction} "
  r,c = rowcol
  #value = getvalue_for_paint
  acolor = get_color @bgcolor
  bcolor = get_color @color
  @graphic = @form.window if @graphic.nil? ## HACK messagebox givig this in repaint, 423 not working ??
  len = 0
  w2 = @width - 6 #2 account for brackets and printing of percentage
  if @fraction
    @fraction = 1.0 if @fraction > 1.0
    @fraction = 0 if @fraction < 0
    if @fraction > 0
      len = @fraction * @width
    end
  end
  if @style == :old
    ftext=""
    char = @char || "="
    if @fraction && @fraction >= 0
      len = @fraction * (w2) 
      ftext << sprintf("%3d%s",(@fraction * 100).to_i, "%")
    end
    incomplete = w2 - len
    complete = len
    # I am printing 2 times since sometimes the ending bracket gets printed one position less
    str = @surround_chars[0] + " "*w2 + @surround_chars[1] + ftext
    @graphic.printstring r, c, str , acolor,@attr
    str = char*complete 
    str[-1] = ">" if char == "=" && complete > 2
    @graphic.printstring r, c+1, str , acolor,@attr
  else

    char = @char || " "
    # first print the background horizonal bar
    @graphic.printstring r, c, " " * @width , acolor,@attr

    # if the user has passed a percentage we need to print that in @color
    if @fraction
      #bcolor = get_color @color
      #@fraction = 1.0 if @fraction > 1.0
      #@fraction = 0 if @fraction < 0
      #if @fraction > 0
      #len = @fraction * @width
      #char = @char || " "

      # if text is to printed over the bar
      if @text
        textcolor = get_color $datacolor, 'black'
        txt = @text
        txt = @text[0..@width] if @text.length > @width
        textattr = 'bold'
        # write the text in a color that contrasts with the background
        # typically black
        @graphic.printstring r, c, txt , textcolor, textattr if @text

        # now write the text again, in a color that contrasts with the progress
        # bar color that is expanding. However, the text must be padded to len and truncated 
        # to len as well. it must be exactly len in size.
        txt = sprintf("%-*s", len, txt)
        if len > 0
          if len < txt.length
            txt = txt[0..len]
          end
          textcolor = get_color $datacolor, 'white', @color
          @graphic.printstring r, c, txt , textcolor, textattr if @text
        end
      else
        # no text was given just print a horizontal bar
        @graphic.printstring r, c, char * len , bcolor, 'reverse'
      end
    end # frac > 0
  end # fraction
end