about summary refs log tree commit diff stats
path: root/channel.mu
blob: 4a553148cb85162095af5f3d23726c11c05bec63 (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
# example program: communicating between routines using channels

def producer sink:&:sink:char -> sink:&:sink:char [
  # produce characters 1 to 5 on a channel
  local-scope
  load-inputs
  # n = 0
  n:char <- copy 0
  {
    done?:bool <- lesser-than n, 5
    break-unless done?
    # other threads might get between these prints
    $print [produce: ], n, [ 
]
    sink <- write sink, n
    n <- add n, 1
    loop
  }
  close sink
]

def consumer source:&:source:char -> source:&:source:char [
  # consume and print integers from a channel
  local-scope
  load-inputs
  {
    # read an integer from the channel
    n:char, eof?:bool, source <- read source
    break-if eof?
    # other threads might get between these prints
    $print [consume: ], n:char, [ 
]
    loop
  }
]

def main [
  local-scope
  source:&:source:char, sink:&:sink:char <- new-channel 3/capacity
  # create two background 'routines' that communicate by a channel
  routine1:num <- start-running producer, sink
  routine2:num <- start-running consumer, source
  wait-for-routine routine1
  wait-for-routine routine2
]
mp;id=e6dfc442a984bc50a70be5017f4ab9a0e6efadef'>e6dfc442 ^
cc952d63 ^







e6dfc442 ^

cc952d63 ^



e6dfc442 ^



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


                                     
                                                                                    





                                                                 
                                                     

                               

                                                            
               
 

                                                             



                                                             
                                                                           



                                                                        








                                                 


                                                                  

                                                         
                                                         

                                                             


                                                    








                                       


                                        



                                                                          
                                                                
 
                          


                                                      
                                           

                                                   
                                                 


                                                 
 




                                             

                          














                                                                  

                       

                  







                                                                             

                          



                                                               



                                           
def initialize_commands(cl):
	from ranger.fm import FM
	from curses.ascii import ctrl
	from ranger.container.bookmarks import ALLOWED_KEYS as ALLOWED_BOOKMARK_KEYS
	import curses

	# syntax for binding keys: cl.bind(fnc, *keys)
	# fnc is a function which is called with the FM instance,
	# keys are one or more key-combinations which are either:
	# * a string
	# * an integer which represents an ascii code
	# * a tuple of integers

	def curry(fnc, *args, **keywords):
		return lambda fm: fnc(fm, *args, **keywords)
	c=curry

	def move(**keywords):
		return lambda fm: fm.move_pointer(**keywords)

	def move_pages(n):
		return lambda fm: fm.move_pointer_by_pages(n)

	cl.bind(FM.move_left,               'h', curses.KEY_BACKSPACE, 127)
	cl.bind(FM.move_right,              'l')
	cl.bind(c(FM.move_right, mode=1),   curses.KEY_ENTER, ctrl('j'))
	cl.bind(c(FM.history_go, -1),       'H')
	cl.bind(c(FM.history_go,  1),       'L')
	cl.bind(move( relative = 1 ),       'j')
	cl.bind(move_pages( 0.5 ),          'J')
	cl.bind(move( relative = -1 ),      'k')
	cl.bind(move_pages( -0.5 ),         'K')
	cl.bind(move( absolute = 0 ),       'gg')
	cl.bind(move( absolute = -1 ),      'G')
	cl.bind(FM.edit_file,               'E')

	# toggle options
	def toggle_option(string):
		return lambda fm: fm.toggle_boolean_option(string)

	cl.bind(toggle_option('show_hidden'),       'th')
	cl.bind(toggle_option('preview_files'),     'tp')
	cl.bind(toggle_option('directories_first'), 'td')

	# key combinations which change the current directory
	def cd(path):
		return lambda fm: fm.enter_dir(path)

	cl.bind(cd("~"),          'gh')
	cl.bind(cd("/etc"),       'ge')
	cl.bind(cd("/usr"),       'gu')
	cl.bind(cd("/"),          'gr')
	cl.bind(cd("/media"),     'gm')
	cl.bind(cd("/mnt"),       'gn')
	cl.bind(cd("~/.trash"),   'gt')
	cl.bind(cd("/srv"),       'gs')

	cl.bind(FM.search_forward,  'n')
	cl.bind(FM.search_backward, 'N')

	# bookmarks
	for key in ALLOWED_BOOKMARK_KEYS:
		cl.bind(c(FM.enter_bookmark, key),   "`" + key, "'" + key)
		cl.bind(c(FM.set_bookmark, key),     "m" + key)
		cl.bind(c(FM.unset_bookmark, key),   "um" + key)

	# system functions
	cl.bind(FM.exit,         ctrl('D'), 'q', 'ZZ')
	cl.bind(FM.reset,        ctrl('R'))
	cl.bind(FM.redraw,       ctrl('L'))
	cl.bind(FM.interrupt,    ctrl('C'))
	cl.bind(FM.resize,       curses.KEY_RESIZE)
	cl.bind(FM.handle_mouse, curses.KEY_MOUSE)
	cl.bind(curry(FM.open_console, ':'), ':')
	cl.bind(curry(FM.open_console, '/'), '/')
	cl.bind(curry(FM.open_console, '!'), '!')
	cl.bind(curry(FM.open_console, '@'), 'r')

	def test(fm):
		from ranger.helper import log
		log(fm.bookmarks.dct)
	cl.bind(test, 'x')

	cl.rebuild_paths()


def initialize_console_commands(cl):
	from ranger.fm import FM
	from ranger.gui.wconsole import WConsole
	from curses.ascii import ctrl, ESC
	import curses

	def type_key(key):
		return lambda con, fm: con.type_key(key)

	def curry(fnc, *args, **keywords):
		return lambda con, fm: fnc(con, *args, **keywords)

	def curry_fm(fnc, *args, **keywords):
		return lambda con, fm: fnc(fm, *args, **keywords)
	c_fm = curry_fm
	c = curry

	# movement
	cl.bind(c(WConsole.move, relative = -1), curses.KEY_LEFT, ctrl('b'))
	cl.bind(c(WConsole.move, relative =  1), curses.KEY_RIGHT, ctrl('f'))
	cl.bind(c(WConsole.move, absolute = 0), curses.KEY_HOME, ctrl('a'))
	cl.bind(c(WConsole.move, absolute = -1), curses.KEY_END, ctrl('e'))
	cl.bind(c(WConsole.delete, 0), curses.KEY_DC, ctrl('d'))
	cl.bind(c(WConsole.delete, -1), curses.KEY_BACKSPACE, 127, ctrl('h'))
	cl.bind(c(WConsole.delete_rest, -1), ctrl('U'))
	cl.bind(c(WConsole.delete_rest,  1), ctrl('K'))

	# system functions
	cl.bind(c(WConsole.close),    ESC, ctrl('C'))
	cl.bind(WConsole.execute,  curses.KEY_ENTER, ctrl('j'))
	cl.bind(c_fm(FM.redraw), ctrl('L'))
	cl.bind(c_fm(FM.resize), curses.KEY_RESIZE)

	for i in range(ord(' '), ord('~')):
		cl.bind(type_key(i), i)