Skip to content

Commit

Permalink
fix(zsh): Remnant characters when tab (#239)
Browse files Browse the repository at this point in the history
Signed-off-by: Ce Gao <cegao@tensorchord.ai>
  • Loading branch information
gaocegege committed Jun 7, 2022
1 parent c5ebe85 commit 1a50052
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 8 deletions.
1 change: 1 addition & 0 deletions pkg/lang/ir/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/cockroachdb/errors"
"github.com/moby/buildkit/client/llb"

"github.com/tensorchord/envd/pkg/flag"
"github.com/tensorchord/envd/pkg/progress/compileui"
"github.com/tensorchord/envd/pkg/shell"
Expand Down
90 changes: 85 additions & 5 deletions pkg/shell/zsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ package shell

import (
_ "embed"
"os"
"path/filepath"

"github.com/cockroachdb/errors"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/sirupsen/logrus"

"github.com/tensorchord/envd/pkg/home"
"github.com/tensorchord/envd/pkg/util/fileutil"
)
Expand Down Expand Up @@ -64,7 +67,7 @@ func (m generalManager) DownloadOrCache() (bool, error) {
}).Debug("oh-my-zsh already exists in cache")
return true, nil
}
url := "https://github.com/ohmyzsh/ohmyzsh"
url := "https://github.com/ohmyzsh/ohmyzsh.git"
l := logrus.WithFields(logrus.Fields{
"cache-dir": m.OHMyZSHDir(),
"URL": url,
Expand All @@ -75,12 +78,67 @@ func (m generalManager) DownloadOrCache() (bool, error) {
return false, errors.New("failed to remove oh-my-zsh dir")
}
l.Debug("cache miss, downloading oh-my-zsh")
_, err := git.PlainClone(m.OHMyZSHDir(), false, &git.CloneOptions{
URL: url,

// Init the git repository.
repo, err := git.PlainInit(m.OHMyZSHDir(), false)
if err != nil {
return false, errors.Wrap(err, "failed to init oh-my-zsh repo")
}
cfg, err := repo.Config()
if err != nil {
return false, errors.Wrap(err, "failed to get repo config")
}
// Refer to https://github.com/tensorchord/envd/issues/183#issuecomment-1148113323
cfg.Raw.AddOption("core", "", "eol", "lf")
cfg.Raw.AddOption("core", "", "autocrlf", "false")
cfg.Raw.AddOption("core", "", "filemode", "true")
cfg.Raw.AddOption("core", "", "logallrefupdates", "true")
cfg.Raw.AddOption("core", "", "repositoryformatversion", "0")
cfg.Raw.AddOption("core", "", "bare", "false")
cfg.Raw.AddOption("fsck", "", "zeroPaddedFilemode", "ignore")
cfg.Raw.AddOption("fetch", "fsck", "zeroPaddedFilemode", "ignore")
cfg.Raw.AddOption("receive", "fsck", "zeroPaddedFilemode", "ignore")
cfg.Remotes["origin"] = &config.RemoteConfig{
Name: "origin",
URLs: []string{url},
Fetch: []config.RefSpec{
config.RefSpec("+refs/heads/master:refs/remotes/origin/master"),
},
}
cfg.Branches["master"] = &config.Branch{
Name: "master",
Remote: "origin",
Merge: "refs/heads/master",
}

if err := cfg.Validate(); err != nil {
return false, errors.Wrap(err, "failed to validate config")
}
if err := repo.SetConfig(cfg); err != nil {
return false, errors.Wrap(err, "failed to set config")
}

if err := repo.Fetch(&git.FetchOptions{
RemoteName: "origin",
RefSpecs: []config.RefSpec{
config.RefSpec("+refs/heads/master:refs/remotes/origin/master"),
},
Depth: 1,
})
}); err != nil {
return false, errors.Wrap(err, "failed to fetch oh-my-zsh")
}
wktree, err := repo.Worktree()
if err != nil {
return false, err
return false, errors.Wrap(err, "failed to get worktree")
}
if err := wktree.Checkout(&git.CheckoutOptions{
Branch: "refs/remotes/origin/master",
}); err != nil {
return false, errors.Wrap(err, "failed to checkout master")
}

if err := m.createEnvdPromptTheme(); err != nil {
return false, errors.Wrap(err, "failed to create envd.zsh-theme")
}

if err := home.GetManager().MarkCache(cacheKey, true); err != nil {
Expand All @@ -90,6 +148,28 @@ func (m generalManager) DownloadOrCache() (bool, error) {
return false, nil
}

// Refer to https://github.com/tensorchord/envd/issues/183#issuecomment-1148172564
func (m generalManager) createEnvdPromptTheme() error {
path := "themes/envd.zsh-theme"
content := `
PROMPT="(envd) %(?:%{$fg_bold[green]%}%{%G➜%} :%{$fg_bold[red]%}%{%G➜%} )"
PROMPT+=' %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)'
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}%{%G✗%}"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"
`
absolutePath := filepath.Join(m.OHMyZSHDir(), path)
logger := logrus.WithField("path", absolutePath)
logger.Debug("creating envd.zsh-theme")
if err := os.WriteFile(
absolutePath, []byte(content), 0644); err != nil {
return err
}
return nil
}

func (m generalManager) OHMyZSHDir() string {
return filepath.Join(home.GetManager().CacheDir(), "oh-my-zsh")
}
4 changes: 1 addition & 3 deletions pkg/shell/zshrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export ZSH="$HOME/.oh-my-zsh"
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="robbyrussell"
ZSH_THEME="envd"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
Expand Down Expand Up @@ -101,7 +101,5 @@ alias zshconfig="vim ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
#

# Set prompt
export PS1="(envd 🐳) $PS1"
alias python=python3
export PATH=$PATH:$HOME/.local/bin
1 change: 1 addition & 0 deletions pkg/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// limitations under the License.

// https://gist.github.com/stefanprodan/2d20d0c6fdab6f14ce8219464e8b4b9a
// Refer to okteto/pkg/ssh/exec.go
package ssh

import (
Expand Down

0 comments on commit 1a50052

Please sign in to comment.