@@ -4,13 +4,11 @@ use crate::prelude::*;
44
55pub fn delete_char (
66 input : In < ConsoleActionSystemInput > ,
7- mut console_q : Query < & mut ConsoleInputText > ,
7+ mut console_q : Query < ( & mut ConsoleInputText , & mut ConsoleBuffer ) > ,
88) {
9- if let Ok ( mut input) = console_q. get_mut ( input. console_id ) {
10- let popped = input. text . pop ( ) ;
11- if let Some ( popped) = popped {
12- input. move_cursor ( -( popped. len_utf8 ( ) as isize ) ) ;
13- }
9+ if let Ok ( ( mut input, mut buffer) ) = console_q. get_mut ( input. console_id ) {
10+ let len = buffer. pop_front ( ) . map ( |c| c. len_utf8 ( ) ) . unwrap_or_default ( ) ;
11+ input. move_cursor ( -( len as isize ) ) ;
1412 } else {
1513 error ! (
1614 "Could not delete char from console with id {}" ,
@@ -23,9 +21,10 @@ pub fn delete_word(
2321 mut console_q : Query < & mut ConsoleInputText > ,
2422) {
2523 if let Ok ( mut input) = console_q. get_mut ( input. console_id ) {
26- let last_ws = input. text . rfind ( char:: is_whitespace) . unwrap_or_default ( ) ;
27- input. text . truncate ( last_ws) ;
28- input. set_cursor ( last_ws) ;
24+ // TODO
25+ // let last_ws = input.text.rfind(char::is_whitespace).unwrap_or_default();
26+ // input.text.truncate(last_ws);
27+ // input.set_cursor(last_ws)
2928 } else {
3029 error ! (
3130 "Could not delete word from console with id {}" ,
@@ -39,22 +38,31 @@ pub fn write_char(
3938 mut commands : Commands ,
4039) {
4140 if let Ok ( mut input_text) = console_q. get_mut ( input. console_id ) {
42- let pos = input_text. cursor ( ) ;
4341 for key in input. matched_logical_keys ( ) {
4442 match key {
4543 Key :: Character ( c) => {
46- input_text. text . insert_str ( pos, c. as_str ( ) ) ;
44+ commands. write_message ( ConsoleWriteMsg {
45+ message : c. to_string ( ) ,
46+ console_id : input. console_id ,
47+ pos : input_text. anchor + input_text. cursor ( ) ,
48+ } ) ;
4749 input_text. move_cursor ( c. len ( ) as isize ) ;
4850 }
4951 Key :: Space => {
50- input_text. text . insert ( pos, ' ' ) ;
52+ commands. write_message ( ConsoleWriteMsg {
53+ message : " " . to_string ( ) ,
54+ console_id : input. console_id ,
55+ pos : input_text. anchor + input_text. cursor ( ) ,
56+ } ) ;
5157 input_text. move_cursor ( 1 ) ;
5258 }
5359 Key :: Enter => {
54- if input_text. cursor ( ) == input_text. text . len ( ) {
55- input_text. text . insert ( pos, '\n' ) ;
56- }
57- input_text. text . push ( '\n' ) ;
60+ commands. write_message ( ConsoleWriteMsg {
61+ message : "\n " . to_string ( ) ,
62+ console_id : input. console_id ,
63+ pos : input_text. anchor + input_text. cursor ( ) ,
64+ } ) ;
65+ input_text. move_cursor ( 1 ) ;
5866 }
5967 _ => { }
6068 }
@@ -70,32 +78,32 @@ pub fn write_char(
7078
7179pub fn submit (
7280 input : In < ConsoleActionSystemInput > ,
73- mut query : Query < ( & mut ConsoleInputText , & ConsoleAssetHandle < ConsoleHistory > ) > ,
81+ mut query : Query < (
82+ & mut ConsoleInputText ,
83+ & ConsoleAssetHandle < ConsoleHistory > ,
84+ & ConsoleBuffer ,
85+ ) > ,
7486 mut assets : ResMut < Assets < ConsoleHistory > > ,
7587 mut commands : Commands ,
7688) {
77- if let Ok ( ( mut input_text, history_handle) ) = query. get_mut ( input. console_id ) {
89+ if let Ok ( ( mut input_text, history_handle, buffer ) ) = query. get_mut ( input. console_id ) {
7890 let history = assets. get_mut ( history_handle. as_asset_id ( ) ) ;
7991 if history. is_none ( ) {
8092 error ! ( "Failed to get console history!" ) ;
8193 return ;
8294 }
8395 let history = history. unwrap ( ) ;
84- commands. write_message ( ConsoleWriteMsg {
85- message : "\n " . to_string ( ) ,
86- console_id : input. console_id ,
87- } ) ;
88- if let Some ( event) = SubmitEvent :: new ( input. console_id , input_text. text . clone ( ) ) {
96+ let command = buffer. range ( input_text. anchor , buffer. len ( ) - input_text. anchor ) ;
97+ if let Some ( event) = SubmitEvent :: new ( input. console_id , command. clone ( ) ) {
8998 commands. trigger ( event) ;
9099 } else {
91- commands. write_message ( ConsoleWriteMsg {
92- message : "Invalid shell expression \n " . into ( ) ,
93- console_id : input . console_id ,
94- } ) ;
100+ commands. trigger ( ConsolePrintln :: new (
101+ input . console_id ,
102+ "Invalid shell expression" . to_string ( ) ,
103+ ) ) ;
95104 }
96- let history_value = std:: mem:: take ( & mut input_text. text ) ;
97105 input_text. set_cursor ( 0 ) ;
98- history. push ( history_value ) ;
106+ history. push ( command ) ;
99107 } else {
100108 error ! ( "Could not submit from console with id {}" , input. console_id) ;
101109 }
@@ -110,9 +118,15 @@ fn clear(input: In<ConsoleActionSystemInput>, mut commands: Commands) {
110118 commands. run_system_cached_with ( clear_buffer, input. console_id ) ;
111119}
112120
113- fn clear_input ( input : In < ConsoleActionSystemInput > , mut query : Query < & mut ConsoleInputText > ) {
114- let mut input = query. get_mut ( input. console_id ) . unwrap ( ) ;
115- input. text . clear ( ) ;
121+ fn clear_input (
122+ input : In < ConsoleActionSystemInput > ,
123+ mut query : Query < ( & mut ConsoleInputText , & mut ConsoleBuffer ) > ,
124+ ) {
125+ // TODO: Don't edit buffer directly! Use a message
126+ let ( mut input, mut buffer) = query. get_mut ( input. console_id ) . unwrap ( ) ;
127+ for _ in 0 ..( buffer. len ( ) - input. anchor ) {
128+ buffer. pop_front ( ) ;
129+ }
116130 input. set_cursor ( 0 ) ;
117131}
118132
0 commit comments