about summary refs log tree commit diff stats
path: root/shell/evaluate.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-04-29 16:11:23 -0700
committerKartik K. Agaram <vc@akkartik.com>2021-04-29 16:11:23 -0700
commite13fd3e0a1d7042ef8267ac9ebb04ed5ed24887f (patch)
tree9d3929777c81c095df14ec44f38ee412a6e93264 /shell/evaluate.mu
parent05879d4f9923e737a685776a68f527f752d56263 (diff)
downloadmu-e13fd3e0a1d7042ef8267ac9ebb04ed5ed24887f.tar.gz
render in a narrow column
Diffstat (limited to 'shell/evaluate.mu')
0 files changed, 0 insertions, 0 deletions
100' href='#n100'>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 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
# 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/>.

from ranger.ext.openstruct import OpenStruct

class CommandArgument(object):
	def __init__(self, fm, displayable, keybuffer):
		self.fm = fm
		self.wdg = displayable
		self.keybuffer = keybuffer
		self.n = keybuffer.number
		self.keys = str(keybuffer)

def cmdarg(displayable):
	return CommandArgument(displayable.fm, \
			displayable, displayable.env.keybuffer)

class CommandList(object):
	"""
	CommandLists are dictionary-like objects which give you a command
	for a given key combination.  CommandLists must be filled before use.
	"""

	dummy_object = None
	dummies_in_paths = False
	paths = {}
	commandlist = []

	def __init__(self):
		self.commandlist = []
		self.paths = {}

	def __getitem__(self, key):
		"""Returns the command with the given key combination"""
		if isinstance(key, str):
			key = self._str_to_tuple(key)
		return self.paths[key]

	def rebuild_paths(self):
		"""
		Fill the path dictionary with dummie objects.
		We need to know when to clear the keybuffer (when a wrong key is pressed)
		and when to wait for the rest of the key combination.  For "gg" we
		will assign "g" to a dummy which tells the program to do the latter
		and wait.
		"""
		if self.dummies_in_paths:
			self.remove_dummies()

		for cmd in self.commandlist:
			for key in cmd.keys:
				for path in self._keypath(key):
					if path not in self.paths:
						self.paths[path] = self.dummy_object

		self.dummies_in_paths = True

	def _keypath(self, tup):
		"""split a tuple like (a,b,c,d) into [(a,), (a,b), (a,b,c)]"""
		length = len(tup)

		if length == 0:
			return ()
		if length == 1:
			return (tup, )

		current = []
		all = []

		for i in range(len(tup) - 1):
			current.append(tup[i])
			all.append(tuple(current))

		return all

	def remove_dummies(self):
		"""
		Remove dummie objects in case you have to rebuild a path dictionary
		which already contains dummie objects.
		"""
		for k in tuple(self.paths.keys()):
			if self.paths[k] == self.dummy_object: del self.paths[k]
		self.dummies_in_paths = False


	def _str_to_tuple(self, obj):
		"""splits a string into a tuple of integers"""
		if isinstance(obj, tuple):
			return obj
		elif isinstance(obj, str):
			return tuple(map(ord, obj))
		elif isinstance(obj, int):
			return (obj, )
		else:
			raise TypeError('need a str, int or tuple for str_to_tuple')

	def bind(self, fnc, *keys):
		"""create a Command object and assign it to the given key combinations."""
		if len(keys) == 0: return

		keys = tuple(map(self._str_to_tuple, keys))

		cmd = Command(fnc, keys)

		self.commandlist.append(cmd)
		for key in cmd.keys:
			self.paths[key] = cmd

	def show(self, *keys, **keywords):
		"""create a Show object and assign it to the given key combinations."""
		if len(keys) == 0: return

		keys = tuple(map(self._str_to_tuple, keys))

		obj = Show(keywords, keys)

		self.commandlist.append(obj)
		for key in obj.keys:
			self.paths[key] = obj

	def alias(self, existing, *new):
		"""bind the <new> keys to the command of the <existing> key"""
		existing = self._str_to_tuple(existing)
		new = tuple(map(self._str_to_tuple, new))

		obj = AliasedCommand(_make_getter(self.paths, existing), new)

		self.commandlist.append(obj)
		for key in new:
			self.paths[key] = obj

	def unbind(self, *keys):
		i = len(self.commandlist)
		keys = set(map(self._str_to_tuple, keys))

		while i > 0:
			i -= 1
			cmd = self.commandlist[i]
			cmd.keys -= keys
			if not cmd.keys:
				del self.commandlist[i]

		for k in keys:
			del self.paths[k]

	def clear(self):
		"""remove all bindings"""
		self.paths.clear()
		del self.commandlist[:]


