Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment/gui render target #119

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
abstract render context into generic type
  • Loading branch information
tauraamui committed Dec 12, 2023
commit a8f6948788170e54a571bece5bdeef909059d93c
3 changes: 2 additions & 1 deletion src/editor.v
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import term.ui as tui
import lib.buffer
import lib.clipboard
import lib.workspace
import lib.draw

struct Editor {
mut:
Expand Down Expand Up @@ -96,7 +97,7 @@ fn (mut editor Editor) close_file_finder() {
editor.file_finder_modal_open = false
}

pub fn (mut editor Editor) draw(mut ctx tui.Context) {
pub fn (mut editor Editor) draw(mut ctx draw.Context) {
editor.view.draw(mut ctx)
if editor.file_finder_modal_open {
editor.file_finder_modal.draw(mut ctx)
Expand Down
5 changes: 3 additions & 2 deletions src/file_finder_modal.v
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module main

import term.ui as tui
import strings
import lib.draw

const max_height = 20

Expand Down Expand Up @@ -51,7 +52,7 @@ fn (mut file_search FileSearch) backspace() {
if file_search.cursor_x < 0 { file_search.cursor_x = 0 }
}

fn (mut file_finder_modal FileFinderModal) draw(mut ctx tui.Context) {
fn (mut file_finder_modal FileFinderModal) draw(mut ctx draw.Context) {
defer { ctx.reset_bg_color() }
ctx.set_color(r: 245, g: 245, b: 245)
ctx.set_bg_color(r: 15, g: 15, b: 15)
Expand All @@ -67,7 +68,7 @@ fn (mut file_finder_modal FileFinderModal) draw(mut ctx tui.Context) {
ctx.draw_text(1+utf8_str_visible_length(search_label)+1, y_offset, file_finder_modal.search.query)
}

fn (mut file_finder_modal FileFinderModal) draw_scrollable_list(mut ctx tui.Context, y_offset int, list []ScoredFilePath) int {
fn (mut file_finder_modal FileFinderModal) draw_scrollable_list(mut ctx draw.Context, y_offset int, list []ScoredFilePath) int {
ctx.reset_bg_color()
ctx.set_bg_color(r: 15, g: 15, b: 15)
ctx.draw_rect(1, y_offset, ctx.window_width, y_offset+max_height - 1)
Expand Down
28 changes: 28 additions & 0 deletions src/lib/draw/ctx.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module draw

import term.ui as tui

pub interface Context {
mut:
window_width int
window_height int

set_cursor_position(x int, y int)

draw_text(x int, y int, text string)
write(c string)
draw_rect(x int, y int, width int, height int)
draw_point(x int, y int)

bold()

set_color(c tui.Color)
set_bg_color(c tui.Color)
reset_color()
reset_bg_color()
reset()

run() !
clear()
flush()
}
40 changes: 20 additions & 20 deletions src/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ import os
import term.ui as tui
import log
import lib.clipboard
import lib.draw

struct App {
mut:
log &log.Log
tui &tui.Context = unsafe { nil }
ui &draw.Context = unsafe { nil }
editor &Editor = unsafe { nil }
view &View = unsafe { nil }
views []View
Expand Down Expand Up @@ -57,11 +59,11 @@ fn event(e &tui.Event, mut app &App) {
fn frame(mut app &App) {
if app.changed {
app.changed = false
app.tui.clear()
app.ui.clear()

app.editor.draw(mut app.tui)
app.editor.draw(mut app.ui)

app.tui.flush()
app.ui.flush()
}
}

Expand All @@ -76,26 +78,24 @@ fn main() {
l.close()
}

$if gui ? {
} $else {
mut app := &App{
log: &l
changed: true
}
mut app := &App{
log: &l
changed: true
}

app.tui = tui.init(
user_data: app
event_fn: event
frame_fn: frame
capture_events: true
use_alternate_buffer: true
)
$if !gui ? {
app.ui = tui.init(
user_data: app
event_fn: event
frame_fn: frame
capture_events: true
use_alternate_buffer: true)
valxntine marked this conversation as resolved.
Show resolved Hide resolved
} $else { print_and_exit("gui render target not yet available") }

path := os.args[1] or { "" }
app.editor = open_editor(clipboard.new(), path) or { print_and_exit("${err}"); unsafe { nil } }
path := os.args[1] or { "" }
app.editor = open_editor(clipboard.new(), path) or { print_and_exit("${err}"); unsafe { nil } }

app.tui.run()!
}
app.ui.run()!
}

fn print_and_exit(msg string) {
Expand Down
4 changes: 2 additions & 2 deletions src/mode.v
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

module main

import term.ui as tui
import lib.draw

enum Mode as u8 {
normal
Expand All @@ -27,7 +27,7 @@ enum Mode as u8 {
replace
}

fn (mode Mode) draw(mut ctx tui.Context, x int, y int) int {
fn (mode Mode) draw(mut ctx draw.Context, x int, y int) int {
defer { ctx.reset() }
label := mode.str()
status_line_y := y
Expand Down
3 changes: 2 additions & 1 deletion src/splash_view.v
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module main
import term.ui as tui
import math
import term { strikethrough }
import lib.draw

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

Expand Down Expand Up @@ -52,7 +53,7 @@ pub fn new_splash(leader_key string) Viewable {
return splash
}

pub fn (splash SplashScreen) draw(mut ctx tui.Context) {
pub fn (splash SplashScreen) draw(mut ctx draw.Context) {
offset_x := 1
mut offset_y := 1 + f64(ctx.window_height) * 0.1
ctx.set_color(r: 245, g: 191, b: 243)
Expand Down
12 changes: 6 additions & 6 deletions src/status_line.v
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

module main

import term.ui as tui
import lib.draw

struct SearchSelection {
active bool
Expand All @@ -31,7 +31,7 @@ struct Status {
git_branch string
}

fn draw_status_line(mut ctx tui.Context, status Status) {
fn draw_status_line(mut ctx draw.Context, status Status) {
defer { ctx.reset() }

y := ctx.window_height - 1
Expand All @@ -58,7 +58,7 @@ fn draw_status_line(mut ctx tui.Context, status Status) {
draw_cursor_position_segment(mut ctx, 1, y, status.cursor_x, status.cursor_y)
}

fn draw_file_name_segment(mut ctx tui.Context, x int, y int, file_name string) int {
fn draw_file_name_segment(mut ctx draw.Context, x int, y int, file_name string) int {
paint_shape_text(mut ctx, x, y, Color{ 86, 86, 86 }, "${slant_left_flat_top}${block}")
mut offset := 2
ctx.bold()
Expand All @@ -69,7 +69,7 @@ fn draw_file_name_segment(mut ctx tui.Context, x int, y int, file_name string) i
return offset
}

fn draw_search_selection_info_segment(mut ctx tui.Context, x int, y int, selection SearchSelection) int {
fn draw_search_selection_info_segment(mut ctx draw.Context, x int, y int, selection SearchSelection) int {
selection_info_label := "${selection.current}/${selection.total}"
mut offset := 2
paint_shape_text(mut ctx, x, y, status_purple, "${slant_left_flat_top}${block}")
Expand All @@ -80,7 +80,7 @@ fn draw_search_selection_info_segment(mut ctx tui.Context, x int, y int, selecti
return offset
}

fn draw_git_branch_section(mut ctx tui.Context, x int, y int, git_branch string) int {
fn draw_git_branch_section(mut ctx draw.Context, x int, y int, git_branch string) int {
paint_shape_text(mut ctx, x, y, status_dark_lilac, "${slant_left_flat_top}${block}")
mut offset := 2
paint_text_on_background(mut ctx, x + offset, y, status_dark_lilac, Color{ 230, 230, 230 }, git_branch)
Expand All @@ -90,7 +90,7 @@ fn draw_git_branch_section(mut ctx tui.Context, x int, y int, git_branch string)
return offset
}

fn draw_cursor_position_segment(mut ctx tui.Context, x int, y int, cursor_x int, cursor_y int) int {
fn draw_cursor_position_segment(mut ctx draw.Context, x int, y int, cursor_x int, cursor_y int) int {
cursor_info_label := "${cursor_y+1}:${cursor_x+1}"
paint_shape_text(mut ctx, ctx.window_width - 1, y, Color { 245, 42, 42 }, "${block}${block}")
ctx.bold()
Expand Down
25 changes: 13 additions & 12 deletions src/view.v
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import arrays
import lib.buffer
import lib.workspace
import lib.chords
import lib.draw

struct Cursor {
mut:
Expand Down Expand Up @@ -174,7 +175,7 @@ fn (mut search Search) get_line_matches(line_num int) []Match {
return matches
}

fn (mut search Search) draw(mut ctx tui.Context, draw_cursor bool) {
fn (mut search Search) draw(mut ctx draw.Context, draw_cursor bool) {
ctx.draw_text(1, ctx.window_height, search.to_find)
ctx.set_bg_color(r: 230, g: 230, b: 230)
ctx.draw_point(search.cursor_x+1, ctx.window_height)
Expand Down Expand Up @@ -299,7 +300,7 @@ mut:
cmd_history datatypes.Queue[string]
}

fn (mut cmd_buf CmdBuffer) draw(mut ctx tui.Context, draw_cursor bool) {
fn (mut cmd_buf CmdBuffer) draw(mut ctx draw.Context, draw_cursor bool) {
defer { ctx.reset_bg_color() }
if cmd_buf.code != .blank {
color := cmd_buf.code.color()
Expand Down Expand Up @@ -472,11 +473,11 @@ fn (mut view View) open_file(path string) {
interface Viewable {
file_path string
mut:
draw(mut tui.Context)
draw(mut draw.Context)
on_key_down(&tui.Event, mut Root)
}

fn (mut view View) draw(mut ctx tui.Context) {
fn (mut view View) draw(mut ctx draw.Context) {
view.height = ctx.window_height
view.x = "${view.buffer.lines.len}".len + 1
view.width = ctx.window_width
Expand Down Expand Up @@ -523,7 +524,7 @@ fn (mut view View) draw(mut ctx tui.Context) {
ctx.set_cursor_position(view.x+1+offset, cursor_screen_space_y+1)
}

fn (mut view View) draw_document(mut ctx tui.Context) {
fn (mut view View) draw_document(mut ctx draw.Context) {
mut to := view.from + view.code_view_height()
if to > view.buffer.lines.len { to = view.buffer.lines.len }
view.to = to
Expand Down Expand Up @@ -578,7 +579,7 @@ struct LineSegment {
typ SegmentKind
}

fn (mut view View) draw_text_line(mut ctx tui.Context, y int, line string, within_selection bool) {
fn (mut view View) draw_text_line(mut ctx draw.Context, y int, line string, within_selection bool) {
mut linex := line.replace("\t", " ".repeat(4))
mut max_width := view.width
visible_len := utf8_str_visible_length(linex)
Expand Down Expand Up @@ -712,7 +713,7 @@ fn resolve_line_segments(syntax workspace.Syntax, line string, is_multiline_comm
return segments, is_multiline_commentx
}

fn (mut view View) draw_text_line_number(mut ctx tui.Context, y int) {
fn (mut view View) draw_text_line_number(mut ctx draw.Context, y int) {
cursor_screenspace_y := view.cursor.pos.y - view.from
ctx.set_color(r: 117, g: 118, b: 120)

Expand Down Expand Up @@ -788,15 +789,15 @@ fn (mut view View) draw_line_show_whitespace(mut ctx tui.Context, i int, line_cp
// 4 - Underline (steady)
// 5 - Bar (blinking)
// 6 - Bar (steady)
fn set_cursor_to_block(mut ctx tui.Context) {
fn set_cursor_to_block(mut ctx draw.Context) {
ctx.write("\x1b[0 q")
}

fn set_cursor_to_underline(mut ctx tui.Context) {
fn set_cursor_to_underline(mut ctx draw.Context) {
ctx.write("\x1b[4 q")
}

fn set_cursor_to_vertical_bar(mut ctx tui.Context) {
fn set_cursor_to_vertical_bar(mut ctx draw.Context) {
ctx.write("\x1b[6 q")
}

Expand All @@ -806,13 +807,13 @@ struct Color {
b u8
}

fn paint_shape_text(mut ctx tui.Context, x int, y int, color Color, text string) {
fn paint_shape_text(mut ctx draw.Context, x int, y int, color Color, text string) {
ctx.set_color(r: color.r, g: color.g, b: color.b)
ctx.reset_bg_color()
ctx.draw_text(x, y, text)
}

fn paint_text_on_background(mut ctx tui.Context, x int, y int, bg_color Color, fg_color Color, text string) {
fn paint_text_on_background(mut ctx draw.Context, x int, y int, bg_color Color, fg_color Color, text string) {
ctx.set_bg_color(r: bg_color.r, g: bg_color.g, b: bg_color.b)
ctx.set_color(r: fg_color.r, g: fg_color.g, b: fg_color.b)
ctx.draw_text(x, y, text)
Expand Down
Loading