@ -28,6 +28,7 @@ pub struct Prompt {
prompt : Cow < ' static , str > ,
prompt : Cow < ' static , str > ,
line : String ,
line : String ,
cursor : usize ,
cursor : usize ,
anchor : usize ,
completion : Vec < Completion > ,
completion : Vec < Completion > ,
selection : Option < usize > ,
selection : Option < usize > ,
history_register : Option < char > ,
history_register : Option < char > ,
@ -80,6 +81,7 @@ impl Prompt {
prompt ,
prompt ,
line : String ::new ( ) ,
line : String ::new ( ) ,
cursor : 0 ,
cursor : 0 ,
anchor : 0 ,
completion : Vec ::new ( ) ,
completion : Vec ::new ( ) ,
selection : None ,
selection : None ,
history_register ,
history_register ,
@ -329,6 +331,7 @@ impl Prompt {
pub fn clear ( & mut self , editor : & Editor ) {
pub fn clear ( & mut self , editor : & Editor ) {
self . line . clear ( ) ;
self . line . clear ( ) ;
self . cursor = 0 ;
self . cursor = 0 ;
self . recalculate_completion ( editor ) ;
self . recalculate_completion ( editor ) ;
}
}
@ -395,13 +398,14 @@ impl Prompt {
const BASE_WIDTH : u16 = 30 ;
const BASE_WIDTH : u16 = 30 ;
impl Prompt {
impl Prompt {
pub fn render_prompt ( & self , area : Rect , surface : & mut Surface , cx : & mut Context ) {
pub fn render_prompt ( & mut self , area : Rect , surface : & mut Surface , cx : & mut Context ) {
let theme = & cx . editor . theme ;
let theme = & cx . editor . theme ;
let prompt_color = theme . get ( "ui.text" ) ;
let prompt_color = theme . get ( "ui.text" ) ;
let completion_color = theme . get ( "ui.menu" ) ;
let completion_color = theme . get ( "ui.menu" ) ;
let selected_color = theme . get ( "ui.menu.selected" ) ;
let selected_color = theme . get ( "ui.menu.selected" ) ;
let suggestion_color = theme . get ( "ui.text.inactive" ) ;
let suggestion_color = theme . get ( "ui.text.inactive" ) ;
let background = theme . get ( "ui.background" ) ;
let background = theme . get ( "ui.background" ) ;
// completion
// completion
let max_len = self
let max_len = self
@ -500,7 +504,11 @@ impl Prompt {
// render buffer text
// render buffer text
surface . set_string ( area . x , area . y + line , & self . prompt , prompt_color ) ;
surface . set_string ( area . x , area . y + line , & self . prompt , prompt_color ) ;
let line_area = area . clip_left ( self . prompt . len ( ) as u16 ) . clip_top ( line ) ;
let line_area = area
. clip_left ( self . prompt . len ( ) as u16 )
. clip_top ( line )
. clip_right ( 2 ) ;
if self . line . is_empty ( ) {
if self . line . is_empty ( ) {
// Show the most recently entered value as a suggestion.
// Show the most recently entered value as a suggestion.
if let Some ( suggestion ) = self . first_history_completion ( cx . editor ) {
if let Some ( suggestion ) = self . first_history_completion ( cx . editor ) {
@ -517,7 +525,22 @@ impl Prompt {
. into ( ) ;
. into ( ) ;
text . render ( line_area , surface , cx ) ;
text . render ( line_area , surface , cx ) ;
} else {
} else {
surface . set_string ( line_area . x , line_area . y , self . line . clone ( ) , prompt_color ) ;
if self . line . len ( ) < line_area . width as usize {
self . anchor = 0 ;
} else if self . cursor < self . anchor {
self . anchor = self . cursor ;
} else if self . cursor - self . anchor > line_area . width as usize {
self . anchor = self . cursor - line_area . width as usize ;
}
surface . set_string_anchored (
line_area . x ,
line_area . y ,
self . anchor ,
self . line . as_str ( ) ,
line_area . width as usize ,
| _ | prompt_color ,
) ;
}
}
}
}
}
}
@ -687,14 +710,24 @@ impl Component for Prompt {
}
}
fn cursor ( & self , area : Rect , editor : & Editor ) -> ( Option < Position > , CursorKind ) {
fn cursor ( & self , area : Rect , editor : & Editor ) -> ( Option < Position > , CursorKind ) {
let area = area
. clip_left ( self . prompt . len ( ) as u16 )
. clip_right ( if self . prompt . len ( ) > 0 { 0 } else { 2 } ) ;
let mut upbound = area . left ( ) as usize
+ UnicodeWidthStr ::width ( & self . line [ self . anchor .. self . cursor . max ( self . anchor ) ] ) ;
if self . anchor > 0 {
upbound + = 1 ;
}
if self . anchor > 0 & & self . cursor > self . anchor & & self . line . len ( ) > self . cursor {
upbound - = 1 ;
}
let line = area . height as usize - 1 ;
let line = area . height as usize - 1 ;
(
(
Some ( Position ::new (
Some ( Position ::new ( area . y as usize + line , upbound ) ) ,
area . y as usize + line ,
area . x as usize
+ self . prompt . len ( )
+ UnicodeWidthStr ::width ( & self . line [ .. self . cursor ] ) ,
) ) ,
editor . config ( ) . cursor_shape . from_mode ( Mode ::Insert ) ,
editor . config ( ) . cursor_shape . from_mode ( Mode ::Insert ) ,
)
)
}
}