class Command(object):
	"""Command objects store information about a command"""

	keys = []

	def __init__(self, fnc, keys):
		self.keys = set(keys)
		self.execute = fnc

	def execute(self, *args):
		"""Execute the command"""

	def execute_wrap(self, displayable):
		self.execute(cmdarg(displayable))


class AliasedCommand(Command):
	def __init__(self, getter, keys):
		self.getter = getter
		self.keys = set(keys)

	def get_execute(self):
		return self.getter()

	execute = property(get_execute)


class Show(object):
	"""Show objects do things without clearing the keybuffer"""

	keys = []
	text = ''

	def __init__(self, dictionary, keys):
		self.keys = set(keys)
		self.show_obj = OpenStruct(dictionary)


def _make_getter(paths, key):
	def getter():
		try:
			return paths[key].execute
		except:
			return lambda: None
	return getter
ef='#n12'>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

                   

                   
                   
                   
                 

                      
 
                  
                       
 
                            
                              
 

                          

                                                        
 
                                               
 
                                                         
 
                                                       
                        

 

                                                       



                                                     

                                                        



                                                      

                                                     



                                                   

                                                        
 
                             
 
                                                                                                                  
                                                                                                                                                                                       
 
                                                         
                                                          

                                               
 


                                                                    
                                                                              
                                             
 
                                                       
                        

 

                                                    
 
                                                           
 
                                                         
 


                                                                
                                                                          





                                                       

                                                   
 
                                               
 
                                                                                                                  
                                                                                                                                                                                       





                                                          


                                                                                    

                                                        
 
                                                       
                        
 
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>

#include "xmpp/xmpp.h"

#include "ui/ui.h"
#include "ui/stub_ui.h"

#include "config/accounts.h"
#include "command/cmd_funcs.h"

#define CMD_ROOMS "/rooms"

static void
test_with_connection_status(jabber_conn_status_t status)
{
    will_return(connection_get_status, status);

    expect_cons_show("You are not currently connected.");

    gboolean result = cmd_rooms(NULL, CMD_ROOMS, NULL);
    assert_true(result);
}

void
cmd_rooms_shows_message_when_disconnected(void** state)
{
    test_with_connection_status(JABBER_DISCONNECTED);
}

void
cmd_rooms_shows_message_when_disconnecting(void** state)
{
    test_with_connection_status(JABBER_DISCONNECTING);
}

void
cmd_rooms_shows_message_when_connecting(void** state)
{
    test_with_connection_status(JABBER_CONNECTING);
}

void
cmd_rooms_uses_account_default_when_no_arg(void** state)
{
    gchar* args[] = { NULL };

    ProfAccount* account = account_new(g_strdup("testaccount"), NULL, NULL, NULL, TRUE, NULL, 0, NULL, NULL, NULL,
                                       0, 0, 0, 0, 0, g_strdup("default_conf_server"), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

    will_return(connection_get_status, JABBER_CONNECTED);
    will_return(session_get_account_name, "account_name");
    expect_any(accounts_get_account, name);
    will_return(accounts_get_account, account);

    expect_cons_show("");
    expect_cons_show("Room list request sent: default_conf_server");

    expect_string(iq_room_list_request, conferencejid, "default_conf_server");
    expect_any(iq_room_list_request, filter);

    gboolean result = cmd_rooms(NULL, CMD_ROOMS, args);
    assert_true(result);
}

void
cmd_rooms_service_arg_used_when_passed(void** state)
{
    gchar* args[] = { "service", "conf_server_arg", NULL };

    will_return(connection_get_status, JABBER_CONNECTED);

    expect_cons_show("");
    expect_cons_show("Room list request sent: conf_server_arg");

    expect_string(iq_room_list_request, conferencejid, "conf_server_arg");
    expect_any(iq_room_list_request, filter);

    gboolean result = cmd_rooms(NULL, CMD_ROOMS, args);
    assert_true(result);
}

void
cmd_rooms_filter_arg_used_when_passed(void** state)
{
    gchar* args[] = { "filter", "text", NULL };

    ProfAccount* account = account_new(g_strdup("testaccount"), NULL, NULL, NULL, TRUE, NULL, 0, NULL, NULL, NULL,
                                       0, 0, 0, 0, 0, g_strdup("default_conf_server"), NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

    will_return(connection_get_status, JABBER_CONNECTED);
    will_return(session_get_account_name, "account_name");
    expect_any(accounts_get_account, name);
    will_return(accounts_get_account, account);

    expect_cons_show("");
    expect_cons_show("Room list request sent: default_conf_server, filter: 'text'");

    expect_any(iq_room_list_request, conferencejid);
    expect_string(iq_room_list_request, filter, "text");

    gboolean result = cmd_rooms(NULL, CMD_ROOMS, args);
    assert_true(result);
}