Skip to content

Commit df3e1c9

Browse files
committed
Horizontal scroll for source, context and stack
1 parent 283c43c commit df3e1c9

File tree

11 files changed

+66
-53
lines changed

11 files changed

+66
-53
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
main
55
----
66

7+
- Horizontal scrolling
78
- Improved property rendering
89
- Introduced themes (including solarized and solarized dark)
910
- Show value of variables on current line

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ Prefix with number to repeat:
3434
- `J` scroll down 10
3535
- `k` scroll up
3636
- `K` scroll up 10
37+
- `h` scroll left
38+
- `H` scroll left 10
39+
- `l` scroll right
40+
- `L` scroll right 10
3741
- `+` increase context depth
3842
- `-` decrease context depth
3943
- `tab` switch pane

src/app.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -344,22 +344,13 @@ impl App {
344344
).await?;
345345
},
346346
AppEvent::ScrollSource(amount) => {
347-
self.session_view.source_scroll = Some(self
348-
.session_view
349-
.source_scroll.unwrap_or(0)
350-
.saturating_add(amount * self.take_motion() as i16));
347+
self.session_view.source_scroll = apply_scroll(self.session_view.source_scroll, amount, self.take_motion() as i16);
351348
}
352349
AppEvent::ScrollContext(amount) => {
353-
self.session_view.context_scroll = self
354-
.session_view
355-
.context_scroll
356-
.saturating_add_signed(amount * self.take_motion() as i16);
350+
self.session_view.context_scroll = apply_scroll(self.session_view.context_scroll, amount, self.take_motion() as i16);
357351
}
358352
AppEvent::ScrollStack(amount) => {
359-
self.session_view.stack_scroll = self
360-
.session_view
361-
.stack_scroll
362-
.saturating_add_signed(amount * self.take_motion() as i16);
353+
self.session_view.stack_scroll = apply_scroll(self.session_view.stack_scroll, amount, self.take_motion() as i16);
363354
}
364355
AppEvent::ToggleFullscreen => {
365356
self.session_view.full_screen = !self.session_view.full_screen;
@@ -522,3 +513,10 @@ impl App {
522513
self.theme.scheme()
523514
}
524515
}
516+
517+
fn apply_scroll(scroll: (u16, u16), amount: (i16, i16), motion: i16) -> (u16, u16) {
518+
(
519+
(scroll.0 as i16).saturating_add(amount.0 * motion).max(0) as u16,
520+
(scroll.1 as i16).saturating_add(amount.1 * motion).max(0) as u16,
521+
)
522+
}

src/event/input.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use tokio::sync::mpsc::Sender;
1414
use crate::app::CurrentView;
1515
use crate::dbgp::client::ContinuationStatus;
1616
use crate::view::session::SessionViewMode;
17+
use crate::view::Scroll;
1718

