/
base.rb
183 lines (165 loc) · 5.54 KB
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
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
113
114
115
116
117
118
119
120
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Copyright (c) [2020] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
require "yast"
require "abstract_method"
require "y2partitioner/device_graphs"
require "y2partitioner/bidi"
module Y2Partitioner
module Widgets
module Columns
# Base class for all widgets representing a column of a table displaying a collection of
# devices
#
# Each subclasses must define the following methods:
#
# * #title returning the column title.
# * #value_for(device) returning the content to display for the given device.
#
# Additionally, if the subclass needs access to the information contained directly in the
# table entry, it can redefine {#entry_value} which in the base class is just a direct
# call to {#value_for} with the device of the entry as single argument.
#
# @example
# class StorageId < Base
# def title
# _("SID")
# end
#
# def value_for(device)
# device.respond_to?(:storage_id) ? device.storage_id : "N/A"
# end
# end
#
# class DeviceName < Base
# def title
# _("SID")
# end
#
# def value_for(device)
# device.respond_to?(:name) ? device.name : "N/A"
# end
# end
#
# class Widgets::SimpleDevicesTable < Widgets::BlkDevicesTable
# def devices
# @devices ||= Y2Storage::Device.all
# end
#
# def columns
# [
# Columns::StorageId,
# Columns::DeviceName
# ]
# end
# end
class Base
extend Yast::I18n
include Yast::I18n
include Yast::UIShortcuts
# @!method title
# Title of the column
#
# @return [String, Yast::Term]
abstract_method :title
# @!method value_for(device)
# The value to display for the given device
#
# @param device [Y2Storage::Device, Y2Storage::SimpleEtcFstabEntry]
# @return [String, Yast::Term]
abstract_method :value_for
# The value to display for the given table entry
#
# @param entry [DeviceTableEntry]
# @return [String, Yast::Term]
def entry_value(entry)
value_for(entry.device)
end
# Convenience method to internally identify the column
#
# @note The column id is used to find its help text in the {Y2Partitioner::Widgets::Help}
# module. Ideally, each column should have its own #help_text method but those texts still
# being shared with the device overview (see {Y2Partitioner:: Widgets::DeviceDescription}.
#
# @return [Symbol] usually, the column type
def id
self.class.name
.gsub(/^.*::/, "") # demodulize
.gsub(/(.)([A-Z])/, "\1_\2") # underscore
.downcase.to_sym
end
private
def left_to_right(path_string)
return path_string unless bidi_supported?
pn = Pathname.new(path_string)
Bidi.pathname_bidi_to_s(pn)
end
# In ncurses the bidi characters would mess up columns,
# making things worse; return false there.
def bidi_supported?
return @bidi_supported unless @bidi_supported.nil?
di = Yast::UI.GetDisplayInfo() || {}
text_mode = di.fetch("TextMode", true)
@bidi_supported = !text_mode
end
# Helper method to create a `cell` term
#
# @param args [Array] content of the cell
# @return [Yast::Term]
def cell(*args)
Yast::Term.new(:cell, *args.compact)
end
# Helper method to create a `sortKey` term
#
# @param value [String] a value to be used as a sort key
# @return [Yast::Term]
def sort_key(value)
Yast::Term.new(:sortKey, value)
end
# @return [Y2Storage::Devicegraph]
def system_graph
DeviceGraphs.instance.system
end
# Returns the filesystem for the given device, when possible
#
# @return [Y2Storage::Filesystems::Base, nil]
def filesystem_for(device)
if device.is?(:filesystem)
device
elsif device.respond_to?(:filesystem)
device.filesystem
end
end
# Whether the device belongs to a multi-device filesystem
#
# @param device [Device]
# @return [Boolean]
def part_of_multidevice?(device, filesystem)
return false unless device.is?(:blk_device)
filesystem.multidevice?
end
# Determines if given device is actually an fstab entry
#
# @return [Boolean]
def fstab_entry?(device)
device.is_a?(Y2Storage::SimpleEtcFstabEntry)
end
end
end
end
end