Skip to content

Commit

Permalink
Feat/implement show version flag (#181)
Browse files Browse the repository at this point in the history
* chore: define option struct fields

Introduce new fields on the Options struct to store the string rep
of the flag content the user should pass to the exec of lilly to invoke
the printing of the version it was built with.

Currently unsure how to handle passing multiple display flags, such as
if the user passes --help --version, not sure what to do, I suppose the
most sensible behaviour would be to just print out both version + help?

* refactor: use string builder over long ass string

We would like a helpful function that takes an option and prints
all of the currently supported flags and their behaviour to actually
be useful. The string it returns was becoming unweidly and unbecoming
of a useful function so we've now switched to using a string builder
instead as this lets us nicely lay out the building of the output string

* refactor: adjust location of gitcommit hash resolve

In order to allow the main function body to access the current Lilly
version, the compile time constant resolve has been moved from the
splashscreen module to the root/main and is now just passed in.

* docs: add note comment

The note indicates intentions regarding behaviour handling when the user
potentially passes both the --help and --version flags at the same time
and what to do in this situation.
  • Loading branch information
tauraamui authored Jul 29, 2024
1 parent 1c6596f commit 7471246
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/editor.v
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mut:
quit()
}

pub fn open_editor(mut _log log.Log, _clipboard clipboard.Clipboard, workspace_root_dir string) !&Editor {
pub fn open_editor(mut _log log.Log, _clipboard clipboard.Clipboard, commit_hash string, workspace_root_dir string) !&Editor {
mut editor := Editor{ clipboard: _clipboard, file_finder_modal: unsafe { nil } }
editor.workspace = workspace.open_workspace(
mut _log,
Expand All @@ -54,7 +54,7 @@ pub fn open_editor(mut _log log.Log, _clipboard clipboard.Clipboard, workspace_r
) or { return error("unable to open workspace '${workspace_root_dir}' -> ${err}")
}

editor.views << new_splash(editor.workspace.config.leader_key)
editor.views << new_splash(commit_hash, editor.workspace.config.leader_key)
editor.view = &editor.views[0]
return &editor
}
Expand Down
34 changes: 28 additions & 6 deletions src/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import log
import lib.clipboard
import lib.draw
import os.cmdline
import strings

const gitcommit_hash = $embed_file("./src/.githash").to_string()

struct App {
mut:
Expand Down Expand Up @@ -67,7 +70,10 @@ fn frame(mut app App) {

struct Options {
mut:
log_level string
log_level string
long_show_version_flag string
short_show_version_flag string
show_version bool
long_show_help_flag string
short_show_help_flag string
show_help bool
Expand All @@ -85,6 +91,8 @@ mut:
fn resolve_options_from_args(args []string) Options {
flags := cmdline.only_options(args)
mut opts := Options {
long_show_version_flag: "version",
short_show_version_flag: "v",
long_show_help_flag: "help",
short_show_help_flag: "h",
long_debug_mode_flag: "debug",
Expand All @@ -95,16 +103,27 @@ fn resolve_options_from_args(args []string) Options {
short_disable_panic_capture_flag: "dpc"
}

opts.show_help = "--${opts.long_show_help_flag}" in flags || "-${opts.short_show_help_flag}" in flags
opts.debug_mode = "--${opts.long_debug_mode_flag}" in flags || "-${opts.short_debug_mode_flag}" in flags
opts.capture_panics = "--${opts.long_capture_panics_flag}" in flags || "-${opts.short_capture_panics_flag}" in flags
opts.show_version = "--${opts.long_show_version_flag}" in flags || "-${opts.short_show_version_flag}" in flags
opts.show_help = "--${opts.long_show_help_flag}" in flags || "-${opts.short_show_help_flag}" in flags
opts.debug_mode = "--${opts.long_debug_mode_flag}" in flags || "-${opts.short_debug_mode_flag}" in flags
opts.capture_panics = "--${opts.long_capture_panics_flag}" in flags || "-${opts.short_capture_panics_flag}" in flags
opts.disable_panic_capture = "--${opts.long_disable_panic_capture_flag}" in flags || "-${opts.short_disable_panic_capture_flag}" in flags

return opts
}

fn (opts Options) flags_str() string {
return "--${opts.long_show_help_flag} (show help)\n\t--${opts.long_debug_mode_flag} (enable debug log out)\n\t--${opts.long_disable_panic_capture_flag} (disable persistance of panic stack trace output)"
mut sb := strings.new_builder(512)
sb.write_string("--${opts.long_show_help_flag} (show help)")
sb.write_string("\n\t--${opts.long_show_version_flag} (show version)")
sb.write_string("\n\t--${opts.long_debug_mode_flag} (enable debug log out)")
sb.write_string("\n\t--${opts.long_disable_panic_capture_flag} (disable persistance of panic stack trace output)")
return sb.str()
}

fn output_version_and_close(commit_hash string) {
version_label := "lilly - dev version (#${commit_hash})"
print_and_exit(version_label)
}

fn output_help_and_close(opts Options) {
Expand All @@ -116,6 +135,9 @@ fn main() {
args := os.args[1..]
opts := resolve_options_from_args(args)

// NOTE(tauraamui): I would like it to be possible to output both the
// version and help simultaniously but this is low priority atm.
if opts.show_version { output_version_and_close(gitcommit_hash) }
if opts.show_help { output_help_and_close(opts) }

if opts.disable_panic_capture == false { persist_stderr_to_disk() }
Expand Down Expand Up @@ -144,7 +166,7 @@ fn main() {
files := cmdline.only_non_options(args)
if files.len == 0 { print_and_exit("missing directoy path") }
if files.len > 1 { print_and_exit("too many directory paths (${files.len}) expected one") }
app.editor = open_editor(mut l, clipboard.new(), files[0]) or { print_and_exit("${err}"); unsafe { nil } }
app.editor = open_editor(mut l, clipboard.new(), gitcommit_hash, files[0]) or { print_and_exit("${err}"); unsafe { nil } }
if opts.debug_mode {
app.editor.start_debug()
}
Expand Down
8 changes: 5 additions & 3 deletions src/splash_view.v
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import term { strikethrough }
import lib.draw

const logo_contents = $embed_file("./src/splash-logo.txt")
const gitcommit_hash = $embed_file("./src/.githash").to_string()

struct Logo{
mut:
Expand All @@ -28,6 +27,7 @@ mut:
}

struct SplashScreen {
commit_hash string
pub:
file_path string
mut:
Expand All @@ -37,8 +37,9 @@ mut:
leader_key string
}

pub fn new_splash(leader_key string) Viewable {
pub fn new_splash(leader_key string, commit_hash string) Viewable {
mut splash := SplashScreen{
commit_hash: commit_hash
file_path: "**lss**"
logo: Logo{
data: logo_contents.to_string().split_into_lines()
Expand Down Expand Up @@ -75,7 +76,8 @@ pub fn (splash SplashScreen) draw(mut ctx draw.Contextable) {
offset_y += splash.logo.data.len
offset_y += (ctx.window_height() - offset_y) * 0.05

version_label := "lilly - dev version (#${gitcommit_hash})"
version_label := "lilly - dev version (#${splash.commit_hash})"
// version_label := "lilly - dev version (#${gitcommit_hash})"
ctx.draw_text(offset_x+(ctx.window_width() / 2) - (version_label.len / 2), int(math.floor(offset_y)), version_label)

offset_y += 2
Expand Down

0 comments on commit 7471246

Please sign in to comment.