| 1 |
# modeline_element.rb: the class definition of Edmaru::ModeLineElement |
| 2 |
# |
| 3 |
# Copyright (C) 2007 Takashi Nakamoto |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License version 2 as |
| 7 |
# published by the Free Software Foundation. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, but |
| 10 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
# General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program; if not, write to the Free Software |
| 16 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 |
# 02110-1301 USA. |
| 18 |
# |
| 19 |
|
| 20 |
module Edmaru |
| 21 |
|
| 22 |
#An instance of this element represents one element in a ModeLine bar. |
| 23 |
class ModeLineElement |
| 24 |
|
| 25 |
#Create a new ModeLine element. |
| 26 |
# |
| 27 |
#=== Arguments |
| 28 |
#_n_ :: The name of this element. |
| 29 |
#_w_ :: The width of this element excluding both of the left |
| 30 |
#border width and the right one. A negative value for this |
| 31 |
#argument makes this element take variable width. |
| 32 |
#_l_ :: Left border width. |
| 33 |
#_r_ :: Right border width. |
| 34 |
#_t_ :: Default text. |
| 35 |
# |
| 36 |
#=== Return |
| 37 |
#An initialized element. |
| 38 |
def initialize(n, w = -1, l = 0, r = 0, t = "") |
| 39 |
@name = n |
| 40 |
@width = w |
| 41 |
@left_border = l |
| 42 |
@right_border = r |
| 43 |
@text = t |
| 44 |
@visible = false |
| 45 |
end |
| 46 |
|
| 47 |
#The name of this element. |
| 48 |
def name |
| 49 |
@name |
| 50 |
end |
| 51 |
|
| 52 |
#The actual width of this element including both the left border |
| 53 |
#width and the right one. |
| 54 |
def actual_width |
| 55 |
if @width < 0 |
| 56 |
return @left_border + @text.size + @right_border |
| 57 |
else |
| 58 |
return @left_border + @width + @right_border |
| 59 |
end |
| 60 |
end |
| 61 |
|
| 62 |
#Set the text of this element. |
| 63 |
# |
| 64 |
#=== Warning |
| 65 |
#Do *NOT* use this assignment operator to change the surface text |
| 66 |
#of this element in the ModeLine that this element belongs |
| 67 |
#to. Call ModeLine#set_element_text to change the text. |
| 68 |
def text=(new_text) |
| 69 |
@text = new_text |
| 70 |
end |
| 71 |
|
| 72 |
#The text to be displayed. |
| 73 |
def text |
| 74 |
@text |
| 75 |
end |
| 76 |
|
| 77 |
#Set the visibility of this element. |
| 78 |
# |
| 79 |
#=== Warning |
| 80 |
#Do *NOT* use this assignment operator to show this element in the ModeLine |
| 81 |
#that this element belongs to. Call ModeLine#show_element or |
| 82 |
#ModeLine#hide_element to show or hide this element. |
| 83 |
def visible=(new_flag) |
| 84 |
@visible = new_flag |
| 85 |
end |
| 86 |
|
| 87 |
#Get the visibility of this element. |
| 88 |
def visible? |
| 89 |
@visible |
| 90 |
end |
| 91 |
|
| 92 |
#The surface string of this element for the ModeLine. |
| 93 |
# |
| 94 |
#=== Return |
| 95 |
#The surface string of this element. |
| 96 |
def surface |
| 97 |
#Return the empty string for any sake as long as the width is 0. |
| 98 |
if @width == 0 |
| 99 |
return "" |
| 100 |
end |
| 101 |
|
| 102 |
ret = " " * @left_border |
| 103 |
if @width > 0 |
| 104 |
#For the fixed width |
| 105 |
if @width <= @text.size |
| 106 |
#Cut the excessive tail characters. |
| 107 |
ret << @text[0, @width] |
| 108 |
else |
| 109 |
#Pad the text with space characters. |
| 110 |
ret << @text |
| 111 |
ret << " " * (@width - @text.size) |
| 112 |
end |
| 113 |
else |
| 114 |
#For the variable width. |
| 115 |
ret << @text |
| 116 |
end |
| 117 |
ret << " " * @right_border |
| 118 |
|
| 119 |
return ret |
| 120 |
end |
| 121 |
end |
| 122 |
end |