summary refs log tree commit diff stats
path: root/ranger/gui/bar.py
blob: c06a201e9ce907f9a5a4e3b275e9891941d28a0c (plain) (blame)
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
# Copyright (C) 2009, 2010  Roman Zimbelmann <romanz@lavabit.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.

class Bar(object):
	left = None
	right = None
	gap = None

	def __init__(self, base_color_tag):
		self.left = BarSide(base_color_tag)
		self.right = BarSide(base_color_tag)
		self.gap = BarSide(base_color_tag)

	def add(self, *a, **kw):
		self.left.add(*a, **kw)

	def addright(self, *a, **kw):
		self.right.add(*a, **kw)

	def sumsize(self):
		return self.left.sumsize() + self.right.sumsize()

	def fixedsize(self):
		return self.left.fixedsize() + self.right.fixedsize()

	def shrink_by_removing(self, wid):
		leftsize = self.left.sumsize()
		rightsize = self.right.sumsize()
		sumsize = leftsize + rightsize

		# remove elemets from the left until it fits
		if sumsize > wid:
			while len(self.left) > 0:
				leftsize -= len(self.left.pop(-1).string)
				if leftsize + rightsize <= wid:
					break
			sumsize = leftsize + rightsize

			# remove elemets from the right until it fits
			if sumsize > wid:
				while len(self.right) > 0:
					rightsize -= len(self.right.pop(0).string)
					if leftsize + rightsize <= wid:
						break
				sumsize = leftsize + rightsize

		if sumsize < wid:
			self.fill_gap(' ', (wid - sumsize), gapwidth=True)

	def shrink_by_cutting(self, wid):
		fixedsize = self.fixedsize()
		if wid < fixedsize:
			raise ValueError("Cannot shrink down to that size by cutting")

		leftsize = self.left.sumsize()
		rightsize = self.right.sumsize()
		nonfixed_items = self.left.nonfixed_items()

		itemsize = int(float(wid - rightsize - fixedsize) / nonfixed_items) + 1

		for item in self.left:
			if not item.fixed:
				item.cut_off_to(itemsize)

		self.fill_gap(' ', wid, gapwidth=False)

	def fill_gap(self, char, wid, gapwidth=False):
		del self.gap[:]

		if not gapwidth:
			wid = wid - self.sumsize()

		if wid > 0:
			self.gap.add(char * wid, 'space')

	def combine(self):
		return self.left + self.gap + self.right


class BarSide(list):
	def __init__(self, base_color_tag):
		self.base_color_tag = base_color_tag

	def add(self, string, *lst, **kw):
		cs = ColoredString(string, self.base_color_tag, *lst)
		if 'fixedsize' in kw:
			cs.fixed = kw['fixedsize']
		self.append(cs)

	def add_space(self, n=1):
		self.add(' ' * n, 'space')

	def sumsize(self):
		return sum(len(item) for item in self)

	def fixedsize(self):
		n = 0
		for item in self:
			if item.fixed:
				n += len(item)
			else:
				n += 1
		return n

	def nonfixed_items(self):
		return sum(1 for item in self if not item.fixed)


class ColoredString(object):
	fixed = False

	def __init__(self, string, *lst):
		self.string = string
		self.lst = lst

	def cut_off(self, n):
		n = max(n, min(len(self.string), 1))
		self.string = self.string[:-n]

	def cut_off_to(self, n):
		self.string = self.string[:n]

	def __len__(self):
		return len(self.string)

	def __str__(self):
		return self.string
ass="o">= 0; in < SIZE(inst.ingredients); ++in) { reagent& ingredient = inst.ingredients.at(in); if (is_disqualified(ingredient, inst, caller.name)) continue; if (is_numeric_location(ingredient)) numeric_locations_used = true; if (is_named_location(ingredient)) names_used = true; if (is_integer(ingredient.name)) continue; if (!already_transformed(ingredient, names)) { raise << maybe(caller.name) << "tried to read ingredient '" << ingredient.name << "' in '" << to_original_string(inst) << "' but it hasn't been written to yet\n" << end(); // use-before-set Error return; } int v = lookup_name(ingredient, r); if (v >= 0) { ingredient.set_value(v); // Done Placing Ingredient(ingredient, inst, caller) } else { raise << maybe(caller.name) << "can't find a place to store '" << ingredient.name << "'\n" << end(); return; } } for (int out = 0; out < SIZE(inst.products); ++out) { reagent& product = inst.products.at(out); if (is_disqualified(product, inst, caller.name)) continue; if (is_numeric_location(product)) numeric_locations_used = true; if (is_named_location(product)) names_used = true; if (is_integer(product.name)) continue; if (names.find(product.name) == names.end()) { trace(103, "name") << "assign " << product.name << " " << curr_idx << end(); names[product.name] = curr_idx; curr_idx += size_of(product); } int v = lookup_name(product, r); if (v >= 0) { product.set_value(v); // Done Placing Product(product, inst, caller) } else { raise << maybe(caller.name) << "can't find a place to store '" << product.name << "'\n" << end(); return; } } } if (names_used && numeric_locations_used) raise << maybe(caller.name) << "mixing variable names and numeric addresses\n" << end(); } bool is_disqualified(/*mutable*/ reagent& x, const instruction& inst, const string& recipe_name) { if (!x.type) { raise << maybe(recipe_name) << "missing type for '" << x.original_string << "' in '" << to_original_string(inst) << "'\n" << end(); // missing-type Error 1 return true; } if (is_raw(x)) return true; if (is_literal(x)) return true; // End is_disqualified Special-cases if (x.initialized) return true; return false; } bool already_transformed(const reagent& r, const map<string, int>& names) { return contains_key(names, r.name); } int lookup_name(const reagent& r, const recipe_ordinal default_recipe) { return Name[default_recipe][r.name]; } type_ordinal skip_addresses(type_tree* type) { while (type && is_compound_type_starting_with(type, "address")) type = type->right; if (!type) return -1; // error handled elsewhere if (type->atom) return type->value; const type_tree* base_type = type; // Update base_type in skip_addresses if (base_type->atom) return base_type->value; assert(base_type->left->atom); return base_type->left->value; } bool is_compound_type_starting_with(const type_tree* type, const string& expected_name) { if (!type) return false; if (type->atom) return false; if (!type->left->atom) return false; return type->left->value == get(Type_ordinal, expected_name); } int find_element_offset(const type_ordinal t, const string& name, const string& recipe_name) { const type_info& container = get(Type, t); for (int i = 0; i < SIZE(container.elements); ++i) if (container.elements.at(i).name == name) return i; raise << maybe(recipe_name) << "unknown element '" << name << "' in container '" << get(Type, t).name << "'\n" << end(); return -1; } int find_element_location(int base_address, const string& name, const type_tree* type, const string& recipe_name) { int offset = find_element_offset(get_base_type(type)->value, name, recipe_name); if (offset == -1) return offset; int result = base_address; for (int i = 0; i < offset; ++i) result += size_of(element_type(type, i)); return result; } bool is_numeric_location(const reagent& x) { if (is_literal(x)) return false; if (is_raw(x)) return false; if (x.name == "0") return false; // used for chaining lexical scopes return is_integer(x.name); } bool is_named_location(const reagent& x) { if (is_literal(x)) return false; if (is_raw(x)) return false; if (is_special_name(x.name)) return false; return !is_integer(x.name); } // all names here should either be disqualified or also in bind_special_scenario_names bool is_special_name(const string& s) { if (s == "_") return true; if (s == "0") return true; // End is_special_name Special-cases return false; } bool is_raw(const reagent& r) { return has_property(r, "raw"); } void test_transform_names_supports_containers() { transform( "def main [\n" " x:point <- merge 34, 35\n" " y:num <- copy 3\n" "]\n" ); CHECK_TRACE_CONTENTS( "name: assign x 2\n" // skip location 3 because x occupies two locations "name: assign y 4\n" ); } void test_transform_names_supports_static_arrays() { transform( "def main [\n" " x:@:num:3 <- create-array\n" " y:num <- copy 3\n" "]\n" ); CHECK_TRACE_CONTENTS( "name: assign x 2\n" // skip locations 3, 4, 5 because x occupies four locations "name: assign y 6\n" ); } void test_transform_names_passes_dummy() { transform( "def main [\n" // _ is just a dummy result that never gets consumed " _, x:num <- copy 0, 1\n" "]\n" ); CHECK_TRACE_CONTENTS( "name: assign x 2\n" ); CHECK_TRACE_DOESNT_CONTAIN("name: assign _ 2"); } //: an escape hatch to suppress name conversion that we'll use later void test_transform_names_passes_raw() { Hide_errors = true; run( "def main [\n" " x:num/raw <- copy 0\n" "]\n" ); CHECK_TRACE_DOESNT_CONTAIN("name: assign x 2"); CHECK_TRACE_CONTENTS( "error: can't write to location 0 in 'x:num/raw <- copy 0'\n" ); } void test_transform_names_fails_when_mixing_names_and_numeric_locations() { Hide_errors = true; transform( "def main [\n" " x:num <- copy 1:num\n" "]\n" ); CHECK_TRACE_CONTENTS( "error: main: mixing variable names and numeric addresses\n" ); } void test_transform_names_fails_when_mixing_names_and_numeric_locations_2() { Hide_errors = true; transform( "def main [\n" " x:num <- copy 1\n" " 1:num <- copy x:num\n" "]\n" ); CHECK_TRACE_CONTENTS( "error: main: mixing variable names and numeric addresses\n" ); } void test_transform_names_does_not_fail_when_mixing_names_and_raw_locations() { transform( "def main [\n" " x:num <- copy 1:num/raw\n" "]\n" ); CHECK_TRACE_DOESNT_CONTAIN("error: main: mixing variable names and numeric addresses"); CHECK_TRACE_COUNT("error", 0); } void test_transform_names_does_not_fail_when_mixing_names_and_literals() { transform( "def main [\n" " x:num <- copy 1\n" "]\n" ); CHECK_TRACE_DOESNT_CONTAIN("error: main: mixing variable names and numeric addresses"); CHECK_TRACE_COUNT("error", 0); } //:: Support element names for containers in 'get' and 'get-location' and 'put'. //: (get-location is implemented later) :(before "End update GET offset_value in Check") else { if (!offset.initialized) { raise << maybe(get(Recipe, r).name) << "uninitialized offset '" << offset.name << "' in '" << to_original_string(inst) << "'\n" << end(); break; } offset_value = offset.value; } :(code) void test_transform_names_transforms_container_elements() { transform( "def main [\n" " p:&:point <- copy null\n" " a:num <- get *p:&:point, y:offset\n" " b:num <- get *p:&:point, x:offset\n" "]\n" ); CHECK_TRACE_CONTENTS( "name: element y of type point is at offset 1\n" "name: element x of type point is at offset 0\n" ); } :(before "End transform_names(inst) Special-cases") // replace element names of containers with offsets if (inst.name == "get" || inst.name == "get-location" || inst.name == "put") { //: avoid raising any errors here; later layers will support overloading new //: instructions with the same names (static dispatch), which could lead to //: spurious errors if (SIZE(inst.ingredients) < 2) break; // error raised elsewhere if (!is_literal(inst.ingredients.at(1))) break; // error raised elsewhere if (inst.ingredients.at(1).name.find_first_not_of("0123456789") != string::npos) { // since first non-address in base type must be a container, we don't have to canonize type_ordinal base_type = skip_addresses(inst.ingredients.at(0).type); if (contains_key(Type, base_type)) { // otherwise we'll raise an error elsewhere inst.ingredients.at(1).set_value(find_element_offset(base_type, inst.ingredients.at(1).name, get(Recipe, r).name)); trace(103, "name") << "element " << inst.ingredients.at(1).name << " of type " << get(Type, base_type).name << " is at offset " << no_scientific(inst.ingredients.at(1).value) << end(); } } } :(code) void test_missing_type_in_get() { Hide_errors = true; transform( "def main [\n" " get a, x:offset\n" "]\n" ); CHECK_TRACE_CONTENTS( "error: main: missing type for 'a' in 'get a, x:offset'\n" ); } void test_transform_names_handles_containers() { transform( "def main [\n" " a:point <- merge 0, 0\n" " b:num <- copy 0\n" "]\n" ); CHECK_TRACE_CONTENTS( "name: assign a 2\n" "name: assign b 4\n" ); } //:: Support variant names for exclusive containers in 'maybe-convert'. void test_transform_names_handles_exclusive_containers() { run( "def main [\n" " 12:num <- copy 1\n" " 13:num <- copy 35\n" " 14:num <- copy 36\n" " 20:point, 22:bool <- maybe-convert 12:number-or-point/unsafe, p:variant\n" "]\n" ); CHECK_TRACE_CONTENTS( "name: variant p of type number-or-point has tag 1\n" "mem: storing 1 in location 22\n" "mem: storing 35 in location 20\n" "mem: storing 36 in location 21\n" ); } :(before "End transform_names(inst) Special-cases") // convert variant names of exclusive containers if (inst.name == "maybe-convert") { if (SIZE(inst.ingredients) != 2) { raise << maybe(get(Recipe, r).name) << "exactly 2 ingredients expected in '" << to_original_string(inst) << "'\n" << end(); break; } assert(is_literal(inst.ingredients.at(1))); if (inst.ingredients.at(1).name.find_first_not_of("0123456789") != string::npos) { // since first non-address in base type must be an exclusive container, we don't have to canonize type_ordinal base_type = skip_addresses(inst.ingredients.at(0).type); if (contains_key(Type, base_type)) { // otherwise we'll raise an error elsewhere inst.ingredients.at(1).set_value(find_element_offset(base_type, inst.ingredients.at(1).name, get(Recipe, r).name)); trace(103, "name") << "variant " << inst.ingredients.at(1).name << " of type " << get(Type, base_type).name << " has tag " << no_scientific(inst.ingredients.at(1).value) << end(); } } } :(code) void test_missing_type_in_maybe_convert() { Hide_errors = true; run( "def main [\n" " maybe-convert a, x:variant\n" "]\n" ); CHECK_TRACE_CONTENTS( "error: main: missing type for 'a' in 'maybe-convert a, x:variant'\n" ); }