cocial/cocial.v

179 lines
3.4 KiB
V

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)
}