Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tui: Add Ctrl+u/d, Ctrl+e/y keys to move half page and scroll line #1803

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 188 additions & 0 deletions cmds/tui.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
#define KEY_ESCAPE 27
#define BLANK 32

/* ncurses format - octal number */
#define KEY_CTRL_D 0004
#define KEY_CTRL_E 0005
#define KEY_CTRL_U 0025
#define KEY_CTRL_Y 0031

#define TUI_ASSERT(cond) \
do { \
endwin(); \
Expand Down Expand Up @@ -150,6 +156,8 @@ static const char *help[] = {
"ARROW Navigation",
"PgUp/Dn",
"Home/End",
"Ctrl + u/d Move half page up/down",
"Ctrl + e/y Scroll line up/down",
"Enter Fold/unfold graph or Select session",
"G Show (full) call graph",
"g Show call graph for this function",
Expand Down Expand Up @@ -2079,6 +2087,170 @@ static void tui_window_move_end(struct tui_window *win)
}
}

static void tui_window_move_scroll_line_up(struct tui_window *win)
{
void *node = win->ops->next(win, win->top, true);

/* cannot go top next */
if (node == NULL)
return;

/* can go top next */
win->top_index++;
if (win->ops->needs_blank(win, win->top, node))
win->top_index++;

/* update win->top */
win->top = node;

/* update win->curr - out of cursor line case*/
if (win->top_index > win->curr_index) {
win->curr = win->top;
win->curr_index = win->top_index;
}
}

static void tui_window_move_scroll_line_down(struct tui_window *win)
{
void *node = win->ops->prev(win, win->top, true);

/* cannot go top prev */
if (node == NULL)
return;

/* can go top prev */
win->top_index--;
if (win->ops->needs_blank(win, node, win->top)) // (win, prev_node, curr_node)
win->top_index--;

/* update win->top */
win->top = node;

/* update win->curr - out of cursor line case, cursor must follow top in ranges */
while (win->curr_index - win->top_index >= LINES - 2) {
/* find prev node */
node = win->ops->prev(win, win->curr, false);
win->curr_index--;

/* blank check */
if (win->ops->needs_blank(win, node, win->curr))
win->curr_index--;

/* update win->curr */
win->curr = node;
}
}
Comment on lines +2090 to +2142
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they have very similar code structure. So it'd be better to have a common function and use it in both functions. The common function name could be tui_window_move_scroll_line.

The same common function can be used for tui_window_move_halfpage_up and tui_window_move_halfpage_down as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they have very similar code structure. So it'd be better to have a common function and use it in both functions. The common function name could be tui_window_move_scroll_line.

The same common function can be used for tui_window_move_halfpage_up and tui_window_move_halfpage_down as well.

Thank you for reviewing my code !! you are right. I think so, too. In fact, I found that vim is also using common function for directions, and they manage directions like up and down by using function arguments field.

However, In uftrace, It seems that all command functions use only one argument, struct tui_window *win, So I didn't want to break that implicit structure rules. But I can edit it if you want.


static void tui_window_move_halfpage_up(struct tui_window *win)
{
void *node;
int move_cnt = 0;

/* move win->top */
while (move_cnt < (LINES - 2) / 2 - 1) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(LINES - 2) / 2 - 1 can be assigned to a meaningful name at the top of this function.

/* find prev node */
node = win->ops->prev(win, win->top, true);

/* cannot go top prev */
if (node == NULL)
break;

/* can go top prev */
win->top_index--;
move_cnt++;

/* blank check */
if (win->ops->needs_blank(win, node, win->top)) {
win->top_index--;
move_cnt++;
}

/* update win->top */
win->top = node;
}

/* init move_cnt */
move_cnt = 0;

/* move win->curr */
while (move_cnt < (LINES - 2) / 2 - 1) {
/* find prev node */
node = win->ops->prev(win, win->curr, false);

/* cannot go curr prev */
if (node == NULL)
break;

/* can go curr prev */
win->curr_index--;
move_cnt++;

/* blank check */
if (win->ops->needs_blank(win, node, win->curr)) {
win->curr_index--;
move_cnt++;
}

/* update win->curr */
win->curr = node;
}
}

static void tui_window_move_halfpage_down(struct tui_window *win)
{
void *node;
int move_cnt = 0;

/* move win->top */
while (move_cnt < (LINES - 2) / 2 - 1) {
/* find next node */
node = win->ops->next(win, win->top, true);

/* cannot go top next */
if (node == NULL)
break;

/* can go top next */
win->top_index++;
move_cnt++;

/* blank check */
if (win->ops->needs_blank(win, win->top, node)) {
win->top_index++;
move_cnt++;
}

/* update win->top */
win->top = node;
}

/* init move_cnt */
move_cnt = 0;

/* move win->curr */
while (move_cnt < (LINES - 2) / 2 - 1) {
/* find next node */
node = win->ops->next(win, win->curr, false);

/* cannot go curr next */
if (node == NULL)
break;

/* can go curr next */
win->curr_index++;
move_cnt++;

/* blank check */
if (win->ops->needs_blank(win, win->curr, node)) {
win->curr_index++;
move_cnt++;
}

/* update win->curr */
win->curr = node;
}
}

/* move to the previous sibling */
static bool tui_window_move_prev(struct tui_window *win)
{
Expand Down Expand Up @@ -2770,6 +2942,22 @@ static void tui_main_loop(struct uftrace_opts *opts, struct uftrace_data *handle
cancel_search();
tui_window_move_end(win);
break;
case KEY_CTRL_U:
cancel_search();
tui_window_move_halfpage_up(win);
break;
case KEY_CTRL_D:
cancel_search();
tui_window_move_halfpage_down(win);
break;
case KEY_CTRL_E:
cancel_search();
tui_window_move_scroll_line_up(win);
break;
case KEY_CTRL_Y:
cancel_search();
tui_window_move_scroll_line_down(win);
break;
case KEY_ENTER:
case '\n':
full_redraw = tui_window_enter(win, win->curr);
Expand Down
2 changes: 2 additions & 0 deletions doc/ko/uftrace-tui.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ TUI 창에서 다음과 같은 키들을 사용할 수 있음:
* `Up`, `Down`: 커서를 위/아래로 움직임
* `PageUp`, `PageDown`: 페이지를 위/아래로 움직임
* `Home`, `End`: 첫번째/마지막 항목으로 이동
* `Ctrl` + `u`/`d`: 페이지 절반을 위/아래로 움직임
* `Ctrl` + `e`/`y`: 스크롤 라인 위/아래 움직임
* `Enter`: 그래프 접기/펴기 또는 세션 선택
* `G`: 현재 세션의 전체 그래프 창으로 전환
* `g`: 현재 함수의 백트레이스와 부분 호출 그래프 창으로 전환
Expand Down
2 changes: 2 additions & 0 deletions doc/uftrace-tui.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ Following keys can be used in the TUI window:
* `Up`, `Down`: Move cursor up/down
* `PageUp`, `PageDown`: Move page up/down
* `Home`, `End`: Move to the first/last entry
* `Ctrl` + `u`/`d`: Move half page up/down
* `Ctrl` + `e`/`y`: Scroll line up/down
* `Enter`: Fold/unfold graph or Select session
* `G`: Show full graph of the current session
* `g`: Show backtrace and call graph of the current function
Expand Down