Skip to content

Commit

Permalink
🎨 Improve code by OOP
Browse files Browse the repository at this point in the history
  • Loading branch information
Freed-Wu committed Jun 17, 2024
1 parent 7e74079 commit c295ae5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 40 deletions.
42 changes: 14 additions & 28 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: check-added-large-files
- id: fix-byte-order-marker
Expand All @@ -21,11 +21,11 @@ repos:
- id: check-toml
- id: check-json
- repo: https://github.com/Lucas-C/pre-commit-hooks
rev: v1.5.4
rev: v1.5.5
hooks:
- id: remove-crlf
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
rev: v2.3.0
hooks:
- id: codespell
additional_dependencies:
Expand All @@ -45,11 +45,11 @@ repos:
hooks:
- id: check-mailmap
- repo: https://github.com/rhysd/actionlint
rev: v1.6.26
rev: v1.7.1
hooks:
- id: actionlint
- repo: https://github.com/adrienverge/yamllint
rev: v1.33.0
rev: v1.35.1
hooks:
- id: yamllint
- repo: https://github.com/executablebooks/mdformat
Expand All @@ -63,39 +63,25 @@ repos:
- mdformat-toc
- mdformat-deflist
- mdformat-beautysh
- mdformat-black
- mdformat-ruff
- mdformat-config
- mdformat-web
- repo: https://github.com/DavidAnson/markdownlint-cli2
rev: v0.10.0
rev: v0.13.0
hooks:
- id: markdownlint-cli2
additional_dependencies:
- markdown-it-texmath
- repo: https://github.com/psf/black
rev: 23.11.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.8
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/pycqa/pydocstyle
rev: 6.3.0
hooks:
- id: pydocstyle
additional_dependencies:
- tomli
- id: ruff
- id: ruff-format
- repo: https://github.com/kumaraditya303/mirrors-pyright
rev: v1.1.335
rev: v1.1.366
hooks:
- id: pyright
- repo: https://github.com/PyCQA/bandit
rev: 1.7.5
hooks:
- id: bandit
additional_dependencies:
- tomli
- repo: https://github.com/lunarmodules/luacheck
rev: v1.1.1
rev: v1.2.0
hooks:
- id: luacheck
1 change: 1 addition & 0 deletions prompt-style-0.0.1-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ build = {
type = "builtin",
modules = {["prompt-style"] = "prompt-style.lua"},
install = {
-- cannot use _VERSION
bin = {
"bin/nvimp",
"bin/texluap",
Expand Down
23 changes: 11 additions & 12 deletions prompt-style.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---prompt style.
local os = require "os"
local string = require "string"
local table = require "table"
local lfs = require "lfs"
local ansicolors = require "ansicolors"
Expand All @@ -14,14 +13,14 @@ if table.unpack == nil then table.unpack = unpack end
local function wakatime(cmd)
cmd = cmd or "wakatime-cli --write --plugin=repl-lua-wakatime " ..
"--entity-type=app --entity=lua --alternate-language=lua --project=%s &"
local s, _ = string.find(cmd, "%s")
local s, _ = cmd:find("%s")
if s ~= nil then
local project = io.popen("git rev-parse --show-toplevel 2> /dev/null"):read()
if project == nil then
project = lfs.currentdir()
end
project = string.match(project, "[^/]+$")
cmd = string.format(cmd, project)
project = project:match("[^/]+$")
cmd = cmd:format(project)
end
io.popen(cmd)
return ""
Expand All @@ -32,7 +31,7 @@ end
local function get_distribution()
local line = io.popen("lsb_release -i 2>/dev/null"):read()
if line == nil then return "linux" end
return string.lower(string.gsub(line, ".*:%s*", ""))
return line:gsub(".*:%s*", ""):lower()
end

---get os
Expand All @@ -41,7 +40,7 @@ local function get_os()
if os.getenv("PREFIX") == "/data/data/com.termux/files/usr" then
return "android"
end
local binary_format = string.match(package.cpath, '([^.]+)[;|$]')
local binary_format = package.cpath:match('([^.]+)[;|$]')
if binary_format == "so" then
return get_distribution()
elseif binary_format == "dll" then
Expand Down Expand Up @@ -81,8 +80,8 @@ local function get_version(name, logo, format)
name = name or prompt.name
logo = logo or ""
format = format or " %s "
name = string.format(format, name)
local version = string.gsub(_VERSION, ".*%s+", "")
name = format:format(name)
local version = _VERSION:gsub(".*%s+", "")
return logo .. name .. version
end

Expand All @@ -99,8 +98,8 @@ end
---get cwd
---@return string
local function get_cwd()
local cwd = string.gsub(lfs.currentdir(), os.getenv("HOME") or "", "~")
cwd = string.gsub(cwd, "[^/]+$", "%%{bright}%0")
local cwd = lfs.currentdir():gsub(os.getenv("HOME") or "", "~")
cwd = cwd:gsub("[^/]+$", "%%{bright}%0")
return cwd
end

Expand All @@ -125,7 +124,7 @@ local function generate_ps1(char, sections)
local last_bg = ""
for _, v in ipairs(sections) do
if type(v) == "string" then
if string.match(v, "%s") then
if v:match("%s") then
format = v
else
sep = v
Expand All @@ -134,7 +133,7 @@ local function generate_ps1(char, sections)
local fg, bg, text = table.unpack(v)
if type(text) == "function" then text = text() end
if text ~= "" then
text = string.format(format, text)
text = format:format(text)
if last_bg == "" then
ps1 = ps1 .. "%{" .. fg .. " " .. bg .. "bg}" .. text
else
Expand Down

0 comments on commit c295ae5

Please sign in to comment.