docs/readmes: format almost all remaining code blocks (#8590)

pull/8593/head^2
Lukas Neubert 2021-02-05 18:50:28 +01:00 committed by GitHub
parent 576492af4e
commit 58b3a30b47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 80 additions and 76 deletions

View File

@ -418,7 +418,7 @@ Literals like `123` or `4.56` are treated in a special way. They do
not lead to type promotions, however they default to `int` and `f64`
respectively, when their type has to be decided:
```v ignore
```v nofmt
u := u16(12)
v := 13 + u // v is of type `u16` - no promotion
x := f32(45.6)
@ -718,7 +718,7 @@ numbers.sort() // 1, 2, 3
numbers.sort(a > b) // 3, 2, 1
```
```v nofmt
```v
struct User {
age int
name string
@ -1487,7 +1487,7 @@ assert button.height == 20
As you can see, both the struct name and braces can be omitted, instead of:
```v ignore
```v oksyntax nofmt
new_button(ButtonConfig{text:'Click me', width:100})
```
@ -1499,20 +1499,20 @@ Struct fields are private and immutable by default (making structs immutable as
Their access modifiers can be changed with
`pub` and `mut`. In total, there are 5 possible options:
```v nofmt
```v
struct Foo {
a int // private immutable (default)
a int // private immutable (default)
mut:
b int // private mutable
c int // (you can list multiple fields with the same access modifier)
b int // private mutable
c int // (you can list multiple fields with the same access modifier)
pub:
d int // public immutable (readonly)
d int // public immutable (readonly)
pub mut:
e int // public, but mutable only in parent module
e int // public, but mutable only in parent module
__global:
f int // public and mutable both inside and outside parent module
} // (not recommended to use, that's why the 'global' keyword
// starts with __)
// (not recommended to use, that's why the 'global' keyword starts with __)
f int // public and mutable both inside and outside parent module
}
```
For example, here's the `string` type defined in the `builtin` module:
@ -1587,15 +1587,15 @@ intended for low-level applications like kernels and drivers.
It is possible to modify function arguments by using the keyword `mut`:
```v nofmt
```v
struct User {
name string
mut:
is_registered bool
is_registered bool
}
fn (mut u User) register() {
u.is_registered = true
u.is_registered = true
}
mut user := User{}
@ -1803,7 +1803,7 @@ module, and inside it. That restriction is relaxed only for the `main` module
constants too, i.e. just `println(numbers)`, not `println(main.numbers)` .
vfmt takes care of this rule, so you can type `println(pi)` inside the `math` module,
and vffmt will automatically update it to `println(math.pi)`.
and vfmt will automatically update it to `println(math.pi)`.
<!--
Many people prefer all caps consts: `TOP_CITIES`. This wouldn't work
@ -1812,8 +1812,8 @@ They can represent complex structures, and this is used quite often since there
are no globals:
-->
```v ignore
println('Top cities: $top_cities.filter(.usa)')
```v oksyntax
println('Top cities: ${top_cities.filter(.usa)}')
```
## Builtin functions
@ -1835,12 +1835,16 @@ fn print_backtrace() // print backtraces on stderr
`println` is a simple yet powerful builtin function, that can print anything:
strings, numbers, arrays, maps, structs.
```v nofmt
struct User{ name string age int }
```v
struct User {
name string
age int
}
println(1) // "1"
println('hi') // "hi"
println([1,2,3]) // "[1, 2, 3]"
println(User{name:'Bob', age:20}) // "User{name:'Bob', age:20}"
println([1, 2, 3]) // "[1, 2, 3]"
println(User{ name: 'Bob', age: 20 }) // "User{name:'Bob', age:20}"
```
## Custom print of types
@ -2157,10 +2161,10 @@ That's why you have to declare a `mut` before the `is` expression:
```v ignore
if mut w is Mars {
assert typeof(w).name == 'Mars'
if w.dust_storm() {
println('bad weather!')
}
assert typeof(w).name == 'Mars'
if w.dust_storm() {
println('bad weather!')
}
}
```
Otherwise `w` would keep its original type.

View File

@ -4,40 +4,40 @@ A V module for designing terminal UI apps
#### Quickstart
```v nofmt
```v
import term.ui as tui
struct App {
mut:
tui &tui.Context = 0
tui &tui.Context = 0
}
fn event(e &tui.Event, x voidptr) {
mut app := &App(x)
println(e)
mut app := &App(x)
println(e)
}
fn frame(x voidptr) {
mut app := &App(x)
mut app := &App(x)
app.tui.clear()
app.tui.set_bg_color(r: 63, g: 81, b: 181)
app.tui.draw_rect(20, 6, 41, 10)
app.tui.draw_text(24, 8, 'Hello from V!')
app.tui.set_cursor_position(0, 0)
app.tui.clear()
app.tui.set_bg_color(r: 63, g: 81, b: 181)
app.tui.draw_rect(20, 6, 41, 10)
app.tui.draw_text(24, 8, 'Hello from V!')
app.tui.set_cursor_position(0, 0)
app.tui.reset()
app.tui.flush()
app.tui.reset()
app.tui.flush()
}
mut app := &App{}
app.tui = tui.init(
user_data: app,
event_fn: event,
frame_fn: frame
hide_cursor: true
user_data: app
event_fn: event
frame_fn: frame
hide_cursor: true
)
app.tui.run()
app.tui.run() ?
```
See the `/examples/term.ui/` folder for more usage examples.

View File

@ -4,51 +4,51 @@
`x.json2` is an experimental JSON parser written from scratch on V.
## Usage
```v oksyntax nofmt
```v oksyntax
import x.json2
import net.http
fn main() {
// Decoding
resp := http.get('https://example.com')?
// Decoding
resp := http.get('https://example.com') ?
// raw decode
raw_person := json2.raw_decode(resp.text)?
// raw decode
raw_person := json2.raw_decode(resp.text) ?
// Casting `Any` type / Navigating
person := raw_person.as_map()
name := person['name'].str() // Bob
age := person['age'].int() // 19
pi := person['pi'].f64() // 3.14....
// Casting `Any` type / Navigating
person := raw_person.as_map()
name := person['name'].str() // Bob
age := person['age'].int() // 19
pi := person['pi'].f64() // 3.14....
// Constructing an `Any` type
mut me := map[string]json2.Any
me['name'] = 'Bob'
me['age'] = 18
// Constructing an `Any` type
mut me := map[string]json2.Any{}
me['name'] = 'Bob'
me['age'] = 18
mut arr := []json2.Any
arr << 'rock'
arr << 'papers'
arr << json2.null
arr << 12
mut arr := []json2.Any{}
arr << 'rock'
arr << 'papers'
arr << json2.null
arr << 12
me['interests'] = arr
me['interests'] = arr
mut pets := map[string]json2.Any
pets['Sam'] = 'Maltese Shitzu'
me['pets'] = pets
mut pets := map[string]json2.Any{}
pets['Sam'] = 'Maltese Shitzu'
me['pets'] = pets
// Stringify to JSON
println(me.str())
//{
// "name":"Bob",
// "age":18,
// "interests":["rock","papers","scissors",null,12],
// "pets":{"Sam":"Maltese"}
//}
// Stringify to JSON
println(me.str())
//{
// "name":"Bob",
// "age":18,
// "interests":["rock","papers","scissors",null,12],
// "pets":{"Sam":"Maltese"}
//}
// Encode a struct/type to JSON
encoded_json := json2.encode<Person>(person2)
// Encode a struct/type to JSON
encoded_json := json2.encode<Person>(person2)
}
```
## Using `decode<T>` and `encode<T>`