The Julia REPL

تأتي جوليا مع حلقة REPL (قراءة-تقييم-طباعة) تفاعلية كاملة الميزات مدمجة في القابل للتنفيذ julia. بالإضافة إلى السماح بالتقييم السريع والسهل لبيانات جوليا، تحتوي على تاريخ قابل للبحث، وإكمال تلقائي، والعديد من اختصارات المفاتيح المفيدة، وأنماط مساعدة وصندوق أوامر مخصصة. يمكن بدء REPL ببساطة عن طريق استدعاء julia بدون أي معلمات أو النقر المزدوج على القابل للتنفيذ:

$ julia

               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.11.5 (2025-04-14)
 _/ |\__'_|_|_|\__'_|  |  HEAD/760b2e5* (fork: 1 commits, 81 days)
|__/                   |


julia>

لخروج من الجلسة التفاعلية، اكتب ^D – مفتاح التحكم مع مفتاح d في سطر فارغ – أو اكتب exit() متبوعًا بمفتاح العودة أو الإدخال. يرحب بك REPL مع لافتة و提示 julia>.

The different prompt modes

The Julian mode

يحتوي REPL على خمسة أوضاع رئيسية للتشغيل. الأول والأكثر شيوعًا هو موجه جوليا. إنه وضع التشغيل الافتراضي؛ يبدأ كل سطر جديد في البداية بـ julia>. هنا يمكنك إدخال تعبيرات جوليا. الضغط على زر العودة أو الإدخال بعد إدخال تعبير كامل سيقوم بتقييم الإدخال وعرض نتيجة آخر تعبير.

julia> string(1 + 2)
"3"

هناك عدد من الميزات المفيدة الفريدة من نوعها للعمل التفاعلي. بالإضافة إلى عرض النتيجة، يقوم REPL أيضًا بربط النتيجة بالمتغير ans. يمكن استخدام فاصلة منقوطة في نهاية السطر كعلامة لكتم عرض النتيجة.

julia> string(3 * 4);

julia> ans
"12"

في وضع جوليا، يدعم REPL شيئًا يسمى لصق الموجه. يتم تفعيله عند لصق نص يبدأ بـ julia> في REPL. في هذه الحالة، يتم تحليل التعبيرات التي تبدأ بـ julia> (بالإضافة إلى الموجهات الأخرى لوضع REPL: shell>, help?>, pkg>) فقط، بينما يتم إزالة الأخرى. هذا يجعل من الممكن لصق كتلة من النص تم نسخها من جلسة REPL دون الحاجة إلى إزالة الموجهات والمخرجات. هذه الميزة مفعلة بشكل افتراضي ولكن يمكن تعطيلها أو تفعيلها حسب الرغبة باستخدام REPL.enable_promptpaste(::Bool). إذا كانت مفعلة، يمكنك تجربتها عن طريق لصق كتلة الكود أعلاه مباشرة في REPL. هذه الميزة لا تعمل على موجه الأوامر القياسي في ويندوز بسبب قيوده في اكتشاف متى يحدث اللصق.

Objects are printed at the REPL using the show function with a specific IOContext. In particular, the :limit attribute is set to true. Other attributes can receive in certain show methods a default value if it's not already set, like :compact. It's possible, as an experimental feature, to specify the attributes used by the REPL via the Base.active_repl.options.iocontext dictionary (associating values to attributes). For example:

julia> rand(2, 2)
2×2 Array{Float64,2}:
 0.8833    0.329197
 0.719708  0.59114

julia> show(IOContext(stdout, :compact => false), "text/plain", rand(2, 2))
 0.43540323669187075  0.15759787870609387
 0.2540832269192739   0.4597637838786053
julia> Base.active_repl.options.iocontext[:compact] = false;

julia> rand(2, 2)
2×2 Array{Float64,2}:
 0.2083967319174056  0.13330606013126012
 0.6244375177790158  0.9777957560761545

لتعريف القيم تلقائيًا في هذه القاموس عند وقت بدء التشغيل، يمكن استخدام دالة atreplinit في ملف ~/.julia/config/startup.jl، على سبيل المثال:

atreplinit() do repl
    repl.options.iocontext[:compact] = false
end

Help mode

عندما يكون المؤشر في بداية السطر، يمكن تغيير الموجه إلى وضع المساعدة عن طريق كتابة ?. ستقوم جوليا بمحاولة طباعة المساعدة أو الوثائق لأي شيء يتم إدخاله في وضع المساعدة:

julia> ? # upon typing ?, the prompt changes (in place) to: help?>

help?> string
search: string String Cstring Cwstring RevString randstring bytestring SubString

  string(xs...)

  Create a string from any values using the print function.

يمكن أيضًا استعلام الماكروز والأنواع والمتغيرات:

help?> @time
  @time

  A macro to execute an expression, printing the time it took to execute, the number of allocations,
  and the total number of bytes its execution caused to be allocated, before returning the value of the
  expression.

  See also @timev, @timed, @elapsed, and @allocated.

help?> Int32
search: Int32 UInt32

  Int32 <: Signed

  32-bit signed integer type.

سلسلة أو تعبير عادي يبحث في جميع وثائق السلسلة باستخدام apropos:

help?> "aprop"
REPL.stripmd
Base.Docs.apropos

help?> r"ap..p"
Base.:∘
Base.shell_escape_posixly
Distributed.CachingPool
REPL.stripmd
Base.Docs.apropos

ميزة أخرى لوضع المساعدة هي القدرة على الوصول إلى سلاسل الوثائق الموسعة. يمكنك القيام بذلك عن طريق كتابة شيء مثل ??Print بدلاً من ?Print والذي سيعرض قسم # المساعدة الموسعة من وثائق الشيفرة المصدرية.

