File: console.R

package info (click to toggle)
r-cran-igraph 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 27,044 kB
  • sloc: ansic: 204,981; cpp: 21,711; fortran: 4,090; yacc: 1,229; lex: 519; sh: 52; makefile: 8
file content (299 lines) | stat: -rw-r--r-- 9,431 bytes parent folder | download
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

#' The igraph console
#'
#' @description
#' `r lifecycle::badge("deprecated")`
#'
#' `igraph.console()` was renamed to `console()` to create a more
#' consistent API.
#'
#' @keywords internal
#' @export
igraph.console <- function() { # nocov start
  lifecycle::deprecate_soft("2.0.0", "igraph.console()", "console()")
  console()
} # nocov end

#   IGraph R package
#   Copyright (C) 2010-2012  Gabor Csardi <csardi.gabor@gmail.com>
#   334 Harvard street, Cambridge, MA 02139 USA
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc.,  51 Franklin Street, Fifth Floor, Boston, MA
#   02110-1301 USA
#
###################################################################


#' The igraph console
#'
#' The igraph console is a GUI window that shows what the currently running
#' igraph function is doing.
#'
#' The console can be started by calling the `console()` function.
#' Then it stays open, until the user closes it.
#'
#' Another way to start it to set the `verbose` igraph option to
#' \dQuote{tkconsole} via `igraph_options()`. Then the console (re)opens
#' each time an igraph function supporting it starts; to close it, set the
#' `verbose` option to another value.
#'
#' The console is written in Tcl/Tk and required the `tcltk` package.
#'
#' @return `NULL`, invisibly.
#' @author Gabor Csardi \email{csardi.gabor@@gmail.com}
#' @seealso [igraph_options()] and the `verbose` option.
#' @keywords graphs
#' @family console
#' @export
console <- function() {
  oldverb <- igraph_opt("verbose")
  igraph_options(verbose = "tkconsole")
  pb <- .igraph.progress.tkconsole.create(oldverb)
  assign(".igraph.pb", pb, envir = asNamespace("igraph"))
  .igraph.progress.tkconsole.message("Console started.\n")
  invisible()
}

.igraph.pb <- NULL

#' igraph progress
#' @param percent,message,clean Used internally by `.igraph.progress()` and `.igraph.status()`
#' @dev
.igraph.progress <- function(percent, message, clean = FALSE) {
  if (clean) {
    if (!is.null(.igraph.pb)) {
      close(.igraph.pb)
    }
    return(invisible())
  }
  type <- igraph_opt("verbose")
  if (is.logical(type) && type) {
    .igraph.progress.txt(percent, message)
  } else {
    switch(type,
      "tk" = .igraph.progress.tk(percent, message),
      "tkconsole" = .igraph.progress.tkconsole(percent, message),
      stop("Cannot interpret 'verbose' option, this should not happen")
    )
  }
}

#' igraph status
#' @inheritParams .igraph.progress
#' @dev
.igraph.status <- function(message) {
  type <- igraph_opt("verbose")
  if (is.logical(type) && type) {
    message(message, appendLF = FALSE)
  } else {
    switch(type,
      "tk" = message(message, appendLF = FALSE),
      "tkconsole" = .igraph.progress.tkconsole.message(message, start = TRUE),
      stop("Cannot interpret 'verbose' option, this should not happen")
    )
  }
  0L
}

#' @importFrom utils txtProgressBar setTxtProgressBar
.igraph.progress.txt <- function(percent, message) {
  pb <- get(".igraph.pb", asNamespace("igraph"))
  if (percent == 0) {
    if (!is.null(pb)) {
      close(pb)
    }
    cat(sep = "", "  ", message, "\n")
    pb <- txtProgressBar(min = 0, max = 100, style = 3)
  }
  setTxtProgressBar(pb, percent)
  if (percent == 100) {
    close(pb)
    pb <- NULL
  }
  assign(".igraph.pb", pb, envir = asNamespace("igraph"))
  0L
}

.igraph.progress.tk <- function(percent, message) {
  pb <- get(".igraph.pb", asNamespace("igraph"))
  if (percent == 0) {
    if (!is.null(pb)) {
      close(pb)
    }
    pb <- tcltk::tkProgressBar(min = 0, max = 100, title = message, label = "0 %")
  }
  tcltk::setTkProgressBar(pb, percent, label = paste(percent, "%"))
  if (percent == 100) {
    close(pb)
    pb <- NULL
  }
  assign(".igraph.pb", pb, envir = asNamespace("igraph"))
  0L
}

.igraph.progress.tkconsole <- function(percent, message) {
  pb <- get(".igraph.pb", asNamespace("igraph"))
  startmess <- FALSE

  ## Open the console, if it is not open
  if (is.null(pb)) {
    startmess <- TRUE
    pb <- .igraph.progress.tkconsole.create(NA)
  }

  ## Update progress bar
  pb$pb$set(pb$pb$widget, percent)
  tcltk::tkconfigure(pb$pb$label, text = substr(message, 1, 20))
  tcltk::tcl("update", "idletasks")

  ## Done
  assign(".igraph.pb", pb, envir = asNamespace("igraph"))
  if (startmess) .igraph.progress.tkconsole.message("Console started.\n")
  0L
}

