about summary refs log tree commit diff stats
path: root/subx/examples/ex4.subx
blob: 3060ae3f5779903cb3f723e884b49e7e523a815f (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
# Read a character from stdin, save it to a global, write it to stdout.
#
# To run (from the subx directory):
#   $ ./subx translate examples/ex4.subx -o examples/ex4
#   $ ./subx run examples/ex4

== code

# syscall(read, stdin, X, 1)
# . fd = 0 (stdin)
bb/copy-to-EBX  0/imm32
# . data = X (location to write result to)
b9/copy-to-ECX  X/imm32
# . size = 1 character
ba/copy-to-EDX  1/imm32
# . syscall
b8/copy-to-EAX  3/imm32/read
cd/syscall  0x80/imm8

# syscall(write, stdout, X, 1)
# . fd = 1 (stdout)
bb/copy-to-EBX  1/imm32
# . initialize X (location to read from)
b9/copy-to-ECX  X/imm32
# . size = 1 character
ba/copy-to-EDX  1/imm32
# . syscall
b8/copy-to-EAX  4/imm32/write
cd/syscall  0x80/imm8

# syscall(exit, EBX)
b8/copy-to-EAX  1/imm32/exit
cd/syscall  0x80/imm8

== data

X:
    00 00 00 00  # space for read() to write to

# . . vim:nowrap:textwidth=0
89a27f94a43771526d2ff0b7'>^
b3d031a9 ^
d1a1173d ^
9cce9fab ^
d1a1173d ^
b3d031a9 ^


ea87d005 ^
d1a1173d ^

ea87d005 ^
d1a1173d ^

9cce9fab ^
d1a1173d ^







ea87d005 ^
9cce9fab ^

76791a70 ^
d1a1173d ^



84a22ae0 ^
d1a1173d ^




















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

                                                                 
 
                                                                  
 
          



                                                            
                              
 
 
                  



























                                                             
                            






                                                         
                                 







































                                                                          


                    
 
                                                                                
                                            
 
                                      


                                                                   
 

                                  
 

                                              
 







                                  
 

                            
 



                                        
                                               




















                                                                                
# This file is part of ranger, the console file manager.
# License: GNU GPL version 3, see the file "AUTHORS" for details.

from __future__ import (absolute_import, division, print_function)

import sys

from ranger.ext.widestring import WideString, utf_char_width


PY3 = sys.version_info[0] >= 3


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 self.left:
                leftsize -= len(self.left.pop(-1))
                if leftsize + rightsize <= wid:
                    break
            sumsize = leftsize + rightsize

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

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

    def shrink_from_the_left(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()
        oversize = leftsize + rightsize - wid
        if oversize <= 0:
            return self.fill_gap(' ', wid, gapwidth=False)

        # Shrink items to a minimum size until there is enough room.
        for item in self.left:
            if not item.fixed:
                itemlen = len(item)
                if oversize > itemlen - item.min_size:
                    item.cut_off_to(item.min_size)
                    oversize -= (itemlen - item.min_size)
                else:
                    item.cut_off(oversize)
                    break

    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):  # pylint: disable=super-init-not-called
        self.base_color_tag = base_color_tag

    def add(self, string, *lst, **kw):
        colorstr = ColoredString(string, self.base_color_tag, *lst)
        colorstr.__dict__.update(kw)
        self.append(colorstr)

    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 += item.min_size
        return n


class ColoredString(object):

    def __init__(self, string, *lst):
        self.string = WideString(string)
        self.lst = lst
        self.fixed = False
        if not string or not self.string.chars:
            self.min_size = 0
        elif PY3:
            self.min_size = utf_char_width(string[0])
        else:
            self.min_size = utf_char_width(self.string.chars[0].decode('utf-8'))

    def cut_off(self, n):
        if n >= 1:
            self.string = self.string[:-n]

    def cut_off_to(self, n):
        if n < self.min_size:
            self.string = self.string[:self.min_size]
        elif n < len(self.string):
            self.string = self.string[:n]

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

    def __str__(self):
        return str(self.string)