<commit_before>#lang racket/base
(require racket/gui/base
         racket/class
         racket/contract
         framework
         string-constants)

(provide 
 (contract-out
  [on-terminal-run (parameter/c (-> void?))]
  [in-terminal (->* ((-> eventspace? (or/c #f (is-a?/c top-level-window<%>)) void?))
                    (#:container (is-a?/c area-container<%>)
                                 #:title string?
                                 #:abort-label string?
                                 #:aborted-message string?
                                 #:cleanup-thunk (-> void?))
                    (is-a?/c terminal<%>))])
 terminal<%>)

(define terminal<%>
  (interface ()
    is-closed?
    close
    can-close?
    can-close-evt))

(define on-terminal-run (make-parameter void))

;; creates a frame and sets up the current error and output ports
;; before calling `do-install'. 
;; runs the installer in a separate thread and returns immediately,
;; before the installation is complete. The cleanup thunk is called when installation completes
(define (in-terminal do-install
                     #:container [container #f]
                     #:title [title "mrlib/terminal"]
                     #:abort-label [abort-label (string-constant plt-installer-abort-installation)]
                     #:aborted-message [aborted-message (string-constant plt-installer-aborted)]
                     #:cleanup-thunk [cleanup-thunk void])
  (define orig-eventspace (current-eventspace))
  (define orig-custodian (current-custodian))
  (define inst-eventspace (make-eventspace))

  (define on-terminal-run-proc (on-terminal-run))
  
  (define frame #f)
  (define sub-container #f)
  (define text #f)
  (define close-button #f)
  (define kill-button #f)
  (define setup-sema (make-semaphore 0))
  (define can-close-sema (make-semaphore))

  (define (close)
    (if frame
        (send frame show #f)
        (send container delete-child sub-container))
    (close-callback))
  (define (close-callback)
    (custodian-shutdown-all installer-cust))
  
  (parameterize ([current-eventspace inst-eventspace])
    (queue-callback
     (λ ()
       (unless container
         (set! frame
               (new (class frame%
                      (define/augment (can-close?) (send close-button is-enabled?))
                      (define/augment (on-close) (close-callback))
                      (super-new [label title]
                                 [width 600]
                                 [height 300]))))
         (set! container frame)
         (define mb (new menu-bar% [parent frame]))
         (define edit-menu (new menu% 
                                [label (string-constant edit-menu)]
                                [parent mb]))
         (define copy-menu-item
           (new menu-item%
                [parent edit-menu]
                [label (string-constant copy-menu-item)]
                [shortcut #\c]
                [demand-callback
                 (λ (item)
                    (send copy-menu-item enable
                          (not (= (send text get-start-position)
                                  (send text get-end-position)))))]
                [callback
                 (λ (item evt)
                    (send text copy))]))
         (new menu-item%
              [parent edit-menu]
              [label (string-constant select-all-menu-item)]
              [shortcut #\a]
              [callback
               (λ (item evt)
                  (send text set-position 0 (send text last-position)))]))

       (set! sub-container
             (or frame
                 (new (class vertical-panel%
                        (super-new)
                        (define/override (on-superwindow-show on?)
                          (unless on? (close-callback))))
                      [parent container])))
                                    
       (set! text (new (text:hide-caret/selection-mixin text:standard-style-list%)))
       (define canvas (new editor-canvas%
                           [parent sub-container]
                           [editor text]))
       (define button-panel (new horizontal-panel%
                                 [parent sub-container]
                                 [stretchable-height #f]
                                 [alignment '(center center)]))
       (set! kill-button (new button%
                              [label abort-label]
                              [parent button-panel]
                              [callback (λ (b e) (kill-callback))]))
       (set! close-button (new button% 
                              [label (string-constant close)]
                              [parent button-panel]
                              [callback (λ (b e) (close))]))

       (define (close)
         (if frame
             (send frame show #f)
             (send container delete-child sub-container))
         (close-callback))

       (define (kill-callback)
         (custodian-shutdown-all installer-cust)
         (fprintf output-port "\n~a\n" aborted-message))
       (send close-button enable #f)
       (send canvas allow-tab-exit #t)
       ((current-text-keymap-initializer) (send text get-keymap))
       (send text set-styles-sticky #f)
       (send text lock #t)
       (send text hide-caret #t)
       (semaphore-post setup-sema)
       (when frame
         (send frame show #t)))))
  
  (semaphore-wait setup-sema)<commit_msg>mrlib/terminal: create embedded terminal in a container sequence<commit_after>#lang racket/base
(require racket/gui/base
         racket/class
         racket/contract
         framework
         string-constants)

(provide 
 (contract-out
  [on-terminal-run (parameter/c (-> void?))]
  [in-terminal (->* ((-> eventspace? (or/c #f (is-a?/c top-level-window<%>)) void?))
                    (#:container (is-a?/c area-container<%>)
                                 #:title string?
                                 #:abort-label string?
                                 #:aborted-message string?
                                 #:cleanup-thunk (-> void?))
                    (is-a?/c terminal<%>))])
 terminal<%>)

(define terminal<%>
  (interface ()
    is-closed?
    close
    can-close?
    can-close-evt))

(define on-terminal-run (make-parameter void))

;; creates a frame and sets up the current error and output ports
;; before calling `do-install'. 
;; runs the installer in a separate thread and returns immediately,
;; before the installation is complete. The cleanup thunk is called when installation completes
(define (in-terminal do-install
                     #:container [container #f]
                     #:title [title "mrlib/terminal"]
                     #:abort-label [abort-label (string-constant plt-installer-abort-installation)]
                     #:aborted-message [aborted-message (string-constant plt-installer-aborted)]
                     #:cleanup-thunk [cleanup-thunk void])
  (define orig-eventspace (current-eventspace))
  (define orig-custodian (current-custodian))
  (define inst-eventspace (make-eventspace))

  (define on-terminal-run-proc (on-terminal-run))
  
  (define frame #f)
  (define sub-container #f)
  (define text #f)
  (define close-button #f)
  (define kill-button #f)
  (define setup-sema (make-semaphore 0))
  (define can-close-sema (make-semaphore))

  (define (close)
    (if frame
        (send frame show #f)
        (send container delete-child sub-container))
    (close-callback))
  (define (close-callback)
    (custodian-shutdown-all installer-cust))
  
  (parameterize ([current-eventspace inst-eventspace])
    (queue-callback
     (λ ()
       (unless container
         (set! frame
               (new (class frame%
                      (define/augment (can-close?) (send close-button is-enabled?))
                      (define/augment (on-close) (close-callback))
                      (super-new [label title]
                                 [width 600]
                                 [height 300]))))
         (set! container frame)
         (define mb (new menu-bar% [parent frame]))
         (define edit-menu (new menu% 
                                [label (string-constant edit-menu)]
                                [parent mb]))
         (define copy-menu-item
           (new menu-item%
                [parent edit-menu]
                [label (string-constant copy-menu-item)]
                [shortcut #\c]
                [demand-callback
                 (λ (item)
                    (send copy-menu-item enable
                          (not (= (send text get-start-position)
                                  (send text get-end-position)))))]
                [callback
                 (λ (item evt)
                    (send text copy))]))
         (new menu-item%
              [parent edit-menu]
              [label (string-constant select-all-menu-item)]
              [shortcut #\a]
              [callback
               (λ (item evt)
                  (send text set-position 0 (send text last-position)))]))
       
       (when container
         (send container begin-container-sequence))

       (set! sub-container
             (or frame
                 (new (class vertical-panel%
                        (super-new)
                        (define/override (on-superwindow-show on?)
                          (unless on? (close-callback))))
                      [parent container])))
                                    
       (set! text (new (text:hide-caret/selection-mixin text:standard-style-list%)))
       (define canvas (new editor-canvas%
                           [parent sub-container]
                           [editor text]))
       (define button-panel (new horizontal-panel%
                                 [parent sub-container]
                                 [stretchable-height #f]
                                 [alignment '(center center)]))
       (set! kill-button (new button%
                              [label abort-label]
                              [parent button-panel]
                              [callback (λ (b e) (kill-callback))]))
       (set! close-button (new button% 
                              [label (string-constant close)]
                              [parent button-panel]
                              [callback (λ (b e) (close))]))

       (define (close)
         (if frame
             (send frame show #f)
             (send container delete-child sub-container))
         (close-callback))

       (define (kill-callback)
         (custodian-shutdown-all installer-cust)
         (fprintf output-port "\n~a\n" aborted-message))
       (send close-button enable #f)
       (send canvas allow-tab-exit #t)
       ((current-text-keymap-initializer) (send text get-keymap))
       (send text set-styles-sticky #f)
       (send text lock #t)
       (send text hide-caret #t)
       (semaphore-post setup-sema)
       (when container
         (send container end-container-sequence))
       (when frame
         (send frame show #t)))))
  
  (semaphore-wait setup-sema)
{'fraction_non_alphanumeric': 0.11553389295062731, 'fraction_numerical': 0.0014441736618828415, 'mean_word_length': 1.436771497690785, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '895128', 'n_tokens_mistral': 3044, 'n_tokens_neox': 2908, 'n_words': 760}