.igraph.progress.tkconsole.create <- function(oldverb) {
  console <- tcltk::tktoplevel()
  tcltk::tktitle(console) <- "igraph console"

  fn <- tcltk::tkfont.create(family = "courier", size = 8)

  lfr <- tcltk::tkframe(console)
  image <- tcltk::tkimage.create("photo", "img",
    format = "gif",
    file = system.file("igraph2.gif", package = "igraph")
  )
  logo <- tcltk::tklabel(lfr, relief = "flat", padx = 10, pady = 10, image = image)

  scr <- tcltk::tkscrollbar(console,
    repeatinterval = 5,
    command = function(...) tcltk::tkyview(txt, ...)
  )
  txt <- tcltk::tktext(console,
    yscrollcommand = function(...) tcltk::tkset(scr, ...),
    width = 60, height = 7, font = fn
  )
  tcltk::tkconfigure(txt, state = "disabled")
  pbar <- .igraph.progress.tkconsole.pbar(console)

  bclear <- tcltk::tkbutton(lfr, text = "Clear", command = function() {
    tcltk::tkconfigure(txt, state = "normal")
    tcltk::tkdelete(txt, "0.0", "end")
    tcltk::tkconfigure(txt, state = "disabled")
  })
  bstop <- tcltk::tkbutton(lfr, text = "Stop", command = function() {})
  bclose <- tcltk::tkbutton(lfr, text = "Close", command = function() {
    if (!is.na(oldverb) && igraph_opt("verbose") == "tkconsole") {
      igraph_options(verbose = oldverb)
    }
    tcltk::tkdestroy(console)
  })

  tcltk::tkpack(logo,
    side = "top", fill = "none", expand = 0, anchor = "n",
    ipadx = 10, ipady = 10
  )
  tcltk::tkpack(bclear, side = "top", fill = "x", expand = 0, padx = 10)
  ## tcltk::tkpack(bstop, side="top", fill="x", expand=0, padx=10)
  tcltk::tkpack(bclose, side = "top", fill = "x", expand = 0, padx = 10)

  tcltk::tkpack(lfr, side = "left", fill = "none", expand = 0, anchor = "n")
  tcltk::tkpack(pbar$frame, side = "bottom", fill = "x", expand = 0)
  tcltk::tkpack(scr, side = "right", fill = "y", expand = 0)
  tcltk::tkpack(txt, side = "left", fill = "both", expand = 1)

  tcltk::tkbind(console, "<Destroy>", function() {
    if (!is.na(oldverb) && igraph_opt("verbose") == "tkconsole") {
      igraph_options(verbose = oldverb)
    }
    assign(".igraph.pb", NULL, envir = asNamespace("igraph"))
  })

  res <- list(top = console, txt = txt, pb = pbar$pb, oldverb = oldverb)
  class(res) <- "igraphconsole"
  res
}

.igraph.progress.tkconsole.message <- function(message, start = FALSE) {
  txt <- get(".igraph.pb", asNamespace("igraph"))$txt
  if (is.null(txt)) {
    if (start) {
      pb <- .igraph.progress.tkconsole.create(NA)
      assign(".igraph.pb", pb, envir = asNamespace("igraph"))
      txt <- pb$txt
    } else {
      return()
    }
  }
  tcltk::tkconfigure(txt, state = "normal")
  now <- paste(sep = "", substr(date(), 5, 19), ": ")
  s1 <- grepl("^ ", message)
  if (!s1) {
    tcltk::tkinsert(txt, "insert", now)
  }
  tcltk::tkinsert(txt, "insert", message)
  tcltk::tksee(txt, "end")
  tcltk::tkconfigure(txt, state = "disabled")
  tcltk::tcl("update", "idletasks")
}

#' @exportS3Method NULL
close.igraphconsole <- function(con, ...) {
  invisible()
}

## Much of this is from tkProgressbar

.igraph.progress.tkconsole.pbar <- function(top) {
  useText <- FALSE
  have_ttk <- as.character(tcltk::tcl("info", "tclversion")) >= "8.5"
  if (!have_ttk && as.character(tcltk::tclRequire("PBar")) == "FALSE") {
    useText <- TRUE
  }
  fn <- tcltk::tkfont.create(family = "helvetica", size = 10)
  frame <- tcltk::tkframe(top)
  if (useText) {
    .lab <- tcltk::tklabel(frame,
      text = " ", font = fn, anchor = "w",
      padx = 20
    )
    tcltk::tkpack(.lab, side = "left", anchor = "w", padx = 5)
    fn2 <- tcltk::tkfont.create(family = "helvetica", size = 12)
    .vlab <- tcltk::tklabel(frame, text = "0%", font = fn2, padx = 20)
    tcltk::tkpack(.vlab, side = "right")
  } else {
    .lab <- tcltk::tklabel(frame,
      text = " ", font = fn, anchor = "w",
      pady = 5
    )
    tcltk::tkpack(.lab, side = "top", anchor = "w", padx = 5)
    tcltk::tkpack(tcltk::tklabel(frame, text = "", font = fn), side = "bottom")
    .val <- tcltk::tclVar()
    pBar <- if (have_ttk) {
      tcltk::ttkprogressbar(frame, length = 300, variable = .val)
    } else {
      tcltk::tkwidget(frame, "ProgressBar", width = 300, variable = .val)
    }
    tcltk::tkpack(pBar, side = "bottom", anchor = "w", padx = 5)
  }
  get <- function(w) {
    return(tcltk::tclvalue(.val))
  }
  set <- function(w, val) {
    tcltk::tclvalue(.val) <<- val
  }
  pb <- list(widget = pBar, get = get, set = set, label = .lab)
  list(frame = frame, pb = pb)
}