From 987823e106163f6bfad12084d594288d7b48079f Mon Sep 17 00:00:00 2001 From: Eric Nielsen <4120606+ericbn@users.noreply.github.com> Date: Fri, 21 Mar 2025 07:47:12 -0500 Subject: [PATCH] Generate .zimrc if not found This hopefully makes installation easier. Also, command line tools usually create a default config file when it's missing, so this should me a familiar behavior to command line users. Leaving zsh-history-substring-search commented because it does not work out of the box and unfortunately needs to be manually configured. --- src/stage2/31_zimfw_source_zimrc.zsh.erb | 5 ++ src/templates/zimrc | 52 +++++++++++++++++++++ src/zimfw.zsh.erb | 6 +++ zimfw.zsh | 59 +++++++++++++++++++++++- 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 src/templates/zimrc diff --git a/src/stage2/31_zimfw_source_zimrc.zsh.erb b/src/stage2/31_zimfw_source_zimrc.zsh.erb index eecae85..6518449 100644 --- a/src/stage2/31_zimfw_source_zimrc.zsh.erb +++ b/src/stage2/31_zimfw_source_zimrc.zsh.erb @@ -3,6 +3,11 @@ _zimfw_source_zimrc() { { local -ri _zeager=${1} local -i _zfailed=0 + if [[ ! -e ${_zconfig} ]]; then + _zimfw_print -u2 "${_zyellow}Config file not found, will create ${_zbold}${_zconfig}${_znormal}" + command mkdir -p ${_zconfig:h} || return 1 + print -R "<%= render_template("src/templates/zimrc") %>" >${_zconfig} || return 1 + fi if ! source ${_zconfig} || (( _zfailed )); then print -u2 -R "${_zred}Failed to source ${_zbold}${_zconfig}${_znormal}" return 1 diff --git a/src/templates/zimrc b/src/templates/zimrc new file mode 100644 index 0000000..8ab5e2b --- /dev/null +++ b/src/templates/zimrc @@ -0,0 +1,52 @@ +# +# This is not sourced during shell startup and it's only used to configure zimfw +# + +# +# Modules +# + +# Sets sane Zsh built-in environment options. +zmodule environment +# Provides handy git aliases and functions. +zmodule git +# Applies correct bindkeys for input events. +zmodule input +# Sets a custom terminal title. +zmodule termtitle +# Utility aliases and functions. Adds colour to ls, grep and less. +zmodule utility + +# +# Prompt +# + +# Exposes to prompts how long the last command took to run, used by asciiship. +zmodule duration-info +# Exposes git repository status information to prompts, used by asciiship. +zmodule git-info +# A heavily reduced, ASCII-only version of the Spaceship and Starship prompts. +zmodule asciiship + +# +# Completion +# + +# Additional completion definitions for Zsh. +zmodule zsh-users/zsh-completions --fpath src +# Enables and configures smart and extensive tab completion. +# completion must be sourced after all modules that add completion definitions. +zmodule completion + +# +# Modules that must be initialized last +# + +# Fish-like syntax highlighting for Zsh. +# zsh-users/zsh-syntax-highlighting must be sourced after completion +zmodule zsh-users/zsh-syntax-highlighting +# Fish-like history search (up arrow) for Zsh. +# zsh-users/zsh-history-substring-search must be sourced after zsh-users/zsh-syntax-highlighting +#zmodule zsh-users/zsh-history-substring-search +# Fish-like autosuggestions for Zsh. +zmodule zsh-users/zsh-autosuggestions diff --git a/src/zimfw.zsh.erb b/src/zimfw.zsh.erb index 8a6dea7..e904d75 100644 --- a/src/zimfw.zsh.erb +++ b/src/zimfw.zsh.erb @@ -24,6 +24,12 @@ class Zim Dir[pattern].sort.map { |filename| render(filename) }.join("\n") end + def render_template(filename) + render(filename) + .gsub(/\\/, "\\\\\\\\") + .gsub(/(?=\$[^']|"|`)/, "\\\\") + end + def render_commented(filename) render(filename).gsub(/^(?=.)/, "# ").gsub(/^$/, "#") end diff --git a/zimfw.zsh b/zimfw.zsh index 143ae44..b5bf4b3 100644 --- a/zimfw.zsh +++ b/zimfw.zsh @@ -383,6 +383,63 @@ Per-call initialization options: { local -ri _zeager=${1} local -i _zfailed=0 + if [[ ! -e ${_zconfig} ]]; then + _zimfw_print -u2 "${_zyellow}Config file not found, will create ${_zbold}${_zconfig}${_znormal}" + command mkdir -p ${_zconfig:h} || return 1 + print -R "# +# This is not sourced during shell startup and it's only used to configure zimfw +# + +# +# Modules +# + +# Sets sane Zsh built-in environment options. +zmodule environment +# Provides handy git aliases and functions. +zmodule git +# Applies correct bindkeys for input events. +zmodule input +# Sets a custom terminal title. +zmodule termtitle +# Utility aliases and functions. Adds colour to ls, grep and less. +zmodule utility + +# +# Prompt +# + +# Exposes to prompts how long the last command took to run, used by asciiship. +zmodule duration-info +# Exposes git repository status information to prompts, used by asciiship. +zmodule git-info +# A heavily reduced, ASCII-only version of the Spaceship and Starship prompts. +zmodule asciiship + +# +# Completion +# + +# Additional completion definitions for Zsh. +zmodule zsh-users/zsh-completions --fpath src +# Enables and configures smart and extensive tab completion. +# completion must be sourced after all modules that add completion definitions. +zmodule completion + +# +# Modules that must be initialized last +# + +# Fish-like syntax highlighting for Zsh. +# zsh-users/zsh-syntax-highlighting must be sourced after completion +zmodule zsh-users/zsh-syntax-highlighting +# Fish-like history search (up arrow) for Zsh. +# zsh-users/zsh-history-substring-search must be sourced after zsh-users/zsh-syntax-highlighting +#zmodule zsh-users/zsh-history-substring-search +# Fish-like autosuggestions for Zsh. +zmodule zsh-users/zsh-autosuggestions +" >${_zconfig} || return 1 + fi if ! source ${_zconfig} || (( _zfailed )); then print -u2 -R "${_zred}Failed to source ${_zbold}${_zconfig}${_znormal}" return 1 @@ -489,7 +546,7 @@ _zimfw_info() { _zimfw_info_print_symlink ZIM_HOME ${ZIM_HOME} _zimfw_info_print_symlink 'zimfw config' ${_zconfig} _zimfw_info_print_symlink 'zimfw script' ${__ZIMFW_FILE} - print -R 'zimfw version: '${_zversion}' (built at 2025-03-20 20:43:33 UTC, previous commit is 7d0a56b)' + print -R 'zimfw version: '${_zversion}' (built at 2025-03-21 12:46:56 UTC, previous commit is 52dd146)' local zparam for zparam in LANG ${(Mk)parameters:#LC_*} OSTYPE TERM TERM_PROGRAM TERM_PROGRAM_VERSION ZSH_VERSION; do print -R ${(r.22....:.)zparam}${(P)zparam}