Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

406 lines
7.6 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // TextConsole.cpp: implementation of the TextConsole class.
  9. //
  10. //////////////////////////////////////////////////////////////////////
  11. //#include "port.h"
  12. #pragma warning(disable:4100) //unreferenced formal parameter
  13. #include "textconsole.h"
  14. #pragma warning(default:4100) //unreferenced formal parameter
  15. #include "utlvector.h"
  16. #include "tier1/strtools.h"
  17. //////////////////////////////////////////////////////////////////////
  18. // Construction/Destruction
  19. //////////////////////////////////////////////////////////////////////
  20. bool CTextConsole::Init( /*IBaseSystem * system*/ )
  21. {
  22. //m_System = system; // NULL or a valid base system interface
  23. memset( m_szConsoleText, 0, sizeof( m_szConsoleText ) );
  24. m_nConsoleTextLen = 0;
  25. m_nCursorPosition = 0;
  26. memset( m_szSavedConsoleText, 0, sizeof( m_szSavedConsoleText ) );
  27. m_nSavedConsoleTextLen = 0;
  28. memset( m_aszLineBuffer, 0, sizeof( m_aszLineBuffer ) );
  29. m_nTotalLines = 0;
  30. m_nInputLine = 0;
  31. m_nBrowseLine = 0;
  32. // these are log messages, not related to console
  33. Msg( "\n" );
  34. Msg( "Console initialized.\n" );
  35. m_ConsoleVisible = true;
  36. return true;
  37. }
  38. void CTextConsole::SetVisible( bool visible )
  39. {
  40. m_ConsoleVisible = visible;
  41. }
  42. bool CTextConsole::IsVisible()
  43. {
  44. return m_ConsoleVisible;
  45. }
  46. void CTextConsole::ShutDown( void )
  47. {
  48. }
  49. void CTextConsole::Print( char * pszMsg )
  50. {
  51. if ( m_nConsoleTextLen )
  52. {
  53. int nLen;
  54. nLen = m_nConsoleTextLen;
  55. while ( nLen-- )
  56. {
  57. PrintRaw( "\b \b" );
  58. }
  59. }
  60. PrintRaw( pszMsg );
  61. if ( m_nConsoleTextLen )
  62. {
  63. PrintRaw( m_szConsoleText, m_nConsoleTextLen );
  64. }
  65. UpdateStatus();
  66. }
  67. int CTextConsole::ReceiveNewline( void )
  68. {
  69. int nLen = 0;
  70. Echo( "\n" );
  71. if ( m_nConsoleTextLen )
  72. {
  73. nLen = m_nConsoleTextLen;
  74. m_szConsoleText[ m_nConsoleTextLen ] = 0;
  75. m_nConsoleTextLen = 0;
  76. m_nCursorPosition = 0;
  77. // cache line in buffer, but only if it's not a duplicate of the previous line
  78. if ( ( m_nInputLine == 0 ) || ( strcmp( m_aszLineBuffer[ m_nInputLine - 1 ], m_szConsoleText ) ) )
  79. {
  80. strncpy( m_aszLineBuffer[ m_nInputLine ], m_szConsoleText, MAX_CONSOLE_TEXTLEN );
  81. m_nInputLine++;
  82. if ( m_nInputLine > m_nTotalLines )
  83. m_nTotalLines = m_nInputLine;
  84. if ( m_nInputLine >= MAX_BUFFER_LINES )
  85. m_nInputLine = 0;
  86. }
  87. m_nBrowseLine = m_nInputLine;
  88. }
  89. return nLen;
  90. }
  91. void CTextConsole::ReceiveBackspace( void )
  92. {
  93. int nCount;
  94. if ( m_nCursorPosition == 0 )
  95. {
  96. return;
  97. }
  98. m_nConsoleTextLen--;
  99. m_nCursorPosition--;
  100. Echo( "\b" );
  101. for ( nCount = m_nCursorPosition; nCount < m_nConsoleTextLen; nCount++ )
  102. {
  103. m_szConsoleText[ nCount ] = m_szConsoleText[ nCount + 1 ];
  104. Echo( m_szConsoleText + nCount, 1 );
  105. }
  106. Echo( " " );
  107. nCount = m_nConsoleTextLen;
  108. while ( nCount >= m_nCursorPosition )
  109. {
  110. Echo( "\b" );
  111. nCount--;
  112. }
  113. m_nBrowseLine = m_nInputLine;
  114. }
  115. void CTextConsole::ReceiveTab( void )
  116. {
  117. CUtlVector<char *> matches;
  118. //if ( !m_System )
  119. // return;
  120. m_szConsoleText[ m_nConsoleTextLen ] = 0;
  121. //m_System->GetCommandMatches( m_szConsoleText, &matches );
  122. if ( matches.Count() == 0 )
  123. {
  124. return;
  125. }
  126. if ( matches.Count() == 1 )
  127. {
  128. char * pszCmdName;
  129. char * pszRest;
  130. pszCmdName = matches[0];
  131. pszRest = pszCmdName + strlen( m_szConsoleText );
  132. if ( pszRest )
  133. {
  134. Echo( pszRest );
  135. strcat( m_szConsoleText, pszRest );
  136. m_nConsoleTextLen += strlen( pszRest );
  137. Echo( " " );
  138. strcat( m_szConsoleText, " " );
  139. m_nConsoleTextLen++;
  140. }
  141. }
  142. else
  143. {
  144. int nLongestCmd;
  145. int nTotalColumns;
  146. int nCurrentColumn;
  147. char * pszCurrentCmd;
  148. int i = 0;
  149. nLongestCmd = 0;
  150. pszCurrentCmd = matches[0];
  151. while ( pszCurrentCmd )
  152. {
  153. if ( (int)strlen( pszCurrentCmd) > nLongestCmd )
  154. {
  155. nLongestCmd = strlen( pszCurrentCmd);
  156. }
  157. i++;
  158. pszCurrentCmd = (char *)matches[i];
  159. }
  160. nTotalColumns = ( GetWidth() - 1 ) / ( nLongestCmd + 1 );
  161. nCurrentColumn = 0;
  162. Echo( "\n" );
  163. // Would be nice if these were sorted, but not that big a deal
  164. pszCurrentCmd = matches[0];
  165. i = 0;
  166. while ( pszCurrentCmd )
  167. {
  168. char szFormatCmd[ 256 ];
  169. nCurrentColumn++;
  170. if ( nCurrentColumn > nTotalColumns )
  171. {
  172. Echo( "\n" );
  173. nCurrentColumn = 1;
  174. }
  175. Q_snprintf( szFormatCmd, sizeof(szFormatCmd), "%-*s ", nLongestCmd, pszCurrentCmd );
  176. Echo( szFormatCmd );
  177. i++;
  178. pszCurrentCmd = matches[i];
  179. }
  180. Echo( "\n" );
  181. Echo( m_szConsoleText );
  182. // TODO: Tack on 'common' chars in all the matches, i.e. if I typed 'con' and all the
  183. // matches begin with 'connect_' then print the matches but also complete the
  184. // command up to that point at least.
  185. }
  186. m_nCursorPosition = m_nConsoleTextLen;
  187. m_nBrowseLine = m_nInputLine;
  188. }
  189. void CTextConsole::ReceiveStandardChar( const char ch )
  190. {
  191. int nCount;
  192. // If the line buffer is maxed out, ignore this char
  193. if ( m_nConsoleTextLen >= ( sizeof( m_szConsoleText ) - 2 ) )
  194. {
  195. return;
  196. }
  197. nCount = m_nConsoleTextLen;
  198. while ( nCount > m_nCursorPosition )
  199. {
  200. m_szConsoleText[ nCount ] = m_szConsoleText[ nCount - 1 ];
  201. nCount--;
  202. }
  203. m_szConsoleText[ m_nCursorPosition ] = ch;
  204. Echo( m_szConsoleText + m_nCursorPosition, m_nConsoleTextLen - m_nCursorPosition + 1 );
  205. m_nConsoleTextLen++;
  206. m_nCursorPosition++;
  207. nCount = m_nConsoleTextLen;
  208. while ( nCount > m_nCursorPosition )
  209. {
  210. Echo( "\b" );
  211. nCount--;
  212. }
  213. m_nBrowseLine = m_nInputLine;
  214. }
  215. void CTextConsole::ReceiveUpArrow( void )
  216. {
  217. int nLastCommandInHistory;
  218. nLastCommandInHistory = m_nInputLine + 1;
  219. if ( nLastCommandInHistory > m_nTotalLines )
  220. {
  221. nLastCommandInHistory = 0;
  222. }
  223. if ( m_nBrowseLine == nLastCommandInHistory )
  224. {
  225. return;
  226. }
  227. if ( m_nBrowseLine == m_nInputLine )
  228. {
  229. if ( m_nConsoleTextLen > 0 )
  230. {
  231. // Save off current text
  232. strncpy( m_szSavedConsoleText, m_szConsoleText, m_nConsoleTextLen );
  233. // No terminator, it's a raw buffer we always know the length of
  234. }
  235. m_nSavedConsoleTextLen = m_nConsoleTextLen;
  236. }
  237. m_nBrowseLine--;
  238. if ( m_nBrowseLine < 0 )
  239. {
  240. m_nBrowseLine = m_nTotalLines - 1;
  241. }
  242. while ( m_nConsoleTextLen-- ) // delete old line
  243. {
  244. Echo( "\b \b" );
  245. }
  246. // copy buffered line
  247. Echo( m_aszLineBuffer[ m_nBrowseLine ] );
  248. strncpy( m_szConsoleText, m_aszLineBuffer[ m_nBrowseLine ], MAX_CONSOLE_TEXTLEN );
  249. m_nConsoleTextLen = strlen( m_aszLineBuffer[ m_nBrowseLine ] );
  250. m_nCursorPosition = m_nConsoleTextLen;
  251. }
  252. void CTextConsole::ReceiveDownArrow( void )
  253. {
  254. if ( m_nBrowseLine == m_nInputLine )
  255. {
  256. return;
  257. }
  258. m_nBrowseLine++;
  259. if ( m_nBrowseLine > m_nTotalLines )
  260. {
  261. m_nBrowseLine = 0;
  262. }
  263. while ( m_nConsoleTextLen-- ) // delete old line
  264. {
  265. Echo( "\b \b" );
  266. }
  267. if ( m_nBrowseLine == m_nInputLine )
  268. {
  269. if ( m_nSavedConsoleTextLen > 0 )
  270. {
  271. // Restore current text
  272. strncpy( m_szConsoleText, m_szSavedConsoleText, m_nSavedConsoleTextLen );
  273. // No terminator, it's a raw buffer we always know the length of
  274. Echo( m_szConsoleText, m_nSavedConsoleTextLen );
  275. }
  276. m_nConsoleTextLen = m_nSavedConsoleTextLen;
  277. }
  278. else
  279. {
  280. // copy buffered line
  281. Echo( m_aszLineBuffer[ m_nBrowseLine ] );
  282. strncpy( m_szConsoleText, m_aszLineBuffer[ m_nBrowseLine ], MAX_CONSOLE_TEXTLEN );
  283. m_nConsoleTextLen = strlen( m_aszLineBuffer[ m_nBrowseLine ] );
  284. }
  285. m_nCursorPosition = m_nConsoleTextLen;
  286. }
  287. void CTextConsole::ReceiveLeftArrow( void )
  288. {
  289. if ( m_nCursorPosition == 0 )
  290. {
  291. return;
  292. }
  293. Echo( "\b" );
  294. m_nCursorPosition--;
  295. }
  296. void CTextConsole::ReceiveRightArrow( void )
  297. {
  298. if ( m_nCursorPosition == m_nConsoleTextLen )
  299. {
  300. return;
  301. }
  302. Echo( m_szConsoleText + m_nCursorPosition, 1 );
  303. m_nCursorPosition++;
  304. }