mirror of
https://github.com/sbrow/envr.git
synced 2026-06-27 18:48:33 -04:00
Compare commits
42 Commits
v0.3.0
...
623c2f5f29
| Author | SHA1 | Date | |
|---|---|---|---|
| 623c2f5f29 | |||
| 5ea760a7fa | |||
| c5d578dd1b | |||
| a4417affa1 | |||
| b47ca8f14d | |||
| e6f09a037b | |||
| 46c2baf726 | |||
| a56d349da4 | |||
| 2229affe69 | |||
| 6394c42d8b | |||
| 8e00c78f12 | |||
| 8d2c5403e8 | |||
| 7c7d3d5c23 | |||
| 6151a5efaf | |||
| fbb1f86945 | |||
| 0c5aa74256 | |||
| eed360895f | |||
| 75b778453f | |||
| 4ec2b22b52 | |||
| 0276db767e | |||
| a0e2c99581 | |||
| d0dc93ab56 | |||
| 91ada61c06 | |||
| 9b39567720 | |||
| 43dd8aca13 | |||
| db1b863e7e | |||
| e966050137 | |||
| 7629dd2ce7 | |||
| 7c7ddf46f6 | |||
| a1e945a630 | |||
| 0a332adfdf | |||
| 4e1e359076 | |||
| 82bec68bd1 | |||
| 2cb6067a3a | |||
| 3668df57d1 | |||
| d2127e4780 | |||
| cb7db96781 | |||
| c92155a17b | |||
| b1d2416182 | |||
| 40f0b3c36d | |||
| d84e43d044 | |||
| 28f96df4c0 |
1
.github/workflows/release-please.yml
vendored
1
.github/workflows/release-please.yml
vendored
@@ -25,4 +25,3 @@ jobs:
|
||||
# this is a built-in strategy in release-please, see "Action Inputs"
|
||||
# for more options
|
||||
release-type: simple
|
||||
target-branch: ${{ github.ref_name }}
|
||||
|
||||
18
CHANGELOG.md
18
CHANGELOG.md
@@ -1,23 +1,5 @@
|
||||
# Changelog
|
||||
|
||||
## [0.3.0](https://github.com/sbrow/envr/compare/v0.2.1...v0.3.0) (2026-06-16)
|
||||
|
||||
|
||||
### ⚠ BREAKING CHANGES
|
||||
|
||||
* The encryption format of databases has changed. Age encryption is no longer supported, and no automatic migration path was implemented.
|
||||
|
||||
### Features
|
||||
|
||||
* All encryption/decryption now happens in-memory. ([fe2b256](https://github.com/sbrow/envr/commit/fe2b256bd61eaf551d53faf3893b473a64a94667))
|
||||
* Config can be loaded from any path with `--config-file (-c)` flag. ([4a26ee8](https://github.com/sbrow/envr/commit/4a26ee814591e6aab0eb99d2359d51b31011edfe))
|
||||
* Switched from age to libsodium. ([23b8c2d](https://github.com/sbrow/envr/commit/23b8c2dc671a23cf76cf6746b33806ded9381486))
|
||||
|
||||
|
||||
### Performance Improvements
|
||||
|
||||
* Improved writer performance. ([365e914](https://github.com/sbrow/envr/commit/365e9149b1a738ac9119bb5f74dc7e047ecfed5b))
|
||||
|
||||
## [0.2.1](https://github.com/sbrow/envr/compare/v0.2.0...v0.2.1) (2026-01-12)
|
||||
|
||||
|
||||
|
||||
19
TODOS.md
19
TODOS.md
@@ -1,7 +1,12 @@
|
||||
# TODOs
|
||||
# TODO
|
||||
|
||||
Note: These todos can wait until all the subcommands have been ported.
|
||||
|
||||
1. Consider giving db its own allocator
|
||||
## HIGH
|
||||
|
||||
1. **db.odin:380-383, 405, 446** — `sqlite.bind_text` return values overwritten but never checked. A failed bind means `sqlite.step` operates on unbound params.
|
||||
|
||||
## MEDIUM
|
||||
|
||||
2. **db.odin:324-327** — Map iteration (`remote_set`) is non-deterministic. Same file can produce different JSON on each backup, causing spurious DB diffs. Sort remotes before storing.
|
||||
|
||||
@@ -13,14 +18,22 @@
|
||||
|
||||
6. **cmd_restore.odin:44** — `os.mkdir_all` error silently discarded. Subsequent write failure will be confusing.
|
||||
|
||||
7. **cmd_edit_config.odin:27** — `$EDITOR` used as single binary name. Breaks for multi-word values like `"code -w"`. Needs `strings.fields()`.
|
||||
|
||||
8. **config.odin:178** — `search_paths` silently ignores `os.user_home_dir` error. If home is empty, `~` isn't expanded. Same class of bug as issue 3.
|
||||
|
||||
9. **prompt.odin:124** — `make([dynamic]bool, len(options))` creates N zero-initialized elements. Works because `false` is the default, but same footgun as original issue 1. Should be `make([dynamic]bool, 0, len(options))`.
|
||||
|
||||
## LOW
|
||||
|
||||
10. **db.odin:115** — `json.unmarshal_string` error not checked. Malformed JSON silently produces empty/partial data.
|
||||
|
||||
11. **db.odin:352-353** — `hex.encode` error ignored. `string(hex_bytes)` aliases the byte slice.
|
||||
|
||||
12. **cmd_sync.odin:80, cmd_list.odin:33, cmd_deps.odin:9** — `make([]string, 2)` for table rows never freed. Leaks per row. Defer to memory pass.
|
||||
|
||||
## REFACTOR
|
||||
|
||||
13. **cmd_list.odin** — Non-TTY branch builds `ListEntry` structs and marshals JSON separately. Now that `render_json_rows` (issue 1) accepts an `io.Writer` and uses `json.marshal`, unify both branches to use it. Note: will change JSON keys from `"directory"/"path"` to `"Directory"/"Path"`.
|
||||
|
||||
14. Check for prealloc opportunities. i.e. `make([dynamic]string)` -> `make([dynamic]string, 5)`.
|
||||
@@ -41,8 +54,6 @@
|
||||
|
||||
22. Change struct field names from PascalCase to snake_case.
|
||||
|
||||
23. procedures should be ordered by use, main at the top, then in the order they are called from main.
|
||||
|
||||
## Double-check AI output
|
||||
|
||||
- [ ] cli.odin
|
||||
|
||||
@@ -75,7 +75,7 @@ envr_dir :: proc(config_path: string) -> string {
|
||||
return filepath.dir(config_path)
|
||||
}
|
||||
|
||||
data_path :: proc(config_path: string) -> string {
|
||||
data_encrypted_path :: proc(config_path: string) -> string {
|
||||
path, _ := filepath.join([]string{envr_dir(config_path), "data.envr"})
|
||||
return path
|
||||
}
|
||||
@@ -126,7 +126,6 @@ find_ssh_private_keys :: proc() -> (keys: [dynamic]string, ok: bool) {
|
||||
return
|
||||
}
|
||||
|
||||
// Caller is responsible for calling delete_config()
|
||||
new_config :: proc(
|
||||
private_key_paths: []string,
|
||||
cfg_path: string = "~/.envr/config.json",
|
||||
|
||||
@@ -163,8 +163,8 @@ test_envr_dir :: proc(t: ^testing.T) {
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_data_path :: proc(t: ^testing.T) {
|
||||
p := data_path("/tmp/envr-fake-home-datapath/config.json")
|
||||
test_data_encrypted_path :: proc(t: ^testing.T) {
|
||||
p := data_encrypted_path("/tmp/envr-fake-home-datapath/config.json")
|
||||
defer delete(p)
|
||||
testing.expectf(t, strings.has_suffix(p, "data.envr"), "should end with data.envr, got %s", p)
|
||||
testing.expectf(t, strings.contains(p, ".envr"), "should contain .envr dir, got %s", p)
|
||||
|
||||
123
db.odin
123
db.odin
@@ -66,7 +66,7 @@ db_open :: proc(cfg_path: string) -> (Db, bool) {
|
||||
return Db{}, false
|
||||
}
|
||||
|
||||
data_path := data_path(cfg.config_path)
|
||||
data_path := data_encrypted_path(cfg.config_path)
|
||||
_, stat_err := os.stat(data_path, context.allocator)
|
||||
|
||||
db: ^rawptr
|
||||
@@ -95,31 +95,32 @@ db_open :: proc(cfg_path: string) -> (Db, bool) {
|
||||
}
|
||||
|
||||
db_close :: proc(d: ^Db) {
|
||||
defer sqlite.db_close(d.db)
|
||||
|
||||
if d.changed {
|
||||
rc := sqlite.db_exec(d.db, "VACUUM", nil, nil, nil)
|
||||
if rc != sqlite.OK {
|
||||
fmt.printf("Error vacuuming database: %s\n", sqlite.db_errmsg(d.db))
|
||||
tmp_path := make_temp_path()
|
||||
|
||||
if !db_vacuum_to_file(d.db, tmp_path) {
|
||||
os.remove(tmp_path)
|
||||
sqlite.db_close(d.db)
|
||||
return
|
||||
}
|
||||
|
||||
sz: i64
|
||||
data := sqlite.serialize(d.db, "main", &sz, 0)
|
||||
if data == nil {
|
||||
fmt.println("Error: failed to serialize database")
|
||||
sqlite_data, read_err := os.read_entire_file_from_path(tmp_path, context.allocator)
|
||||
os.remove(tmp_path)
|
||||
if read_err != nil {
|
||||
fmt.printf("Error reading vacuumed database: %v\n", read_err)
|
||||
sqlite.db_close(d.db)
|
||||
return
|
||||
}
|
||||
defer sqlite.free(data)
|
||||
|
||||
sqlite_data := data[:sz]
|
||||
encrypted, enc_ok := encrypt(sqlite_data, d.cfg.Keys[:])
|
||||
delete(sqlite_data)
|
||||
if !enc_ok {
|
||||
fmt.println("Error: encryption failed")
|
||||
sqlite.db_close(d.db)
|
||||
return
|
||||
}
|
||||
|
||||
data_path := data_path(d.cfg.config_path)
|
||||
data_path := data_encrypted_path(d.cfg.config_path)
|
||||
envr_d := envr_dir(d.cfg.config_path)
|
||||
os.mkdir_all(envr_d)
|
||||
|
||||
@@ -127,11 +128,13 @@ db_close :: proc(d: ^Db) {
|
||||
delete(encrypted)
|
||||
if write_err != nil {
|
||||
fmt.printf("Error writing encrypted database: %v\n", write_err)
|
||||
sqlite.db_close(d.db)
|
||||
return
|
||||
}
|
||||
|
||||
d.changed = false
|
||||
}
|
||||
sqlite.db_close(d.db)
|
||||
}
|
||||
|
||||
// Caller is responsible for calling:
|
||||
@@ -189,12 +192,22 @@ db_list :: proc(d: ^Db, allocator := context.allocator) -> (results: [dynamic]En
|
||||
return
|
||||
}
|
||||
|
||||
db_vacuum_to_file :: proc(db: ^rawptr, path: string) -> bool {
|
||||
b: strings.Builder
|
||||
strings.builder_init(&b)
|
||||
defer strings.builder_destroy(&b)
|
||||
fmt.sbprintf(&b, "VACUUM INTO '%s'", path)
|
||||
rc := sqlite.db_exec(db, to_cstring(&b), nil, nil, nil)
|
||||
if rc != sqlite.OK {
|
||||
fmt.printf("Error vacuuming database: %s\n", sqlite.db_errmsg(db))
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
db_restore_from_encrypted :: proc(db: ^rawptr, cfg: Config) -> bool {
|
||||
encrypted_data, read_err := os.read_entire_file_from_path(
|
||||
data_path(cfg.config_path),
|
||||
context.allocator,
|
||||
)
|
||||
defer delete(encrypted_data)
|
||||
data_path := data_encrypted_path(cfg.config_path)
|
||||
encrypted_data, read_err := os.read_entire_file_from_path(data_path, context.temp_allocator)
|
||||
if read_err != nil {
|
||||
fmt.printf("Error reading encrypted database: %v\n", read_err)
|
||||
return false
|
||||
@@ -207,31 +220,49 @@ db_restore_from_encrypted :: proc(db: ^rawptr, cfg: Config) -> bool {
|
||||
}
|
||||
defer delete(plaintext)
|
||||
|
||||
n := i64(len(plaintext))
|
||||
buf := sqlite.malloc64(n)
|
||||
if buf == nil {
|
||||
fmt.println("Error: failed to allocate buffer for deserialization")
|
||||
tmp_path := make_temp_path()
|
||||
write_err := os.write_entire_file(tmp_path, plaintext)
|
||||
if write_err != nil {
|
||||
fmt.printf("Error writing temp database: %v\n", write_err)
|
||||
return false
|
||||
}
|
||||
copy(buf[:len(plaintext)], plaintext)
|
||||
defer os.remove(tmp_path)
|
||||
|
||||
rc := sqlite.deserialize(
|
||||
db,
|
||||
"main",
|
||||
buf,
|
||||
n,
|
||||
n,
|
||||
sqlite.DESERIALIZE_FREEONCLOSE | sqlite.DESERIALIZE_RESIZEABLE,
|
||||
)
|
||||
if rc != sqlite.OK {
|
||||
sqlite.free(buf)
|
||||
fmt.printf("Error deserializing database: %s\n", sqlite.db_errmsg(db))
|
||||
if !db_attach_and_copy(db, tmp_path) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
db_attach_and_copy :: proc(mem_db: ^rawptr, src_path: string) -> bool {
|
||||
b: strings.Builder
|
||||
strings.builder_init(&b)
|
||||
defer strings.builder_destroy(&b)
|
||||
fmt.sbprintf(&b, "ATTACH DATABASE '%s' AS source", src_path)
|
||||
|
||||
rc := sqlite.db_exec(mem_db, to_cstring(&b), nil, nil, nil)
|
||||
if rc != sqlite.OK {
|
||||
fmt.printf("Error attaching database: %s\n", sqlite.db_errmsg(mem_db))
|
||||
return false
|
||||
}
|
||||
|
||||
rc = sqlite.db_exec(
|
||||
mem_db,
|
||||
"INSERT INTO main.envr_env_files SELECT * FROM source.envr_env_files",
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
if rc != sqlite.OK {
|
||||
fmt.printf("Error copying data: %s\n", sqlite.db_errmsg(mem_db))
|
||||
sqlite.db_exec(mem_db, "DETACH DATABASE source", nil, nil, nil)
|
||||
return false
|
||||
}
|
||||
|
||||
sqlite.db_exec(mem_db, "DETACH DATABASE source", nil, nil, nil)
|
||||
return true
|
||||
}
|
||||
|
||||
get_git_remotes :: proc(dir: string) -> [dynamic]string {
|
||||
remotes: [dynamic]string
|
||||
@@ -351,34 +382,18 @@ db_insert :: proc(d: ^Db, file: EnvFile) -> bool {
|
||||
cpath := to_cstring(file.Path)
|
||||
defer delete(cpath)
|
||||
rc = sqlite.bind_text(stmt, 1, cpath, -1, nil)
|
||||
if rc != sqlite.OK {
|
||||
fmt.printf("Error binding path: %s\n", sqlite.db_errmsg(d.db))
|
||||
return false
|
||||
}
|
||||
|
||||
cremotes := to_cstring(string(remotes_json))
|
||||
defer delete(cremotes)
|
||||
rc = sqlite.bind_text(stmt, 2, cremotes, -1, nil)
|
||||
if rc != sqlite.OK {
|
||||
fmt.printf("Error binding remotes: %s\n", sqlite.db_errmsg(d.db))
|
||||
return false
|
||||
}
|
||||
|
||||
csha := to_cstring(file.Sha256)
|
||||
defer delete(csha)
|
||||
rc = sqlite.bind_text(stmt, 3, csha, -1, nil)
|
||||
if rc != sqlite.OK {
|
||||
fmt.printf("Error binding sha256: %s\n", sqlite.db_errmsg(d.db))
|
||||
return false
|
||||
}
|
||||
|
||||
ccontents := to_cstring(file.contents)
|
||||
defer delete(ccontents)
|
||||
rc = sqlite.bind_text(stmt, 4, ccontents, -1, nil)
|
||||
if rc != sqlite.OK {
|
||||
fmt.printf("Error binding contents: %s\n", sqlite.db_errmsg(d.db))
|
||||
return false
|
||||
}
|
||||
|
||||
rc = sqlite.step(stmt)
|
||||
if rc != sqlite.DONE {
|
||||
@@ -403,10 +418,6 @@ db_fetch :: proc(d: ^Db, path: string, allocator := context.allocator) -> (EnvFi
|
||||
cpath := to_cstring(path, allocator)
|
||||
defer delete(cpath, allocator)
|
||||
rc = sqlite.bind_text(stmt, 1, cpath, -1, nil)
|
||||
if rc != sqlite.OK {
|
||||
fmt.printf("Error binding path: %s\n", sqlite.db_errmsg(d.db))
|
||||
return EnvFile{}, false
|
||||
}
|
||||
rc = sqlite.step(stmt)
|
||||
if rc == sqlite.DONE {
|
||||
fmt.printf("No file found with path: %s\n", path)
|
||||
@@ -448,10 +459,6 @@ db_delete :: proc(d: ^Db, path: string) -> bool {
|
||||
cpath := to_cstring(path)
|
||||
defer delete(cpath)
|
||||
rc = sqlite.bind_text(stmt, 1, cpath, -1, nil)
|
||||
if rc != sqlite.OK {
|
||||
fmt.printf("Error binding path: %s\n", sqlite.db_errmsg(d.db))
|
||||
return false
|
||||
}
|
||||
rc = sqlite.step(stmt)
|
||||
if rc != sqlite.DONE {
|
||||
fmt.printf("Error deleting: %s\n", sqlite.db_errmsg(d.db))
|
||||
|
||||
@@ -136,7 +136,7 @@ test_encrypt_write_read_decrypt :: proc(t: ^testing.T) {
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_decrypt_then_deserialize_sqlite :: proc(t: ^testing.T) {
|
||||
test_decrypt_then_attach_sqlite :: proc(t: ^testing.T) {
|
||||
cfg := fixture_config()
|
||||
defer {
|
||||
delete(cfg.Keys)
|
||||
@@ -164,6 +164,14 @@ test_decrypt_then_deserialize_sqlite :: proc(t: ^testing.T) {
|
||||
}
|
||||
defer delete(plaintext)
|
||||
|
||||
tmp_db_path := fmt.tprintf("/tmp/envr-test-attach-%d.db", os.get_pid())
|
||||
write_err := os.write_entire_file(tmp_db_path, plaintext)
|
||||
testing.expectf(t, write_err == nil, "failed to write temp db: %v", write_err)
|
||||
if write_err != nil {
|
||||
return
|
||||
}
|
||||
defer os.remove(tmp_db_path)
|
||||
|
||||
mem_db: ^rawptr
|
||||
rc := sqlite.db_open(":memory:", &mem_db)
|
||||
testing.expectf(t, rc == sqlite.OK, "failed to open in-memory db")
|
||||
@@ -172,25 +180,12 @@ test_decrypt_then_deserialize_sqlite :: proc(t: ^testing.T) {
|
||||
}
|
||||
defer sqlite.db_close(mem_db)
|
||||
|
||||
n := i64(len(plaintext))
|
||||
buf := sqlite.malloc64(n)
|
||||
testing.expect(t, buf != nil, "malloc64 should succeed")
|
||||
if buf == nil do return
|
||||
copy(buf[:len(plaintext)], plaintext)
|
||||
create_sql: cstring = "CREATE TABLE IF NOT EXISTS envr_env_files (path TEXT PRIMARY KEY NOT NULL, remotes TEXT, sha256 TEXT NOT NULL, contents TEXT NOT NULL)"
|
||||
rc = sqlite.db_exec(mem_db, create_sql, nil, nil, nil)
|
||||
testing.expect(t, rc == sqlite.OK, "failed to create table")
|
||||
|
||||
rc = sqlite.deserialize(
|
||||
mem_db,
|
||||
"main",
|
||||
buf,
|
||||
n,
|
||||
n,
|
||||
sqlite.DESERIALIZE_FREEONCLOSE | sqlite.DESERIALIZE_RESIZEABLE,
|
||||
)
|
||||
testing.expect(t, rc == sqlite.OK, "deserialize should succeed")
|
||||
if rc != sqlite.OK {
|
||||
sqlite.free(buf)
|
||||
return
|
||||
}
|
||||
attach_ok := db_attach_and_copy(mem_db, tmp_db_path)
|
||||
testing.expect(t, attach_ok, "failed to attach and copy")
|
||||
|
||||
sql: cstring = "SELECT path FROM envr_env_files"
|
||||
stmt: ^rawptr
|
||||
|
||||
21
db_test.odin
21
db_test.odin
@@ -215,7 +215,7 @@ test_db_delete_sets_changed :: proc(t: ^testing.T) {
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_db_serialize :: proc(t: ^testing.T) {
|
||||
test_db_vacuum_to_file :: proc(t: ^testing.T) {
|
||||
d, ok := make_test_db()
|
||||
testing.expect(t, ok, "failed to create test db")
|
||||
if !ok do return
|
||||
@@ -225,13 +225,20 @@ test_db_serialize :: proc(t: ^testing.T) {
|
||||
defer delete(f.Remotes)
|
||||
db_insert(&d, f)
|
||||
|
||||
sz: i64
|
||||
data := sqlite.serialize(d.db, "main", &sz, 0)
|
||||
testing.expect(t, data != nil, "serialize should return non-nil")
|
||||
if data == nil do return
|
||||
defer sqlite.free(data)
|
||||
vacuum_path := fmt.tprintf("/tmp/envr-test-vacuum-%d.db", os.get_pid())
|
||||
defer os.remove(vacuum_path)
|
||||
|
||||
testing.expect(t, sz > 0, "serialized size should be > 0")
|
||||
testing.expect(t, db_vacuum_to_file(d.db, vacuum_path), "vacuum should succeed")
|
||||
|
||||
info, stat_err := os.stat(vacuum_path, context.allocator)
|
||||
defer os.file_info_delete(info, context.allocator)
|
||||
testing.expect(t, stat_err == nil, "vacuumed file should exist")
|
||||
|
||||
data, read_err := os.read_entire_file_from_path(vacuum_path, context.allocator)
|
||||
testing.expect(t, read_err == nil, "should read vacuumed file")
|
||||
defer delete(data)
|
||||
|
||||
testing.expect(t, len(data) > 0, "vacuumed file should be non-empty")
|
||||
}
|
||||
|
||||
@(test)
|
||||
|
||||
28
flake.nix
28
flake.nix
@@ -11,12 +11,11 @@
|
||||
};
|
||||
|
||||
outputs =
|
||||
inputs@{
|
||||
flake-parts,
|
||||
nixpkgs,
|
||||
nixpkgs-unstable,
|
||||
self,
|
||||
treefmt-nix,
|
||||
inputs@{ flake-parts
|
||||
, nixpkgs
|
||||
, nixpkgs-unstable
|
||||
, self
|
||||
, treefmt-nix
|
||||
}:
|
||||
flake-parts.lib.mkFlake { inherit inputs; } {
|
||||
imports = [
|
||||
@@ -30,18 +29,7 @@
|
||||
];
|
||||
|
||||
perSystem =
|
||||
{
|
||||
pkgs,
|
||||
system,
|
||||
inputs',
|
||||
...
|
||||
}:
|
||||
let
|
||||
mysqlite = pkgs.sqlite.overrideAttrs (old: {
|
||||
configureFlags = (old.configureFlags or [ ]) ++ [ "--enable-deserialize" ];
|
||||
});
|
||||
in
|
||||
{
|
||||
{ pkgs, system, inputs', ... }: {
|
||||
_module.args.pkgs = import nixpkgs {
|
||||
inherit system;
|
||||
config.allowUnfree = true;
|
||||
@@ -76,7 +64,7 @@
|
||||
|
||||
buildInputs = [
|
||||
pkgs.libsodium
|
||||
mysqlite
|
||||
pkgs.sqlite
|
||||
];
|
||||
|
||||
buildPhase = ''
|
||||
@@ -99,7 +87,7 @@
|
||||
nushell
|
||||
|
||||
libsodium
|
||||
mysqlite
|
||||
sqlite
|
||||
unstable.odin
|
||||
unstable.ols
|
||||
|
||||
|
||||
@@ -5,8 +5,6 @@ import "core:fmt"
|
||||
import "core:os"
|
||||
|
||||
main :: proc() {
|
||||
defer free_all(context.temp_allocator)
|
||||
|
||||
cmd, ok := parse_args(os.args, os.to_writer(os.stdout), os.to_writer(os.stderr))
|
||||
defer bufio.writer_flush(cmd.out_buf)
|
||||
if !ok {
|
||||
|
||||
192
prompt.odin
192
prompt.odin
@@ -3,9 +3,36 @@ package main
|
||||
import "core:fmt"
|
||||
import "core:sys/posix"
|
||||
|
||||
MultiSelect_Result :: enum {
|
||||
Confirm,
|
||||
Cancel,
|
||||
Raw_State :: struct {
|
||||
original: posix.termios,
|
||||
fd: posix.FD,
|
||||
}
|
||||
|
||||
enable_raw_mode :: proc(fd: posix.FD) -> (Raw_State, bool) {
|
||||
state: Raw_State
|
||||
state.fd = fd
|
||||
|
||||
if posix.tcgetattr(fd, &state.original) != .OK {
|
||||
return state, false
|
||||
}
|
||||
|
||||
attr: posix.termios = state.original
|
||||
attr.c_lflag -= {.ICANON, .ECHO, .ISIG, .IEXTEN}
|
||||
attr.c_iflag -= {.IXON, .ICRNL, .BRKINT, .INPCK, .ISTRIP}
|
||||
attr.c_oflag -= {.OPOST}
|
||||
attr.c_cflag += {.CS8}
|
||||
attr.c_cc[.VMIN] = 1
|
||||
attr.c_cc[.VTIME] = 0
|
||||
|
||||
if posix.tcsetattr(fd, .TCSAFLUSH, &attr) != .OK {
|
||||
return state, false
|
||||
}
|
||||
|
||||
return state, true
|
||||
}
|
||||
|
||||
disable_raw_mode :: proc(state: ^Raw_State) {
|
||||
posix.tcsetattr(state.fd, .TCSAFLUSH, &state.original)
|
||||
}
|
||||
|
||||
Key :: enum {
|
||||
@@ -17,9 +44,71 @@ Key :: enum {
|
||||
Unknown,
|
||||
}
|
||||
|
||||
Raw_State :: struct {
|
||||
original: posix.termios,
|
||||
fd: posix.FD,
|
||||
read_key :: proc() -> Key {
|
||||
buf: [3]u8
|
||||
|
||||
n := posix.read(posix.STDIN_FILENO, &buf[0], 1)
|
||||
if n <= 0 {
|
||||
return .Unknown
|
||||
}
|
||||
|
||||
switch buf[0] {
|
||||
case ' ':
|
||||
return .Space
|
||||
case '\n', '\r':
|
||||
return .Enter
|
||||
case 0x03:
|
||||
return .Escape
|
||||
case 0x1b:
|
||||
tv: posix.timeval
|
||||
tv.tv_sec = 0
|
||||
tv.tv_usec = posix.suseconds_t(100000)
|
||||
|
||||
set: posix.fd_set
|
||||
posix.FD_ZERO(&set)
|
||||
posix.FD_SET(posix.STDIN_FILENO, &set)
|
||||
|
||||
ready := posix.select(1, &set, nil, nil, &tv)
|
||||
if ready <= 0 {
|
||||
return .Escape
|
||||
}
|
||||
|
||||
n2 := posix.read(posix.STDIN_FILENO, &buf[1], 1)
|
||||
if n2 <= 0 || buf[1] != '[' {
|
||||
return .Escape
|
||||
}
|
||||
|
||||
posix.FD_ZERO(&set)
|
||||
posix.FD_SET(posix.STDIN_FILENO, &set)
|
||||
tv.tv_sec = 0
|
||||
tv.tv_usec = posix.suseconds_t(100000)
|
||||
|
||||
ready = posix.select(1, &set, nil, nil, &tv)
|
||||
if ready <= 0 {
|
||||
return .Escape
|
||||
}
|
||||
|
||||
n3 := posix.read(posix.STDIN_FILENO, &buf[2], 1)
|
||||
if n3 <= 0 {
|
||||
return .Escape
|
||||
}
|
||||
|
||||
switch buf[2] {
|
||||
case 'A':
|
||||
return .Up
|
||||
case 'B':
|
||||
return .Down
|
||||
case:
|
||||
return .Escape
|
||||
}
|
||||
case:
|
||||
return .Unknown
|
||||
}
|
||||
}
|
||||
|
||||
MultiSelect_Result :: enum {
|
||||
Confirm,
|
||||
Cancel,
|
||||
}
|
||||
|
||||
MAX_VISIBLE :: 7
|
||||
@@ -36,7 +125,7 @@ multi_select :: proc(
|
||||
return
|
||||
}
|
||||
|
||||
selected = make([dynamic]bool, 0, len(options))
|
||||
selected = make([dynamic]bool, len(options))
|
||||
cursor: int = 0
|
||||
scroll_offset: int = 0
|
||||
|
||||
@@ -110,92 +199,3 @@ render_options :: proc(
|
||||
return end - scroll_offset
|
||||
}
|
||||
|
||||
enable_raw_mode :: proc(fd: posix.FD) -> (Raw_State, bool) {
|
||||
state: Raw_State
|
||||
state.fd = fd
|
||||
|
||||
if posix.tcgetattr(fd, &state.original) != .OK {
|
||||
return state, false
|
||||
}
|
||||
|
||||
attr: posix.termios = state.original
|
||||
attr.c_lflag -= {.ICANON, .ECHO, .ISIG, .IEXTEN}
|
||||
attr.c_iflag -= {.IXON, .ICRNL, .BRKINT, .INPCK, .ISTRIP}
|
||||
attr.c_oflag -= {.OPOST}
|
||||
attr.c_cflag += {.CS8}
|
||||
attr.c_cc[.VMIN] = 1
|
||||
attr.c_cc[.VTIME] = 0
|
||||
|
||||
if posix.tcsetattr(fd, .TCSAFLUSH, &attr) != .OK {
|
||||
return state, false
|
||||
}
|
||||
|
||||
return state, true
|
||||
}
|
||||
|
||||
disable_raw_mode :: proc(state: ^Raw_State) {
|
||||
posix.tcsetattr(state.fd, .TCSAFLUSH, &state.original)
|
||||
}
|
||||
|
||||
read_key :: proc() -> Key {
|
||||
buf: [3]u8
|
||||
|
||||
n := posix.read(posix.STDIN_FILENO, &buf[0], 1)
|
||||
if n <= 0 {
|
||||
return .Unknown
|
||||
}
|
||||
|
||||
switch buf[0] {
|
||||
case ' ':
|
||||
return .Space
|
||||
case '\n', '\r':
|
||||
return .Enter
|
||||
case 0x03:
|
||||
return .Escape
|
||||
case 0x1b:
|
||||
tv: posix.timeval
|
||||
tv.tv_sec = 0
|
||||
tv.tv_usec = posix.suseconds_t(100000)
|
||||
|
||||
set: posix.fd_set
|
||||
posix.FD_ZERO(&set)
|
||||
posix.FD_SET(posix.STDIN_FILENO, &set)
|
||||
|
||||
ready := posix.select(1, &set, nil, nil, &tv)
|
||||
if ready <= 0 {
|
||||
return .Escape
|
||||
}
|
||||
|
||||
n2 := posix.read(posix.STDIN_FILENO, &buf[1], 1)
|
||||
if n2 <= 0 || buf[1] != '[' {
|
||||
return .Escape
|
||||
}
|
||||
|
||||
posix.FD_ZERO(&set)
|
||||
posix.FD_SET(posix.STDIN_FILENO, &set)
|
||||
tv.tv_sec = 0
|
||||
tv.tv_usec = posix.suseconds_t(100000)
|
||||
|
||||
ready = posix.select(1, &set, nil, nil, &tv)
|
||||
if ready <= 0 {
|
||||
return .Escape
|
||||
}
|
||||
|
||||
n3 := posix.read(posix.STDIN_FILENO, &buf[2], 1)
|
||||
if n3 <= 0 {
|
||||
return .Escape
|
||||
}
|
||||
|
||||
switch buf[2] {
|
||||
case 'A':
|
||||
return .Up
|
||||
case 'B':
|
||||
return .Down
|
||||
case:
|
||||
return .Escape
|
||||
}
|
||||
case:
|
||||
return .Unknown
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,9 +8,6 @@ OK :: 0
|
||||
ROW :: 100
|
||||
DONE :: 101
|
||||
|
||||
DESERIALIZE_FREEONCLOSE :: 1
|
||||
DESERIALIZE_RESIZEABLE :: 2
|
||||
|
||||
foreign lib {
|
||||
@(link_name="sqlite3_open")
|
||||
db_open :: proc(filename: cstring, ppDb: ^^rawptr) -> c.int ---
|
||||
@@ -34,12 +31,4 @@ foreign lib {
|
||||
bind_text :: proc(stmt: ^rawptr, idx: c.int, val: cstring, n: c.int, destructor: rawptr) -> c.int ---
|
||||
@(link_name="sqlite3_changes")
|
||||
changes :: proc(db: ^rawptr) -> c.int ---
|
||||
@(link_name="sqlite3_serialize")
|
||||
serialize :: proc(db: ^rawptr, zSchema: cstring, piSize: ^i64, mFlags: u32) -> [^]u8 ---
|
||||
@(link_name="sqlite3_deserialize")
|
||||
deserialize :: proc(db: ^rawptr, zSchema: cstring, pData: [^]u8, szDb: i64, szBuf: i64, mFlags: u32) -> c.int ---
|
||||
@(link_name="sqlite3_malloc64")
|
||||
malloc64 :: proc(n: i64) -> [^]u8 ---
|
||||
@(link_name="sqlite3_free")
|
||||
free :: proc(p: rawptr) ---
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
0.3.0
|
||||
0.2.0
|
||||
|
||||
Reference in New Issue
Block a user