ansi_port.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * @file ansi_port.c
  3. * @author Ji Youzhou
  4. * @version V0.1
  5. * @date 28 Oct 2019
  6. * @brief [brief]
  7. * *****************************************************************************
  8. * @attention
  9. *
  10. * MIT License
  11. *
  12. * Copyright (C) 2019 Ji Youzhou. or its affiliates. All Rights Reserved.
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this software and associated documentation files (the "Software"), to deal
  16. * in the Software without restriction, including without limitation the rights
  17. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. * copies of the Software, and to permit persons to whom the Software is
  19. * furnished to do so, subject to the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in all
  22. * copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. /* Includes ------------------------------------------------------------------*/
  33. #include "ansi_port.h"
  34. #include "ansi.h"
  35. #include <stdio.h>
  36. #include "nr_micro_shell.h"
  37. #include <string.h>
  38. // show string
  39. void ansi_show_str(char *str, unsigned int len)
  40. {
  41. unsigned int i;
  42. for (i = 0; i < len; i++)
  43. {
  44. ansi_show_char(str[i]);
  45. }
  46. }
  47. void nr_ansi_common_char_slover(ansi_st *ansi,char x)
  48. {
  49. unsigned int i;
  50. if (ansi->counter < NR_ANSI_LINE_SIZE - 2)
  51. {
  52. if (ansi->p < ansi->counter)
  53. {
  54. for (i = ansi->counter; i > ansi->p; i--)
  55. {
  56. ansi->current_line[i] = ansi->current_line[i - 1];
  57. }
  58. }
  59. ansi->p++;
  60. ansi->counter++;
  61. ansi->current_line[ansi->p] = x;
  62. ansi->current_line[ansi->counter] = '\0';
  63. if(ansi->p+1 < ansi->counter)
  64. {
  65. shell_printf("\033[1@");
  66. }
  67. #ifndef NR_MICRO_SHELL_SIMULATOR
  68. ansi_show_char(x);
  69. #endif
  70. }
  71. else
  72. {
  73. ansi->counter = NR_ANSI_LINE_SIZE - 3;
  74. if (ansi->p >= ansi->counter)
  75. {
  76. ansi->p = ansi->counter - 1;
  77. }
  78. ansi->current_line[ansi->counter] = '\0';
  79. }
  80. }
  81. void nr_ansi_ctrl_common_slover(ansi_st *ansi)
  82. {
  83. unsigned int i;
  84. for (i = 0; i < ansi->cmd_num; i++)
  85. {
  86. ansi_show_char((*(ansi->combine_buf + i)));
  87. }
  88. }
  89. // line break '\r' processing
  90. void nr_ansi_in_enter(ansi_st *ansi)
  91. {
  92. #if NR_SHELL_END_OF_LINE == 1
  93. ansi->p = -1;
  94. ansi->counter = 0;
  95. nr_shell.cmd_his.index = nr_shell.cmd_his.len;
  96. ansi_show_char('\r');
  97. ansi_show_char('\n');
  98. #else
  99. ansi_show_char('\r');
  100. #endif
  101. }
  102. // line break '\n' processing
  103. void nr_ansi_in_newline(ansi_st *ansi)
  104. {
  105. ansi->p = -1;
  106. ansi->counter = 0;
  107. nr_shell.cmd_his.index = nr_shell.cmd_his.len;
  108. #ifndef NR_MICRO_SHELL_SIMULATOR
  109. #if NR_SHELL_END_OF_LINE != 1
  110. ansi_show_char('\r');
  111. ansi_show_char('\n');
  112. #else
  113. ansi_show_char('\n');
  114. #endif
  115. #endif
  116. }
  117. // Backspace '\b' processing
  118. void nr_ansi_in_backspace(ansi_st *ansi)
  119. {
  120. unsigned int i;
  121. if (ansi->p >= 0)
  122. {
  123. for (i = ansi->p; i < ansi->counter; i++)
  124. {
  125. ansi->current_line[i] = ansi->current_line[i + 1];
  126. }
  127. ansi->p--;
  128. ansi->counter--;
  129. ansi_show_char('\b');
  130. #if NR_SHLL_FULL_ANSI == 1
  131. shell_printf(NR_ANSI_CLR_R_MV_L_NCHAR(1));
  132. #endif
  133. }
  134. }
  135. // up key processing
  136. void nr_ansi_in_up(ansi_st *ansi)
  137. {
  138. if (nr_shell.cmd_his.index > 0)
  139. {
  140. #if NR_SHLL_FULL_ANSI == 1
  141. shell_printf("\033[%dD", ansi->p + 1);
  142. shell_printf(NR_ANSI_CLEAR_RIGHT);
  143. #else
  144. shell_printf("\r\n");
  145. shell_printf(nr_shell.user_name);
  146. #endif
  147. shell_his_copy_queue_item(&nr_shell.cmd_his, nr_shell.cmd_his.index, ansi->current_line);
  148. ansi->counter = strlen(ansi->current_line);
  149. ansi->p = ansi->counter - 1;
  150. ansi_show_str(ansi->current_line, ansi->counter);
  151. nr_shell.cmd_his.index--;
  152. nr_shell.cmd_his.index = (nr_shell.cmd_his.index == 0) ? nr_shell.cmd_his.len : nr_shell.cmd_his.index;
  153. }
  154. }
  155. // down key processing
  156. void nr_ansi_in_down(ansi_st *ansi)
  157. {
  158. if (nr_shell.cmd_his.index > 0)
  159. {
  160. #if NR_SHLL_FULL_ANSI == 1
  161. shell_printf("\033[%dD", ansi->p + 1);
  162. shell_printf(NR_ANSI_CLEAR_RIGHT);
  163. #else
  164. shell_printf("\r\n");
  165. shell_printf(nr_shell.user_name);
  166. #endif
  167. shell_his_copy_queue_item(&nr_shell.cmd_his, nr_shell.cmd_his.index, ansi->current_line);
  168. ansi->counter = strlen(ansi->current_line);
  169. ansi->p = ansi->counter - 1;
  170. ansi_show_str(ansi->current_line, ansi->counter);
  171. nr_shell.cmd_his.index++;
  172. nr_shell.cmd_his.index = (nr_shell.cmd_his.index > nr_shell.cmd_his.len) ? 1 : nr_shell.cmd_his.index;
  173. }
  174. }
  175. // left key <- processing
  176. void nr_ansi_in_left(ansi_st *ansi)
  177. {
  178. if (ansi->p > -1)
  179. {
  180. ansi->p--;
  181. #if NR_SHLL_FULL_ANSI == 1
  182. shell_printf("\033[1D");
  183. #endif
  184. }
  185. }
  186. // right key <- processing
  187. void nr_ansi_in_right(ansi_st *ansi)
  188. {
  189. if (ansi->p < (int)(ansi->counter - 1))
  190. {
  191. ansi->p++;
  192. #if NR_SHLL_FULL_ANSI == 1
  193. shell_printf("\033[1C");
  194. #endif
  195. }
  196. }
  197. // tab key processing
  198. void nr_ansi_in_tab(ansi_st *ansi)
  199. {
  200. unsigned char i;
  201. char *cmd;
  202. cmd = shell_cmd_complete(&nr_shell, ansi->current_line);
  203. if (cmd != NULL)
  204. {
  205. if (ansi->counter == 0)
  206. {
  207. shell_printf("\r\n");
  208. for (i = 0; nr_shell.static_cmd[i].fp != NULL; i++)
  209. {
  210. shell_printf("%s",nr_shell.static_cmd[i].cmd);
  211. shell_printf("\r\n");
  212. }
  213. shell_printf("%s",nr_shell.user_name);
  214. }
  215. else
  216. {
  217. #if NR_SHLL_FULL_ANSI == 1
  218. shell_printf("\033[%dD", ansi->p + 1);
  219. shell_printf(NR_ANSI_CLEAR_RIGHT);
  220. #else
  221. shell_printf("\r\n");
  222. shell_printf("%s",nr_shell.user_name);
  223. #endif
  224. ansi->counter = strlen(cmd);
  225. ansi->p = ansi->counter - 1;
  226. strcpy(ansi->current_line, cmd);
  227. ansi_show_str(ansi->current_line, ansi->counter);
  228. }
  229. }
  230. }
  231. /*ansi delete*/
  232. void nr_ansi_in__(ansi_st *ansi)
  233. {
  234. unsigned int i;
  235. if(ansi->combine_buf[2] == '3')
  236. {
  237. for(i = ansi->p+1;i<ansi->counter;i++)
  238. {
  239. ansi->current_line[i] = ansi->current_line[i+1];
  240. }
  241. if((short)ansi->counter > ansi->p)
  242. {
  243. ansi->counter--;
  244. #if NR_SHLL_FULL_ANSI == 1
  245. ansi_show_str("\033[1P",4);
  246. #endif
  247. }
  248. }
  249. }
  250. /******************* (C) COPYRIGHT 2019 Ji Youzhou *****END OF FILE*****************/