local-llm-rn 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cpp/CMakeLists.txt +285 -0
- package/cpp/common/CMakeLists.txt +149 -0
- package/cpp/common/arg.cpp +3799 -0
- package/cpp/common/arg.h +131 -0
- package/cpp/common/base64.hpp +392 -0
- package/cpp/common/build-info.cpp.in +4 -0
- package/cpp/common/chat-parser-xml-toolcall.cpp +879 -0
- package/cpp/common/chat-parser-xml-toolcall.h +45 -0
- package/cpp/common/chat-parser.cpp +1649 -0
- package/cpp/common/chat-parser.h +133 -0
- package/cpp/common/chat-peg-parser.cpp +124 -0
- package/cpp/common/chat-peg-parser.h +105 -0
- package/cpp/common/chat.cpp +3355 -0
- package/cpp/common/chat.h +252 -0
- package/cpp/common/common.cpp +1824 -0
- package/cpp/common/common.h +930 -0
- package/cpp/common/console.cpp +1137 -0
- package/cpp/common/console.h +41 -0
- package/cpp/common/debug.cpp +167 -0
- package/cpp/common/debug.h +43 -0
- package/cpp/common/download.cpp +792 -0
- package/cpp/common/download.h +84 -0
- package/cpp/common/http.h +84 -0
- package/cpp/common/jinja/README.md +88 -0
- package/cpp/common/jinja/caps.cpp +285 -0
- package/cpp/common/jinja/caps.h +30 -0
- package/cpp/common/jinja/lexer.cpp +341 -0
- package/cpp/common/jinja/lexer.h +157 -0
- package/cpp/common/jinja/parser.cpp +591 -0
- package/cpp/common/jinja/parser.h +21 -0
- package/cpp/common/jinja/runtime.cpp +867 -0
- package/cpp/common/jinja/runtime.h +638 -0
- package/cpp/common/jinja/string.cpp +213 -0
- package/cpp/common/jinja/string.h +61 -0
- package/cpp/common/jinja/utils.h +149 -0
- package/cpp/common/jinja/value.cpp +1393 -0
- package/cpp/common/jinja/value.h +756 -0
- package/cpp/common/json-partial.cpp +324 -0
- package/cpp/common/json-partial.h +39 -0
- package/cpp/common/json-schema-to-grammar.cpp +1153 -0
- package/cpp/common/json-schema-to-grammar.h +43 -0
- package/cpp/common/llguidance.cpp +258 -0
- package/cpp/common/log.cpp +446 -0
- package/cpp/common/log.h +119 -0
- package/cpp/common/ngram-cache.cpp +285 -0
- package/cpp/common/ngram-cache.h +101 -0
- package/cpp/common/ngram-map.cpp +530 -0
- package/cpp/common/ngram-map.h +115 -0
- package/cpp/common/ngram-mod.cpp +60 -0
- package/cpp/common/ngram-mod.h +38 -0
- package/cpp/common/peg-parser.cpp +1712 -0
- package/cpp/common/peg-parser.h +459 -0
- package/cpp/common/preset.cpp +483 -0
- package/cpp/common/preset.h +83 -0
- package/cpp/common/regex-partial.cpp +204 -0
- package/cpp/common/regex-partial.h +56 -0
- package/cpp/common/sampling.cpp +745 -0
- package/cpp/common/sampling.h +119 -0
- package/cpp/common/speculative.cpp +1074 -0
- package/cpp/common/speculative.h +41 -0
- package/cpp/common/unicode.cpp +64 -0
- package/cpp/common/unicode.h +22 -0
- package/cpp/ggml/CMakeLists.txt +494 -0
- package/cpp/ggml/cmake/GitVars.cmake +22 -0
- package/cpp/ggml/cmake/common.cmake +50 -0
- package/cpp/ggml/cmake/ggml-config.cmake.in +191 -0
- package/cpp/ggml/include/ggml-alloc.h +85 -0
- package/cpp/ggml/include/ggml-backend.h +373 -0
- package/cpp/ggml/include/ggml-blas.h +25 -0
- package/cpp/ggml/include/ggml-cann.h +123 -0
- package/cpp/ggml/include/ggml-cpp.h +39 -0
- package/cpp/ggml/include/ggml-cpu.h +151 -0
- package/cpp/ggml/include/ggml-cuda.h +47 -0
- package/cpp/ggml/include/ggml-hexagon.h +19 -0
- package/cpp/ggml/include/ggml-metal.h +61 -0
- package/cpp/ggml/include/ggml-opencl.h +26 -0
- package/cpp/ggml/include/ggml-opt.h +256 -0
- package/cpp/ggml/include/ggml-rpc.h +30 -0
- package/cpp/ggml/include/ggml-sycl.h +49 -0
- package/cpp/ggml/include/ggml-virtgpu.h +14 -0
- package/cpp/ggml/include/ggml-vulkan.h +29 -0
- package/cpp/ggml/include/ggml-webgpu.h +19 -0
- package/cpp/ggml/include/ggml-zdnn.h +17 -0
- package/cpp/ggml/include/ggml-zendnn.h +22 -0
- package/cpp/ggml/include/ggml.h +2753 -0
- package/cpp/ggml/include/gguf.h +204 -0
- package/cpp/ggml/src/CMakeLists.txt +492 -0
- package/cpp/ggml/src/ggml-alloc.c +1244 -0
- package/cpp/ggml/src/ggml-backend-dl.cpp +48 -0
- package/cpp/ggml/src/ggml-backend-dl.h +45 -0
- package/cpp/ggml/src/ggml-backend-impl.h +255 -0
- package/cpp/ggml/src/ggml-backend-reg.cpp +566 -0
- package/cpp/ggml/src/ggml-backend.cpp +2270 -0
- package/cpp/ggml/src/ggml-blas/CMakeLists.txt +101 -0
- package/cpp/ggml/src/ggml-blas/ggml-blas.cpp +518 -0
- package/cpp/ggml/src/ggml-common.h +1878 -0
- package/cpp/ggml/src/ggml-cpu/CMakeLists.txt +691 -0
- package/cpp/ggml/src/ggml-cpu/amx/amx.cpp +247 -0
- package/cpp/ggml/src/ggml-cpu/amx/amx.h +8 -0
- package/cpp/ggml/src/ggml-cpu/amx/common.h +91 -0
- package/cpp/ggml/src/ggml-cpu/amx/mmq.cpp +2512 -0
- package/cpp/ggml/src/ggml-cpu/amx/mmq.h +10 -0
- package/cpp/ggml/src/ggml-cpu/arch/arm/cpu-feats.cpp +98 -0
- package/cpp/ggml/src/ggml-cpu/arch/arm/quants.c +4052 -0
- package/cpp/ggml/src/ggml-cpu/arch/arm/repack.cpp +4935 -0
- package/cpp/ggml/src/ggml-cpu/arch/loongarch/quants.c +2159 -0
- package/cpp/ggml/src/ggml-cpu/arch/powerpc/cpu-feats.cpp +82 -0
- package/cpp/ggml/src/ggml-cpu/arch/powerpc/quants.c +2305 -0
- package/cpp/ggml/src/ggml-cpu/arch/riscv/cpu-feats.cpp +38 -0
- package/cpp/ggml/src/ggml-cpu/arch/riscv/quants.c +2726 -0
- package/cpp/ggml/src/ggml-cpu/arch/riscv/repack.cpp +342 -0
- package/cpp/ggml/src/ggml-cpu/arch/s390/cpu-feats.cpp +50 -0
- package/cpp/ggml/src/ggml-cpu/arch/s390/quants.c +1468 -0
- package/cpp/ggml/src/ggml-cpu/arch/wasm/quants.c +1221 -0
- package/cpp/ggml/src/ggml-cpu/arch/x86/cpu-feats.cpp +327 -0
- package/cpp/ggml/src/ggml-cpu/arch/x86/quants.c +3820 -0
- package/cpp/ggml/src/ggml-cpu/arch/x86/repack.cpp +6307 -0
- package/cpp/ggml/src/ggml-cpu/arch-fallback.h +313 -0
- package/cpp/ggml/src/ggml-cpu/binary-ops.cpp +154 -0
- package/cpp/ggml/src/ggml-cpu/binary-ops.h +16 -0
- package/cpp/ggml/src/ggml-cpu/cmake/FindSIMD.cmake +100 -0
- package/cpp/ggml/src/ggml-cpu/common.h +95 -0
- package/cpp/ggml/src/ggml-cpu/ggml-cpu-impl.h +529 -0
- package/cpp/ggml/src/ggml-cpu/ggml-cpu.c +3734 -0
- package/cpp/ggml/src/ggml-cpu/ggml-cpu.cpp +701 -0
- package/cpp/ggml/src/ggml-cpu/hbm.cpp +55 -0
- package/cpp/ggml/src/ggml-cpu/hbm.h +8 -0
- package/cpp/ggml/src/ggml-cpu/kleidiai/kernels.cpp +938 -0
- package/cpp/ggml/src/ggml-cpu/kleidiai/kernels.h +90 -0
- package/cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.cpp +798 -0
- package/cpp/ggml/src/ggml-cpu/kleidiai/kleidiai.h +17 -0
- package/cpp/ggml/src/ggml-cpu/llamafile/sgemm.cpp +4033 -0
- package/cpp/ggml/src/ggml-cpu/llamafile/sgemm.h +25 -0
- package/cpp/ggml/src/ggml-cpu/ops.cpp +10978 -0
- package/cpp/ggml/src/ggml-cpu/ops.h +116 -0
- package/cpp/ggml/src/ggml-cpu/quants.c +1193 -0
- package/cpp/ggml/src/ggml-cpu/quants.h +97 -0
- package/cpp/ggml/src/ggml-cpu/repack.cpp +3316 -0
- package/cpp/ggml/src/ggml-cpu/repack.h +173 -0
- package/cpp/ggml/src/ggml-cpu/simd-gemm.h +136 -0
- package/cpp/ggml/src/ggml-cpu/simd-mappings.h +1279 -0
- package/cpp/ggml/src/ggml-cpu/spacemit/ime.cpp +1025 -0
- package/cpp/ggml/src/ggml-cpu/spacemit/ime.h +13 -0
- package/cpp/ggml/src/ggml-cpu/spacemit/ime1_kernels.cpp +3196 -0
- package/cpp/ggml/src/ggml-cpu/spacemit/ime_kernels.h +26 -0
- package/cpp/ggml/src/ggml-cpu/traits.cpp +36 -0
- package/cpp/ggml/src/ggml-cpu/traits.h +38 -0
- package/cpp/ggml/src/ggml-cpu/unary-ops.cpp +337 -0
- package/cpp/ggml/src/ggml-cpu/unary-ops.h +35 -0
- package/cpp/ggml/src/ggml-cpu/vec.cpp +629 -0
- package/cpp/ggml/src/ggml-cpu/vec.h +1585 -0
- package/cpp/ggml/src/ggml-hexagon/CMakeLists.txt +117 -0
- package/cpp/ggml/src/ggml-hexagon/ggml-hexagon.cpp +3232 -0
- package/cpp/ggml/src/ggml-hexagon/htp/CMakeLists.txt +45 -0
- package/cpp/ggml/src/ggml-hexagon/htp/act-ops.c +815 -0
- package/cpp/ggml/src/ggml-hexagon/htp/argsort-ops.c +281 -0
- package/cpp/ggml/src/ggml-hexagon/htp/binary-ops.c +827 -0
- package/cpp/ggml/src/ggml-hexagon/htp/cmake-toolchain.cmake +157 -0
- package/cpp/ggml/src/ggml-hexagon/htp/cpy-ops.c +251 -0
- package/cpp/ggml/src/ggml-hexagon/htp/flash-attn-ops.c +666 -0
- package/cpp/ggml/src/ggml-hexagon/htp/get-rows-ops.c +111 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hex-dma.c +63 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hex-dma.h +182 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hex-dump.h +77 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hex-fastdiv.h +37 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hex-utils.h +51 -0
- package/cpp/ggml/src/ggml-hexagon/htp/htp-ctx.h +35 -0
- package/cpp/ggml/src/ggml-hexagon/htp/htp-msg.h +154 -0
- package/cpp/ggml/src/ggml-hexagon/htp/htp-ops.h +65 -0
- package/cpp/ggml/src/ggml-hexagon/htp/htp_iface.idl +16 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-arith.h +470 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-base.h +173 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-copy.h +245 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-div.h +116 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-dump.h +129 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-exp.h +215 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-floor.h +100 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-inverse.h +176 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-reduce.h +266 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-scale.h +133 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-sigmoid.h +141 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-sqrt.h +126 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-types.h +36 -0
- package/cpp/ggml/src/ggml-hexagon/htp/hvx-utils.h +18 -0
- package/cpp/ggml/src/ggml-hexagon/htp/main.c +1150 -0
- package/cpp/ggml/src/ggml-hexagon/htp/matmul-ops.c +2595 -0
- package/cpp/ggml/src/ggml-hexagon/htp/rope-ops.c +498 -0
- package/cpp/ggml/src/ggml-hexagon/htp/set-rows-ops.c +167 -0
- package/cpp/ggml/src/ggml-hexagon/htp/softmax-ops.c +421 -0
- package/cpp/ggml/src/ggml-hexagon/htp/sum-rows-ops.c +130 -0
- package/cpp/ggml/src/ggml-hexagon/htp/unary-ops.c +384 -0
- package/cpp/ggml/src/ggml-hexagon/htp/worker-pool.c +293 -0
- package/cpp/ggml/src/ggml-hexagon/htp/worker-pool.h +57 -0
- package/cpp/ggml/src/ggml-hexagon/htp-drv.cpp +418 -0
- package/cpp/ggml/src/ggml-hexagon/htp-drv.h +121 -0
- package/cpp/ggml/src/ggml-hexagon/libdl.h +79 -0
- package/cpp/ggml/src/ggml-hexagon/libggml-htp.inf +38 -0
- package/cpp/ggml/src/ggml-hexagon/op-desc.h +153 -0
- package/cpp/ggml/src/ggml-impl.h +724 -0
- package/cpp/ggml/src/ggml-metal/CMakeLists.txt +124 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-common.cpp +457 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-common.h +52 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-context.h +41 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-context.m +702 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-device.cpp +1890 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-device.h +290 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-device.m +1749 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-impl.h +1054 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-ops.cpp +4370 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal-ops.h +94 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal.cpp +937 -0
- package/cpp/ggml/src/ggml-metal/ggml-metal.metal +9819 -0
- package/cpp/ggml/src/ggml-musa/CMakeLists.txt +125 -0
- package/cpp/ggml/src/ggml-musa/mudnn.cu +112 -0
- package/cpp/ggml/src/ggml-musa/mudnn.cuh +12 -0
- package/cpp/ggml/src/ggml-opencl/CMakeLists.txt +150 -0
- package/cpp/ggml/src/ggml-opencl/ggml-opencl.cpp +11553 -0
- package/cpp/ggml/src/ggml-opencl/kernels/add.cl +190 -0
- package/cpp/ggml/src/ggml-opencl/kernels/add_id.cl +42 -0
- package/cpp/ggml/src/ggml-opencl/kernels/argsort.cl +86 -0
- package/cpp/ggml/src/ggml-opencl/kernels/clamp.cl +20 -0
- package/cpp/ggml/src/ggml-opencl/kernels/concat.cl +51 -0
- package/cpp/ggml/src/ggml-opencl/kernels/conv2d.cl +185 -0
- package/cpp/ggml/src/ggml-opencl/kernels/conv2d_f16_f32.cl +176 -0
- package/cpp/ggml/src/ggml-opencl/kernels/cpy.cl +184 -0
- package/cpp/ggml/src/ggml-opencl/kernels/cvt.cl +417 -0
- package/cpp/ggml/src/ggml-opencl/kernels/diag_mask_inf.cl +58 -0
- package/cpp/ggml/src/ggml-opencl/kernels/div.cl +138 -0
- package/cpp/ggml/src/ggml-opencl/kernels/embed_kernel.py +26 -0
- package/cpp/ggml/src/ggml-opencl/kernels/expm1.cl +113 -0
- package/cpp/ggml/src/ggml-opencl/kernels/fill.cl +17 -0
- package/cpp/ggml/src/ggml-opencl/kernels/flash_attn_f16.cl +370 -0
- package/cpp/ggml/src/ggml-opencl/kernels/flash_attn_f32.cl +371 -0
- package/cpp/ggml/src/ggml-opencl/kernels/flash_attn_f32_f16.cl +373 -0
- package/cpp/ggml/src/ggml-opencl/kernels/gelu.cl +89 -0
- package/cpp/ggml/src/ggml-opencl/kernels/gemm_moe_mxfp4_f32.cl +162 -0
- package/cpp/ggml/src/ggml-opencl/kernels/gemv_moe_mxfp4_f32.cl +156 -0
- package/cpp/ggml/src/ggml-opencl/kernels/gemv_noshuffle.cl +268 -0
- package/cpp/ggml/src/ggml-opencl/kernels/gemv_noshuffle_general.cl +274 -0
- package/cpp/ggml/src/ggml-opencl/kernels/gemv_noshuffle_general_q8_0_f32.cl +195 -0
- package/cpp/ggml/src/ggml-opencl/kernels/get_rows.cl +187 -0
- package/cpp/ggml/src/ggml-opencl/kernels/glu.cl +378 -0
- package/cpp/ggml/src/ggml-opencl/kernels/group_norm.cl +121 -0
- package/cpp/ggml/src/ggml-opencl/kernels/im2col_f16.cl +57 -0
- package/cpp/ggml/src/ggml-opencl/kernels/im2col_f32.cl +57 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mean.cl +140 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul.cl +152 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mat_Ab_Bi_8x4.cl +139 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mat_f16_f32.cl +130 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mm_f16_f32_kq_kqv.cl +273 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mm_f16_f32_l4_lm.cl +146 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mm_f32_f32_l4_lm.cl +147 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mm_q4_0_f32_l4_lm.cl +163 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mm_q4_1_f32_l4_lm.cl +165 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mm_q6_k_f32_l4_lm.cl +158 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mm_q8_0_f32_8x4.cl +129 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mm_q8_0_f32_l4_lm.cl +154 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_f16_f16.cl +118 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_f16_f32.cl +118 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_f16_f32_1row.cl +94 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_f16_f32_l4.cl +84 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_f32_f32.cl +118 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_id_mxfp4_f32.cl +189 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_id_mxfp4_f32_flat.cl +176 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_id_q4_0_f32_8x_flat.cl +283 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_id_q8_0_f32.cl +140 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_id_q8_0_f32_flat.cl +222 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_mxfp4_f32.cl +144 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_mxfp4_f32_flat.cl +167 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32.cl +192 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_1d_16x_flat.cl +307 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_1d_8x_flat.cl +265 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_8x_flat.cl +272 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_0_f32_v.cl +254 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_1_f32.cl +219 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_1_f32_flat.cl +229 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q4_k_f32.cl +180 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q6_k_f32.cl +194 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q6_k_f32_flat.cl +194 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q8_0_f32.cl +125 -0
- package/cpp/ggml/src/ggml-opencl/kernels/mul_mv_q8_0_f32_flat.cl +202 -0
- package/cpp/ggml/src/ggml-opencl/kernels/norm.cl +161 -0
- package/cpp/ggml/src/ggml-opencl/kernels/pad.cl +39 -0
- package/cpp/ggml/src/ggml-opencl/kernels/relu.cl +16 -0
- package/cpp/ggml/src/ggml-opencl/kernels/repeat.cl +38 -0
- package/cpp/ggml/src/ggml-opencl/kernels/rms_norm.cl +190 -0
- package/cpp/ggml/src/ggml-opencl/kernels/rope.cl +747 -0
- package/cpp/ggml/src/ggml-opencl/kernels/scale.cl +27 -0
- package/cpp/ggml/src/ggml-opencl/kernels/set_rows.cl +208 -0
- package/cpp/ggml/src/ggml-opencl/kernels/sigmoid.cl +29 -0
- package/cpp/ggml/src/ggml-opencl/kernels/silu.cl +30 -0
- package/cpp/ggml/src/ggml-opencl/kernels/softmax_4_f16.cl +108 -0
- package/cpp/ggml/src/ggml-opencl/kernels/softmax_4_f32.cl +108 -0
- package/cpp/ggml/src/ggml-opencl/kernels/softmax_f16.cl +107 -0
- package/cpp/ggml/src/ggml-opencl/kernels/softmax_f32.cl +107 -0
- package/cpp/ggml/src/ggml-opencl/kernels/softplus.cl +116 -0
- package/cpp/ggml/src/ggml-opencl/kernels/solve_tri.cl +51 -0
- package/cpp/ggml/src/ggml-opencl/kernels/sqr.cl +53 -0
- package/cpp/ggml/src/ggml-opencl/kernels/sqrt.cl +53 -0
- package/cpp/ggml/src/ggml-opencl/kernels/ssm_conv.cl +77 -0
- package/cpp/ggml/src/ggml-opencl/kernels/sub.cl +138 -0
- package/cpp/ggml/src/ggml-opencl/kernels/sum_rows.cl +140 -0
- package/cpp/ggml/src/ggml-opencl/kernels/tanh.cl +109 -0
- package/cpp/ggml/src/ggml-opencl/kernels/transpose.cl +117 -0
- package/cpp/ggml/src/ggml-opencl/kernels/tri.cl +32 -0
- package/cpp/ggml/src/ggml-opencl/kernels/tsembd.cl +48 -0
- package/cpp/ggml/src/ggml-opencl/kernels/upscale.cl +120 -0
- package/cpp/ggml/src/ggml-opt.cpp +1093 -0
- package/cpp/ggml/src/ggml-quants.c +5325 -0
- package/cpp/ggml/src/ggml-quants.h +106 -0
- package/cpp/ggml/src/ggml-rpc/CMakeLists.txt +9 -0
- package/cpp/ggml/src/ggml-rpc/ggml-rpc.cpp +2118 -0
- package/cpp/ggml/src/ggml-threading.cpp +12 -0
- package/cpp/ggml/src/ggml-threading.h +14 -0
- package/cpp/ggml/src/ggml-virtgpu/CMakeLists.txt +70 -0
- package/cpp/ggml/src/ggml-virtgpu/apir_cs_ggml-rpc-front.cpp +87 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/CMakeLists.txt +21 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/apir_cs_ggml-rpc-back.cpp +115 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-convert.h +13 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-dispatched-backend.cpp +102 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-dispatched-buffer-type.cpp +105 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-dispatched-buffer.cpp +179 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-dispatched-device.cpp +148 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-dispatched.cpp +51 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-dispatched.gen.h +73 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-dispatched.h +27 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend-virgl-apir.h +32 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/backend.cpp +144 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/shared/api_remoting.h +95 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/shared/apir_backend.gen.h +94 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/shared/apir_backend.h +50 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/shared/apir_cs.h +378 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/shared/apir_cs_ggml.h +232 -0
- package/cpp/ggml/src/ggml-virtgpu/backend/shared/apir_cs_rpc.h +58 -0
- package/cpp/ggml/src/ggml-virtgpu/ggml-backend-buffer-type.cpp +81 -0
- package/cpp/ggml/src/ggml-virtgpu/ggml-backend-buffer.cpp +119 -0
- package/cpp/ggml/src/ggml-virtgpu/ggml-backend-device.cpp +158 -0
- package/cpp/ggml/src/ggml-virtgpu/ggml-backend-reg.cpp +213 -0
- package/cpp/ggml/src/ggml-virtgpu/ggml-backend.cpp +69 -0
- package/cpp/ggml/src/ggml-virtgpu/ggml-remoting.h +71 -0
- package/cpp/ggml/src/ggml-virtgpu/ggmlremoting_functions.yaml +166 -0
- package/cpp/ggml/src/ggml-virtgpu/include/apir_hw.h +9 -0
- package/cpp/ggml/src/ggml-virtgpu/regenerate_remoting.py +333 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-apir.h +15 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-forward-backend.cpp +58 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-forward-buffer-type.cpp +110 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-forward-buffer.cpp +173 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-forward-device.cpp +192 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-forward-impl.h +36 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-forward.gen.h +53 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-shm.cpp +98 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-shm.h +23 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-utils.cpp +179 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu-utils.h +86 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu.cpp +544 -0
- package/cpp/ggml/src/ggml-virtgpu/virtgpu.h +117 -0
- package/cpp/ggml/src/ggml-webgpu/CMakeLists.txt +80 -0
- package/cpp/ggml/src/ggml-webgpu/ggml-webgpu-shader-lib.hpp +1231 -0
- package/cpp/ggml/src/ggml-webgpu/ggml-webgpu.cpp +3150 -0
- package/cpp/ggml/src/ggml-webgpu/pre_wgsl.hpp +778 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/argmax.wgsl +72 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/argsort.wgsl +106 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/argsort_merge.wgsl +134 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/binary.wgsl +107 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/common_decls.tmpl +923 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/cpy.tmpl.wgsl +107 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/cumsum.wgsl +66 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/embed_wgsl.py +182 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/flash_attn.wgsl +636 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/get_rows.wgsl +668 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/glu.tmpl.wgsl +323 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/memset.wgsl +40 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat.wgsl +713 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_decls.tmpl +103 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_reg_tile.wgsl +138 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_subgroup_matrix.wgsl +188 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/mul_mat_vec.wgsl +194 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/pad.wgsl +86 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/rms_norm.wgsl +123 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/rope.tmpl.wgsl +295 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/scale.wgsl +63 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/set_rows.wgsl +109 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/soft_max.tmpl.wgsl +345 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/sum_rows.wgsl +55 -0
- package/cpp/ggml/src/ggml-webgpu/wgsl-shaders/unary.wgsl +193 -0
- package/cpp/ggml/src/ggml-zdnn/CMakeLists.txt +36 -0
- package/cpp/ggml/src/ggml-zdnn/common.hpp +59 -0
- package/cpp/ggml/src/ggml-zdnn/ggml-zdnn.cpp +633 -0
- package/cpp/ggml/src/ggml-zdnn/mmf.cpp +80 -0
- package/cpp/ggml/src/ggml-zdnn/mmf.hpp +12 -0
- package/cpp/ggml/src/ggml-zdnn/utils.cpp +79 -0
- package/cpp/ggml/src/ggml-zdnn/utils.hpp +19 -0
- package/cpp/ggml/src/ggml-zendnn/CMakeLists.txt +92 -0
- package/cpp/ggml/src/ggml-zendnn/ggml-zendnn.cpp +469 -0
- package/cpp/ggml/src/ggml.c +7669 -0
- package/cpp/ggml/src/ggml.cpp +26 -0
- package/cpp/ggml/src/gguf.cpp +1699 -0
- package/cpp/include/llama-cpp.h +32 -0
- package/cpp/include/llama.h +1568 -0
- package/cpp/mtmd/CMakeLists.txt +98 -0
- package/cpp/mtmd/README.md +63 -0
- package/cpp/mtmd/clip-graph.h +117 -0
- package/cpp/mtmd/clip-impl.h +586 -0
- package/cpp/mtmd/clip-model.h +390 -0
- package/cpp/mtmd/clip.cpp +4154 -0
- package/cpp/mtmd/clip.h +121 -0
- package/cpp/mtmd/deprecation-warning.cpp +22 -0
- package/cpp/mtmd/legacy-models/convert_image_encoder_to_gguf.py +412 -0
- package/cpp/mtmd/legacy-models/glmedge-convert-image-encoder-to-gguf.py +280 -0
- package/cpp/mtmd/legacy-models/glmedge-surgery.py +33 -0
- package/cpp/mtmd/legacy-models/llava_surgery.py +38 -0
- package/cpp/mtmd/legacy-models/llava_surgery_v2.py +180 -0
- package/cpp/mtmd/legacy-models/minicpmv-convert-image-encoder-to-gguf.py +892 -0
- package/cpp/mtmd/legacy-models/minicpmv-surgery.py +47 -0
- package/cpp/mtmd/models/cogvlm.cpp +98 -0
- package/cpp/mtmd/models/conformer.cpp +216 -0
- package/cpp/mtmd/models/glm4v.cpp +122 -0
- package/cpp/mtmd/models/internvl.cpp +69 -0
- package/cpp/mtmd/models/kimik25.cpp +101 -0
- package/cpp/mtmd/models/kimivl.cpp +63 -0
- package/cpp/mtmd/models/llama4.cpp +96 -0
- package/cpp/mtmd/models/llava.cpp +374 -0
- package/cpp/mtmd/models/minicpmv.cpp +114 -0
- package/cpp/mtmd/models/mobilenetv5.cpp +451 -0
- package/cpp/mtmd/models/models.h +128 -0
- package/cpp/mtmd/models/nemotron-v2-vl.cpp +35 -0
- package/cpp/mtmd/models/paddleocr.cpp +52 -0
- package/cpp/mtmd/models/pixtral.cpp +86 -0
- package/cpp/mtmd/models/qwen2vl.cpp +183 -0
- package/cpp/mtmd/models/qwen3vl.cpp +193 -0
- package/cpp/mtmd/models/siglip.cpp +86 -0
- package/cpp/mtmd/models/whisper-enc.cpp +115 -0
- package/cpp/mtmd/models/youtuvl.cpp +179 -0
- package/cpp/mtmd/mtmd-audio.cpp +730 -0
- package/cpp/mtmd/mtmd-audio.h +113 -0
- package/cpp/mtmd/mtmd-cli.cpp +437 -0
- package/cpp/mtmd/mtmd-helper.cpp +521 -0
- package/cpp/mtmd/mtmd-helper.h +96 -0
- package/cpp/mtmd/mtmd.cpp +1156 -0
- package/cpp/mtmd/mtmd.h +319 -0
- package/cpp/mtmd/requirements.txt +5 -0
- package/cpp/mtmd/test-1.jpeg +0 -0
- package/cpp/mtmd/test-2.mp3 +0 -0
- package/cpp/mtmd/tests.sh +192 -0
- package/cpp/src/CMakeLists.txt +169 -0
- package/cpp/src/llama-adapter.cpp +488 -0
- package/cpp/src/llama-adapter.h +89 -0
- package/cpp/src/llama-arch.cpp +2855 -0
- package/cpp/src/llama-arch.h +619 -0
- package/cpp/src/llama-batch.cpp +917 -0
- package/cpp/src/llama-batch.h +173 -0
- package/cpp/src/llama-chat.cpp +896 -0
- package/cpp/src/llama-chat.h +71 -0
- package/cpp/src/llama-context.cpp +3512 -0
- package/cpp/src/llama-context.h +359 -0
- package/cpp/src/llama-cparams.cpp +5 -0
- package/cpp/src/llama-cparams.h +44 -0
- package/cpp/src/llama-grammar.cpp +1464 -0
- package/cpp/src/llama-grammar.h +194 -0
- package/cpp/src/llama-graph.cpp +2685 -0
- package/cpp/src/llama-graph.h +1026 -0
- package/cpp/src/llama-hparams.cpp +234 -0
- package/cpp/src/llama-hparams.h +339 -0
- package/cpp/src/llama-impl.cpp +171 -0
- package/cpp/src/llama-impl.h +73 -0
- package/cpp/src/llama-io.cpp +15 -0
- package/cpp/src/llama-io.h +35 -0
- package/cpp/src/llama-kv-cache-iswa.cpp +330 -0
- package/cpp/src/llama-kv-cache-iswa.h +137 -0
- package/cpp/src/llama-kv-cache.cpp +2271 -0
- package/cpp/src/llama-kv-cache.h +388 -0
- package/cpp/src/llama-kv-cells.h +533 -0
- package/cpp/src/llama-memory-hybrid-iswa.cpp +275 -0
- package/cpp/src/llama-memory-hybrid-iswa.h +140 -0
- package/cpp/src/llama-memory-hybrid.cpp +268 -0
- package/cpp/src/llama-memory-hybrid.h +139 -0
- package/cpp/src/llama-memory-recurrent.cpp +1165 -0
- package/cpp/src/llama-memory-recurrent.h +182 -0
- package/cpp/src/llama-memory.cpp +59 -0
- package/cpp/src/llama-memory.h +122 -0
- package/cpp/src/llama-mmap.cpp +785 -0
- package/cpp/src/llama-mmap.h +92 -0
- package/cpp/src/llama-model-loader.cpp +1414 -0
- package/cpp/src/llama-model-loader.h +203 -0
- package/cpp/src/llama-model-saver.cpp +286 -0
- package/cpp/src/llama-model-saver.h +37 -0
- package/cpp/src/llama-model.cpp +9253 -0
- package/cpp/src/llama-model.h +576 -0
- package/cpp/src/llama-quant.cpp +1119 -0
- package/cpp/src/llama-quant.h +1 -0
- package/cpp/src/llama-sampler.cpp +3885 -0
- package/cpp/src/llama-sampler.h +42 -0
- package/cpp/src/llama-vocab.cpp +3970 -0
- package/cpp/src/llama-vocab.h +187 -0
- package/cpp/src/llama.cpp +1313 -0
- package/cpp/src/models/afmoe.cpp +191 -0
- package/cpp/src/models/apertus.cpp +125 -0
- package/cpp/src/models/arcee.cpp +135 -0
- package/cpp/src/models/arctic.cpp +138 -0
- package/cpp/src/models/arwkv7.cpp +86 -0
- package/cpp/src/models/baichuan.cpp +122 -0
- package/cpp/src/models/bailingmoe.cpp +144 -0
- package/cpp/src/models/bailingmoe2.cpp +135 -0
- package/cpp/src/models/bert.cpp +178 -0
- package/cpp/src/models/bitnet.cpp +160 -0
- package/cpp/src/models/bloom.cpp +101 -0
- package/cpp/src/models/chameleon.cpp +178 -0
- package/cpp/src/models/chatglm.cpp +132 -0
- package/cpp/src/models/codeshell.cpp +111 -0
- package/cpp/src/models/cogvlm.cpp +102 -0
- package/cpp/src/models/cohere2-iswa.cpp +134 -0
- package/cpp/src/models/command-r.cpp +122 -0
- package/cpp/src/models/dbrx.cpp +123 -0
- package/cpp/src/models/deci.cpp +135 -0
- package/cpp/src/models/deepseek.cpp +144 -0
- package/cpp/src/models/deepseek2.cpp +262 -0
- package/cpp/src/models/delta-net-base.cpp +376 -0
- package/cpp/src/models/dots1.cpp +134 -0
- package/cpp/src/models/dream.cpp +105 -0
- package/cpp/src/models/ernie4-5-moe.cpp +150 -0
- package/cpp/src/models/ernie4-5.cpp +110 -0
- package/cpp/src/models/eurobert.cpp +97 -0
- package/cpp/src/models/exaone-moe.cpp +146 -0
- package/cpp/src/models/exaone.cpp +114 -0
- package/cpp/src/models/exaone4.cpp +123 -0
- package/cpp/src/models/falcon-h1.cpp +111 -0
- package/cpp/src/models/falcon.cpp +120 -0
- package/cpp/src/models/gemma-embedding.cpp +116 -0
- package/cpp/src/models/gemma.cpp +112 -0
- package/cpp/src/models/gemma2-iswa.cpp +128 -0
- package/cpp/src/models/gemma3.cpp +155 -0
- package/cpp/src/models/gemma3n-iswa.cpp +384 -0
- package/cpp/src/models/glm4-moe.cpp +170 -0
- package/cpp/src/models/glm4.cpp +157 -0
- package/cpp/src/models/gpt2.cpp +105 -0
- package/cpp/src/models/gptneox.cpp +144 -0
- package/cpp/src/models/granite-hybrid.cpp +196 -0
- package/cpp/src/models/granite.cpp +211 -0
- package/cpp/src/models/grok.cpp +159 -0
- package/cpp/src/models/grovemoe.cpp +141 -0
- package/cpp/src/models/hunyuan-dense.cpp +132 -0
- package/cpp/src/models/hunyuan-moe.cpp +154 -0
- package/cpp/src/models/internlm2.cpp +120 -0
- package/cpp/src/models/jais.cpp +86 -0
- package/cpp/src/models/jais2.cpp +123 -0
- package/cpp/src/models/jamba.cpp +106 -0
- package/cpp/src/models/kimi-linear.cpp +392 -0
- package/cpp/src/models/lfm2.cpp +190 -0
- package/cpp/src/models/llada-moe.cpp +122 -0
- package/cpp/src/models/llada.cpp +99 -0
- package/cpp/src/models/llama-iswa.cpp +178 -0
- package/cpp/src/models/llama.cpp +168 -0
- package/cpp/src/models/maincoder.cpp +117 -0
- package/cpp/src/models/mamba-base.cpp +285 -0
- package/cpp/src/models/mamba.cpp +54 -0
- package/cpp/src/models/mimo2-iswa.cpp +123 -0
- package/cpp/src/models/minicpm3.cpp +200 -0
- package/cpp/src/models/minimax-m2.cpp +124 -0
- package/cpp/src/models/mistral3.cpp +160 -0
- package/cpp/src/models/models.h +684 -0
- package/cpp/src/models/modern-bert.cpp +109 -0
- package/cpp/src/models/mpt.cpp +126 -0
- package/cpp/src/models/nemotron-h.cpp +148 -0
- package/cpp/src/models/nemotron.cpp +122 -0
- package/cpp/src/models/neo-bert.cpp +104 -0
- package/cpp/src/models/olmo.cpp +121 -0
- package/cpp/src/models/olmo2.cpp +150 -0
- package/cpp/src/models/olmoe.cpp +124 -0
- package/cpp/src/models/openai-moe-iswa.cpp +127 -0
- package/cpp/src/models/openelm.cpp +124 -0
- package/cpp/src/models/orion.cpp +123 -0
- package/cpp/src/models/paddleocr.cpp +122 -0
- package/cpp/src/models/pangu-embedded.cpp +121 -0
- package/cpp/src/models/phi2.cpp +121 -0
- package/cpp/src/models/phi3.cpp +152 -0
- package/cpp/src/models/plamo.cpp +110 -0
- package/cpp/src/models/plamo2.cpp +318 -0
- package/cpp/src/models/plamo3.cpp +128 -0
- package/cpp/src/models/plm.cpp +169 -0
- package/cpp/src/models/qwen.cpp +108 -0
- package/cpp/src/models/qwen2.cpp +126 -0
- package/cpp/src/models/qwen2moe.cpp +151 -0
- package/cpp/src/models/qwen2vl.cpp +117 -0
- package/cpp/src/models/qwen3.cpp +117 -0
- package/cpp/src/models/qwen35.cpp +386 -0
- package/cpp/src/models/qwen35moe.cpp +420 -0
- package/cpp/src/models/qwen3moe.cpp +124 -0
- package/cpp/src/models/qwen3next.cpp +525 -0
- package/cpp/src/models/qwen3vl-moe.cpp +140 -0
- package/cpp/src/models/qwen3vl.cpp +132 -0
- package/cpp/src/models/refact.cpp +94 -0
- package/cpp/src/models/rnd1.cpp +126 -0
- package/cpp/src/models/rwkv6-base.cpp +164 -0
- package/cpp/src/models/rwkv6.cpp +94 -0
- package/cpp/src/models/rwkv6qwen2.cpp +86 -0
- package/cpp/src/models/rwkv7-base.cpp +137 -0
- package/cpp/src/models/rwkv7.cpp +90 -0
- package/cpp/src/models/seed-oss.cpp +124 -0
- package/cpp/src/models/smallthinker.cpp +126 -0
- package/cpp/src/models/smollm3.cpp +128 -0
- package/cpp/src/models/stablelm.cpp +146 -0
- package/cpp/src/models/starcoder.cpp +100 -0
- package/cpp/src/models/starcoder2.cpp +121 -0
- package/cpp/src/models/step35-iswa.cpp +168 -0
- package/cpp/src/models/t5-dec.cpp +166 -0
- package/cpp/src/models/t5-enc.cpp +96 -0
- package/cpp/src/models/wavtokenizer-dec.cpp +149 -0
- package/cpp/src/models/xverse.cpp +108 -0
- package/cpp/src/unicode-data.cpp +7034 -0
- package/cpp/src/unicode-data.h +20 -0
- package/cpp/src/unicode.cpp +1103 -0
- package/cpp/src/unicode.h +111 -0
- package/cpp/vendor/nlohmann/json.hpp +25526 -0
- package/cpp/vendor/nlohmann/json_fwd.hpp +187 -0
- package/cpp/vendor/stb/stb_image.h +7988 -0
- package/ios/LocalLLM-Bridging-Header.h +2 -0
- package/ios/LocalLLM.h +5 -0
- package/ios/LocalLLM.mm +1267 -0
- package/local-llm-rn.podspec +60 -0
- package/package.json +35 -0
- package/src/NativeLocalLLM.ts +73 -0
- package/src/device.ts +50 -0
- package/src/download-adapter.ts +17 -0
- package/src/index.ts +21 -0
- package/src/native-bridge.ts +142 -0
- package/src/rn-downloader.ts +37 -0
|
@@ -0,0 +1,1137 @@
|
|
|
1
|
+
#include "console.h"
|
|
2
|
+
#include "log.h"
|
|
3
|
+
#include <vector>
|
|
4
|
+
#include <iostream>
|
|
5
|
+
#include <cassert>
|
|
6
|
+
#include <cstddef>
|
|
7
|
+
#include <cctype>
|
|
8
|
+
#include <cwctype>
|
|
9
|
+
#include <cstdint>
|
|
10
|
+
#include <condition_variable>
|
|
11
|
+
#include <mutex>
|
|
12
|
+
#include <thread>
|
|
13
|
+
#include <stdarg.h>
|
|
14
|
+
|
|
15
|
+
#if defined(_WIN32)
|
|
16
|
+
#define WIN32_LEAN_AND_MEAN
|
|
17
|
+
#ifndef NOMINMAX
|
|
18
|
+
#define NOMINMAX
|
|
19
|
+
#endif
|
|
20
|
+
#include <windows.h>
|
|
21
|
+
#include <fcntl.h>
|
|
22
|
+
#include <io.h>
|
|
23
|
+
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
|
|
24
|
+
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
|
|
25
|
+
#endif
|
|
26
|
+
#else
|
|
27
|
+
#include <climits>
|
|
28
|
+
#include <sys/ioctl.h>
|
|
29
|
+
#include <unistd.h>
|
|
30
|
+
#include <wchar.h>
|
|
31
|
+
#include <stdio.h>
|
|
32
|
+
#include <stdlib.h>
|
|
33
|
+
#include <signal.h>
|
|
34
|
+
#include <termios.h>
|
|
35
|
+
#endif
|
|
36
|
+
|
|
37
|
+
#define ANSI_COLOR_RED "\x1b[31m"
|
|
38
|
+
#define ANSI_COLOR_GREEN "\x1b[32m"
|
|
39
|
+
#define ANSI_COLOR_YELLOW "\x1b[33m"
|
|
40
|
+
#define ANSI_COLOR_BLUE "\x1b[34m"
|
|
41
|
+
#define ANSI_COLOR_MAGENTA "\x1b[35m"
|
|
42
|
+
#define ANSI_COLOR_CYAN "\x1b[36m"
|
|
43
|
+
#define ANSI_COLOR_GRAY "\x1b[90m"
|
|
44
|
+
#define ANSI_COLOR_RESET "\x1b[0m"
|
|
45
|
+
#define ANSI_BOLD "\x1b[1m"
|
|
46
|
+
|
|
47
|
+
namespace console {
|
|
48
|
+
|
|
49
|
+
#if defined (_WIN32)
|
|
50
|
+
namespace {
|
|
51
|
+
// Use private-use unicode values to represent special keys that are not reported
|
|
52
|
+
// as characters (e.g. arrows on Windows). These values should never clash with
|
|
53
|
+
// real input and let the rest of the code handle navigation uniformly.
|
|
54
|
+
static constexpr char32_t KEY_ARROW_LEFT = 0xE000;
|
|
55
|
+
static constexpr char32_t KEY_ARROW_RIGHT = 0xE001;
|
|
56
|
+
static constexpr char32_t KEY_ARROW_UP = 0xE002;
|
|
57
|
+
static constexpr char32_t KEY_ARROW_DOWN = 0xE003;
|
|
58
|
+
static constexpr char32_t KEY_HOME = 0xE004;
|
|
59
|
+
static constexpr char32_t KEY_END = 0xE005;
|
|
60
|
+
static constexpr char32_t KEY_CTRL_ARROW_LEFT = 0xE006;
|
|
61
|
+
static constexpr char32_t KEY_CTRL_ARROW_RIGHT = 0xE007;
|
|
62
|
+
static constexpr char32_t KEY_DELETE = 0xE008;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
//
|
|
66
|
+
// Console state
|
|
67
|
+
//
|
|
68
|
+
#endif
|
|
69
|
+
|
|
70
|
+
static bool advanced_display = false;
|
|
71
|
+
static bool simple_io = true;
|
|
72
|
+
static display_type current_display = DISPLAY_TYPE_RESET;
|
|
73
|
+
|
|
74
|
+
static FILE* out = stdout;
|
|
75
|
+
|
|
76
|
+
#if defined (_WIN32)
|
|
77
|
+
static void* hConsole;
|
|
78
|
+
#else
|
|
79
|
+
static FILE* tty = nullptr;
|
|
80
|
+
static termios initial_state;
|
|
81
|
+
#endif
|
|
82
|
+
|
|
83
|
+
//
|
|
84
|
+
// Init and cleanup
|
|
85
|
+
//
|
|
86
|
+
|
|
87
|
+
void init(bool use_simple_io, bool use_advanced_display) {
|
|
88
|
+
advanced_display = use_advanced_display;
|
|
89
|
+
simple_io = use_simple_io;
|
|
90
|
+
#if defined(_WIN32)
|
|
91
|
+
// Windows-specific console initialization
|
|
92
|
+
DWORD dwMode = 0;
|
|
93
|
+
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
94
|
+
if (hConsole == INVALID_HANDLE_VALUE || !GetConsoleMode(hConsole, &dwMode)) {
|
|
95
|
+
hConsole = GetStdHandle(STD_ERROR_HANDLE);
|
|
96
|
+
if (hConsole != INVALID_HANDLE_VALUE && (!GetConsoleMode(hConsole, &dwMode))) {
|
|
97
|
+
hConsole = nullptr;
|
|
98
|
+
simple_io = true;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (hConsole) {
|
|
102
|
+
// Check conditions combined to reduce nesting
|
|
103
|
+
if (advanced_display && !(dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) &&
|
|
104
|
+
!SetConsoleMode(hConsole, dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) {
|
|
105
|
+
advanced_display = false;
|
|
106
|
+
}
|
|
107
|
+
// Set console output codepage to UTF8
|
|
108
|
+
SetConsoleOutputCP(CP_UTF8);
|
|
109
|
+
}
|
|
110
|
+
HANDLE hConIn = GetStdHandle(STD_INPUT_HANDLE);
|
|
111
|
+
if (hConIn != INVALID_HANDLE_VALUE && GetConsoleMode(hConIn, &dwMode)) {
|
|
112
|
+
// Set console input codepage to UTF16
|
|
113
|
+
_setmode(_fileno(stdin), _O_WTEXT);
|
|
114
|
+
|
|
115
|
+
// Set ICANON (ENABLE_LINE_INPUT) and ECHO (ENABLE_ECHO_INPUT)
|
|
116
|
+
if (simple_io) {
|
|
117
|
+
dwMode |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
|
|
118
|
+
} else {
|
|
119
|
+
dwMode &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
|
|
120
|
+
}
|
|
121
|
+
if (!SetConsoleMode(hConIn, dwMode)) {
|
|
122
|
+
simple_io = true;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
if (simple_io) {
|
|
126
|
+
_setmode(_fileno(stdin), _O_U8TEXT);
|
|
127
|
+
}
|
|
128
|
+
#else
|
|
129
|
+
// POSIX-specific console initialization
|
|
130
|
+
if (!simple_io) {
|
|
131
|
+
struct termios new_termios;
|
|
132
|
+
tcgetattr(STDIN_FILENO, &initial_state);
|
|
133
|
+
new_termios = initial_state;
|
|
134
|
+
new_termios.c_lflag &= ~(ICANON | ECHO);
|
|
135
|
+
new_termios.c_cc[VMIN] = 1;
|
|
136
|
+
new_termios.c_cc[VTIME] = 0;
|
|
137
|
+
tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);
|
|
138
|
+
|
|
139
|
+
tty = fopen("/dev/tty", "w+");
|
|
140
|
+
if (tty != nullptr) {
|
|
141
|
+
out = tty;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
setlocale(LC_ALL, "");
|
|
146
|
+
#endif
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
void cleanup() {
|
|
150
|
+
// Reset console display
|
|
151
|
+
set_display(DISPLAY_TYPE_RESET);
|
|
152
|
+
|
|
153
|
+
#if !defined(_WIN32)
|
|
154
|
+
// Restore settings on POSIX systems
|
|
155
|
+
if (!simple_io) {
|
|
156
|
+
if (tty != nullptr) {
|
|
157
|
+
out = stdout;
|
|
158
|
+
fclose(tty);
|
|
159
|
+
tty = nullptr;
|
|
160
|
+
}
|
|
161
|
+
tcsetattr(STDIN_FILENO, TCSANOW, &initial_state);
|
|
162
|
+
}
|
|
163
|
+
#endif
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
//
|
|
167
|
+
// Display and IO
|
|
168
|
+
//
|
|
169
|
+
|
|
170
|
+
// Keep track of current display and only emit ANSI code if it changes
|
|
171
|
+
void set_display(display_type display) {
|
|
172
|
+
if (advanced_display && current_display != display) {
|
|
173
|
+
common_log_flush(common_log_main());
|
|
174
|
+
switch(display) {
|
|
175
|
+
case DISPLAY_TYPE_RESET:
|
|
176
|
+
fprintf(out, ANSI_COLOR_RESET);
|
|
177
|
+
break;
|
|
178
|
+
case DISPLAY_TYPE_INFO:
|
|
179
|
+
fprintf(out, ANSI_COLOR_MAGENTA);
|
|
180
|
+
break;
|
|
181
|
+
case DISPLAY_TYPE_PROMPT:
|
|
182
|
+
fprintf(out, ANSI_COLOR_YELLOW);
|
|
183
|
+
break;
|
|
184
|
+
case DISPLAY_TYPE_REASONING:
|
|
185
|
+
fprintf(out, ANSI_COLOR_GRAY);
|
|
186
|
+
break;
|
|
187
|
+
case DISPLAY_TYPE_USER_INPUT:
|
|
188
|
+
fprintf(out, ANSI_BOLD ANSI_COLOR_GREEN);
|
|
189
|
+
break;
|
|
190
|
+
case DISPLAY_TYPE_ERROR:
|
|
191
|
+
fprintf(out, ANSI_BOLD ANSI_COLOR_RED);
|
|
192
|
+
}
|
|
193
|
+
current_display = display;
|
|
194
|
+
fflush(out);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
static char32_t getchar32() {
|
|
199
|
+
#if defined(_WIN32)
|
|
200
|
+
HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);
|
|
201
|
+
wchar_t high_surrogate = 0;
|
|
202
|
+
|
|
203
|
+
while (true) {
|
|
204
|
+
INPUT_RECORD record;
|
|
205
|
+
DWORD count;
|
|
206
|
+
if (!ReadConsoleInputW(hConsole, &record, 1, &count) || count == 0) {
|
|
207
|
+
return WEOF;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown) {
|
|
211
|
+
wchar_t wc = record.Event.KeyEvent.uChar.UnicodeChar;
|
|
212
|
+
if (wc == 0) {
|
|
213
|
+
const DWORD ctrl_mask = LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED;
|
|
214
|
+
const bool ctrl_pressed = (record.Event.KeyEvent.dwControlKeyState & ctrl_mask) != 0;
|
|
215
|
+
switch (record.Event.KeyEvent.wVirtualKeyCode) {
|
|
216
|
+
case VK_LEFT: return ctrl_pressed ? KEY_CTRL_ARROW_LEFT : KEY_ARROW_LEFT;
|
|
217
|
+
case VK_RIGHT: return ctrl_pressed ? KEY_CTRL_ARROW_RIGHT : KEY_ARROW_RIGHT;
|
|
218
|
+
case VK_UP: return KEY_ARROW_UP;
|
|
219
|
+
case VK_DOWN: return KEY_ARROW_DOWN;
|
|
220
|
+
case VK_HOME: return KEY_HOME;
|
|
221
|
+
case VK_END: return KEY_END;
|
|
222
|
+
case VK_DELETE: return KEY_DELETE;
|
|
223
|
+
default: continue;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if ((wc >= 0xD800) && (wc <= 0xDBFF)) { // Check if wc is a high surrogate
|
|
228
|
+
high_surrogate = wc;
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
if ((wc >= 0xDC00) && (wc <= 0xDFFF)) { // Check if wc is a low surrogate
|
|
232
|
+
if (high_surrogate != 0) { // Check if we have a high surrogate
|
|
233
|
+
return ((high_surrogate - 0xD800) << 10) + (wc - 0xDC00) + 0x10000;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
high_surrogate = 0; // Reset the high surrogate
|
|
238
|
+
return static_cast<char32_t>(wc);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
#else
|
|
242
|
+
wchar_t wc = getwchar();
|
|
243
|
+
if (static_cast<wint_t>(wc) == WEOF) {
|
|
244
|
+
return WEOF;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
#if WCHAR_MAX == 0xFFFF
|
|
248
|
+
if ((wc >= 0xD800) && (wc <= 0xDBFF)) { // Check if wc is a high surrogate
|
|
249
|
+
wchar_t low_surrogate = getwchar();
|
|
250
|
+
if ((low_surrogate >= 0xDC00) && (low_surrogate <= 0xDFFF)) { // Check if the next wchar is a low surrogate
|
|
251
|
+
return (static_cast<char32_t>(wc & 0x03FF) << 10) + (low_surrogate & 0x03FF) + 0x10000;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if ((wc >= 0xD800) && (wc <= 0xDFFF)) { // Invalid surrogate pair
|
|
255
|
+
return 0xFFFD; // Return the replacement character U+FFFD
|
|
256
|
+
}
|
|
257
|
+
#endif
|
|
258
|
+
|
|
259
|
+
return static_cast<char32_t>(wc);
|
|
260
|
+
#endif
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
static void pop_cursor() {
|
|
264
|
+
#if defined(_WIN32)
|
|
265
|
+
if (hConsole != NULL) {
|
|
266
|
+
CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
|
|
267
|
+
GetConsoleScreenBufferInfo(hConsole, &bufferInfo);
|
|
268
|
+
|
|
269
|
+
COORD newCursorPosition = bufferInfo.dwCursorPosition;
|
|
270
|
+
if (newCursorPosition.X == 0) {
|
|
271
|
+
newCursorPosition.X = bufferInfo.dwSize.X - 1;
|
|
272
|
+
newCursorPosition.Y -= 1;
|
|
273
|
+
} else {
|
|
274
|
+
newCursorPosition.X -= 1;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
SetConsoleCursorPosition(hConsole, newCursorPosition);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
#endif
|
|
281
|
+
putc('\b', out);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
static int estimateWidth(char32_t codepoint) {
|
|
285
|
+
#if defined(_WIN32)
|
|
286
|
+
(void)codepoint;
|
|
287
|
+
return 1;
|
|
288
|
+
#else
|
|
289
|
+
return wcwidth(codepoint);
|
|
290
|
+
#endif
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
static int put_codepoint(const char* utf8_codepoint, size_t length, int expectedWidth) {
|
|
294
|
+
#if defined(_WIN32)
|
|
295
|
+
CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
|
|
296
|
+
if (!GetConsoleScreenBufferInfo(hConsole, &bufferInfo)) {
|
|
297
|
+
// go with the default
|
|
298
|
+
return expectedWidth;
|
|
299
|
+
}
|
|
300
|
+
COORD initialPosition = bufferInfo.dwCursorPosition;
|
|
301
|
+
DWORD nNumberOfChars = length;
|
|
302
|
+
WriteConsole(hConsole, utf8_codepoint, nNumberOfChars, &nNumberOfChars, NULL);
|
|
303
|
+
|
|
304
|
+
CONSOLE_SCREEN_BUFFER_INFO newBufferInfo;
|
|
305
|
+
GetConsoleScreenBufferInfo(hConsole, &newBufferInfo);
|
|
306
|
+
|
|
307
|
+
// Figure out our real position if we're in the last column
|
|
308
|
+
if (utf8_codepoint[0] != 0x09 && initialPosition.X == newBufferInfo.dwSize.X - 1) {
|
|
309
|
+
DWORD nNumberOfChars;
|
|
310
|
+
WriteConsole(hConsole, &" \b", 2, &nNumberOfChars, NULL);
|
|
311
|
+
GetConsoleScreenBufferInfo(hConsole, &newBufferInfo);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
int width = newBufferInfo.dwCursorPosition.X - initialPosition.X;
|
|
315
|
+
if (width < 0) {
|
|
316
|
+
width += newBufferInfo.dwSize.X;
|
|
317
|
+
}
|
|
318
|
+
return width;
|
|
319
|
+
#else
|
|
320
|
+
// We can trust expectedWidth if we've got one
|
|
321
|
+
if (expectedWidth >= 0 || tty == nullptr) {
|
|
322
|
+
fwrite(utf8_codepoint, length, 1, out);
|
|
323
|
+
return expectedWidth;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
fputs("\033[6n", tty); // Query cursor position
|
|
327
|
+
int x1;
|
|
328
|
+
int y1;
|
|
329
|
+
int x2;
|
|
330
|
+
int y2;
|
|
331
|
+
int results = 0;
|
|
332
|
+
results = fscanf(tty, "\033[%d;%dR", &y1, &x1);
|
|
333
|
+
|
|
334
|
+
fwrite(utf8_codepoint, length, 1, tty);
|
|
335
|
+
|
|
336
|
+
fputs("\033[6n", tty); // Query cursor position
|
|
337
|
+
results += fscanf(tty, "\033[%d;%dR", &y2, &x2);
|
|
338
|
+
|
|
339
|
+
if (results != 4) {
|
|
340
|
+
return expectedWidth;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
int width = x2 - x1;
|
|
344
|
+
if (width < 0) {
|
|
345
|
+
// Calculate the width considering text wrapping
|
|
346
|
+
struct winsize w;
|
|
347
|
+
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
|
348
|
+
width += w.ws_col;
|
|
349
|
+
}
|
|
350
|
+
return width;
|
|
351
|
+
#endif
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
static void replace_last(char ch) {
|
|
355
|
+
#if defined(_WIN32)
|
|
356
|
+
pop_cursor();
|
|
357
|
+
put_codepoint(&ch, 1, 1);
|
|
358
|
+
#else
|
|
359
|
+
fprintf(out, "\b%c", ch);
|
|
360
|
+
#endif
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
static char32_t decode_utf8(const std::string & input, size_t pos, size_t & advance) {
|
|
364
|
+
unsigned char c = static_cast<unsigned char>(input[pos]);
|
|
365
|
+
if ((c & 0x80u) == 0u) {
|
|
366
|
+
advance = 1;
|
|
367
|
+
return c;
|
|
368
|
+
}
|
|
369
|
+
if ((c & 0xE0u) == 0xC0u && pos + 1 < input.size()) {
|
|
370
|
+
unsigned char c1 = static_cast<unsigned char>(input[pos + 1]);
|
|
371
|
+
if ((c1 & 0xC0u) != 0x80u) {
|
|
372
|
+
advance = 1;
|
|
373
|
+
return 0xFFFD;
|
|
374
|
+
}
|
|
375
|
+
advance = 2;
|
|
376
|
+
return ((c & 0x1Fu) << 6) | (static_cast<unsigned char>(input[pos + 1]) & 0x3Fu);
|
|
377
|
+
}
|
|
378
|
+
if ((c & 0xF0u) == 0xE0u && pos + 2 < input.size()) {
|
|
379
|
+
unsigned char c1 = static_cast<unsigned char>(input[pos + 1]);
|
|
380
|
+
unsigned char c2 = static_cast<unsigned char>(input[pos + 2]);
|
|
381
|
+
if ((c1 & 0xC0u) != 0x80u || (c2 & 0xC0u) != 0x80u) {
|
|
382
|
+
advance = 1;
|
|
383
|
+
return 0xFFFD;
|
|
384
|
+
}
|
|
385
|
+
advance = 3;
|
|
386
|
+
return ((c & 0x0Fu) << 12) |
|
|
387
|
+
((static_cast<unsigned char>(input[pos + 1]) & 0x3Fu) << 6) |
|
|
388
|
+
(static_cast<unsigned char>(input[pos + 2]) & 0x3Fu);
|
|
389
|
+
}
|
|
390
|
+
if ((c & 0xF8u) == 0xF0u && pos + 3 < input.size()) {
|
|
391
|
+
unsigned char c1 = static_cast<unsigned char>(input[pos + 1]);
|
|
392
|
+
unsigned char c2 = static_cast<unsigned char>(input[pos + 2]);
|
|
393
|
+
unsigned char c3 = static_cast<unsigned char>(input[pos + 3]);
|
|
394
|
+
if ((c1 & 0xC0u) != 0x80u || (c2 & 0xC0u) != 0x80u || (c3 & 0xC0u) != 0x80u) {
|
|
395
|
+
advance = 1;
|
|
396
|
+
return 0xFFFD;
|
|
397
|
+
}
|
|
398
|
+
advance = 4;
|
|
399
|
+
return ((c & 0x07u) << 18) |
|
|
400
|
+
((static_cast<unsigned char>(input[pos + 1]) & 0x3Fu) << 12) |
|
|
401
|
+
((static_cast<unsigned char>(input[pos + 2]) & 0x3Fu) << 6) |
|
|
402
|
+
(static_cast<unsigned char>(input[pos + 3]) & 0x3Fu);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
advance = 1;
|
|
406
|
+
return 0xFFFD; // replacement character for invalid input
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
static void append_utf8(char32_t ch, std::string & out) {
|
|
410
|
+
if (ch <= 0x7F) {
|
|
411
|
+
out.push_back(static_cast<unsigned char>(ch));
|
|
412
|
+
} else if (ch <= 0x7FF) {
|
|
413
|
+
out.push_back(static_cast<unsigned char>(0xC0 | ((ch >> 6) & 0x1F)));
|
|
414
|
+
out.push_back(static_cast<unsigned char>(0x80 | (ch & 0x3F)));
|
|
415
|
+
} else if (ch <= 0xFFFF) {
|
|
416
|
+
out.push_back(static_cast<unsigned char>(0xE0 | ((ch >> 12) & 0x0F)));
|
|
417
|
+
out.push_back(static_cast<unsigned char>(0x80 | ((ch >> 6) & 0x3F)));
|
|
418
|
+
out.push_back(static_cast<unsigned char>(0x80 | (ch & 0x3F)));
|
|
419
|
+
} else if (ch <= 0x10FFFF) {
|
|
420
|
+
out.push_back(static_cast<unsigned char>(0xF0 | ((ch >> 18) & 0x07)));
|
|
421
|
+
out.push_back(static_cast<unsigned char>(0x80 | ((ch >> 12) & 0x3F)));
|
|
422
|
+
out.push_back(static_cast<unsigned char>(0x80 | ((ch >> 6) & 0x3F)));
|
|
423
|
+
out.push_back(static_cast<unsigned char>(0x80 | (ch & 0x3F)));
|
|
424
|
+
} else {
|
|
425
|
+
// Invalid Unicode code point
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// Helper function to remove the last UTF-8 character from a string
|
|
430
|
+
static size_t prev_utf8_char_pos(const std::string & line, size_t pos) {
|
|
431
|
+
if (pos == 0) return 0;
|
|
432
|
+
pos--;
|
|
433
|
+
while (pos > 0 && (line[pos] & 0xC0) == 0x80) {
|
|
434
|
+
pos--;
|
|
435
|
+
}
|
|
436
|
+
return pos;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
static size_t next_utf8_char_pos(const std::string & line, size_t pos) {
|
|
440
|
+
if (pos >= line.length()) return line.length();
|
|
441
|
+
pos++;
|
|
442
|
+
while (pos < line.length() && (line[pos] & 0xC0) == 0x80) {
|
|
443
|
+
pos++;
|
|
444
|
+
}
|
|
445
|
+
return pos;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
static void move_cursor(int delta);
|
|
449
|
+
static void move_word_left(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line);
|
|
450
|
+
static void move_word_right(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line);
|
|
451
|
+
static void move_to_line_start(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths);
|
|
452
|
+
static void move_to_line_end(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line);
|
|
453
|
+
|
|
454
|
+
static void delete_at_cursor(std::string & line, std::vector<int> & widths, size_t & char_pos, size_t & byte_pos) {
|
|
455
|
+
if (char_pos >= widths.size()) {
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
size_t next_pos = next_utf8_char_pos(line, byte_pos);
|
|
460
|
+
int w = widths[char_pos];
|
|
461
|
+
size_t char_len = next_pos - byte_pos;
|
|
462
|
+
|
|
463
|
+
line.erase(byte_pos, char_len);
|
|
464
|
+
widths.erase(widths.begin() + char_pos);
|
|
465
|
+
|
|
466
|
+
size_t p = byte_pos;
|
|
467
|
+
int tail_width = 0;
|
|
468
|
+
for (size_t i = char_pos; i < widths.size(); ++i) {
|
|
469
|
+
size_t following = next_utf8_char_pos(line, p);
|
|
470
|
+
put_codepoint(line.c_str() + p, following - p, widths[i]);
|
|
471
|
+
tail_width += widths[i];
|
|
472
|
+
p = following;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
for (int i = 0; i < w; ++i) {
|
|
476
|
+
fputc(' ', out);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
move_cursor(-(tail_width + w));
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
static void clear_current_line(const std::vector<int> & widths) {
|
|
483
|
+
int total_width = 0;
|
|
484
|
+
for (int w : widths) {
|
|
485
|
+
total_width += (w > 0 ? w : 1);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if (total_width > 0) {
|
|
489
|
+
std::string spaces(total_width, ' ');
|
|
490
|
+
fwrite(spaces.c_str(), 1, total_width, out);
|
|
491
|
+
move_cursor(-total_width);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
static void set_line_contents(std::string new_line, std::string & line, std::vector<int> & widths, size_t & char_pos,
|
|
496
|
+
size_t & byte_pos) {
|
|
497
|
+
move_to_line_start(char_pos, byte_pos, widths);
|
|
498
|
+
clear_current_line(widths);
|
|
499
|
+
|
|
500
|
+
line = std::move(new_line);
|
|
501
|
+
widths.clear();
|
|
502
|
+
byte_pos = 0;
|
|
503
|
+
char_pos = 0;
|
|
504
|
+
|
|
505
|
+
size_t idx = 0;
|
|
506
|
+
while (idx < line.size()) {
|
|
507
|
+
size_t advance = 0;
|
|
508
|
+
char32_t cp = decode_utf8(line, idx, advance);
|
|
509
|
+
int expected_width = estimateWidth(cp);
|
|
510
|
+
int real_width = put_codepoint(line.c_str() + idx, advance, expected_width);
|
|
511
|
+
if (real_width < 0) real_width = 0;
|
|
512
|
+
widths.push_back(real_width);
|
|
513
|
+
idx += advance;
|
|
514
|
+
++char_pos;
|
|
515
|
+
byte_pos = idx;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
static void move_to_line_start(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths) {
|
|
520
|
+
int back_width = 0;
|
|
521
|
+
for (size_t i = 0; i < char_pos; ++i) {
|
|
522
|
+
back_width += widths[i];
|
|
523
|
+
}
|
|
524
|
+
move_cursor(-back_width);
|
|
525
|
+
char_pos = 0;
|
|
526
|
+
byte_pos = 0;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
static void move_to_line_end(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line) {
|
|
530
|
+
int forward_width = 0;
|
|
531
|
+
for (size_t i = char_pos; i < widths.size(); ++i) {
|
|
532
|
+
forward_width += widths[i];
|
|
533
|
+
}
|
|
534
|
+
move_cursor(forward_width);
|
|
535
|
+
char_pos = widths.size();
|
|
536
|
+
byte_pos = line.length();
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
static bool has_ctrl_modifier(const std::string & params) {
|
|
540
|
+
size_t start = 0;
|
|
541
|
+
while (start < params.size()) {
|
|
542
|
+
size_t end = params.find(';', start);
|
|
543
|
+
size_t len = (end == std::string::npos) ? params.size() - start : end - start;
|
|
544
|
+
if (len > 0) {
|
|
545
|
+
int value = 0;
|
|
546
|
+
for (size_t i = 0; i < len; ++i) {
|
|
547
|
+
char ch = params[start + i];
|
|
548
|
+
if (!std::isdigit(static_cast<unsigned char>(ch))) {
|
|
549
|
+
value = -1;
|
|
550
|
+
break;
|
|
551
|
+
}
|
|
552
|
+
value = value * 10 + (ch - '0');
|
|
553
|
+
}
|
|
554
|
+
if (value == 5) {
|
|
555
|
+
return true;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
if (end == std::string::npos) {
|
|
560
|
+
break;
|
|
561
|
+
}
|
|
562
|
+
start = end + 1;
|
|
563
|
+
}
|
|
564
|
+
return false;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
static bool is_space_codepoint(char32_t cp) {
|
|
568
|
+
return std::iswspace(static_cast<wint_t>(cp)) != 0;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
static void move_word_left(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line) {
|
|
572
|
+
if (char_pos == 0) {
|
|
573
|
+
return;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
size_t new_char_pos = char_pos;
|
|
577
|
+
size_t new_byte_pos = byte_pos;
|
|
578
|
+
int move_width = 0;
|
|
579
|
+
|
|
580
|
+
while (new_char_pos > 0) {
|
|
581
|
+
size_t prev_byte = prev_utf8_char_pos(line, new_byte_pos);
|
|
582
|
+
size_t advance = 0;
|
|
583
|
+
char32_t cp = decode_utf8(line, prev_byte, advance);
|
|
584
|
+
if (!is_space_codepoint(cp)) {
|
|
585
|
+
break;
|
|
586
|
+
}
|
|
587
|
+
move_width += widths[new_char_pos - 1];
|
|
588
|
+
new_char_pos--;
|
|
589
|
+
new_byte_pos = prev_byte;
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
while (new_char_pos > 0) {
|
|
593
|
+
size_t prev_byte = prev_utf8_char_pos(line, new_byte_pos);
|
|
594
|
+
size_t advance = 0;
|
|
595
|
+
char32_t cp = decode_utf8(line, prev_byte, advance);
|
|
596
|
+
if (is_space_codepoint(cp)) {
|
|
597
|
+
break;
|
|
598
|
+
}
|
|
599
|
+
move_width += widths[new_char_pos - 1];
|
|
600
|
+
new_char_pos--;
|
|
601
|
+
new_byte_pos = prev_byte;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
move_cursor(-move_width);
|
|
605
|
+
char_pos = new_char_pos;
|
|
606
|
+
byte_pos = new_byte_pos;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
static void move_word_right(size_t & char_pos, size_t & byte_pos, const std::vector<int> & widths, const std::string & line) {
|
|
610
|
+
if (char_pos >= widths.size()) {
|
|
611
|
+
return;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
size_t new_char_pos = char_pos;
|
|
615
|
+
size_t new_byte_pos = byte_pos;
|
|
616
|
+
int move_width = 0;
|
|
617
|
+
|
|
618
|
+
while (new_char_pos < widths.size()) {
|
|
619
|
+
size_t advance = 0;
|
|
620
|
+
char32_t cp = decode_utf8(line, new_byte_pos, advance);
|
|
621
|
+
if (!is_space_codepoint(cp)) {
|
|
622
|
+
break;
|
|
623
|
+
}
|
|
624
|
+
move_width += widths[new_char_pos];
|
|
625
|
+
new_char_pos++;
|
|
626
|
+
new_byte_pos += advance;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
while (new_char_pos < widths.size()) {
|
|
630
|
+
size_t advance = 0;
|
|
631
|
+
char32_t cp = decode_utf8(line, new_byte_pos, advance);
|
|
632
|
+
if (is_space_codepoint(cp)) {
|
|
633
|
+
break;
|
|
634
|
+
}
|
|
635
|
+
move_width += widths[new_char_pos];
|
|
636
|
+
new_char_pos++;
|
|
637
|
+
new_byte_pos += advance;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
while (new_char_pos < widths.size()) {
|
|
641
|
+
size_t advance = 0;
|
|
642
|
+
char32_t cp = decode_utf8(line, new_byte_pos, advance);
|
|
643
|
+
if (!is_space_codepoint(cp)) {
|
|
644
|
+
break;
|
|
645
|
+
}
|
|
646
|
+
move_width += widths[new_char_pos];
|
|
647
|
+
new_char_pos++;
|
|
648
|
+
new_byte_pos += advance;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
move_cursor(move_width);
|
|
652
|
+
char_pos = new_char_pos;
|
|
653
|
+
byte_pos = new_byte_pos;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
static void move_cursor(int delta) {
|
|
657
|
+
if (delta == 0) return;
|
|
658
|
+
#if defined(_WIN32)
|
|
659
|
+
if (hConsole != NULL) {
|
|
660
|
+
CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
|
|
661
|
+
GetConsoleScreenBufferInfo(hConsole, &bufferInfo);
|
|
662
|
+
COORD newCursorPosition = bufferInfo.dwCursorPosition;
|
|
663
|
+
int width = bufferInfo.dwSize.X;
|
|
664
|
+
int newX = newCursorPosition.X + delta;
|
|
665
|
+
int newY = newCursorPosition.Y;
|
|
666
|
+
|
|
667
|
+
while (newX >= width) {
|
|
668
|
+
newX -= width;
|
|
669
|
+
newY++;
|
|
670
|
+
}
|
|
671
|
+
while (newX < 0) {
|
|
672
|
+
newX += width;
|
|
673
|
+
newY--;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
newCursorPosition.X = newX;
|
|
677
|
+
newCursorPosition.Y = newY;
|
|
678
|
+
SetConsoleCursorPosition(hConsole, newCursorPosition);
|
|
679
|
+
}
|
|
680
|
+
#else
|
|
681
|
+
if (delta < 0) {
|
|
682
|
+
for (int i = 0; i < -delta; i++) fprintf(out, "\b");
|
|
683
|
+
} else {
|
|
684
|
+
for (int i = 0; i < delta; i++) fprintf(out, "\033[C");
|
|
685
|
+
}
|
|
686
|
+
#endif
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
struct history_t {
|
|
690
|
+
std::vector<std::string> entries;
|
|
691
|
+
size_t viewing_idx = SIZE_MAX;
|
|
692
|
+
std::string backup_line; // current line before viewing history
|
|
693
|
+
void add(const std::string & line) {
|
|
694
|
+
if (line.empty()) {
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
// avoid duplicates with the last entry
|
|
698
|
+
if (entries.empty() || entries.back() != line) {
|
|
699
|
+
entries.push_back(line);
|
|
700
|
+
}
|
|
701
|
+
// also clear viewing state
|
|
702
|
+
end_viewing();
|
|
703
|
+
}
|
|
704
|
+
bool prev(std::string & cur_line) {
|
|
705
|
+
if (entries.empty()) {
|
|
706
|
+
return false;
|
|
707
|
+
}
|
|
708
|
+
if (viewing_idx == SIZE_MAX) {
|
|
709
|
+
return false;
|
|
710
|
+
}
|
|
711
|
+
if (viewing_idx > 0) {
|
|
712
|
+
viewing_idx--;
|
|
713
|
+
}
|
|
714
|
+
cur_line = entries[viewing_idx];
|
|
715
|
+
return true;
|
|
716
|
+
}
|
|
717
|
+
bool next(std::string & cur_line) {
|
|
718
|
+
if (entries.empty() || viewing_idx == SIZE_MAX) {
|
|
719
|
+
return false;
|
|
720
|
+
}
|
|
721
|
+
viewing_idx++;
|
|
722
|
+
if (viewing_idx >= entries.size()) {
|
|
723
|
+
cur_line = backup_line;
|
|
724
|
+
end_viewing();
|
|
725
|
+
} else {
|
|
726
|
+
cur_line = entries[viewing_idx];
|
|
727
|
+
}
|
|
728
|
+
return true;
|
|
729
|
+
}
|
|
730
|
+
void begin_viewing(const std::string & line) {
|
|
731
|
+
backup_line = line;
|
|
732
|
+
viewing_idx = entries.size();
|
|
733
|
+
}
|
|
734
|
+
void end_viewing() {
|
|
735
|
+
viewing_idx = SIZE_MAX;
|
|
736
|
+
backup_line.clear();
|
|
737
|
+
}
|
|
738
|
+
bool is_viewing() const {
|
|
739
|
+
return viewing_idx != SIZE_MAX;
|
|
740
|
+
}
|
|
741
|
+
} history;
|
|
742
|
+
|
|
743
|
+
static bool readline_advanced(std::string & line, bool multiline_input) {
|
|
744
|
+
if (out != stdout) {
|
|
745
|
+
fflush(stdout);
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
line.clear();
|
|
749
|
+
std::vector<int> widths;
|
|
750
|
+
bool is_special_char = false;
|
|
751
|
+
bool end_of_stream = false;
|
|
752
|
+
|
|
753
|
+
size_t byte_pos = 0; // current byte index
|
|
754
|
+
size_t char_pos = 0; // current character index (one char can be multiple bytes)
|
|
755
|
+
|
|
756
|
+
char32_t input_char;
|
|
757
|
+
while (true) {
|
|
758
|
+
assert(char_pos <= byte_pos);
|
|
759
|
+
assert(char_pos <= widths.size());
|
|
760
|
+
auto history_prev = [&]() {
|
|
761
|
+
if (!history.is_viewing()) {
|
|
762
|
+
history.begin_viewing(line);
|
|
763
|
+
}
|
|
764
|
+
std::string new_line;
|
|
765
|
+
if (!history.prev(new_line)) {
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
set_line_contents(new_line, line, widths, char_pos, byte_pos);
|
|
769
|
+
};
|
|
770
|
+
auto history_next = [&]() {
|
|
771
|
+
if (history.is_viewing()) {
|
|
772
|
+
std::string new_line;
|
|
773
|
+
if (!history.next(new_line)) {
|
|
774
|
+
return;
|
|
775
|
+
}
|
|
776
|
+
set_line_contents(new_line, line, widths, char_pos, byte_pos);
|
|
777
|
+
}
|
|
778
|
+
};
|
|
779
|
+
|
|
780
|
+
fflush(out); // Ensure all output is displayed before waiting for input
|
|
781
|
+
input_char = getchar32();
|
|
782
|
+
|
|
783
|
+
if (input_char == '\r' || input_char == '\n') {
|
|
784
|
+
break;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
if (input_char == (char32_t) WEOF || input_char == 0x04 /* Ctrl+D */) {
|
|
788
|
+
end_of_stream = true;
|
|
789
|
+
break;
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
if (is_special_char) {
|
|
793
|
+
replace_last(line.back());
|
|
794
|
+
is_special_char = false;
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
if (input_char == '\033') { // Escape sequence
|
|
798
|
+
char32_t code = getchar32();
|
|
799
|
+
if (code == '[') {
|
|
800
|
+
std::string params;
|
|
801
|
+
while (true) {
|
|
802
|
+
code = getchar32();
|
|
803
|
+
if ((code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z') || code == '~' || code == (char32_t) WEOF) {
|
|
804
|
+
break;
|
|
805
|
+
}
|
|
806
|
+
params.push_back(static_cast<char>(code));
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
const bool ctrl_modifier = has_ctrl_modifier(params);
|
|
810
|
+
|
|
811
|
+
if (code == 'D') { // left
|
|
812
|
+
if (ctrl_modifier) {
|
|
813
|
+
move_word_left(char_pos, byte_pos, widths, line);
|
|
814
|
+
} else if (char_pos > 0) {
|
|
815
|
+
int w = widths[char_pos - 1];
|
|
816
|
+
move_cursor(-w);
|
|
817
|
+
char_pos--;
|
|
818
|
+
byte_pos = prev_utf8_char_pos(line, byte_pos);
|
|
819
|
+
}
|
|
820
|
+
} else if (code == 'C') { // right
|
|
821
|
+
if (ctrl_modifier) {
|
|
822
|
+
move_word_right(char_pos, byte_pos, widths, line);
|
|
823
|
+
} else if (char_pos < widths.size()) {
|
|
824
|
+
int w = widths[char_pos];
|
|
825
|
+
move_cursor(w);
|
|
826
|
+
char_pos++;
|
|
827
|
+
byte_pos = next_utf8_char_pos(line, byte_pos);
|
|
828
|
+
}
|
|
829
|
+
} else if (code == 'H') { // home
|
|
830
|
+
move_to_line_start(char_pos, byte_pos, widths);
|
|
831
|
+
} else if (code == 'F') { // end
|
|
832
|
+
move_to_line_end(char_pos, byte_pos, widths, line);
|
|
833
|
+
} else if (code == 'A' || code == 'B') {
|
|
834
|
+
// up/down
|
|
835
|
+
if (code == 'A') {
|
|
836
|
+
history_prev();
|
|
837
|
+
is_special_char = false;
|
|
838
|
+
} else if (code == 'B') {
|
|
839
|
+
history_next();
|
|
840
|
+
is_special_char = false;
|
|
841
|
+
}
|
|
842
|
+
} else if ((code == '~' || (code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z')) && !params.empty()) {
|
|
843
|
+
std::string digits;
|
|
844
|
+
for (char ch : params) {
|
|
845
|
+
if (ch == ';') {
|
|
846
|
+
break;
|
|
847
|
+
}
|
|
848
|
+
if (std::isdigit(static_cast<unsigned char>(ch))) {
|
|
849
|
+
digits.push_back(ch);
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
if (code == '~') {
|
|
854
|
+
if (digits == "1" || digits == "7") { // home
|
|
855
|
+
move_to_line_start(char_pos, byte_pos, widths);
|
|
856
|
+
} else if (digits == "4" || digits == "8") { // end
|
|
857
|
+
move_to_line_end(char_pos, byte_pos, widths, line);
|
|
858
|
+
} else if (digits == "3") { // delete
|
|
859
|
+
delete_at_cursor(line, widths, char_pos, byte_pos);
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
} else if (code == 0x1B) {
|
|
864
|
+
// Discard the rest of the escape sequence
|
|
865
|
+
while ((code = getchar32()) != (char32_t) WEOF) {
|
|
866
|
+
if ((code >= 'A' && code <= 'Z') || (code >= 'a' && code <= 'z') || code == '~') {
|
|
867
|
+
break;
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
#if defined(_WIN32)
|
|
872
|
+
} else if (input_char == KEY_ARROW_LEFT) {
|
|
873
|
+
if (char_pos > 0) {
|
|
874
|
+
int w = widths[char_pos - 1];
|
|
875
|
+
move_cursor(-w);
|
|
876
|
+
char_pos--;
|
|
877
|
+
byte_pos = prev_utf8_char_pos(line, byte_pos);
|
|
878
|
+
}
|
|
879
|
+
} else if (input_char == KEY_ARROW_RIGHT) {
|
|
880
|
+
if (char_pos < widths.size()) {
|
|
881
|
+
int w = widths[char_pos];
|
|
882
|
+
move_cursor(w);
|
|
883
|
+
char_pos++;
|
|
884
|
+
byte_pos = next_utf8_char_pos(line, byte_pos);
|
|
885
|
+
}
|
|
886
|
+
} else if (input_char == KEY_CTRL_ARROW_LEFT) {
|
|
887
|
+
move_word_left(char_pos, byte_pos, widths, line);
|
|
888
|
+
} else if (input_char == KEY_CTRL_ARROW_RIGHT) {
|
|
889
|
+
move_word_right(char_pos, byte_pos, widths, line);
|
|
890
|
+
} else if (input_char == KEY_HOME) {
|
|
891
|
+
move_to_line_start(char_pos, byte_pos, widths);
|
|
892
|
+
} else if (input_char == KEY_END) {
|
|
893
|
+
move_to_line_end(char_pos, byte_pos, widths, line);
|
|
894
|
+
} else if (input_char == KEY_DELETE) {
|
|
895
|
+
delete_at_cursor(line, widths, char_pos, byte_pos);
|
|
896
|
+
} else if (input_char == KEY_ARROW_UP || input_char == KEY_ARROW_DOWN) {
|
|
897
|
+
if (input_char == KEY_ARROW_UP) {
|
|
898
|
+
history_prev();
|
|
899
|
+
is_special_char = false;
|
|
900
|
+
} else if (input_char == KEY_ARROW_DOWN) {
|
|
901
|
+
history_next();
|
|
902
|
+
is_special_char = false;
|
|
903
|
+
}
|
|
904
|
+
#endif
|
|
905
|
+
} else if (input_char == 0x08 || input_char == 0x7F) { // Backspace
|
|
906
|
+
if (char_pos > 0) {
|
|
907
|
+
int w = widths[char_pos - 1];
|
|
908
|
+
move_cursor(-w);
|
|
909
|
+
char_pos--;
|
|
910
|
+
size_t prev_pos = prev_utf8_char_pos(line, byte_pos);
|
|
911
|
+
size_t char_len = byte_pos - prev_pos;
|
|
912
|
+
byte_pos = prev_pos;
|
|
913
|
+
|
|
914
|
+
// remove the character
|
|
915
|
+
line.erase(byte_pos, char_len);
|
|
916
|
+
widths.erase(widths.begin() + char_pos);
|
|
917
|
+
|
|
918
|
+
// redraw tail
|
|
919
|
+
size_t p = byte_pos;
|
|
920
|
+
int tail_width = 0;
|
|
921
|
+
for (size_t i = char_pos; i < widths.size(); ++i) {
|
|
922
|
+
size_t next_p = next_utf8_char_pos(line, p);
|
|
923
|
+
put_codepoint(line.c_str() + p, next_p - p, widths[i]);
|
|
924
|
+
tail_width += widths[i];
|
|
925
|
+
p = next_p;
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
// clear display
|
|
929
|
+
for (int i = 0; i < w; ++i) {
|
|
930
|
+
fputc(' ', out);
|
|
931
|
+
}
|
|
932
|
+
move_cursor(-(tail_width + w));
|
|
933
|
+
}
|
|
934
|
+
} else {
|
|
935
|
+
// insert character
|
|
936
|
+
std::string new_char_str;
|
|
937
|
+
append_utf8(input_char, new_char_str);
|
|
938
|
+
int w = estimateWidth(input_char);
|
|
939
|
+
|
|
940
|
+
if (char_pos == widths.size()) {
|
|
941
|
+
// insert at the end
|
|
942
|
+
line += new_char_str;
|
|
943
|
+
int real_w = put_codepoint(new_char_str.c_str(), new_char_str.length(), w);
|
|
944
|
+
if (real_w < 0) real_w = 0;
|
|
945
|
+
widths.push_back(real_w);
|
|
946
|
+
byte_pos += new_char_str.length();
|
|
947
|
+
char_pos++;
|
|
948
|
+
} else {
|
|
949
|
+
// insert in middle
|
|
950
|
+
line.insert(byte_pos, new_char_str);
|
|
951
|
+
|
|
952
|
+
int real_w = put_codepoint(new_char_str.c_str(), new_char_str.length(), w);
|
|
953
|
+
if (real_w < 0) real_w = 0;
|
|
954
|
+
|
|
955
|
+
widths.insert(widths.begin() + char_pos, real_w);
|
|
956
|
+
|
|
957
|
+
// print the tail
|
|
958
|
+
size_t p = byte_pos + new_char_str.length();
|
|
959
|
+
int tail_width = 0;
|
|
960
|
+
for (size_t i = char_pos + 1; i < widths.size(); ++i) {
|
|
961
|
+
size_t next_p = next_utf8_char_pos(line, p);
|
|
962
|
+
put_codepoint(line.c_str() + p, next_p - p, widths[i]);
|
|
963
|
+
tail_width += widths[i];
|
|
964
|
+
p = next_p;
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
move_cursor(-tail_width);
|
|
968
|
+
|
|
969
|
+
byte_pos += new_char_str.length();
|
|
970
|
+
char_pos++;
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
if (!line.empty() && (line.back() == '\\' || line.back() == '/')) {
|
|
975
|
+
replace_last(line.back());
|
|
976
|
+
is_special_char = true;
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
bool has_more = multiline_input;
|
|
981
|
+
if (is_special_char) {
|
|
982
|
+
replace_last(' ');
|
|
983
|
+
pop_cursor();
|
|
984
|
+
|
|
985
|
+
char last = line.back();
|
|
986
|
+
line.pop_back();
|
|
987
|
+
if (last == '\\') {
|
|
988
|
+
line += '\n';
|
|
989
|
+
fputc('\n', out);
|
|
990
|
+
has_more = !has_more;
|
|
991
|
+
} else {
|
|
992
|
+
// llama will just eat the single space, it won't act as a space
|
|
993
|
+
if (line.length() == 1 && line.back() == ' ') {
|
|
994
|
+
line.clear();
|
|
995
|
+
pop_cursor();
|
|
996
|
+
}
|
|
997
|
+
has_more = false;
|
|
998
|
+
}
|
|
999
|
+
} else {
|
|
1000
|
+
if (end_of_stream) {
|
|
1001
|
+
has_more = false;
|
|
1002
|
+
} else {
|
|
1003
|
+
line += '\n';
|
|
1004
|
+
fputc('\n', out);
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
if (!end_of_stream && !line.empty()) {
|
|
1009
|
+
// remove the trailing newline for history storage
|
|
1010
|
+
if (!line.empty() && line.back() == '\n') {
|
|
1011
|
+
line.pop_back();
|
|
1012
|
+
}
|
|
1013
|
+
// TODO: maybe support multiline history entries?
|
|
1014
|
+
history.add(line);
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
fflush(out);
|
|
1018
|
+
return has_more;
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
static bool readline_simple(std::string & line, bool multiline_input) {
|
|
1022
|
+
#if defined(_WIN32)
|
|
1023
|
+
std::wstring wline;
|
|
1024
|
+
if (!std::getline(std::wcin, wline)) {
|
|
1025
|
+
// Input stream is bad or EOF received
|
|
1026
|
+
line.clear();
|
|
1027
|
+
GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
|
|
1028
|
+
return false;
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wline[0], (int)wline.size(), NULL, 0, NULL, NULL);
|
|
1032
|
+
line.resize(size_needed);
|
|
1033
|
+
WideCharToMultiByte(CP_UTF8, 0, &wline[0], (int)wline.size(), &line[0], size_needed, NULL, NULL);
|
|
1034
|
+
#else
|
|
1035
|
+
if (!std::getline(std::cin, line)) {
|
|
1036
|
+
// Input stream is bad or EOF received
|
|
1037
|
+
line.clear();
|
|
1038
|
+
return false;
|
|
1039
|
+
}
|
|
1040
|
+
#endif
|
|
1041
|
+
if (!line.empty()) {
|
|
1042
|
+
char last = line.back();
|
|
1043
|
+
if (last == '/') { // Always return control on '/' symbol
|
|
1044
|
+
line.pop_back();
|
|
1045
|
+
return false;
|
|
1046
|
+
}
|
|
1047
|
+
if (last == '\\') { // '\\' changes the default action
|
|
1048
|
+
line.pop_back();
|
|
1049
|
+
multiline_input = !multiline_input;
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
line += '\n';
|
|
1053
|
+
|
|
1054
|
+
// By default, continue input if multiline_input is set
|
|
1055
|
+
return multiline_input;
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
bool readline(std::string & line, bool multiline_input) {
|
|
1059
|
+
if (simple_io) {
|
|
1060
|
+
return readline_simple(line, multiline_input);
|
|
1061
|
+
}
|
|
1062
|
+
return readline_advanced(line, multiline_input);
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
namespace spinner {
|
|
1066
|
+
static const char LOADING_CHARS[] = {'|', '/', '-', '\\'};
|
|
1067
|
+
static std::condition_variable cv_stop;
|
|
1068
|
+
static std::thread th;
|
|
1069
|
+
static size_t frame = 0; // only modified by one thread
|
|
1070
|
+
static bool running = false;
|
|
1071
|
+
static std::mutex mtx;
|
|
1072
|
+
static auto wait_time = std::chrono::milliseconds(100);
|
|
1073
|
+
static void draw_next_frame() {
|
|
1074
|
+
// don't need lock because only one thread modifies running
|
|
1075
|
+
frame = (frame + 1) % sizeof(LOADING_CHARS);
|
|
1076
|
+
replace_last(LOADING_CHARS[frame]);
|
|
1077
|
+
fflush(out);
|
|
1078
|
+
}
|
|
1079
|
+
void start() {
|
|
1080
|
+
std::unique_lock<std::mutex> lock(mtx);
|
|
1081
|
+
if (simple_io || running) {
|
|
1082
|
+
return;
|
|
1083
|
+
}
|
|
1084
|
+
common_log_flush(common_log_main());
|
|
1085
|
+
fprintf(out, "%c", LOADING_CHARS[0]);
|
|
1086
|
+
fflush(out);
|
|
1087
|
+
frame = 1;
|
|
1088
|
+
running = true;
|
|
1089
|
+
th = std::thread([]() {
|
|
1090
|
+
std::unique_lock<std::mutex> lock(mtx);
|
|
1091
|
+
while (true) {
|
|
1092
|
+
if (cv_stop.wait_for(lock, wait_time, []{ return !running; })) {
|
|
1093
|
+
break;
|
|
1094
|
+
}
|
|
1095
|
+
draw_next_frame();
|
|
1096
|
+
}
|
|
1097
|
+
});
|
|
1098
|
+
}
|
|
1099
|
+
void stop() {
|
|
1100
|
+
{
|
|
1101
|
+
std::unique_lock<std::mutex> lock(mtx);
|
|
1102
|
+
if (simple_io || !running) {
|
|
1103
|
+
return;
|
|
1104
|
+
}
|
|
1105
|
+
running = false;
|
|
1106
|
+
cv_stop.notify_all();
|
|
1107
|
+
}
|
|
1108
|
+
if (th.joinable()) {
|
|
1109
|
+
th.join();
|
|
1110
|
+
}
|
|
1111
|
+
replace_last(' ');
|
|
1112
|
+
pop_cursor();
|
|
1113
|
+
fflush(out);
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
|
|
1117
|
+
void log(const char * fmt, ...) {
|
|
1118
|
+
va_list args;
|
|
1119
|
+
va_start(args, fmt);
|
|
1120
|
+
vfprintf(out, fmt, args);
|
|
1121
|
+
va_end(args);
|
|
1122
|
+
}
|
|
1123
|
+
|
|
1124
|
+
void error(const char * fmt, ...) {
|
|
1125
|
+
va_list args;
|
|
1126
|
+
va_start(args, fmt);
|
|
1127
|
+
display_type cur = current_display;
|
|
1128
|
+
set_display(DISPLAY_TYPE_ERROR);
|
|
1129
|
+
vfprintf(out, fmt, args);
|
|
1130
|
+
set_display(cur); // restore previous color
|
|
1131
|
+
va_end(args);
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
void flush() {
|
|
1135
|
+
fflush(out);
|
|
1136
|
+
}
|
|
1137
|
+
}
|