键盘快捷键

使用 跳转章节

使用 S/ 在本书内搜索

使用 ? 显示帮助页面

使用 Esc 隐藏帮助页面

Get

Get[object, key]

Retrieves the value associated with key from object.

  • Returns the value stored under key.
  • An error is raised if key does not exist in object.
Let[stu, Object[Student, {
    { #name, "Kevin Stephen" },
    { #age, 24 },
    { #single, #t },
    { #hobbies, { #eat, #book } }
}]];

Get[stu, #name];  (* => "Kevin Stephen" *)

Get[stu, #oops];
(*? Error[ksl::builtin::Get]: Unknown key `oops` for `Object[Student]{hobbies, age, name, single}`. *)

GetChar

GetChar[]

Reads a single unbuffered keystroke from standard input without waiting for Enter. Captures both printable characters and special control keys.

  • Returns a String for printable characters.
  • Returns an Atom for special keys. Supported atoms:
DirectionalFunctionalNavigationSystem/Others
#key_up#key_enter#key_home#key_del
#key_down#key_tab#key_end#key_insert
#key_left#key_backtab#key_pageup#key_unknown
#key_right#key_backspace#key_pagedown
#key_escape

Standalone modifier keys (Alt, Ctrl, Shift) are not supported in standard terminal environments.

Print["Press any key, `Escape` to exit..."];
Let[k, Unit[]];
While[Not[Eq[k, #key_escape]], Block[
  Update[k, GetChar[]],
  Cond[{
    { IsString[k], Print[Concat["Printable: ", k]] },
    { IsAtom[k], Print[Concat["Special Key: ", ToString[k]]] }
  }, Unit[]]
]];
Print["Done."];

Interactive Example

The following multi-threaded example demonstrates a simple interactive CLI with arrow-key navigation using GetChar.

OpenChannel[act_rx, act_tx];
Let[{ m, n }, { 30, 14 }];

Thread[listener, { act_tx }, Block[
  Let[key, Unit[]],
  While[Not[Eq[key, #key_escape]], {
    Update[key, GetChar[]],
    Cond[{
      { Or[Eq[key, "h"], Eq[key, #key_left]], SendValue[act_tx, #left] },
      { Or[Eq[key, "j"], Eq[key, #key_down]], SendValue[act_tx, #down] },
      { Or[Eq[key, "k"], Eq[key, #key_up]], SendValue[act_tx, #up] },
      { Or[Eq[key, "l"], Eq[key, #key_right]], SendValue[act_tx, #right] },
    }, SendValue[act_tx, #keep]],
  }],
  SendValue[act_tx, #quit],
]];

Thread[render, { m, n, act_rx }, Block[
  Let[{ x, y, action }, { 0, 0, Unit[] }],

  Let[draw, Fun[{ x, y }, Block[
    Let[boundary, Many["-", Add[m, 2]]],
    Let[row, Prepend[Append[Many[" ", m], "|"], "|"]],
    Let[map, Prepend[Append[Many[row, n], boundary], boundary]],
    Update[map, Set[map, Add[y, 2], Set[row, Add[x, 2], "&"]]],

    WriteTerm["\x1b[H"],
    Let[cats, Fun[{ strs }, Reduce[strs, Concat, ""]]],
    Print[cats[Map[map, Fun[{ row }, Concat[cats[row], #10]]]]],
    WriteTerm["\x1b[J"],
    Print[Concat[
      "coord: (", ToString[Add[x, 1]], ", ", ToString[Add[y, 1]], ") ",
      "with [", ToString[m], ", ", ToString[n], "]"]],
  ]]],

  WriteTerm["\x1b[?1049h\x1b[?25l"],
  While[Not[Eq[action, #quit]], {
    Or[Eq[action, #keep], draw[x, y]],
    Update[action, ReceiveValue[act_rx]],
    Cond[{
      { Eq[action, #left], Update[x, If[Greater[x, 0], Sub[x, 1], 0]] },
      { Eq[action, #down], Update[y, If[Less[y, Sub[n, 1]], Add[y, 1], y]] },
      { Eq[action, #up], Update[y, If[Greater[y, 0], Sub[y, 1], 0]] },
      { Eq[action, #right], Update[x, If[Less[x, Sub[m, 1]], Add[x, 1], x]] },
    }, Unit[]],
    Sleep[Div[1, 60]],
  }],
  WriteTerm["\x1b[?25h\x1b[?1049l"],
]];

Let[_, Consume[listener]];
Let[_, Consume[render]];

DropSender[act_tx];
DropReceiver[act_rx];

GetDate

GetDate[]

Retrieves the current local date from the system.

  • Returns a List of three Numbers: { year, month, day }.
GetDate[];  (* => {2026, 4, 12} *)

GetEnv

GetEnv[name]

Retrieves the value of the environment variable name from the host system.

  • Returns the value as a String.
  • An error is raised if the variable is not found.
GetEnv["PWD"];   (* => "/Users/username" *)
GetEnv["USER"];  (* => "username" *)

GetEnv["P"];
(*? Error[ksl::builtin::GetEnv]: Failed to get environment variable `P`: environment variable not found. *)

GetTime

GetTime[]

Retrieves the current local time from the system.

  • Returns a List of four Numbers: { hour, minute, second, nanosecond }.
  • hour is in 24-hour format ($0 \sim 23$).
GetTime[];  (* => {16, 49, 24, 742986000} *)

GetType

GetType[object]

Retrieves the type tag of an Object or NativeObject.

  • Returns an Atom representing the type tag.
Let[o, Object[A]];
GetType[o];  (* => #A *)

Glob

Glob[pattern]

Returns a list of file paths matching the shell-style glob pattern.

  • Returns a List of Strings. If no matches are found, returns { }.
  • An error is raised if pattern is syntactically invalid.
Glob["ksl/src/*.rs"];
(* => {"ksl/src/lib.rs", "ksl/src/main.rs", "ksl/src/token.rs", "ksl/src/value.rs"} *)

Glob["ksl/src/v*"];
(* => {"ksl/src/value.rs"} *)

Greater

Greater[x, y]

Compares two numbers and returns #t if x is strictly greater than y.

  • Returns #t if $x > y$, otherwise #f.
Greater[10, 5];    (* => #t *)
Greater[5, 5];     (* => #f *)
Greater[-1.5, -2.5]; (* => #t *)