ansi code LS_COLORS, blinking, cyan, and multicolor codes

LS_COLORS now respects linux style ansi code list, and /bin/ls has been updated to respect that
cursor blinking ws being reset too often that has been fixed
cyan ideally should be 0x00ffff, but that colors becomes green when switching bit depths. Thus, openos will use 0x00B6FF for ansi code for cyan
fixed some vt100 color list parsing
This commit is contained in:
payonel 2017-07-05 16:37:10 -07:00
parent 4b286e2479
commit e1e2e3c800
7 changed files with 45 additions and 43 deletions

View File

@ -684,4 +684,4 @@ while running do
end end
term.clear() term.clear()
term.setCursorBlink(false) term.setCursorBlink(true)

View File

@ -26,7 +26,6 @@ if #args == 0 then
dofile("/etc/profile.lua") dofile("/etc/profile.lua")
end end
io.write(sh.expand(os.getenv("PS1") or "$ ")) io.write(sh.expand(os.getenv("PS1") or "$ "))
tty.setCursorBlink(true)
end end
local command = io.read() local command = io.read()
if command then if command then

View File

@ -5,6 +5,7 @@ local fs = require("filesystem")
if tty.isAvailable() then if tty.isAvailable() then
tty:write("\27[40m\27[37m") tty:write("\27[40m\27[37m")
tty.clear() tty.clear()
tty.setCursorBlink(true)
end end
dofile("/etc/motd") dofile("/etc/motd")
@ -31,7 +32,7 @@ os.setenv("IFS", " ")
os.setenv("MANPATH", "/usr/man:.") os.setenv("MANPATH", "/usr/man:.")
os.setenv("PAGER", "/bin/more") os.setenv("PAGER", "/bin/more")
os.setenv("PS1", "\27[40m\27[31m$HOSTNAME$HOSTNAME_SEPARATOR$PWD # \27[37m") os.setenv("PS1", "\27[40m\27[31m$HOSTNAME$HOSTNAME_SEPARATOR$PWD # \27[37m")
os.setenv("LS_COLORS", "{FILE=0xFFFFFF,DIR=0x66CCFF,LINK=0xFFAA00,['*.lua']=0x00FF00}") os.setenv("LS_COLORS", "di=0;36:fi=0:ln=0;33:*.lua=0;32")
shell.setWorkingDirectory(os.getenv("HOME")) shell.setWorkingDirectory(os.getenv("HOME"))

View File

@ -40,8 +40,8 @@ end
function adapter_api.create_toggle(read, write, switch) function adapter_api.create_toggle(read, write, switch)
return return
{ {
read = function() return tostring(read()) end, read = read and function() return tostring(read()) end,
write = function(value) write = write and function(value)
value = text.trim(tostring(value)) value = text.trim(tostring(value))
local on = value == "1" or value == "true" local on = value == "1" or value == "true"
local off = value == "0" or value == "false" local off = value == "0" or value == "false"

View File

@ -2,6 +2,8 @@ local fs = require("filesystem")
local shell = require("shell") local shell = require("shell")
local tty = require("tty") local tty = require("tty")
local unicode = require("unicode") local unicode = require("unicode")
local tx = require("transforms")
local text = require("text")
local dirsArg, ops = shell.parse(...) local dirsArg, ops = shell.parse(...)
@ -32,7 +34,6 @@ if #dirsArg == 0 then
end end
local ec = 0 local ec = 0
local gpu = tty.gpu()
local fOut = tty.isAvailable() and io.output().tty local fOut = tty.isAvailable() and io.output().tty
local function perr(msg) io.stderr:write(msg,"\n") ec = 2 end local function perr(msg) io.stderr:write(msg,"\n") ec = 2 end
local function stat(names, index) local function stat(names, index)
@ -56,42 +57,30 @@ local function stat(names, index)
return info return info
end end
local function toArray(i) local r={} for n in i do r[#r+1]=n end return r end local function toArray(i) local r={} for n in i do r[#r+1]=n end return r end
local restore_color = function() end
local set_color = function() end local set_color = function() end
local prev_color local function colorize() return end
local function colorize() return prev_color end
if fOut and not ops["no-color"] then if fOut and not ops["no-color"] then
local LSC = os.getenv("LS_COLORS") local LSC = tx.foreach(text.split(os.getenv("LS_COLORS") or "", {":"}, true), function(e)
if type(LSC) == "string" then local parts = text.split(e, {"="}, true)
LSC = require("serialization").unserialize(LSC) return parts[2], parts[1]
end end)
if not LSC then
perr("ls: unparsable value for LS_COLORS environment variable")
else
prev_color = gpu.getForeground()
restore_color = function() gpu.setForeground(prev_color) end
colorize = function(info) colorize = function(info)
return return
info.isLink and LSC.LINK or info.isLink and LSC.ln or
info.isDir and LSC.DIR or info.isDir and LSC.di or
LSC['*'..info.ext] or LSC['*'..info.ext] or
LSC.FILE or LSC.fi
prev_color
end end
set_color=function(c) set_color=function(c)
if gpu.getForeground() ~= c then io.write(string.char(0x1b), "[", c or "", "m")
io.stdout:flush()
gpu.setForeground(c)
end
end
end end
end end
local msft={reports=0,proxies={}} local msft={reports=0,proxies={}}
function msft.report(files, dirs, used, proxy) function msft.report(files, dirs, used, proxy)
local free = proxy.spaceTotal() - proxy.spaceUsed() local free = proxy.spaceTotal() - proxy.spaceUsed()
restore_color() set_color()
local pattern = "%5i File(s) %11i bytes\n%5i Dir(s) %11s bytes free\n" local pattern = "%5i File(s) %s bytes\n%5i Dir(s) %11s bytes free\n"
io.write(string.format(pattern, files, used, dirs, tostring(free))) io.write(string.format(pattern, files, tostring(used), dirs, tostring(free)))
end end
function msft.tail(names) function msft.tail(names)
local fsproxy = fs.get(names.path) local fsproxy = fs.get(names.path)
@ -123,7 +112,7 @@ function msft.final()
for proxy,report in pairs(msft.proxies) do for proxy,report in pairs(msft.proxies) do
table.insert(groups, {proxy=proxy,report=report}) table.insert(groups, {proxy=proxy,report=report})
end end
restore_color() set_color()
print("Total Files Listed:") print("Total Files Listed:")
for _,pair in ipairs(groups) do for _,pair in ipairs(groups) do
local proxy, report = pair.proxy, pair.report local proxy, report = pair.proxy, pair.report
@ -263,7 +252,7 @@ local function display(names)
local format = "%s-r%s %+"..tostring(max_size_width).."s %"..tostring(max_date_width).."s" local format = "%s-r%s %+"..tostring(max_size_width).."s %"..tostring(max_date_width).."s"
local meta = string.format(format, file_type, write_mode, size, modDate) local meta = string.format(format, file_type, write_mode, size, modDate)
local item = info.name..link_target local item = info.name..link_target
return {{color = prev_color, name = meta}, {color = colorize(info), name = item}} return {{name = meta}, {color = colorize(info), name = item}}
end end
elseif ops["1"] or not fOut then elseif ops["1"] or not fOut then
lines.n = #names lines.n = #names
@ -332,7 +321,7 @@ local header = function() end
if #dirsArg > 1 or ops.R then if #dirsArg > 1 or ops.R then
header = function(path) header = function(path)
if not first_display then print() end if not first_display then print() end
restore_color() set_color()
io.write(path,":\n") io.write(path,":\n")
end end
end end
@ -366,11 +355,20 @@ for _,dir in ipairs(dirsArg) do
table.insert(file_set, dir) table.insert(file_set, dir)
end end
end end
io.output():setvbuf("line") io.output():setvbuf("line")
local ok, msg = pcall(function()
if #file_set > 0 then display(sort(file_set)) end if #file_set > 0 then display(sort(file_set)) end
displayDirList(dir_set) displayDirList(dir_set)
msft.final() msft.final()
end)
io.output():flush() io.output():flush()
io.output():setvbuf("no") io.output():setvbuf("no")
restore_color() set_color()
assert(ok, msg)
return ec return ec

View File

@ -352,7 +352,8 @@ function tty.write(_, value)
value = window.ansi_escape:sub(5) value = window.ansi_escape:sub(5)
end end
for _,catt in ipairs(color_attributes) do for _,catt in ipairs(color_attributes) do
local colors = {0x0,0xff0000,0x00ff00,0xffff00,0x0000ff,0xff00ff,0x00ffff,0xffffff} -- B6 is closer to cyan in 4 bit color
local colors = {0x0,0xff0000,0x00ff00,0xffff00,0x0000ff,0xff00ff,0x00B6ff,0xffffff}
catt = catt - 29 catt = catt - 29
local method = "setForeground" local method = "setForeground"
if catt > 10 then if catt > 10 then

View File

@ -25,7 +25,10 @@ local rules = {}
-- [%d+;%d+;..%d+m -- [%d+;%d+;..%d+m
rules[{"%[", "[%d;]*", "m"}] = function(_, _, number_text) rules[{"%[", "[%d;]*", "m"}] = function(_, _, number_text)
local numbers = {} local numbers = {}
number_text:gsub("[^;]*", function(num) -- add a ; at the end to recompute trailing ; as resets
-- e.g. \27[41;m is actually 41 followed by a reset
(number_text..";"):gsub("([^;]*);?", function(num)
-- if not n this could simply be a ; separator
local n = tonumber(num) or 0 local n = tonumber(num) or 0
if n == 0 then if n == 0 then
numbers[#numbers + 1] = 40 numbers[#numbers + 1] = 40