about summary refs log blame commit diff stats
path: root/html/screen.mu.html
blob: 5246a0fcd2d403a6db376c56d7fdfdba08f651c5 (plain) (tree)
1
2
3
4
5
6
7
8
9



                                                                                          
                             
                                         
                                                
                                   
                                                                                                               
                                                 

                       

                                                                                                 
                    
                                     
                             



                             

        



                               






















                                                                                 

         
       
                             
                                                                                                                       
                         



                                                                                                                                                
                                                                                                               
                                                     
                                                                                                                                                                                    
                                                                                                                           




                                                                                                                                                                                                                                                         
                                                                                                                            












                                                                                                                                                                                                                

                                                       


       
                                     
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - screen.mu</title>
<meta name="Generator" content="Vim/8.0">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal-light">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #c6c6c6; }
body { font-size:12pt; font-family: monospace; color: #000000; background-color: #c6c6c6; }
a { color:inherit; }
* { font-size:12pt; font-size: 1em; }
.muRecipe { color: #ff8700; }
.LineNr { }
.Constant { color: #008787; }
.Special { color: #ff6060; }
.Comment { color: #005faf; }
-->
</style>

<script type='text/javascript'>
<!--

/* function to open any folds containing a jumped-to line before jumping to it */
function JumpToLine()
{
  var lineNum;
  lineNum = window.location.hash;
  lineNum = lineNum.substr(1); /* strip off '#' */

  if (lineNum.indexOf('L') == -1) {
    lineNum = 'L'+lineNum;
  }
  lineElem = document.getElementById(lineNum);
  /* Always jump to new location even if the line was hidden inside a fold, or
   * we corrected the raw number to a line ID.
   */
  if (lineElem) {
    lineElem.scrollIntoView(true);
  }
  return true;
}
if ('onhashchange' in window) {
  window.onhashchange = JumpToLine;
}

-->
</script>
</head>
<body onload='JumpToLine();'>
<a href='https://github.com/akkartik/mu/blob/master/screen.mu'>https://github.com/akkartik/mu/blob/master/screen.mu</a>
<pre id='vimCodeElement'>
<span id="L1" class="LineNr"> 1 </span><span class="Comment"># example program: managing the display using 'screen' objects</span>
<span id="L2" class="LineNr"> 2 </span>
<span id="L3" class="LineNr"> 3 </span><span class="Comment"># The zero screen below means 'use the real screen'. Tests can also use fake</span>
<span id="L4" class="LineNr"> 4 </span><span class="Comment"># screens.</span>
<span id="L5" class="LineNr"> 5 </span><span class="muRecipe">def</span> <a href='screen.mu.html#L5'>main</a> [
<span id="L6" class="LineNr"> 6 </span>  open-console
<span id="L7" class="LineNr"> 7 </span>  <a href='081print.mu.html#L46'>clear-screen</a><span class="Constant"> null/screen</span>  <span class="Comment"># non-scrolling app</span>
<span id="L8" class="LineNr"> 8 </span>  10:char <span class="Special">&lt;-</span> copy <span class="Constant">97/a</span>
<span id="L9" class="LineNr"> 9 </span>  print<span class="Constant"> null/screen,</span> 10:char/a, <span class="Constant">1/red</span>, <span class="Constant">2/green</span>
<span id="L10" class="LineNr">10 </span>  1:num/<span class="Special">raw</span>, 2:num/<span class="Special">raw</span> <span class="Special">&lt;-</span> <a href='081print.mu.html#L577'>cursor-position</a><span class="Constant"> null/screen</span>
<span id="L11" class="LineNr">11 </span>  <a href='084console.mu.html#L88'>wait-for-event</a><span class="Constant"> null/console</span>
<span id="L12" class="LineNr">12 </span>  <a href='081print.mu.html#L46'>clear-screen</a><span class="Constant"> null/screen</span>
<span id="L13" class="LineNr">13 </span>  <a href='081print.mu.html#L591'>move-cursor</a><span class="Constant"> null/screen,</span> <span class="Constant">0/row</span>, <span class="Constant">4/column</span>
<span id="L14" class="LineNr">14 </span>  10:char <span class="Special">&lt;-</span> copy <span class="Constant">98/b</span>
<span id="L15" class="LineNr">15 </span>  print<span class="Constant"> null/screen,</span> 10:char
<span id="L16" class="LineNr">16 </span>  <a href='084console.mu.html#L88'>wait-for-event</a><span class="Constant"> null/console</span>
<span id="L17" class="LineNr">17 </span>  <a href='081print.mu.html#L591'>move-cursor</a><span class="Constant"> null/screen,</span> <span class="Constant">0/row</span>, <span class="Constant">0/column</span>
<span id="L18" class="LineNr">18 </span>  <a href='081print.mu.html#L524'>clear-line</a><span class="Constant"> null/screen</span>
<span id="L19" class="LineNr">19 </span>  <a href='084console.mu.html#L88'>wait-for-event</a><span class="Constant"> null/console</span>
<span id="L20" class="LineNr">20 </span>  <a href='081print.mu.html#L645'>cursor-down</a><span class="Constant"> null/screen</span>
<span id="L21" class="LineNr">21 </span>  <a href='084console.mu.html#L88'>wait-for-event</a><span class="Constant"> null/console</span>
<span id="L22" class="LineNr">22 </span>  <a href='081print.mu.html#L706'>cursor-right</a><span class="Constant"> null/screen</span>
<span id="L23" class="LineNr">23 </span>  <a href='084console.mu.html#L88'>wait-for-event</a><span class="Constant"> null/console</span>
<span id="L24" class="LineNr">24 </span>  <a href='081print.mu.html#L726'>cursor-left</a><span class="Constant"> null/screen</span>
<span id="L25" class="LineNr">25 </span>  <a href='084console.mu.html#L88'>wait-for-event</a><span class="Constant"> null/console</span>
<span id="L26" class="LineNr">26 </span>  <a href='081print.mu.html#L688'>cursor-up</a><span class="Constant"> null/screen</span>
<span id="L27" class="LineNr">27 </span>  <a href='084console.mu.html#L88'>wait-for-event</a><span class="Constant"> null/console</span>
<span id="L28" class="LineNr">28 </span>  close-console
<span id="L29" class="LineNr">29 </span>]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
'#n104'>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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272

                                                                 
 
                              
                                                       
                                               
                                                             
         
              
 
                    
                                      

                                       
                         










                                            
                         
                      
                        

                                                  
                                 
                          
                            

                                
                          
                           
                                 
                            
                                        

                                 
                         
                                              
                                       

                                  


                                   
                         
                
                              




                               
                    
                      
                           

                           
                           
                          
                                       
 
 

                                                         
                                                      




                                                        
                                        

 
                  





                     
 
 
 
                                                   


                                                
                                               
                                              


                                            

                                                 

                                                            

                                                  


                                         
                                          
                                                             
                                     
                 
                                                                 
                                       

                                   
                                              




                                                  


                                         






                                                                        
                                                     



                                                                             
                                           
                                            

                                                                             
                                                                
                                                 




                                                                             


                            
                
                                                
                   


                                

                                                             
                                                
                                                             
                                      


                                                   

                                                                               
                                  



                                          
                                      





























                                         















                                                                              
                                                          
                
                                               





                                                


                                                   

                                                                      
                                          


                                              




                                                    



                                           
                                                                             

 
                      





















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

from inspect import isfunction
from ranger.ext.signals import SignalDispatcher, Signal
from ranger.core.shared import FileManagerAware
from ranger.gui.colorscheme import _colorscheme_name_to_class
import re
import os.path

ALLOWED_SETTINGS = {
    'automatically_count_files': bool,
    'autosave_bookmarks': bool,
    'autoupdate_cumulative_size': bool,
    'cd_bookmarks': bool,
    'collapse_preview': bool,
    'colorscheme': str,
    'column_ratios': (tuple, list),
    'confirm_on_delete': str,
    'dirname_in_tabs': bool,
    'display_size_in_main_column': bool,
    'display_size_in_status_bar': bool,
    'display_tags_in_all_columns': bool,
    'draw_borders': bool,
    'draw_progress_bar_in_status_bar': bool,
    'flushinput': bool,
    'hidden_filter': str,
    'idle_delay': int,
    'line_numbers': str,
    'max_console_history_size': (int, type(None)),
    'max_history_size': (int, type(None)),
    'metadata_deep_search': bool,
    'mouse_enabled': bool,
    'open_all_images': bool,
    'padding_right': bool,
    'preview_directories': bool,
    'preview_files': bool,
    'preview_images': bool,
    'preview_images_method': str,
    'preview_max_size': int,
    'preview_script': (str, type(None)),
    'save_console_history': bool,
    'scroll_offset': int,
    'shorten_title': int,
    'show_cursor': bool,  # TODO: not working?
    'show_selection_in_titlebar': bool,
    'show_hidden_bookmarks': bool,
    'show_hidden': bool,
    'sort_case_insensitive': bool,
    'sort_directories_first': bool,
    'sort_reverse': bool,
    'sort_unicode': bool,
    'sort': str,
    'status_bar_on_top': bool,
    'tilde_in_titlebar': bool,
    'unicode_ellipsis': bool,
    'update_title': bool,
    'update_tmux_title': bool,
    'use_preview_script': bool,
    'viewmode': str,
    'vcs_aware': bool,
    'vcs_backend_bzr': str,
    'vcs_backend_git': str,
    'vcs_backend_hg': str,
    'vcs_backend_svn': str,
    'xterm_alt_key': bool,
    'clear_filters_on_dir_change': bool
}

ALLOWED_VALUES = {
    'confirm_on_delete': ['always', 'multiple', 'never'],
    'line_numbers': ['false', 'absolute', 'relative'],
    'preview_images_method': ['w3m', 'iterm2'],
    'vcs_backend_bzr': ['enabled', 'local', 'disabled'],
    'vcs_backend_git': ['enabled', 'local', 'disabled'],
    'vcs_backend_hg': ['enabled', 'local', 'disabled'],
    'vcs_backend_svn': ['enabled', 'local', 'disabled'],
    'viewmode': ['miller', 'multipane'],
}

DEFAULT_VALUES = {
    bool: False,
    type(None): None,
    str: "",
    int: 0,
    list: [],
    tuple: tuple([]),
}


class Settings(SignalDispatcher, FileManagerAware):
    def __init__(self):
        SignalDispatcher.__init__(self)
        self.__dict__['_localsettings'] = dict()
        self.__dict__['_localregexes'] = dict()
        self.__dict__['_tagsettings'] = dict()
        self.__dict__['_settings'] = dict()
        for name in ALLOWED_SETTINGS:
            self.signal_bind('setopt.'+name,
                    self._sanitize, priority=1.0)
            self.signal_bind('setopt.'+name,
                    self._raw_set_with_signal, priority=0.2)

    def _sanitize(self, signal):
        name, value = signal.setting, signal.value
        if name == 'column_ratios':
            # TODO: cover more cases here
            if isinstance(value, tuple):
                signal.value = list(value)
            if not isinstance(value, list) or len(value) < 2:
                signal.value = [1, 1]
            else:
                signal.value = [int(i) if str(i).isdigit() else 1
                        for i in value]

        elif name == 'colorscheme':
            _colorscheme_name_to_class(signal)

        elif name == 'preview_script':
            if isinstance(value, str):
                result = os.path.expanduser(value)
                if os.path.exists(result):
                    signal.value = result
                else:
                    signal.value = None

        elif name == 'use_preview_script':
            if self._settings['preview_script'] is None and value \
                    and self.fm.ui.is_on:
                self.fm.notify("Preview script undefined or not found!",
                        bad=True)

    def set(self, name, value, path=None, tags=None):
        assert name in ALLOWED_SETTINGS, "No such setting: {0}!".format(name)
        if name not in self._settings:
            previous = None
        else:
            previous = self._settings[name]
        assert self._check_type(name, value)
        assert not (tags and path), "Can't set a setting for path and tag " \
            "at the same time!"
        kws = dict(setting=name, value=value, previous=previous,
                path=path, tags=tags, fm=self.fm)
        self.signal_emit('setopt', **kws)
        self.signal_emit('setopt.'+name, **kws)

    def get(self, name, path=None):
        assert name in ALLOWED_SETTINGS, "No such setting: {0}!".format(name)
        if path:
            localpath = path
        else:
            try:
                localpath = self.fm.thisdir.path
            except:
                localpath = path

        if localpath:
            for pattern, regex in self._localregexes.items():
                if name in self._localsettings[pattern] and\
                        regex.search(localpath):
                    return self._localsettings[pattern][name]
        if self._tagsettings and path:
            realpath = os.path.realpath(path)
            if realpath in self.fm.tags:
                tag = self.fm.tags.marker(realpath)
                if tag in self._tagsettings and name in self._tagsettings[tag]:
                    return self._tagsettings[tag][name]
        if name in self._settings:
            return self._settings[name]
        else:
            type_ = self.types_of(name)[0]
            value = DEFAULT_VALUES[type_]
            self._raw_set(name, value)
            self.__setattr__(name, value)
            return self._settings[name]

    def __setattr__(self, name, value):
        if name.startswith('_'):
            self.__dict__[name] = value
        else:
            self.set(name, value, None)

    def __getattr__(self, name):
        if name.startswith('_'):
            return self.__dict__[name]
        else:
            return self.get(name, None)

    def __iter__(self):
        for x in self._settings:
            yield x

    def types_of(self, name):
        try:
            typ = ALLOWED_SETTINGS[name]
        except KeyError:
            return tuple()
        else:
            if isinstance(typ, tuple):
                return typ
            else:
                return (typ, )

    def _check_type(self, name, value):
        typ = ALLOWED_SETTINGS[name]
        if isfunction(typ):
            assert typ(value), \
                "Warning: The option `" + name + "' has an incorrect type!"
        else:
            assert isinstance(value, typ), \
                "Warning: The option `" + name + "' has an incorrect type!"\
                " Got " + str(type(value)) + ", expected " + str(typ) + "!" +\
                " Please check if your commands.py is up to date." if not \
                self.fm.ui.is_set_up else ""
        return True

    __getitem__ = __getattr__
    __setitem__ = __setattr__

    def _raw_set(self, name, value, path=None, tags=None):
        if path:
            if path not in self._localsettings:
                try:
                    regex = re.compile(path)
                except:
                    # Bad regular expression
                    return
                self._localregexes[path] = regex
                self._localsettings[path] = dict()
            self._localsettings[path][name] = value

            # make sure name is in _settings, so __iter__ runs through
            # local settings too.
            if name not in self._settings:
                type_ = self.types_of(name)[0]
                value = DEFAULT_VALUES[type_]
                self._settings[name] = value
        elif tags:
            for tag in tags:
                if tag not in self._tagsettings:
                    self._tagsettings[tag] = dict()
                self._tagsettings[tag][name] = value
        else:
            self._settings[name] = value

    def _raw_set_with_signal(self, signal):
        self._raw_set(signal.setting, signal.value, signal.path, signal.tags)


class LocalSettings():
    def __init__(self, path, parent):
        self.__dict__['_parent'] = parent
        self.__dict__['_path'] = path

    def __setattr__(self, name, value):
        if name.startswith('_'):
            self.__dict__[name] = value
        else:
            self._parent.set(name, value, self._path)

    def __getattr__(self, name):
        if name.startswith('_'):
            return self.__dict__[name]
        else:
            return self._parent.get(name, self._path)

    def __iter__(self):
        for x in self._parent._settings:
            yield x

    __getitem__ = __getattr__
    __setitem__ = __setattr__