Skip to content

Commit

Permalink
Added system apps from examples folder
Browse files Browse the repository at this point in the history
  • Loading branch information
akclace committed Feb 1, 2024
1 parent fa7531e commit 71803d5
Show file tree
Hide file tree
Showing 20 changed files with 19,730 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
.sw*
.vscode
61 changes: 61 additions & 0 deletions system/disk_usage/app.go.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{{ block "clace_body" . }}
{{ block "du_table_block" . }}
<div id="du_table">
<h3>
Disk usage for
{{ .Data.Current }}
<img
id="spinner"
alt="Loading..."
class="htmx-indicator"
src="{{ static "images/spinner.svg" }}" />
</h3>
<div><em>{{ .Data.Error }}</em></div>

<table>
<thead>
<th>Name</th>
<th></th>
<th>Size (MB)</th>
</thead>
{{ range .Data.Dirs }}
<tr>
{{ $dirUrl := printf "%s?dir=%s" $.AppPath .Dir }}
<td>
<a
href="{{ $dirUrl }}"
hx-target="#du_table"
hx-push-url="true"
hx-get="{{ $dirUrl }}"
hx-indicator="#spinner"
>{{ .Dir }}</a
>
</td>
<td>
<progress max="{{- $.Data.MaxSize -}}" value="{{- .Size -}}">
{{ .Size }}
</progress>
</td>
<td>{{ .Size }}</td>
</tr>
{{ end }}

</table>

<h3>
{{ if ne .Data.Current "/" }}
{{ $parentUrl := printf "%s?dir=%s/.." $.AppPath .Data.Current }}
Parent Directory :
<a
href="{{ $parentUrl }}"
hx-target="#du_table"
hx-push-url="true"
hx-get="{{ $parentUrl }}"
hx-indicator="#spinner"
>{{ .Data.Current }}/..</a
>
{{ end }}
</h3>
</div>
{{ end }}
{{ end }}
48 changes: 48 additions & 0 deletions system/disk_usage/app.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
load("exec.in", "exec")


def handler(req):
dir = req.Query.get("dir")
current = dir[0] if dir and dir[0] else "."

# Run readlink -f to get the absolute path for the current directory
ret = exec.run("readlink", ["-f", current], process_partial=True)
if not ret:
print("Failed to run readlink stderr " + ret.error)
return {"Current": current,
"Error": "readlink -f {current} failed with {error}".format(current=current, error=ret.error)}
current = ret.value[0].strip()

args = ["-m", "-d", "1"]
args.append(current)

# run the du command, allow for partial results to handle permission errors on some dirs
ret = exec.run("du", args, process_partial=True)
if not ret:
print("Failed to run du " + ret.error)
return {"Current": current,
"Error": "du -h {current} failed with {error}".format(current=current, error=ret.error)}

# Parse the results
dirs = []
for line in ret.value:
cols = line.split("\t", 1)
dirs.append({"Size": int(cols[0]), "Dir": cols[1]})

# Descending sort on size, limit to 20 dirs
dirs = sorted(dirs, key=lambda d: d["Size"], reverse=True)[:20]
if len(dirs) > 1 and dirs[1]["Dir"] == current:
# swap current dir to the top (if not already), useful when a child is at same usage level as current
dirs[0], dirs[1] = dirs[1], dirs[0]

return {"Current": current, "Dirs": dirs, "Error": "", "MaxSize": dirs[0]["Size"] if dirs else 0}


app = ace.app("Disk Usage",
pages=[ace.page("/", partial="du_table_block")],
permissions=[
ace.permission("exec.in", "run", ["du"], type="READ"),
ace.permission("exec.in", "run", ["readlink"], type="READ")
],
style=ace.style("https://unpkg.com/[email protected]/mvp.css"),
)
37 changes: 37 additions & 0 deletions system/disk_usage/clace_gen.go.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{{ block "clace_gen_import" . }}
<!-- Include the generated style file if present -->
{{ if fileNonEmpty "gen/css/style.css" }}
<link rel="stylesheet" href="{{ static "gen/css/style.css" }}" />
{{ end }}


<!-- Include the custom style file if present -->
{{ if fileNonEmpty "css/style.css" }}
<link rel="stylesheet" href="{{ static "css/style.css" }}" />
{{ end }}

{{ if fileNonEmpty "gen/lib/htmx.min.js" }}
<script src="{{ static "gen/lib/htmx.min.js" }}"></script>
{{ end }}
{{ if or .IsDev .PushEvents }}
{{ if fileNonEmpty "gen/lib/sse.js" }}
<script src="{{ static "gen/lib/sse.js" }}"></script>
{{ end }}
{{ end }}

{{ if .IsDev }}
<div
id="cl_reload_listener"
hx-ext="sse"
sse-connect="{{ .AppPath }}/_clace_app/sse"
sse-swap="clace_reload"
hx-trigger="sse:clace_reload"></div>
<script>
document
.getElementById("cl_reload_listener")
.addEventListener("sse:clace_reload", function (event) {
location.reload();
});
</script>
{{ end }}
{{ end -}}
11 changes: 11 additions & 0 deletions system/disk_usage/config_gen.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"routing": {
"template_locations": [
"*.go.html"
],
"push_events": false
},
"htmx": {
"version": "1.9.2"
}
}
15 changes: 15 additions & 0 deletions system/disk_usage/index_gen.go.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>{{ .AppName }}</title>
{{ template "clace_gen_import" . }}
</head>

<body>
<h1>Clace: {{ .AppName }}</h1>

{{ template "clace_body" . }}
</body>
</html>
4 changes: 4 additions & 0 deletions system/disk_usage/static/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.htmx-indicator {
opacity: 0;
transition: opacity 200ms ease-in;
}
Loading

0 comments on commit 71803d5

Please sign in to comment.