Skip to content

Instantly share code, notes, and snippets.

@losinggeneration
Forked from daurnimator/.bash_prompt.lua
Last active December 10, 2015 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save losinggeneration/4494924 to your computer and use it in GitHub Desktop.
Save losinggeneration/4494924 to your computer and use it in GitHub Desktop.
local ret_code , n_jobs = ...
ret_code = tonumber ( ret_code )
n_jobs = tonumber ( n_jobs ) or 0
-- This MUST be updated to where ljsyscall is located
package.path = "/home/harley/Source/languages/lua/ljsyscall/?.lua;" .. package.path
local ljsyscall = require "syscall"
local getenv = os.getenv
local function cmd ( c )
local fd = io.popen ( c )
local ret = fd:read "*l"
fd:close ( )
return ret
end
local uid = ljsyscall.geteuid()
local hostname = ljsyscall.gethostname()
local username = getenv "USER" or ""
local home = getenv "HOME"
local pwd = (getenv "PWD" or ljsyscall.getcwd()):gsub("^"..home,"~")
local colours = {
black = 30 ;
red = 31 ;
green = 32 ;
yellow = 33 ;
blue = 34 ;
magenta = 35 ;
cyan = 36 ;
white = 37 ;
}
local function esc ( ... )
return "\27["..table.concat({...},";").."m"
end
io.write(
-- {HH:MM:SS:username@hostname:pwd}$
esc(1,colours.green),"{",
esc(0,colours.green),os.date("%H:%M:%S", os.time()),
esc(0), ":",
esc(1,colours.cyan),username,
esc(0), "@",
esc(1,colours.cyan),hostname,
esc(0), ":",
esc(1,colours.red),pwd,
esc(1,colours.green),"}",esc(0)
)
local is_git = os.execute ( "git rev-parse --git-dir &> /dev/null" ) == 0
if is_git then
local status = io.popen "git status -sb"
local branch = status:read"*l":match("^## (.*)") -- First line is ## BRANCH NAME
local dirty = status:read"*l":match("^(..)") -- If there are any uncommited changes they will follow
status:close()
local hash = cmd "git show --format=%h"
local col = colours.green
-- ?? are untracked files at the end.
-- Indicate a different colour if there are only untracked files.
if dirty == "??" then
col = colours.yellow
else
col = colours.red
end
-- {git:[branch:hash]}
io.write(
esc(1,colours.green),"{",
esc(col), "git",
esc(0),":",
esc(col),"[",
esc(0,colours.yellow), branch,
esc(0), ":",
esc(0,cyan), hash,
esc(col), "]",
esc(1,colours.green), "}",esc(0)
)
end
local is_hg = os.execute ( "hg root &> /dev/null" ) == 0
if is_hg then
local t = { }
local summary = io.popen "hg summary"
for line in summary:lines() do
local name , value = line:match("([^:]+): (.*)")
if name then
t [ name ] = value
end
end
summary:close()
local col = colours.green
local hg_status = cmd ( "hg status" )
if hg_status then
col = colours.red
elseif not t.update:match("(current)") then
col = colours.yellow
end
io.write(esc(col),"hg:{",t.branch,":",t.parent:match(":(%x*)"),"} ",esc(0))
end
if n_jobs ~= 0 then
io.write(esc(1,colours.cyan),n_jobs," jobs ",esc(0))
end
if ret_code and ret_code ~= 0 then
io.write(esc(1,colours.red),"exit=",ret_code," ")
end
io.write (esc(1,colours.blue), uid==0 and "#" or "$", " ", esc(0) ) -- Reset
PROMPT_COMMAND='luajit ~/.bash_prompt.lua $? `jobs -rp | wc -l`'
PS1=""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment