Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to add a custom prompt to windows console
Julian
Julian

Posted on • Edited on

     

How to add a custom prompt to windows console

Cmder is an awsome package of software which replaces the default consolecmd.exe orpowershell on windows.

If you are in a git repo, you see the current branch in the prompt, colored in white if its clean or red if dirty. There are similar features for yarn, npm, ...

Cmder Screenshot

Sometimes you want to add the output of to the prompt too. This can be done like this:

In my case i wanted to see on whichterraform workspace i am. with terraform you can do this by runningterraform workspace show in your terraform directory. this would output something like

λ terraform workspace showdefault
Enter fullscreen modeExit fullscreen mode

To do this you have to create aclink extension. Assuming that you installed cmder inC:\Program Files\Cmder you create a file calledterraform.lua inC:\Program Files\Cmder\vendor\clink-completions

terraform.lua

localcolor=require("color")localfunctionis_terraform()fordirinio.popen([[dir ".\" /b /ad]]):lines()doifdir==".terraform"thenreturntrueendendreturnfalseendlocalfunctionterraform_prompt_filter()ifis_terraform()thenf=assert(io.popen("terraform workspace show"))forlineinf:lines()doterraform_context='(workspace: '..line..')'ifline=="production"thenprompt_color=color.REDelseifline=="development"thenprompt_color=color.GREENelseprompt_color=color.YELLOWendbreakend-- for loopf:close()terraform_context=color.color_text(terraform_context,prompt_color)clink.prompt.value=clink.prompt.value:gsub("\n",terraform_context.."\n")returnfalseendendclink.prompt.register_filter(terraform_prompt_filter,1)
Enter fullscreen modeExit fullscreen mode

this code checks if it is currently in a terraform directory by looking if the directory.terraform exists. it colors the workspace output in green if you are ondevelopment workspace, red if you are onproduction and yellow on all others. adapt the code for your needs.

i am sure that the lua code can be optimized, i am open for comments and enhancements.

links:

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
rupankarghosh profile image
RupankarGhosh
I am just a dot on earth who loves to code.
  • Location
    Kolkata
  • Joined

Cmder was the best terminal for custom prompt with zsh before Hyper. I love the git integration and syntax highlighting without any configuration. But now cmder became a bit obsolete.

CollapseExpand
 
c33s profile image
Julian
see https://dev.to/c33s/comment/3eck
  • Location
    Vienna, Austria
  • Joined

thaks for the tip. just wanted to give it a try but hyper has this terrible squirrel installer which collides with my sandboxing system.

why it has become so uncommon to install an app directly inC:\Program Files\Appnam shiped as msi file and additional as zip file -.- *sigh*

CollapseExpand
 
bitsmag profile image
bitsmag
  • Joined

Thanks :) That helped a lot !

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

see https://dev.to/c33s/comment/3eck
  • Location
    Vienna, Austria
  • Joined

More fromJulian

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp