Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjusted to my the the one wit hfaster feedback #10

Closed
wants to merge 1 commit into from
Closed
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
205 changes: 150 additions & 55 deletions powerline_prompt.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,59 @@
-- Source: https://github.com/AmrEldib/cmder-powerline-prompt

-- Resets the prompt
function lambda_prompt_filter()
cwd = clink.get_cwd()
prompt = "\x1b[37;44m{cwd} {git}{hg}\n\x1b[1;30;40m{lamb} \x1b[0m"
new_value = string.gsub(prompt, "{cwd}", cwd)
clink.prompt.value = string.gsub(new_value, "{lamb}", "λ")
end
-- default script for clink, called by init.bat when injecting clink

-- !!! THIS FILE IS OVERWRITTEN WHEN CMDER IS UPDATED
-- !!! Use "%CMDER_ROOT%\config\<whatever>.lua" to add your lua startup scripts


-- At first, load the original clink.lua file
-- this is needed as we set the script path to this dir and therefore the original
-- clink.lua is not loaded.
local clink_lua_file = clink.get_env('CMDER_ROOT')..'\\vendor\\clink\\clink.lua'
local arrowSymbol = ""
local branchSymbol = ""
dofile(clink_lua_file)

-- now add our own things...

---
-- Setting the prompt in clink means that commands which rewrite the prompt do
-- not destroy our own prompt. It also means that started cmds (or batch files
-- which echo) don't get the ugly '{lamb}' shown.
---
function set_prompt_filter()
-- get_cwd() is differently encoded than the clink.prompt.value, so everything other than
-- pure ASCII will get garbled. So try to parse the current directory from the original prompt
-- and only if that doesn't work, use get_cwd() directly.
-- The matching relies on the default prompt which ends in X:\PATH\PATH>
-- (no network path possible here!)
local old_prompt = clink.prompt.value
local cwd = old_prompt:match('.*(.:[^>]*)>')
if cwd == nil then cwd = clink.get_cwd() end

--- copied from clink.lua
-- environment systems like pythons virtualenv change the PROMPT and usually
-- set some variable. But the variables are differently named and we would never
-- get them all, so try to parse the env name out of the PROMPT.
-- envs are usually put in round or square parentheses and before the old prompt
local env = old_prompt:match('.*%(([^%)]+)%).+:')

-- also check for square brackets
if env == nil then env = old_prompt:match('.*%[([^%]]+)%].+:') end

-- build our own prompt
-- orig: $E[1;32;40m$P$S{git}{hg}$S$_$E[1;30;40m{lamb}$S$E[0m
-- color codes: "\x1b[1;37;40m"
-- local cmder_prompt = "\x1b[1;32;40m{cwd} {git}{hg} \n\x1b[1;30;40m{lamb} \x1b[0m"

local cmder_prompt = "\x1b[37;44m{cwd} {git}{hg} \n\x1b[34;40m{lamb} \x1b[0m"
cmder_prompt = string.gsub(cmder_prompt, "{cwd}", cwd)
if env == nil then
lambda = "λ"
else
lambda = "("..env..") λ"
end
clink.prompt.value = string.gsub(cmder_prompt, "{lamb}", lambda)
end

---
-- Resolves closest directory location for specified directory.
-- Navigates subsequently up one level and tries to find specified directory
-- @param {string} path Path to directory will be checked. If not provided
Expand Down Expand Up @@ -62,45 +104,11 @@ local function get_dir_contains(path, dirname)
end
end

-- copied from clink.lua
-- clink.lua is saved under %CMDER_ROOT%\vendor
local function get_hg_dir(path)
return get_dir_contains(path, '.hg')
end

-- adopted from clink.lua
-- clink.lua is saved under %CMDER_ROOT%\vendor
function colorful_hg_prompt_filter()

-- Colors for mercurial status
local colors = {
clean = "\x1b[1;37;40m",
dirty = "\x1b[31;1m",
}

if get_hg_dir() then
-- if we're inside of mercurial repo then try to detect current branch
local branch = get_hg_branch()
if branch then
-- Has branch => therefore it is a mercurial folder, now figure out status
if get_hg_status() then
color = colors.clean
else
color = colors.dirty
end

clink.prompt.value = string.gsub(clink.prompt.value, "{hg}", color.."("..branch..")")
return false
end
end

-- No mercurial present or not in mercurial file
clink.prompt.value = string.gsub(clink.prompt.value, "{hg}", "")
return false
end

-- copied from clink.lua
-- clink.lua is saved under %CMDER_ROOT%\vendor
-- adapted from from clink-completions' git.lua
local function get_git_dir(path)

-- return parent path for specified entry (either file or directory)
Expand Down Expand Up @@ -141,6 +149,82 @@ local function get_git_dir(path)
or (parent_path ~= path and get_git_dir(parent_path) or nil)
end

---
-- Find out current branch
-- @return {false|mercurial branch name}
---
function get_hg_branch()
for line in io.popen("hg branch 2>nul"):lines() do
local m = line:match("(.+)$")
if m then
return m
end
end

return false
end

---
-- Get the status of working dir
-- @return {bool}
---
function get_hg_status()
for line in io.popen("hg status -0"):lines() do
return false
end
return true
end

function hg_prompt_filter()

-- Colors for mercurial status
local colors = {
clean = "\x1b[1;37;40m",
dirty = "\x1b[31;1m",
}

if get_hg_dir() then
-- if we're inside of mercurial repo then try to detect current branch
local branch = get_hg_branch()
if branch then
-- Has branch => therefore it is a mercurial folder, now figure out status
if get_hg_status() then
color = colors.clean
else
color = colors.dirty
end

clink.prompt.value = string.gsub(clink.prompt.value, "{hg}", color.."("..branch..")")
return false
end
end

-- No mercurial present or not in mercurial file
clink.prompt.value = string.gsub(clink.prompt.value, "{hg}", "")
return false
end

---
-- Find out current branch
-- @return {nil|git branch name}
---
function get_git_branch(git_dir)
local git_dir = git_dir or get_git_dir()

-- If git directory not found then we're probably outside of repo
-- or something went wrong. The same is when head_file is nil
local head_file = git_dir and io.open(git_dir..'/HEAD')
if not head_file then return end

local HEAD = head_file:read()
head_file:close()

-- if HEAD matches branch expression, then we're on named branch
-- otherwise it is a detached commit
local branch_name = HEAD:match('ref: refs/heads/(.+)')
return branch_name or 'HEAD detached at '..HEAD:sub(1, 7)
end

---
-- Get the status of working dir
-- @return {bool}
Expand All @@ -155,19 +239,18 @@ function get_git_status()
return true
end

-- adopted from clink.lua
-- Modified to add colors and arrow symbols
function colorful_git_prompt_filter()
function git_prompt_filter()

-- Colors for git status
local colors = {
clean = "\x1b[34;42m"..arrowSymbol.."\x1b[37;42m ",
-- clean = "\x1b[34;42m"..arrowSymbol.."\x1b[37;42m ",
clean = "\x1b[34;42m"..arrowSymbol.."\x1b[30;42m ",
dirty = "\x1b[34;43m"..arrowSymbol.."\x1b[30;43m ",
}

local closingcolors = {
clean = " \x1b[32;40m"..arrowSymbol,
dirty = "± \x1b[33;40m"..arrowSymbol,
clean = "\x1b[32;40m"..arrowSymbol,
dirty = "\x1b[33;40m"..arrowSymbol,
}

local git_dir = get_git_dir()
Expand Down Expand Up @@ -195,7 +278,19 @@ function colorful_git_prompt_filter()
return false
end

-- override the built-in filters
clink.prompt.register_filter(lambda_prompt_filter, 55)
clink.prompt.register_filter(colorful_hg_prompt_filter, 60)
clink.prompt.register_filter(colorful_git_prompt_filter, 60)
-- insert the set_prompt at the very beginning so that it runs first
clink.prompt.register_filter(set_prompt_filter, 1)
clink.prompt.register_filter(git_prompt_filter, 40)
clink.prompt.register_filter(hg_prompt_filter, 50)


local completions_dir = clink.get_env('CMDER_ROOT')..'/vendor/clink-completions/'
for _,lua_module in ipairs(clink.find_files(completions_dir..'*.lua')) do
-- Skip files that starts with _. This could be useful if some files should be ignored
if not string.match(lua_module, '^_.*') then
local filename = completions_dir..lua_module
-- use dofile instead of require because require caches loaded modules
-- so config reloading using Alt-Q won't reload updated modules.
dofile(filename)
end
end