يمكن الخروج من وضع المساعدة بالضغط على مفتاح العودة في بداية السطر.

Shell mode

تمامًا كما أن وضع المساعدة مفيد للوصول السريع إلى الوثائق، فإن مهمة شائعة أخرى هي استخدام شل النظام لتنفيذ أوامر النظام. تمامًا كما أن إدخال ? يدخل وضع المساعدة عند بداية السطر، فإن الفاصلة المنقوطة (;) ستدخل وضع الشل. ويمكن الخروج منه بالضغط على زر العودة للخلف في بداية السطر.

julia> ; # upon typing ;, the prompt changes (in place) to: shell>

shell> echo hello
hello
Note

بالنسبة لمستخدمي ويندوز، وضع الشل في جوليا لا يكشف عن أوامر شل ويندوز. لذلك، سيفشل هذا:

julia> ; # upon typing ;, the prompt changes (in place) to: shell>

shell> dir
ERROR: IOError: could not spawn `dir`: no such file or directory (ENOENT)
Stacktrace!
.......

ومع ذلك، يمكنك الوصول إلى PowerShell بهذه الطريقة:

julia> ; # upon typing ;, the prompt changes (in place) to: shell>

shell> powershell
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
PS C:\Users\elm>

... وإلى cmd.exe بهذه الطريقة (انظر إلى أمر dir):

julia> ; # upon typing ;, the prompt changes (in place) to: shell>

shell> cmd
Microsoft Windows [version 10.0.17763.973]
(c) 2018 Microsoft Corporation. All rights reserved.
C:\Users\elm>dir
 Volume in drive C has no label
 Volume Serial Number is 1643-0CD7
  Directory of C:\Users\elm

29/01/2020  22:15    <DIR>          .
29/01/2020  22:15    <DIR>          ..
02/02/2020  08:06    <DIR>          .atom

Pkg mode

وضع إدارة الحزم يقبل أوامر متخصصة لتحميل وتحديث الحزم. يتم الدخول إليه بالضغط على مفتاح ] عند موجه REPL الخاص بـ Julian ويتم الخروج منه بالضغط على CTRL-C أو الضغط على مفتاح العودة للخلف في بداية السطر. الموجه لهذا الوضع هو pkg>. يدعم وضع إدارة الحزم وضع المساعدة الخاص به، والذي يتم الدخول إليه بالضغط على ? في بداية سطر موجه pkg>. يتم توثيق وضع إدارة الحزم في دليل Pkg، المتاح على https://julialang.github.io/Pkg.jl/v1/.

Search modes

في جميع الأوضاع المذكورة أعلاه، يتم حفظ الأسطر المنفذة في ملف تاريخ، يمكن البحث فيه. لبدء بحث تزايدي من خلال التاريخ السابق، اكتب ^R - مفتاح التحكم مع مفتاح r. سيتغير الموجه إلى (reverse-i-search)`':, وعند الكتابة، ستظهر استعلام البحث في الاقتباسات. سيتم تحديث أحدث نتيجة تتطابق مع الاستعلام ديناميكيًا إلى يمين النقطتين كلما تم كتابة المزيد. للعثور على نتيجة أقدم باستخدام نفس الاستعلام، ما عليك سوى كتابة ^R مرة أخرى.

تمامًا كما أن ^R هو بحث عكسي، فإن ^S هو بحث للأمام، مع المطالبة (i-search)`':. يمكن استخدام الاثنين معًا للتنقل عبر النتائج المطابقة السابقة أو التالية، على التوالي.

يتم تسجيل جميع الأوامر المنفذة في REPL الخاص بـ Julia في ~/.julia/logs/repl_history.jl مع طابع زمني يوضح متى تم تنفيذها ووضع REPL الحالي الذي كنت فيه. يقوم وضع البحث بالاستعلام عن ملف السجل هذا للعثور على الأوامر التي قمت بتشغيلها سابقًا. يمكن تعطيل ذلك عند بدء التشغيل عن طريق تمرير علامة --history-file=no إلى Julia.

Key bindings

تستخدم واجهة سطر الأوامر في جوليا (REPL) مفاتيح اختصار بشكل كبير. تم تقديم العديد من اختصارات مفاتيح التحكم بالفعل أعلاه (^D للخروج، ^R و ^S للبحث)، ولكن هناك المزيد. بالإضافة إلى مفتاح التحكم، هناك أيضًا اختصارات مفاتيح ميتا. تختلف هذه بشكل أكبر حسب النظام الأساسي، ولكن معظم المحطات الافتراضية تستخدم مفتاح alt- أو option- مع الضغط على مفتاح لإرسال مفتاح الميتا (أو يمكن تكوينها للقيام بذلك)، أو الضغط على Esc ثم المفتاح.

KeybindingDescription
Program control
^DExit (when buffer is empty)
^CInterrupt or cancel
^LClear console screen
Return/Enter, ^JNew line, executing if it is complete
meta-Return/EnterInsert new line without executing it
? or ;Enter help or shell mode (when at start of a line)
^R, ^SIncremental history search, described above
Cursor movement
Right arrow, ^FMove right one character
Left arrow, ^BMove left one character
ctrl-Right, meta-FMove right one word
ctrl-Left, meta-BMove left one word
Home, ^AMove to beginning of line
End, ^EMove to end of line
Up arrow, ^PMove up one line (or change to the previous history entry that matches the text before the cursor)
Down arrow, ^NMove down one line (or change to the next history entry that matches the text before the cursor)
Shift-Arrow KeyMove cursor according to the direction of the Arrow key, while activating the region ("shift selection")
Page-up, meta-PChange to the previous history entry
Page-down, meta-NChange to the next history entry
meta-<Change to the first history entry (of the current session if it is before the current position in history)
meta->Change to the last history entry
^-SpaceSet the "mark" in the editing region (and de-activate the region if it's active)
^-Space ^-SpaceSet the "mark" in the editing region and make the region "active", i.e. highlighted
^GDe-activate the region (i.e. make it not highlighted)
^X^XExchange the current position with the mark
Editing
Backspace, ^HDelete the previous character, or the whole region when it's active
Delete, ^DForward delete one character (when buffer has text)
meta-BackspaceDelete the previous word
meta-dForward delete the next word
^WDelete previous text up to the nearest whitespace
meta-wCopy the current region in the kill ring
meta-W"Kill" the current region, placing the text in the kill ring
^U"Kill" to beginning of line, placing the text in the kill ring
^K"Kill" to end of line, placing the text in the kill ring
^Y"Yank" insert the text from the kill ring
meta-yReplace a previously yanked text with an older entry from the kill ring
^TTranspose the characters about the cursor
meta-Up arrowTranspose current line with line above
meta-Down arrowTranspose current line with line below
meta-uChange the next word to uppercase
meta-cChange the next word to titlecase
meta-lChange the next word to lowercase
^/, ^_Undo previous editing action
^QWrite a number in REPL and press ^Q to open editor at corresponding stackframe or method
meta-Left ArrowIndent the current line on the left
meta-Right ArrowIndent the current line on the right
meta-.Insert last word from previous history entry
meta-eEdit the current input in an editor

Customizing keybindings

يمكن تخصيص مفاتيح REPL في جوليا بالكامل وفقًا لتفضيلات المستخدم عن طريق تمرير قاموس إلى REPL.setup_interface. قد تكون مفاتيح هذا القاموس أحرفًا أو سلاسل نصية. المفتاح '*' يشير إلى الإجراء الافتراضي. يتم الإشارة إلى مفاتيح التحكم مع الحرف x باستخدام "^x". يمكن كتابة ميتا زائد x كـ "\\M-x" أو "\ex"، ويمكن كتابة التحكم زائد x كـ "\\C-x" أو "^x". يجب أن تكون قيم خريطة المفاتيح المخصصة nothing (مما يشير إلى أنه يجب تجاهل الإدخال) أو وظائف تقبل التوقيع (PromptState, AbstractREPL, Char). يجب استدعاء دالة REPL.setup_interface قبل تهيئة REPL، عن طريق تسجيل العملية باستخدام atreplinit. على سبيل المثال، لربط مفاتيح السهم لأعلى ولأسفل للتحرك عبر التاريخ دون البحث المسبق، يمكن وضع الكود التالي في ~/.julia/config/startup.jl:

import REPL
import REPL.LineEdit

const mykeys = Dict{Any,Any}(
    # Up Arrow
    "\e[A" => (s,o...)->(LineEdit.edit_move_up(s) || LineEdit.history_prev(s, LineEdit.mode(s).hist)),
    # Down Arrow
    "\e[B" => (s,o...)->(LineEdit.edit_move_down(s) || LineEdit.history_next(s, LineEdit.mode(s).hist))
)

function customize_keys(repl)
    repl.interface = REPL.setup_interface(repl; extra_repl_keymap = mykeys)
end

atreplinit(customize_keys)

يجب على المستخدمين الرجوع إلى LineEdit.jl لاكتشاف الإجراءات المتاحة عند إدخال المفاتيح.

Tab completion

في أوضاع جوليا و pkg و help في REPL، يمكن للمرء إدخال الأحرف القليلة الأولى من دالة أو نوع ثم الضغط على مفتاح التبويب للحصول على قائمة بجميع المطابقات:

julia> x[TAB]
julia> xor

في بعض الحالات، يكمل الاسم جزئيًا فقط، حتى يصل إلى الغموض التالي:

julia> mapf[TAB]
julia> mapfold

إذا ضغطت على مفتاح التبويب مرة أخرى، ستحصل على قائمة بالأشياء التي قد تكمل هذا:

julia> mapfold[TAB]
mapfoldl mapfoldr

عندما تكون نتيجة إكمال علامة التبويب الكاملة الوحيدة متاحة في نهاية سطر الإدخال وتم كتابة حرفين أو أكثر، ستظهر تلميح الإكمال بلون أفتح. يمكن تعطيل ذلك عبر Base.active_repl.options.hint_tab_completes = false أو عن طريق إضافة

atreplinit() do repl
    if VERSION >= v"1.11.0-0"
        repl.options.hint_tab_completes = false
    end
end

إلى ~/.julia/config/startup.jl.

Julia 1.11

تم إضافة تلميحات الإكمال التلقائي في جوليا 1.11

مثل المكونات الأخرى في REPL، فإن البحث حساس لحالة الأحرف:

julia> stri[TAB]
stride     strides     string      strip

julia> Stri[TAB]
StridedArray    StridedMatrix    StridedVecOrMat  StridedVector    String

يمكن أيضًا استخدام مفتاح التبويب لاستبدال رموز الرياضيات LaTeX بمعادلاتها Unicode، والحصول على قائمة بمطابقات LaTeX أيضًا:

julia> \pi[TAB]
julia> π
π = 3.1415926535897...

julia> e\_1[TAB] = [1,0]
julia> e₁ = [1,0]
2-element Array{Int64,1}:
 1
 0

julia> e\^1[TAB] = [1 0]
julia> e¹ = [1 0]
1×2 Array{Int64,2}:
 1  0

julia> \sqrt[TAB]2     # √ is equivalent to the sqrt function
julia> √2
1.4142135623730951

julia> \hbar[TAB](h) = h / 2\pi[TAB]
julia> ħ(h) = h / 2π
ħ (generic function with 1 method)

julia> \h[TAB]
\hat              \hermitconjmatrix  \hkswarow          \hrectangle
\hatapprox        \hexagon           \hookleftarrow     \hrectangleblack
\hbar             \hexagonblack      \hookrightarrow    \hslash
\heartsuit        \hksearow          \house             \hspace

julia> α="\alpha[TAB]"   # LaTeX completion also works in strings
julia> α="α"

يمكن العثور على قائمة كاملة من الإكمال التلقائي في قسم Unicode Input من الدليل.

إكمال المسارات يعمل مع السلاسل ووضع الشل في جوليا:

julia> path="/[TAB]"
.dockerenv  .juliabox/   boot/        etc/         lib/         media/       opt/         root/        sbin/        sys/         usr/
.dockerinit bin/         dev/         home/        lib64/       mnt/         proc/        run/         srv/         tmp/         var/
shell> /[TAB]
.dockerenv  .juliabox/   boot/        etc/         lib/         media/       opt/         root/        sbin/        sys/         usr/
.dockerinit bin/         dev/         home/        lib64/       mnt/         proc/        run/         srv/         tmp/         var/

يمكن أيضًا إكمال مفاتيح القاموس باستخدام زر التبويب:

julia> foo = Dict("qwer1"=>1, "qwer2"=>2, "asdf"=>3)
Dict{String,Int64} with 3 entries:
  "qwer2" => 2
  "asdf"  => 3
  "qwer1" => 1

julia> foo["q[TAB]

"qwer1" "qwer2"
julia> foo["qwer

يمكن أن تساعد إكمال التبويب أيضًا في إكمال الحقول:

julia> x = 3 + 4im;

julia> x.[TAB][TAB]
im re

julia> import UUIDs

julia> UUIDs.uuid[TAB][TAB]
uuid1        uuid4         uuid5        uuid_version

يمكن أيضًا إكمال الحقول الناتجة من الدوال:

julia> split("","")[1].[TAB]
lastindex  offset  string

يستخدم إكمال الحقول للإخراج من الدوال استنتاج النوع، ولا يمكنه اقتراح الحقول إلا إذا كانت الدالة مستقرة من حيث النوع.

يمكن أن تساعد إكمال التبويب في التحقيق في الطرق المتاحة التي تتطابق مع وسائط الإدخال:

julia> max([TAB] # All methods are displayed, not shown here due to size of the list

julia> max([1, 2], [TAB] # All methods where `Vector{Int}` matches as first argument
max(x, y) in Base at operators.jl:215
max(a, b, c, xs...) in Base at operators.jl:281

julia> max([1, 2], max(1, 2), [TAB] # All methods matching the arguments.
max(x, y) in Base at operators.jl:215
max(a, b, c, xs...) in Base at operators.jl:281

تظهر الكلمات الرئيسية أيضًا في الطرق المقترحة بعد ;، انظر إلى السطر أدناه حيث أن limit و keepempty هما وسيطين رئيسيين:

julia> split("1 1 1", [TAB]
split(str::AbstractString; limit, keepempty) in Base at strings/util.jl:302
split(str::T, splitter; limit, keepempty) where T<:AbstractString in Base at strings/util.jl:277

تستخدم إكمال الطرق استنتاج النوع وبالتالي يمكنها رؤية ما إذا كانت المعاملات تتطابق حتى لو كانت المعاملات ناتجة عن دوال. تحتاج الدالة إلى أن تكون مستقرة من حيث النوع لكي يتمكن الإكمال من إزالة الطرق غير المتطابقة.

إذا كنت تتساءل عن الطرق التي يمكن استخدامها مع أنواع معينة من المعاملات، استخدم ? كاسم الدالة. هذا يظهر مثالاً للبحث عن الدوال في InteractiveUtils التي تقبل سلسلة نصية واحدة:

julia> InteractiveUtils.?("somefile")[TAB]
edit(path::AbstractString) in InteractiveUtils at InteractiveUtils/src/editless.jl:197
less(file::AbstractString) in InteractiveUtils at InteractiveUtils/src/editless.jl:266

تتضمن هذه الطرق المدرجة في وحدة InteractiveUtils التي يمكن استدعاؤها على سلسلة نصية. بشكل افتراضي، يستثني هذا الطرق التي تكون جميع المعاملات فيها من نوع Any، ولكن يمكنك رؤية تلك أيضًا عن طريق الضغط على SHIFT-TAB بدلاً من TAB:

julia> InteractiveUtils.?("somefile")[SHIFT-TAB]
apropos(string) in REPL at REPL/src/docview.jl:796
clipboard(x) in InteractiveUtils at InteractiveUtils/src/clipboard.jl:64
code_llvm(f) in InteractiveUtils at InteractiveUtils/src/codeview.jl:221
code_native(f) in InteractiveUtils at InteractiveUtils/src/codeview.jl:243
edit(path::AbstractString) in InteractiveUtils at InteractiveUtils/src/editless.jl:197
edit(f) in InteractiveUtils at InteractiveUtils/src/editless.jl:225
eval(x) in InteractiveUtils at InteractiveUtils/src/InteractiveUtils.jl:3
include(x) in InteractiveUtils at InteractiveUtils/src/InteractiveUtils.jl:3
less(file::AbstractString) in InteractiveUtils at InteractiveUtils/src/editless.jl:266
less(f) in InteractiveUtils at InteractiveUtils/src/editless.jl:274
report_bug(kind) in InteractiveUtils at InteractiveUtils/src/InteractiveUtils.jl:391
separate_kwargs(args...; kwargs...) in InteractiveUtils at InteractiveUtils/src/macros.jl:7

يمكنك أيضًا استخدام ?("somefile")[TAB] والبحث عبر جميع الوحدات، ولكن قوائم الطرق قد تكون طويلة.

من خلال حذف القوس المغلق، يمكنك تضمين الدوال التي قد تتطلب معلمات إضافية:

julia> using Mmap

help?> Mmap.?("file",[TAB]
Mmap.Anonymous(name::String, readonly::Bool, create::Bool) in Mmap at Mmap/src/Mmap.jl:16
mmap(file::AbstractString) in Mmap at Mmap/src/Mmap.jl:245
mmap(file::AbstractString, ::Type{T}) where T<:Array in Mmap at Mmap/src/Mmap.jl:245
mmap(file::AbstractString, ::Type{T}, dims::Tuple{Vararg{Integer, N}}) where {T<:Array, N} in Mmap at Mmap/src/Mmap.jl:245
mmap(file::AbstractString, ::Type{T}, dims::Tuple{Vararg{Integer, N}}, offset::Integer; grow, shared) where {T<:Array, N} in Mmap at Mmap/src/Mmap.jl:245
mmap(file::AbstractString, ::Type{T}, len::Integer) where T<:Array in Mmap at Mmap/src/Mmap.jl:251
mmap(file::AbstractString, ::Type{T}, len::Integer, offset::Integer; grow, shared) where T<:Array in Mmap at Mmap/src/Mmap.jl:251
mmap(file::AbstractString, ::Type{T}, dims::Tuple{Vararg{Integer, N}}) where {T<:BitArray, N} in Mmap at Mmap/src/Mmap.jl:316
mmap(file::AbstractString, ::Type{T}, dims::Tuple{Vararg{Integer, N}}, offset::Integer; grow, shared) where {T<:BitArray, N} in Mmap at Mmap/src/Mmap.jl:316
mmap(file::AbstractString, ::Type{T}, len::Integer) where T<:BitArray in Mmap at Mmap/src/Mmap.jl:322
mmap(file::AbstractString, ::Type{T}, len::Integer, offset::Integer; grow, shared) where T<:BitArray in Mmap at Mmap/src/Mmap.jl:322

Customizing Colors

يمكن تخصيص الألوان المستخدمة من قبل جوليا وREPL أيضًا. لتغيير لون موجه جوليا، يمكنك إضافة شيء مثل ما يلي إلى ملف ~/.julia/config/startup.jl، والذي يجب وضعه داخل دليل المنزل الخاص بك:

function customize_colors(repl)
    repl.prompt_color = Base.text_colors[:cyan]
end

atreplinit(customize_colors)

يمكن رؤية مفاتيح الألوان المتاحة عن طريق كتابة Base.text_colors في وضع المساعدة من REPL. بالإضافة إلى ذلك، يمكن استخدام الأعداد من 0 إلى 255 كمفاتيح ألوان للأجهزة الطرفية التي تدعم 256 لونًا.

يمكنك أيضًا تغيير الألوان لرسائل المساعدة ومطالبات الصدفة ونص الإدخال والإجابة عن طريق تعيين الحقل المناسب لـ repl في دالة customize_colors أعلاه (على التوالي، help_color و shell_color و input_color و answer_color). بالنسبة للأخيرين، تأكد من أن حقل envcolors مضبوط أيضًا على false.

من الممكن أيضًا تطبيق تنسيق الخط العريض باستخدام Base.text_colors[:bold] كلون. على سبيل المثال، لطباعة الإجابات بخط عريض، يمكن استخدام ما يلي كـ ~/.julia/config/startup.jl:

function customize_colors(repl)
    repl.envcolors = false
    repl.answer_color = Base.text_colors[:bold]
end

atreplinit(customize_colors)

يمكنك أيضًا تخصيص اللون المستخدم لعرض رسائل التحذير والمعلومات عن طريق تعيين متغيرات البيئة المناسبة. على سبيل المثال، لعرض رسائل الخطأ والتحذير والمعلومات على التوالي باللون الأرجواني والأصفر والسماوي، يمكنك إضافة ما يلي إلى ملف ~/.julia/config/startup.jl:

ENV["JULIA_ERROR_COLOR"] = :magenta
ENV["JULIA_WARN_COLOR"] = :yellow
ENV["JULIA_INFO_COLOR"] = :cyan

Changing the contextual module which is active at the REPL

عند إدخال التعبيرات في REPL، يتم تقييمها افتراضيًا في وحدة Main؛

julia> @__MODULE__
Main

من الممكن تغيير هذه الوحدة السياقية عبر الدالة REPL.activate(m) حيث m هو Module أو عن طريق كتابة الوحدة في REPL والضغط على اختصار المفاتيح Alt-m مع وضع المؤشر على اسم الوحدة (Esc-m على MacOS). الضغط على اختصار المفاتيح في موجه فارغ يبدل السياق بين الوحدة غير Main النشطة سابقًا و Main. يتم عرض الوحدة النشطة في الموجه (ما لم تكن Main):

julia> using REPL

julia> REPL.activate(Base)

(Base) julia> @__MODULE__
Base

(Base) julia> using REPL # Need to load REPL into Base module to use it

(Base) julia> REPL.activate(Main)

julia>

julia> Core<Alt-m> # using the keybinding to change module

(Core) julia>

(Core) julia> <Alt-m> # going back to Main via keybinding

julia>

julia> <Alt-m> # going back to previously-active Core via keybinding

(Core) julia>

تأخذ الدوال التي تأخذ وسيط وحدة اختياري غالبًا ما تكون افتراضيًا إلى وحدة سياق REPL. كمثال، استدعاء varinfo() سيظهر المتغيرات في الوحدة النشطة الحالية:

julia> module CustomMod
           export var, f
           var = 1
           f(x) = x^2
       end;

julia> REPL.activate(CustomMod)

(Main.CustomMod) julia> varinfo()
  name         size summary
  ––––––––– ––––––– ––––––––––––––––––––––––––––––––––
  CustomMod         Module
  f         0 bytes f (generic function with 1 method)
  var       8 bytes Int64

Numbered prompt

من الممكن الحصول على واجهة مشابهة لـ IPython REPL ودفتر Mathematica مع مطالبات إدخال مرقمة وبدء تشغيل المخرجات. يتم ذلك عن طريق استدعاء REPL.numbered_prompt!(). إذا كنت ترغب في تفعيل ذلك عند بدء التشغيل، أضف

atreplinit() do repl
    @eval import REPL
    if !isdefined(repl, :interface)
        repl.interface = REPL.setup_interface(repl)
    end
    REPL.numbered_prompt!(repl)
end

إلى ملفك startup.jl. في الموجه المرقم، يمكن استخدام المتغير Out[n] (حيث n هو عدد صحيح) للإشارة إلى النتائج السابقة:

In [1]: 5 + 3
Out[1]: 8

In [2]: Out[1] + 5
Out[2]: 13

In [3]: Out
Out[3]: Dict{Int64, Any} with 2 entries:
  2 => 13
  1 => 8
Note

نظرًا لأن جميع المخرجات من تقييمات REPL السابقة محفوظة في متغير Out، يجب أن تكون حذرًا إذا كنت تعيد العديد من الكائنات الكبيرة في الذاكرة مثل المصفوفات، حيث ستظل محمية من جمع القمامة طالما أن هناك مرجعًا لها في Out. إذا كنت بحاجة إلى إزالة المراجع إلى الكائنات في Out، يمكنك مسح التاريخ بالكامل الذي يخزنه باستخدام empty!(Out)، أو مسح إدخال فردي باستخدام Out[n] = nothing.

TerminalMenus

TerminalMenus هو وحدة فرعية من REPL في جوليا ويتيح قوائم تفاعلية صغيرة ومنخفضة الملف في الطرفية.

Examples

import REPL
using REPL.TerminalMenus

options = ["apple", "orange", "grape", "strawberry",
            "blueberry", "peach", "lemon", "lime"]

RadioMenu

قائمة الراديو تتيح للمستخدم اختيار خيار واحد من القائمة. تقوم دالة request بعرض القائمة التفاعلية وتعيد فهرس الخيار المحدد. إذا ضغط المستخدم على 'q' أو ctrl-c، ستعيد دالة request قيمة -1.

# `pagesize` is the number of items to be displayed at a time.
#  The UI will scroll if the number of options is greater
#   than the `pagesize`
menu = RadioMenu(options, pagesize=4)

# `request` displays the menu and returns the index after the
#   user has selected a choice
choice = request("Choose your favorite fruit:", menu)

if choice != -1
    println("Your favorite fruit is ", options[choice], "!")
else
    println("Menu canceled.")
end

Please provide the Markdown content or text that you would like me to translate into Arabic.

Choose your favorite fruit:
^  grape
   strawberry
 > blueberry
v  peach
Your favorite fruit is blueberry!

MultiSelectMenu

تتيح قائمة الاختيار المتعدد للمستخدمين اختيار العديد من الخيارات من قائمة.

# here we use the default `pagesize` 10
menu = MultiSelectMenu(options)

# `request` returns a `Set` of selected indices
# if the menu us canceled (ctrl-c or q), return an empty set
choices = request("Select the fruits you like:", menu)

if length(choices) > 0
    println("You like the following fruits:")
    for i in choices
        println("  - ", options[i])
    end
else
    println("Menu canceled.")
end

Please provide the Markdown content or text that you would like me to translate into Arabic.

Select the fruits you like:
[press: Enter=toggle, a=all, n=none, d=done, q=abort]
   [ ] apple
 > [X] orange
   [X] grape
   [ ] strawberry
   [ ] blueberry
   [X] peach
   [ ] lemon
   [ ] lime
You like the following fruits:
  - orange
  - grape
  - peach

Customization / Configuration

ConfiguredMenu subtypes

بدءًا من Julia 1.6، الطريقة الموصى بها لتكوين القوائم هي عبر المُنشئ. على سبيل المثال، قائمة التحديد المتعدد الافتراضية

julia> menu = MultiSelectMenu(options, pagesize=5);

julia> request(menu) # ASCII is used by default
[press: Enter=toggle, a=all, n=none, d=done, q=abort]
   [ ] apple
   [X] orange
   [ ] grape
 > [X] strawberry
v  [ ] blueberry

يمكن أن يتم عرضه بدلاً من ذلك باستخدام أحرف اختيار التنسيق والتنقل Unicode مع

julia> menu = MultiSelectMenu(options, pagesize=5, charset=:unicode);

julia> request(menu)
[press: Enter=toggle, a=all, n=none, d=done, q=abort]
   ⬚ apple
   ✓ orange
   ⬚ grape
 → ✓ strawberry
↓  ⬚ blueberry

يمكن أيضًا تكوين إعدادات أكثر دقة:

julia> menu = MultiSelectMenu(options, pagesize=5, charset=:unicode, checked="YEP!", unchecked="NOPE", cursor='⧐');

julia> request(menu)
julia> request(menu)
[press: Enter=toggle, a=all, n=none, d=done, q=abort]
   NOPE apple
   YEP! orange
   NOPE grape
 ⧐ YEP! strawberry
↓  NOPE blueberry

بجانب خيار charset العام، فإن الخيارات القابلة للتكوين لـ RadioMenu هي:

  • cursor::Char='>'|'→': حرف لاستخدامه كموشر
  • up_arrow::Char='^'|'↑': حرف للاستخدام كسهام لأعلى
  • down_arrow::Char='v'|'↓': حرف لاستخدامه كسهام لأسفل
  • updown_arrow::Char='I'|'↕': حرف لاستخدامه في السهم لأعلى/أسفل في صفحة ذات سطر واحد
  • scroll_wrap::Bool=false: يتيح لف القائمة في البداية/النهاية بشكل اختياري
  • ctrl_c_interrupt::Bool=true: إذا كانت false، ارجع فارغًا عند ^C، إذا كانت true ارمِ InterruptException() عند ^C

MultiSelectMenu يضيف:

  • checked::String="[X]"|"✓": سلسلة للاستخدام عند التحقق
  • unchecked::String="[ ]"|"⬚"): سلسلة للاستخدام للحالة غير المحددة

يمكنك إنشاء أنواع قوائم جديدة خاصة بك. الأنواع المشتقة من TerminalMenus.ConfiguredMenu تقوم بتكوين خيارات القائمة في وقت الإنشاء.

Legacy interface

قبل Julia 1.6، وما زال مدعومًا طوال Julia 1.x، يمكن أيضًا تكوين القوائم عن طريق استدعاء TerminalMenus.config().

References

REPL

Base.atreplinitFunction
atreplinit(f)

قم بتسجيل دالة تأخذ وسيطًا واحدًا ليتم استدعاؤها قبل تهيئة واجهة REPL في الجلسات التفاعلية؛ هذا مفيد لتخصيص الواجهة. وسيط f هو كائن REPL. يجب استدعاء هذه الدالة من داخل ملف التهيئة .julia/config/startup.jl.

source

TerminalMenus

REPL.TerminalMenus.RadioMenuType
RadioMenu

قائمة تتيح للمستخدم اختيار خيار واحد من قائمة.

مخرجات عينة

julia> request(RadioMenu(options, pagesize=4))
اختر فاكهتك المفضلة:
^  عنب
   فراولة
 > توت أزرق
v  خوخ
فاكهتك المفضلة هي توت أزرق!
source
REPL.TerminalMenus.MultiSelectMenuType
قائمة متعددة الاختيارات

قائمة تتيح للمستخدم اختيار خيارات متعددة من قائمة.

مخرجات عينة

julia> request(MultiSelectMenu(options))
اختر الفواكه التي تحبها:
[اضغط: Enter=تبديل، a=الكل، n=لا شيء، d=تم، q=إلغاء]
   [ ] تفاح
 > [X] برتقال
   [X] عنب
   [ ] فراولة
   [ ] توت أزرق
   [X] خوخ
   [ ] ليمون
   [ ] ليم
تحب الفواكه التالية:
  - برتقال
  - عنب
  - خوخ
source

Configuration

REPL.TerminalMenus.ConfigType
Config(; scroll_wrap=false, ctrl_c_interrupt=true, charset=:ascii, cursor::Char, up_arrow::Char, down_arrow::Char)

قم بتكوين سلوك قوائم الاختيار عبر وسائط الكلمات الرئيسية:

  • scroll_wrap، إذا كانت true، تتسبب في التفاف القائمة عند التمرير فوق الإدخال الأول أو أسفل الإدخال الأخير
  • ctrl_c_interrupt، إذا كانت true، ترمي InterruptException إذا ضغط المستخدم على Ctrl-C أثناء اختيار القائمة. إذا كانت false، ستعيد TerminalMenus.request النتيجة الافتراضية من TerminalMenus.selected.
  • charset يؤثر على القيم الافتراضية لـ cursor و up_arrow و down_arrow، ويمكن أن تكون :ascii أو :unicode
  • cursor هو الحرف المطبوع للإشارة إلى الخيار الذي سيتم اختياره عند الضغط على "Enter". القيم الافتراضية هي '>' أو '→'، اعتمادًا على charset.
  • up_arrow هو الحرف المطبوع عندما لا تتضمن الشاشة الإدخال الأول. القيم الافتراضية هي '^' أو '↑'، اعتمادًا على charset.
  • down_arrow هو الحرف المطبوع عندما لا تتضمن الشاشة الإدخال الأخير. القيم الافتراضية هي 'v' أو '↓'، اعتمادًا على charset.

سوف تطبع الأنواع الفرعية من ConfiguredMenu cursor و up_arrow و down_arrow تلقائيًا حسب الحاجة، يجب ألا تطبع طريقة writeline الخاصة بك.

Julia 1.6

Config متاحة اعتبارًا من Julia 1.6. في الإصدارات الأقدم، استخدم CONFIG العالمية.

source
REPL.TerminalMenus.MultiSelectConfigType
MultiSelectConfig(; charset=:ascii, checked::String, unchecked::String, kwargs...)

قم بتكوين سلوك قائمة الاختيار المتعدد عبر معلمات الكلمات الرئيسية:

  • checked هو السلسلة التي سيتم طباعتها عند اختيار خيار. القيم الافتراضية هي "[X]" أو "✓"، اعتمادًا على charset.
  • unchecked هو السلسلة التي سيتم طباعتها عند عدم اختيار خيار. القيم الافتراضية هي "[ ]" أو "⬚"، اعتمادًا على charset.

جميع معلمات الكلمات الرئيسية الأخرى كما هو موصوف لـ TerminalMenus.Config. checked و unchecked لا يتم طباعتهما تلقائيًا، ويجب طباعتهما بواسطة طريقة writeline الخاصة بك.

Julia 1.6

MultiSelectConfig متاحة اعتبارًا من Julia 1.6. في الإصدارات الأقدم، استخدم CONFIG العالمية.

source
REPL.TerminalMenus.configFunction
config( <see arguments> )

دالة فقط للكلمات الرئيسية لتكوين معلمات القائمة العالمية

المعلمات

  • charset::Symbol=:na: أحرف واجهة المستخدم للاستخدام (:ascii أو :unicode); يتم تجاوزها بواسطة معلمات أخرى
  • cursor::Char='>'|'→': الحرف المستخدم لمؤشر
  • up_arrow::Char='^'|'↑': الحرف المستخدم للسهم لأعلى
  • down_arrow::Char='v'|'↓': الحرف المستخدم للسهم لأسفل
  • checked::String="[X]"|"✓": السلسلة المستخدمة للمؤشر
  • unchecked::String="[ ]"|"⬚"): السلسلة المستخدمة لعدم المؤشر
  • scroll::Symbol=:nowrap: إذا كان :wrap يتم لف المؤشر حول الأعلى والأسفل، إذا كان :nowrap لا يتم لف المؤشر
  • supress_output::Bool=false: معلمة قديمة مهملة، مرر suppress_output كمعلمة كلمة رئيسية إلى request بدلاً من ذلك.
  • ctrl_c_interrupt::Bool=true: إذا كان false، ارجع فارغًا عند ^C، إذا كان true ارمِ InterruptException() عند ^C
Julia 1.6

اعتبارًا من Julia 1.6، تم إهمال config. استخدم Config أو MultiSelectConfig بدلاً من ذلك.

source

User interaction

REPL.TerminalMenus.requestFunction
request(m::AbstractMenu; cursor=1)

عرض القائمة وادخل وضع التفاعل. يشير cursor إلى رقم العنصر المستخدم لموقع المؤشر الأولي. يمكن أن يكون cursor إما Int أو RefValue{Int}. الأخير مفيد للمراقبة والتحكم في موضع المؤشر من الخارج.

يُرجع selected(m).

Julia 1.6

يتطلب وسيط cursor وجود Julia 1.6 أو أحدث.

source
request([term,] msg::AbstractString, m::AbstractMenu)

اختصار لـ println(msg); request(m).

source

AbstractMenu extension interface

يجب أن يكون أي نوع فرعي من AbstractMenu قابلاً للتغيير، ويجب أن يحتوي على الحقول pagesize::Int و pageoffset::Int. يجب على أي نوع فرعي أيضًا تنفيذ الوظائف التالية:

REPL.TerminalMenus.pickFunction
pick(m::AbstractMenu, cursor::Int)

يحدد ما يحدث عندما يضغط المستخدم على مفتاح Enter بينما القائمة مفتوحة. إذا تم إرجاع true، ستخرج request(). cursor يحدد موضع الاختيار.

source
REPL.TerminalMenus.cancelFunction
cancel(m::AbstractMenu)

حدد ما يحدث عندما يقوم المستخدم بإلغاء ('q' أو ctrl-c) قائمة. ستخرج request() دائمًا بعد استدعاء هذه الدالة.

source
REPL.TerminalMenus.writelineFunction
writeline(buf::IO, m::AbstractMenu, idx::Int, iscursor::Bool)

اكتب الخيار في الفهرس idx إلى buf. iscursor، إذا كانت true، تشير إلى أن هذه العنصر في موضع المؤشر الحالي (الذي سيتم تحديده عند الضغط على "Enter").

إذا كان m هو ConfiguredMenu، ستقوم TerminalMenus بطباعة مؤشر المؤشر. خلاف ذلك، يُتوقع من المتصل التعامل مع مثل هذه الطباعة.

Julia 1.6

writeline يتطلب Julia 1.6 أو أعلى.

في الإصدارات الأقدم من Julia، كان هذا هو writeLine(buf::IO, m::AbstractMenu, idx, iscursor::Bool) ويفترض أن m غير مُهيأ. يمكن الحصول على مؤشرات الاختيار والموقع من TerminalMenus.CONFIG.

هذه الوظيفة القديمة مدعومة في جميع إصدارات Julia 1.x ولكن سيتم إسقاطها في Julia 2.0.

source

يجب أن تنفذ أيضًا إما options أو numoptions:

REPL.TerminalMenus.optionsFunction
options(m::AbstractMenu)

إرجاع قائمة من السلاسل ليتم عرضها كخيارات في الصفحة الحالية.

بدلاً من ذلك، قم بتنفيذ numoptions، وفي هذه الحالة لا حاجة لـ options.

source
REPL.TerminalMenus.numoptionsFunction
numoptions(m::AbstractMenu) -> Int

إرجاع عدد الخيارات في القائمة m. الافتراضي هو length(options(m)).

جوليا 1.6

تتطلب هذه الوظيفة جوليا 1.6 أو أحدث.

source

إذا لم يكن لدى النوع الفرعي حقل يسمى selected، يجب عليه أيضًا تنفيذ

REPL.TerminalMenus.selectedFunction
selected(m::AbstractMenu)

إرجاع معلومات حول الخيار الذي اختاره المستخدم. بشكل افتراضي، فإنه يُرجع m.selected.

source

يمكن أن تكون الخيارات التالية اختيارية ولكنها تسمح بمزيد من التخصيص:

REPL.TerminalMenus.headerFunction
header(m::AbstractMenu) -> String

إرجاع سلسلة رأسية ليتم طباعتها فوق القائمة. الافتراضي هو "".

source