first basic boilerplate of cocial
Signed-off-by: Sarmonsiill <sarmonsiill@tilde.guru>
This commit is contained in:
commit
b696f4d2b4
|
@ -0,0 +1,178 @@
|
|||
import data
|
||||
import output
|
||||
import readline { Readline }
|
||||
import sqlite
|
||||
import term
|
||||
import utils
|
||||
|
||||
struct Profile {
|
||||
pub mut:
|
||||
name string
|
||||
bio string
|
||||
age int
|
||||
email string
|
||||
created string
|
||||
}
|
||||
|
||||
struct State_object {
|
||||
pub mut:
|
||||
database sqlite.DB
|
||||
mut:
|
||||
profile Profile
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
println(
|
||||
term.bright_bg_blue(
|
||||
term.bold(
|
||||
term.underline("Welcome to Cocial")
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
mut state := State_object{}
|
||||
|
||||
state.database = data.run() or {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
term.clear()
|
||||
|
||||
if !state.profile_exists() {
|
||||
state.initiate_profile() or {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
state.populate_state_profile()
|
||||
|
||||
mut r := Readline{}
|
||||
for {
|
||||
cmd := r.read_line(term.rgb(255, 112, 112, '- ')) or {
|
||||
output.fail(err.msg)
|
||||
return
|
||||
}
|
||||
state.main_loop(cmd.str().trim_space())
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut state State_object) main_loop(input string) {
|
||||
|
||||
input_split := input.split(' ')
|
||||
cmd := input_split[0]
|
||||
mut args := []string{}
|
||||
if input_split.len > 1 {
|
||||
args << input_split[1..]
|
||||
}
|
||||
|
||||
match cmd {
|
||||
'help' {
|
||||
println('help')
|
||||
}
|
||||
'exit' { exit(0) }
|
||||
'quit' { exit(0) }
|
||||
'write' {
|
||||
state.create_timeline_post() or {
|
||||
output.fail(err)
|
||||
}
|
||||
}
|
||||
'timeline' {
|
||||
state.view_timeline() or {
|
||||
output.fail(err)
|
||||
}
|
||||
}
|
||||
'profile' { println(state.profile) }
|
||||
else {}
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut state State_object) initiate_profile() ? {
|
||||
|
||||
output.inform("No profile found. You will be asked some questions to create one")
|
||||
|
||||
state.profile.name = utils.no_lines(
|
||||
readline.read_line("What is your name? ") or {
|
||||
return err
|
||||
}
|
||||
)
|
||||
age_string := utils.no_lines(
|
||||
readline.read_line("How old are you? ") or {
|
||||
return err
|
||||
}
|
||||
)
|
||||
state.profile.age = age_string.int()
|
||||
state.profile.bio = utils.no_lines(
|
||||
readline.read_line("Tell me a little bit more about you: ") or {
|
||||
return err
|
||||
}
|
||||
)
|
||||
state.profile.email = utils.no_lines(
|
||||
readline.read_line("What is your email? ") or {
|
||||
return err
|
||||
}
|
||||
)
|
||||
|
||||
state.create_profile()
|
||||
}
|
||||
|
||||
fn (mut state State_object) create_profile() {
|
||||
insert_query := 'insert into profile (name,bio,age,email) values ("${state.profile.name}", "${state.profile.bio}", ${state.profile.age}, "${state.profile.email}");'
|
||||
state.database.exec(insert_query)
|
||||
}
|
||||
|
||||
fn (mut state State_object) profile_exists() bool {
|
||||
count := state.database.q_int("select count(*) from profile;")
|
||||
if count > 0 {
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
fn (mut state State_object) populate_state_profile() {
|
||||
profiles, code := state.database.exec(
|
||||
"select * from profile limit 1"
|
||||
)
|
||||
if code == 101 {
|
||||
state.profile.name = profiles[0].vals[1]
|
||||
state.profile.email = profiles[0].vals[2]
|
||||
state.profile.bio = profiles[0].vals[3]
|
||||
state.profile.age = profiles[0].vals[4].int()
|
||||
state.profile.created = profiles[0].vals[5]
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut state State_object) view_timeline() ? {
|
||||
posts, code := state.database.exec(
|
||||
"select * from timeline order by created asc"
|
||||
)
|
||||
if code == 101 {
|
||||
for post in posts {
|
||||
output.post_header(post.vals[1])
|
||||
println(post.vals[2])
|
||||
println("---")
|
||||
println("Created ${post.vals[3]}")
|
||||
println("---\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut state State_object) create_timeline_post() ? {
|
||||
topic := utils.no_lines(
|
||||
readline.read_line("Topic: ") or {
|
||||
return err
|
||||
}
|
||||
)
|
||||
|
||||
/* no enter right now. @todo: open $EDITOR for this part */
|
||||
content := utils.no_lines(
|
||||
readline.read_line("Content (no enter until done): ") or {
|
||||
return err
|
||||
}
|
||||
)
|
||||
|
||||
insert_query := 'insert into timeline (topic,content) values ("${topic}", "${content}");'
|
||||
state.database.exec(insert_query)
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
module data
|
||||
|
||||
import os
|
||||
import sqlite
|
||||
|
||||
const database_location = os.real_path('./data/db.sqlite3')
|
||||
|
||||
pub fn run() ?sqlite.DB {
|
||||
db := sqlite.connect(database_location) or {
|
||||
return err
|
||||
}
|
||||
profile_table := "CREATE TABLE IF NOT EXISTS profile (id integer primary key,name text default 'j doe',email text default 'j.doe@example.org', bio text default '', age integer default 0, created datetime default current_timestamp);"
|
||||
db.exec(profile_table)
|
||||
|
||||
timeline_table := "CREATE TABLE IF NOT EXISTS timeline (id integer primary key, topic text default '', content text default '', created datetime default current_timestamp);"
|
||||
db.exec(timeline_table)
|
||||
|
||||
|
||||
return db
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1,21 @@
|
|||
module output
|
||||
|
||||
import term
|
||||
|
||||
pub fn inform(msg string) {
|
||||
println(
|
||||
term.bright_bg_green(msg)
|
||||
)
|
||||
}
|
||||
|
||||
pub fn post_header(msg string) {
|
||||
println(
|
||||
term.underline(msg)
|
||||
)
|
||||
}
|
||||
|
||||
pub fn fail(msg string) {
|
||||
println(
|
||||
term.bright_bg_red(msg)
|
||||
)
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
module utils
|
||||
|
||||
pub fn no_lines(s string) string {
|
||||
mut without_lines := s.replace('\n', ' ')
|
||||
return without_lines.trim_space()
|
||||
}
|
Loading…
Reference in New Issue