Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 106 additions & 4 deletions tui/liii/pi/tui/cli/help.scm
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,110 @@
;;

(define-library (liii pi tui cli help)
(import (scheme base) (scheme write) (liii raw-string))
(import (scheme base)
(scheme write)
(liii raw-string)
(liii json)
(liii path)
(liii string)
(liii sys)
(srfi srfi-13)
(liii gfproject tools)
) ;import
(export PI_HELP_TEXT display-pi-help)
(begin
(define PI_HELP_TEXT
(string-append (&- #""
;; 库体顶层缓存本源码文件的绝对路径,用于判定正式安装 / 开发者模式。
(define %self-source-file (library-file))

;; 正式安装布局:库位于 ~/.gf/site-lisp 之下;否则为开发者模式。
(define (%dev-mode?)
(and %self-source-file
(catch #t
(lambda ()
(not (path-starts-with? (path %self-source-file)
(path-join (path-home) ".gf" "site-lisp")
) ;path-starts-with?
) ;not
) ;lambda
(lambda args #f)
) ;catch
) ;and
) ;define

;; 把绝对路径中的 home 前缀缩写成 ~,分隔符统一为 /。
(define (%abbreviate-home dir)
(let* ((posix (path-as-posix (path dir)))
(home
(catch #t (lambda () (path-as-posix (path-home))) (lambda args #f))
) ;home
) ;
(if (and home (string-prefix? home posix))
(let ((rest (substring posix (string-length home))))
(if
(or (string-null? rest)
(and (> (string-length rest) 0) (char=? (string-ref rest 0) #\/))
) ;or
(string-append "~" rest)
posix
) ;if
) ;let
posix
) ;if
) ;let*
) ;define

;; 源码仓库根:.../<repo>/tui/liii/pi/tui/cli/help.scm 上溯 5 级。
(define (%source-repo-root)
(and %self-source-file
(catch #t
(lambda ()
(let loop
((p (path-parent (path %self-source-file))) (n 0))
(if (= n 5) (path->string p) (loop (path-parent p) (+ n 1)))
) ;let
) ;lambda
(lambda args #f)
) ;catch
) ;and
) ;define

;; 开发者模式标记:" (~/git/gfcode2)";正式安装为空串。
(define (%dev-suffix)
(if (%dev-mode?)
(let ((root (%source-repo-root)))
(if root (string-append " (" (%abbreviate-home root) ")") "")
) ;let
""
) ;if
) ;define

;; 读取 gfcode 自身 gfproject.json 的 package.version;失败回退 Goldfish 的 (version)。
(define (%pi-version)
(catch #t
(lambda ()
(let* ((bundle (gfproject-resolve-tool-bundle "pi"))
(base (bundle-ref bundle "tool_base_dir"))
) ;
(if (not (string? base))
(version)
(let* ((file (path->string (path-join (path base) "gfproject.json")))
(pkg (json-ref (string->json (path-read-text file)) "package"))
) ;
(or (json-ref-string pkg "version" #f) (version))
) ;let*
) ;if
) ;let*
) ;lambda
(lambda args (version))
) ;catch
) ;define

(define (%pi-help-text)
(string-append "gf pi v"
(%pi-version)
(%dev-suffix)
"\n"
(&- #""
gf pi - AI coding assistant script mode

Usage:
Expand Down Expand Up @@ -61,11 +160,14 @@
(~/.gf/agent/settings.json),
falling back to retry.maxRetries when the nested section is absent.
""
) ;&-
) ;&-
"\n"
) ;string-append
) ;define

;; 完整帮助文本:首行含版本号与开发者模式标记。
(define PI_HELP_TEXT (%pi-help-text))

(define (display-pi-help)
(display PI_HELP_TEXT)
) ;define
Expand Down
82 changes: 78 additions & 4 deletions tui/liii/pi/tui/interactive/header.scm
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,71 @@
(liii json)
(liii path)
(liii string)
(liii sys)
(srfi srfi-13)
(liii tui component)
(liii gfproject tools)
(liii pi tui interactive common)
) ;import
(begin

;; 库体顶层缓存本源码文件的绝对路径(运行期调用 library-file 会返回
;; 调用方文件,必须在此捕获)。用于判定正式安装 / 开发者模式。
(define %self-source-file (library-file))

;; 正式安装布局:库位于 ~/.gf/site-lisp 之下;其余(如直接在本仓库
;; ~/git/gfcode2 运行)视为开发者模式。
(define (%dev-mode?)
(and %self-source-file
(catch #t
(lambda ()
(not (path-starts-with? (path %self-source-file)
(path-join (path-home) ".gf" "site-lisp")
) ;path-starts-with?
) ;not
) ;lambda
(lambda args #f)
) ;catch
) ;and
) ;define

;; 把绝对路径中的 home 前缀缩写成 ~,分隔符统一为 /。
(define (%abbreviate-home dir)
(let* ((posix (path-as-posix (path dir)))
(home
(catch #t (lambda () (path-as-posix (path-home))) (lambda args #f))
) ;home
) ;
(if (and home (string-prefix? home posix))
(let ((rest (substring posix (string-length home))))
(if
(or (string-null? rest)
(and (> (string-length rest) 0) (char=? (string-ref rest 0) #\/))
) ;or
(string-append "~" rest)
posix
) ;if
) ;let
posix
) ;if
) ;let*
) ;define

;; 源码仓库根:.../<repo>/tui/liii/pi/tui/interactive/header.scm 上溯 5 级。
(define (%source-repo-root)
(and %self-source-file
(catch #t
(lambda ()
(let loop
((p (path-parent (path %self-source-file))) (n 0))
(if (= n 5) (path->string p) (loop (path-parent p) (+ n 1)))
) ;let
) ;lambda
(lambda args #f)
) ;catch
) ;and
) ;define

(define (pi-header? obj)
(and (component? obj)
(let ((d (component-data obj)))
Expand All @@ -41,10 +100,15 @@
(show #f (as-bold (apply as-true-color (cons c parts))))
) ;define

;; 页眉内容只随 version 变化,与宽度无关,故一次构造后复用
(define (%header-lines version)
;; 页眉内容只随 version 与开发者标记变化,与宽度无关,故一次构造后复用。
;; dev-suffix 为 #f(正式安装)或形如 " (~/git/gfcode2)" 的标记。
(define (%header-lines version dev-suffix)
(let* ((dot (%fg %c-mid " · "))
(line1 (string-append " " (%fg-bold %c-brand "gfcode") (%fg %c-dim " v" version))
(line1 (string-append " "
(%fg-bold %c-brand "gfcode")
(%fg %c-dim " v" version)
(or dev-suffix "")
) ;string-append
) ;line1
(line2 (string-append " "
(%fg %c-dim "escape")
Expand Down Expand Up @@ -94,9 +158,19 @@
) ;catch
) ;define

;; 开发者模式标记:显示源码仓库根(home 前缀缩写成 ~);正式安装不显示。
(define (%dev-suffix)
(if (%dev-mode?)
(let ((root (%source-repo-root)))
(and root (%fg %c-dim " (" (%abbreviate-home root) ")"))
) ;let
#f
) ;if
) ;define

(define (make-pi-header . args)
(let* ((version (%keyword-value :version args (%pi-tool-version)))
(lines (%header-lines version))
(lines (%header-lines version (%dev-suffix)))
(comp (make-component :render (lambda (self width) lines)))
) ;
(component-data-set! comp (list 'pi-header version))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
(check (list-ref plain-lines 0) => "")
(check-true (string-contains? (list-ref plain-lines 1) " gfcode v"))
(check-true (string-contains? (list-ref plain-lines 1) (version)))
;; 本测试从源码仓库加载 header.scm,属开发者模式,版本号后应有仓库根标记 ( ... )。
;; 不断言具体仓库名(CI 上 checkout 目录名与本地可能不同)。
(check-true (string-contains? (list-ref plain-lines 1) " ("))
(check-true (string-contains? (list-ref plain-lines 1) ")"))
(check-true (string-contains? (list-ref plain-lines 2) "escape interrupt"))
(check-true (string-contains? (list-ref plain-lines 2) "ctrl+c/ctrl+d clear/exit")
) ;check-true
Expand Down
Loading