Merge tab-complete

Fri, 15 May 2015 18:36:22 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Fri, 15 May 2015 18:36:22 +0300
changeset 71
4f7c2c944637
parent 70
0e947b487b18 (current diff)
parent 41
9ab869656b9e (diff)
child 72
1b9c53e0c846

Merge tab-complete

sources/interface.cpp file | annotate | diff | comparison | revisions
sources/interface.h file | annotate | diff | comparison | revisions
sources/list.h file | annotate | diff | comparison | revisions
sources/network/rconsession.cpp file | annotate | diff | comparison | revisions
sources/network/rconsession.h file | annotate | diff | comparison | revisions
--- a/sources/interface.cpp	Mon May 04 18:16:05 2015 +0300
+++ b/sources/interface.cpp	Fri May 15 18:36:22 2015 +0300
@@ -803,6 +803,20 @@
 		}
 		break;
 
+	case '\t':
+		{
+			int space = current_input().find (" ");
+
+			if (CurrentInputState == INPUTSTATE_NORMAL
+				and InputCursor > 0
+				and (space == -1 or space >= InputCursor))
+			{
+				String start = current_input().mid (0, InputCursor);
+				RCONSession::get_session()->request_tab_complete (start);
+			}
+		}
+		break;
+
 	case '\n':
 	case KEY_ENTER:
 		switch (CurrentInputState)
@@ -892,6 +906,8 @@
 		}
 		break;
 	}
+
+	render();
 }
 
 // -------------------------------------------------------------------------------------------------
@@ -978,3 +994,21 @@
 	PlayerNames = names;
 	NeedNicklistRender = true;
 }
+
+// -------------------------------------------------------------------------------------------------
+//
+FUNCTION
+Interface::tab_complete (const String& part, String complete) -> void
+{
+	String& input = mutable_current_input();
+
+	if (input.starts_with (part))
+	{
+		if (input[part.length()] != ' ')
+			complete += ' ';
+
+		input.replace (0, part.length(), complete);
+		InputCursor = complete.length();
+		NeedInputRender = true;
+	}
+}
--- a/sources/interface.h	Mon May 04 18:16:05 2015 +0300
+++ b/sources/interface.h	Fri May 15 18:36:22 2015 +0300
@@ -41,4 +41,6 @@
 	FUNCTION update_statusbar() -> void;
 	FUNCTION connect (String address, String password) -> void;
 	FUNCTION set_player_names (const StringList& names) -> void;
+	FUNCTION need_refresh() -> void;
+	FUNCTION tab_complete (const String& part, String complete) -> void;
 };
--- a/sources/list.h	Mon May 04 18:16:05 2015 +0300
+++ b/sources/list.h	Fri May 15 18:36:22 2015 +0300
@@ -339,7 +339,7 @@
 {
 public:
 	using Super = Container<T, std::vector<T>>;
-	using Super::Container;
+	using typename Super::Container;
 
 	Vector(){}
 
@@ -360,4 +360,4 @@
 	{
 		return data();
 	}
-};
\ No newline at end of file
+};
--- a/sources/network/rconsession.cpp	Mon May 04 18:16:05 2015 +0300
+++ b/sources/network/rconsession.cpp	Fri May 15 18:36:22 2015 +0300
@@ -161,6 +161,37 @@
 			case SVRC_UPDATE:
 				process_server_updates (packet);
 				break;
+
+			case SVRC_TOOMANYTABCOMPLETES:
+				{
+					unsigned int numCompletions = packet.read_short();
+					print ("%1 completions for '%2'.\n",
+						int (numCompletions), m_lastTabComplete);
+				}
+				break;
+
+			case SVRC_TABCOMPLETE:
+				{
+					StringList completes;
+
+					for (signed int i = packet.read_byte(); i > 0; --i)
+						completes << packet.read_string();
+
+					if (completes.size() == 1)
+						Interface::tab_complete (m_lastTabComplete, completes[0]);
+					else if (not completes.is_empty())
+					{
+						print ("Completions for '%1':\n", m_lastTabComplete);
+
+						for (int i = 0; i < completes.size(); i += 8)
+						{
+							Range<int> spliceRange (i, min (i + 8, completes.size() - 1));
+							StringList splice (completes.splice (spliceRange));
+							print ("- %1\n", splice.join (", "));
+						}
+					}
+				}
+				break;
 			}
 		}
 	}
@@ -321,3 +352,21 @@
 {
 	return m_level;
 }
+
+// -------------------------------------------------------------------------------------------------
+//
+METHOD
+RCONSession::request_tab_complete (const String& part) -> void
+{
+	if (m_serverProtocol >= 4)
+	{
+		Bytestream packet;
+		packet.write_byte (CLRC_TABCOMPLETE);
+		packet.write_string (part);
+		send (packet);
+		bump_last_ping();
+		m_lastTabComplete = part;
+	}
+	else
+		print ("Server protocol is %1, cannot tab-complete\n", m_serverProtocol);
+}
--- a/sources/network/rconsession.h	Mon May 04 18:16:05 2015 +0300
+++ b/sources/network/rconsession.h	Fri May 15 18:36:22 2015 +0300
@@ -37,7 +37,7 @@
 //
 enum
 {
-	RCON_PROTOCOL_VERSION = 3
+	RCON_PROTOCOL_VERSION = 4
 };
 
 // -------------------------------------------------------------------------------------------------
@@ -51,6 +51,8 @@
 	SVRC_INVALIDPASSWORD,
 	SVRC_MESSAGE,
 	SVRC_UPDATE,
+	SVRC_TABCOMPLETE,
+	SVRC_TOOMANYTABCOMPLETES,
 };
 
 // -------------------------------------------------------------------------------------------------
@@ -62,6 +64,7 @@
 	CLRC_COMMAND,
 	CLRC_PONG,
 	CLRC_DISCONNECT,
+	CLRC_TABCOMPLETE,
 };
 
 // -------------------------------------------------------------------------------------------------
@@ -107,6 +110,7 @@
 	METHOD state() const -> RCONSessionState;
 	METHOD level() const -> const String&;
 	METHOD is_active() const -> bool;
+	METHOD request_tab_complete (const String& part) -> void;
 
 	static METHOD get_session() -> RCONSession*;
 
@@ -123,4 +127,5 @@
 	String m_hostname;
 	int m_numAdmins;
 	String m_level;
+	String m_lastTabComplete;
 };

mercurial