1819
#[derive(Debug)]
1920
pub enum AppEvent {
@@ -37,12 +38,11 @@ pub enum AppEvent {
3738
UpdateSourceContext(String, String, u32),
3839
UpdateStatus(ContinuationStatus),
3940
NextPane,
40-
ScrollDown(i16),
41-
ScrollUp(i16),
42-
ScrollSource(i16),
43-
ScrollContext(i16),
41+
Scroll(Scroll),
42+
ScrollSource(Scroll),
43+
ScrollContext(Scroll),
44+
ScrollStack(Scroll),
4445
ToggleFullscreen,
45-
ScrollStack(i16),
4646
PushInputPlurality(char),
4747
ContextDepth(i8),
4848
NextTheme,

src/theme.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Theme {
3030
scheme.widget_mode_debug = scheme.widget_mode_debug.fg(Solarized::Base03.to_color()).bg(Solarized::Base3.to_color());
3131
scheme.widget_mode_history = scheme.widget_mode_debug.bg(Solarized::Base3.to_color()).fg(Solarized::Red.to_color());
3232

33-
return scheme;
33+
scheme
3434
}
3535
Theme::SolarizedDark => Scheme{
3636
syntax_variable: Style::default().fg(Solarized::Base00.to_color()),

src/view/context.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@ use ratatui::layout::Rect;
88
use ratatui::text::Line;
99
use ratatui::text::Span;
1010
use ratatui::widgets::Paragraph;
11-
use ratatui::widgets::Wrap;
1211
use ratatui::Frame;
1312

1413
pub struct ContextComponent {}
1514

1615
impl View for ContextComponent {
1716
fn handle(_app: &App, event: AppEvent) -> Option<AppEvent> {
1817
match event {
19-
AppEvent::ScrollDown(amount) => Some(AppEvent::ScrollContext(amount)),
20-
AppEvent::ScrollUp(amount) => Some(AppEvent::ScrollContext(-amount)),
18+
AppEvent::Scroll(scroll) => Some(AppEvent::ScrollContext(scroll)),
2119
_ => None,
2220
}
2321
}
@@ -31,9 +29,8 @@ impl View for ContextComponent {
3129
draw_properties(&app.theme(), &context.properties, &mut lines, 0);
3230

3331
frame.render_widget(
34-
Paragraph::new(lines)
35-
.wrap(Wrap { trim: false })
36-
.scroll((app.session_view.context_scroll, 0)),
32+
Paragraph::new(lines).scroll(app.session_view.context_scroll),
33+
3734
area,
3835
);
3936
}

src/view/help.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ Key mappings (prefix with number to repeat):
4343
[J] scroll down 10
4444
[k] scroll up
4545
[K] scroll up 10
46+
[h] scroll left
47+
[H] scroll left 10
48+
[l] scroll right
49+
[L] scroll right 10
4650
[+] increase context depth
4751
[-] decrease context depth
4852
[t] rotate the theme

src/view/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ pub struct Pane {
2929
pub component_type: ComponentType,
3030
pub constraint: Constraint,
3131
}
32+
33+
pub type Scroll = (i16,i16);

src/view/session.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ impl View for SessionView {
2929
KeyCode::Tab => return Some(AppEvent::NextPane),
3030
KeyCode::Enter => return Some(AppEvent::ToggleFullscreen),
3131
KeyCode::Char(char) => match char {
32-
'j' => return Some(AppEvent::ScrollDown(1)),
33-
'k' => return Some(AppEvent::ScrollUp(1)),
34-
'J' => return Some(AppEvent::ScrollDown(10)),
35-
'K' => return Some(AppEvent::ScrollUp(10)),
32+
'j' => return Some(AppEvent::Scroll((1, 0))),
33+
'k' => return Some(AppEvent::Scroll((-1, 0))),
34+
'J' => return Some(AppEvent::Scroll((10, 0))),
35+
'K' => return Some(AppEvent::Scroll((-10, 0))),
36+
'l' => return Some(AppEvent::Scroll((0, 1))),
37+
'L' => return Some(AppEvent::Scroll((0, 10))),
38+
'h' => return Some(AppEvent::Scroll((0, -1))),
39+
'H' => return Some(AppEvent::Scroll((0, -10))),
3640
'0'..='9' => return Some(AppEvent::PushInputPlurality(char)),
3741
_ => (),
3842
},
@@ -74,7 +78,13 @@ impl View for SessionView {
7478

7579
fn draw(app: &App, frame: &mut Frame, area: ratatui::prelude::Rect) {
7680
if app.session_view.full_screen {
77-
build_pane_widget(frame, app, app.session_view.current_pane(), area, app.session_view.current_pane);
81+
build_pane_widget(
82+
frame,
83+
app,
84+
app.session_view.current_pane(),
85+
area,
86+
app.session_view.current_pane,
87+
);
7888
return;
7989
}
8090

@@ -122,12 +132,10 @@ fn build_pane_widget(frame: &mut Frame, app: &App, pane: &Pane, area: Rect, inde
122132
ComponentType::Context => format!("Context({})", app.context_depth),
123133
ComponentType::Stack => "Stack".to_string(),
124134
})
125-
.style(
126-
match index == app.session_view.current_pane {
127-
true => app.theme().pane_border_active,
128-
false => app.theme().pane_border_inactive,
129-
}
130-
);
135+
.style(match index == app.session_view.current_pane {
136+
true => app.theme().pane_border_active,
137+
false => app.theme().pane_border_inactive,
138+
});
131139

132140
frame.render_widget(&block, area);
133141

@@ -146,9 +154,9 @@ fn build_pane_widget(frame: &mut Frame, app: &App, pane: &Pane, area: Rect, inde
146154

147155
pub struct SessionViewState {
148156
pub full_screen: bool,
149-
pub source_scroll: Option<i16>,
150-
pub context_scroll: u16,
151-
pub stack_scroll: u16,
157+
pub source_scroll: (u16, u16),
158+
pub context_scroll: (u16, u16),
159+
pub stack_scroll: (u16, u16),
152160
pub mode: SessionViewMode,
153161
pub panes: Vec<Pane>,
154162
pub current_pane: usize,
@@ -164,9 +172,9 @@ impl SessionViewState {
164172
pub fn new() -> Self {
165173
Self {
166174
full_screen: false,
167-
source_scroll: None,
168-
context_scroll: 0,
169-
stack_scroll: 0,
175+
source_scroll: (0, 0),
176+
context_scroll: (0, 0),
177+
stack_scroll: (0, 0),
170178
current_pane: 0,
171179
mode: SessionViewMode::Current,
172180
panes: vec![
@@ -196,8 +204,9 @@ impl SessionViewState {
196204
}
197205

198206
pub(crate) fn reset(&mut self) {
199-
self.context_scroll = 0;
200-
self.source_scroll = None;
207+
self.context_scroll = (0, 0);
208+
self.stack_scroll = (0, 0);
209+
self.source_scroll = (0, 0);
201210
}
202211
}
203212

src/view/source.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ pub struct SourceComponent {}
1717
impl View for SourceComponent {
1818
fn handle(_: &App, event: AppEvent) -> Option<AppEvent> {
1919
match event {
20-
AppEvent::ScrollDown(amount) => Some(AppEvent::ScrollSource(amount)),
21-
AppEvent::ScrollUp(amount) => Some(AppEvent::ScrollSource(-amount)),
20+
AppEvent::Scroll(amount) => Some(AppEvent::ScrollSource(amount)),
2221
_ => None,
2322
}
2423
}
@@ -86,17 +85,17 @@ impl View for SourceComponent {
8685
let center = (history_entry.source.line_no as u16)
8786
.saturating_sub(area.height.div_ceil(2)) as i16;
8887
center
89-
.saturating_add(app.session_view.source_scroll.unwrap_or(0))
88+
.saturating_add(app.session_view.source_scroll.0 as i16)
9089
.max(0) as u16
9190
} else {
92-
app.session_view.source_scroll.unwrap_or(0).max(0) as u16
91+
app.session_view.source_scroll.0
9392
};
9493

95-
frame.render_widget(Paragraph::new(lines.clone()).scroll((scroll, 0)), rows[0]);
94+
frame.render_widget(Paragraph::new(lines.clone()).scroll((scroll, app.session_view.source_scroll.1)), rows[0]);
9695

9796
for (line_no, line_length, line) in annotations {
9897
let position = Position {
99-
x: line_length as u16,
98+
x: (line_length as u16).saturating_sub(app.session_view.source_scroll.1),
10099
y: (line_no as u32).saturating_sub(scroll as u32) as u16 + 1,
101100
};
102101
if !rows[0].contains(position) {
@@ -137,7 +136,7 @@ fn render_label(property: &Property) -> Option<String> {
137136
PropertyType::Float => property.value.clone().unwrap_or("".to_string()),
138137
PropertyType::String => format!("\"{}\"", property.value.clone().unwrap_or("".to_string())),
139138
PropertyType::Null => String::from("null"),
140-
PropertyType::Resource => String::from(property.value.clone().unwrap_or("".to_string())),
139+
PropertyType::Resource => property.value.clone().unwrap_or("".to_string()),
141140
PropertyType::Undefined => String::from("undefined"),
142141
})
143142
}

0 commit comments

Comments
 (0)