1
- use bon:: Builder ;
1
+ use bon:: { builder , Builder } ;
2
2
use eyre:: Result ;
3
3
use ndarray:: { s, ArrayViewMut2 } ;
4
4
use ratatui:: { buffer:: Buffer , layout:: Rect , Frame } ;
@@ -46,29 +46,44 @@ impl Widget for Animated {
46
46
}
47
47
}
48
48
49
- /// Wipes content in from right to left, uses `previous` as the source and then
49
+ #[ derive( Clone , Default ) ]
50
+ pub enum Start {
51
+ Left ,
52
+ #[ default]
53
+ Right ,
54
+ }
55
+
56
+ /// Wipes content in from the start edge, uses `previous` as the source and then
50
57
/// the current buffer as the destination.
51
58
#[ derive( Builder , Clone ) ]
52
- pub struct Slider {
59
+ pub struct Wipe {
53
60
#[ builder( into) ]
54
61
timer : EffectTimer ,
62
+ #[ builder( default ) ]
63
+ start : Start ,
55
64
previous : Buffer ,
56
65
#[ builder( default ) ]
57
66
done : bool ,
58
67
}
59
68
60
- pub fn right_to_left < T : Into < EffectTimer > > ( timer : T , previous : Buffer ) -> Effect {
69
+ #[ builder]
70
+ pub fn horizontal_wipe < T : Into < EffectTimer > > (
71
+ timer : T ,
72
+ buffer : Buffer ,
73
+ #[ builder( default ) ] start : Start ,
74
+ ) -> Effect {
61
75
Effect :: new (
62
- Slider :: builder ( )
76
+ Wipe :: builder ( )
63
77
. timer ( timer. into ( ) )
64
- . previous ( previous)
78
+ . previous ( buffer)
79
+ . start ( start)
65
80
. build ( ) ,
66
81
)
67
82
}
68
83
69
84
// This assumes that the area from the original buffer to the new one doesn't
70
85
// change. If it is unable to create a slice correctly, it'll just give up.
71
- impl Shader for Slider {
86
+ impl Shader for Wipe {
72
87
fn name ( & self ) -> & ' static str {
73
88
"slider"
74
89
}
@@ -87,36 +102,40 @@ impl Shader for Slider {
87
102
let y = area. y as usize ;
88
103
let width = area. width as usize ;
89
104
let height = area. height as usize ;
105
+ let buffer_width = self . previous . area . width as usize ;
106
+ let buffer_height = self . previous . area . height as usize ;
90
107
91
108
let window = ( ( width as f32 * alpha) . round ( ) as usize ) . clamp ( 0 , width) ;
92
109
93
110
if window == width {
94
111
self . done = true ;
95
112
}
96
113
97
- let Ok ( previous) = ArrayViewMut2 :: from_shape (
98
- ( area. height as usize , area. width as usize ) ,
99
- & mut self . previous . content ,
100
- ) else {
114
+ let Ok ( previous) =
115
+ ArrayViewMut2 :: from_shape ( ( buffer_height, buffer_width) , & mut self . previous . content )
116
+ else {
101
117
tracing:: debug!( area = ?area, "unable to create view from previous buffer" ) ;
102
118
103
119
self . done = true ;
104
120
return overflow;
105
121
} ;
106
122
107
- let Ok ( mut next) = ArrayViewMut2 :: from_shape (
108
- ( area. height as usize , area. width as usize ) ,
109
- & mut buf. content ,
110
- ) else {
123
+ let Ok ( mut next) =
124
+ ArrayViewMut2 :: from_shape ( ( buffer_height, buffer_width) , & mut buf. content )
125
+ else {
111
126
tracing:: debug!( area = ?area, "unable to create view from next buffer" ) ;
112
127
113
128
self . done = true ;
114
129
return overflow;
115
130
} ;
116
131
117
- let slice = s ! [ y..y + height, x..x + width - window] ;
132
+ let slice = match self . start {
133
+ Start :: Left => s ! [ y..height, x + window..x + width] ,
134
+ Start :: Right => s ! [ y..y + height, x..x + width - window] ,
135
+ } ;
118
136
119
137
let previous_section = previous. slice ( slice) ;
138
+
120
139
next. slice_mut ( slice) . assign ( & previous_section) ;
121
140
122
141
overflow
0 commit comments