"Fossies" - the Fresh Open Source Software Archive

Member "julia-1.1.1/share/julia/stdlib/v1.1/REPL/src/TerminalMenus/util.jl" (16 May 2019, 2894 Bytes) of package /linux/misc/julia-1.1.1-linux-i686.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Julia source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # This file is a part of Julia. License is MIT: https://julialang.org/license
    2 
    3 # Enum for escaped (multi-byte) keys such as the arrows or the home/end keys
    4 @enum(Key,
    5     ARROW_LEFT = 1000,
    6     ARROW_RIGHT,
    7     ARROW_UP,
    8     ARROW_DOWN,
    9     DEL_KEY,
   10     HOME_KEY,
   11     END_KEY,
   12     PAGE_UP,
   13     PAGE_DOWN)
   14 
   15 # Enable raw mode. Allows us to process keyboard inputs directly.
   16 function enableRawMode(term)
   17     try
   18         REPL.Terminals.raw!(term, true)
   19         return true
   20     catch err
   21         @warn("TerminalMenus: Unable to enter raw mode: $err")
   22     end
   23     return false
   24 end
   25 
   26 # Disable raw mode. Give control back to Julia REPL if interactive session.
   27 function disableRawMode(term)
   28     try
   29         REPL.Terminals.raw!(term, false)
   30         return true
   31     catch err
   32         @warn("TerminalMenus: Unable to disable raw mode: $err")
   33     end
   34     return false
   35 end
   36 
   37 
   38 # Reads a single byte from stdin
   39 readNextChar(stream::IO=stdin) = Char(read(stream,1)[1])
   40 
   41 # Read the next key from stdin. It is also able to read several bytes for
   42 #   escaped keys such as the arrow keys, home/end keys, etc.
   43 # Escaped keys are returned using the `Key` enum.
   44 readKey(stream::IO=stdin) = UInt32(_readKey(stream))
   45 function _readKey(stream::IO=stdin)
   46     c = readNextChar(stream)
   47 
   48     # Escape characters
   49     if c == '\x1b'
   50         stream.buffer.size < 2 && return '\x1b'
   51         esc_a = readNextChar(stream)
   52         esc_a == 'v' && return PAGE_UP  # M-v
   53         esc_a == '<' && return HOME_KEY # M-<
   54         esc_a == '>' && return END_KEY  # M->
   55 
   56         stream.buffer.size < 3 && return '\x1b'
   57         esc_b = readNextChar(stream)
   58 
   59         if esc_a == '[' || esc_a == 'O'
   60             if esc_b >= '0' && esc_b <= '9'
   61                 stream.buffer.size < 4 && return '\x1b'
   62                 esc_c = readNextChar(stream)
   63                 if esc_c == '~'
   64                     esc_b == '1' && return HOME_KEY
   65                     esc_b == '4' && return END_KEY
   66                     esc_b == '3' && return DEL_KEY
   67                     esc_b == '5' && return PAGE_UP
   68                     esc_b == '6' && return PAGE_DOWN
   69                     esc_b == '7' && return HOME_KEY
   70                     esc_b == '8' && return END_KEY
   71                     return '\x1b'
   72                 end
   73             else
   74                 # Arrow keys
   75                 esc_b == 'A' && return ARROW_UP
   76                 esc_b == 'B' && return ARROW_DOWN
   77                 esc_b == 'C' && return ARROW_RIGHT
   78                 esc_b == 'D' && return ARROW_LEFT
   79                 esc_b == 'H' && return HOME_KEY
   80                 esc_b == 'F' && return END_KEY
   81                 return '\x1b'
   82             end
   83         end
   84         esc_a == 'H' && return HOME_KEY
   85         esc_a == 'F' && return END_KEY
   86         return '\x1b'
   87     end
   88     c == '\x16' && return PAGE_DOWN  # C-v
   89     c == '\x10' && return ARROW_UP   # C-p
   90     c == '\x0e' && return ARROW_DOWN # C-n
   91     return c
   92 end