-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.R
111 lines (94 loc) · 2.38 KB
/
read.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#' Read screen
#'
#' @param session A rexpect_session.
#' @param ... Other arguments passed on to `tmuxr::capture_pane`.
#'
#' @export
read_screen <- function(session, ...) {
tmuxr::capture_pane(session, ...)
}
#' Read last line
#'
#' @param session A rexpect_session.
#'
#' @export
read_last_line <- function(session) {
n <- as.numeric(tmuxr::prop(session, "cursor_y"))
tmuxr::capture_pane(session, start = n, end = n)
}
#' Number of lines
#'
#' Includes the current line.
#'
#' @param x A rexpect_session.
#'
#' @return An integer.
#'
#' @export
length.rexpect_session <- function(x) {
session <- x
on_screen <- cursor(session)[2]
off_screen <- as.numeric(tmuxr::prop(session, "history_size"))
on_screen + off_screen
}
#' Get cursor position on screen
#'
#' Starts at top-left corner (1, 1) and ends at bottom-right corner
#' (width, height)
#'
#' @param session A rexpect_session.
#'
#' @return A numerical vector with two values.
#'
#' @export
cursor <- function(session) {
x_y <- tmuxr::display_message(session, "#{cursor_x}\n#{cursor_y}")
as.numeric(x_y) + 1 # in tmux, cursor_x and cursor_y start at 0
}
#' Read all output
#'
#' @param session A rexpect_session.
#'
#' @return A vector of strings.
#'
#' @export
read_all <- function(session) {
tmuxr::capture_pane(session, start = "-", end = cursor(session)[2] - 1)
}
#' Read a portion of all output
#'
#' @param session A rexpect_session.
#' @param from,to Integers.
#' @param ... Other arguments passed on to `tmuxr::capture_pane`.
#'
#' @return A vector of strings.
#'
#' @export
read_output <- function(session, from, to, ...) {
off_screen <- as.numeric(tmuxr::prop(session, "history_size"))
start = from - off_screen - 1
end = to - off_screen - 1
tmuxr::capture_pane(session, start = start, end = end, ...)
}
#' @export
`[<-.rexpect_session` <- function(session, ind = NULL, value) {
stop("cannot rewrite history", call. = FALSE)
}
#' @export
`[.rexpect_session` <- function(session, ind = NULL) {
if (is.null(ind)) return(read_all(session))
if (any(ind < 0) && any(ind > 0)) {
stop("only 0's may be mixed with negative subscripts", call. = FALSE)
}
ind <- ind[ind != 0]
if (length(ind) == 0) return(character())
if (all(ind < 0)) {
pos <- 1:length(session)
ind <- pos[ind]
}
from <- min(ind)
to <- max(ind)
lines <- read_output(session, from = from, to = to)
ind <- ind - from + 1
lines[ind]
}