Commit 589ec6b5 by Iwasaki Yudai

Handle hterm preferences with better care

1 parent 86151f1a
Showing with 159 additions and 43 deletions
...@@ -29,15 +29,15 @@ ...@@ -29,15 +29,15 @@
"Rev": "56912fb08d85084aa318edcf2bba735b97cf35c5" "Rev": "56912fb08d85084aa318edcf2bba735b97cf35c5"
}, },
{ {
"ImportPath": "github.com/hashicorp/hcl",
"Rev": "54864211433d45cb780682431585b3e573b49e4a"
},
{
"ImportPath": "github.com/kr/pty", "ImportPath": "github.com/kr/pty",
"Comment": "release.r56-28-g5cf931e", "Comment": "release.r56-28-g5cf931e",
"Rev": "5cf931ef8f76dccd0910001d74a58a7fca84a83d" "Rev": "5cf931ef8f76dccd0910001d74a58a7fca84a83d"
}, },
{ {
"ImportPath": "github.com/yudai/hcl",
"Rev": "7227a719113b77a78318a43dba44951556ff9e3d"
},
{
"ImportPath": "github.com/yudai/umutex", "ImportPath": "github.com/yudai/umutex",
"Rev": "18216d265c6bc72c3bb0ad9c8103d47d530b7003" "Rev": "18216d265c6bc72c3bb0ad9c8103d47d530b7003"
} }
......
...@@ -8,7 +8,7 @@ import ( ...@@ -8,7 +8,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/hashicorp/hcl/hcl" "github.com/yudai/hcl/hcl"
) )
// This is the tag to use with structures to have settings for HCL // This is the tag to use with structures to have settings for HCL
...@@ -173,6 +173,8 @@ func (d *decoder) decodeInterface(name string, o *hcl.Object, result reflect.Val ...@@ -173,6 +173,8 @@ func (d *decoder) decodeInterface(name string, o *hcl.Object, result reflect.Val
set = reflect.Indirect(reflect.New(reflect.TypeOf(result))) set = reflect.Indirect(reflect.New(reflect.TypeOf(result)))
case hcl.ValueTypeString: case hcl.ValueTypeString:
set = reflect.Indirect(reflect.New(reflect.TypeOf(""))) set = reflect.Indirect(reflect.New(reflect.TypeOf("")))
case hcl.ValueTypeNil:
return nil
default: default:
return fmt.Errorf( return fmt.Errorf(
"%s: cannot decode into interface: %T", "%s: cannot decode into interface: %T",
...@@ -259,6 +261,10 @@ func (d *decoder) decodeMap(name string, o *hcl.Object, result reflect.Value) er ...@@ -259,6 +261,10 @@ func (d *decoder) decodeMap(name string, o *hcl.Object, result reflect.Value) er
func (d *decoder) decodePtr(name string, o *hcl.Object, result reflect.Value) error { func (d *decoder) decodePtr(name string, o *hcl.Object, result reflect.Value) error {
// Create an element of the concrete (non pointer) type and decode // Create an element of the concrete (non pointer) type and decode
// into that. Then set the value of the pointer to this type. // into that. Then set the value of the pointer to this type.
switch o.Type {
case hcl.ValueTypeNil:
// NIL
default:
resultType := result.Type() resultType := result.Type()
resultElemType := resultType.Elem() resultElemType := resultType.Elem()
val := reflect.New(resultElemType) val := reflect.New(resultElemType)
...@@ -267,6 +273,7 @@ func (d *decoder) decodePtr(name string, o *hcl.Object, result reflect.Value) er ...@@ -267,6 +273,7 @@ func (d *decoder) decodePtr(name string, o *hcl.Object, result reflect.Value) er
} }
result.Set(val) result.Set(val)
}
return nil return nil
} }
......
...@@ -85,7 +85,7 @@ func (x *hclLex) Lex(yylval *hclSymType) int { ...@@ -85,7 +85,7 @@ func (x *hclLex) Lex(yylval *hclSymType) int {
case '-': case '-':
return MINUS return MINUS
case ',': case ',':
return x.lexComma() return COMMA
case '=': case '=':
return EQUAL return EQUAL
case '[': case '[':
...@@ -166,27 +166,6 @@ func (x *hclLex) consumeComment(c rune) bool { ...@@ -166,27 +166,6 @@ func (x *hclLex) consumeComment(c rune) bool {
} }
} }
// lexComma reads the comma
func (x *hclLex) lexComma() int {
for {
c := x.peek()
// Consume space
if unicode.IsSpace(c) {
x.next()
continue
}
if c == ']' {
return COMMAEND
}
break
}
return COMMA
}
// lexId lexes an identifier // lexId lexes an identifier
func (x *hclLex) lexId(yylval *hclSymType) int { func (x *hclLex) lexId(yylval *hclSymType) int {
var b bytes.Buffer var b bytes.Buffer
...@@ -224,6 +203,8 @@ func (x *hclLex) lexId(yylval *hclSymType) int { ...@@ -224,6 +203,8 @@ func (x *hclLex) lexId(yylval *hclSymType) int {
case "false": case "false":
yylval.b = false yylval.b = false
return BOOL return BOOL
case "null":
return NULL
} }
return IDENTIFIER return IDENTIFIER
......
...@@ -36,7 +36,7 @@ func TestLex(t *testing.T) { ...@@ -36,7 +36,7 @@ func TestLex(t *testing.T) {
"list.hcl", "list.hcl",
[]int{ []int{
IDENTIFIER, EQUAL, LEFTBRACKET, IDENTIFIER, EQUAL, LEFTBRACKET,
NUMBER, COMMA, NUMBER, COMMA, STRING, NUMBER, COMMA, NUMBER, COMMA, STRING, COMMA, BOOL,
RIGHTBRACKET, lexEOF, RIGHTBRACKET, lexEOF,
}, },
}, },
...@@ -63,6 +63,24 @@ func TestLex(t *testing.T) { ...@@ -63,6 +63,24 @@ func TestLex(t *testing.T) {
RIGHTBRACE, lexEOF, RIGHTBRACE, lexEOF,
}, },
}, },
{
"array_comment.hcl",
[]int{
IDENTIFIER, EQUAL, LEFTBRACKET,
STRING, COMMA,
STRING, COMMA,
RIGHTBRACKET, lexEOF,
},
},
{
"null.hcl",
[]int{
IDENTIFIER, EQUAL, NULL,
IDENTIFIER, EQUAL, LEFTBRACKET, NUMBER, COMMA, NULL, COMMA, NUMBER, RIGHTBRACKET,
IDENTIFIER, LEFTBRACE, IDENTIFIER, EQUAL, NULL, RIGHTBRACE,
lexEOF,
},
},
} }
for _, tc := range cases { for _, tc := range cases {
......
...@@ -29,9 +29,10 @@ import ( ...@@ -29,9 +29,10 @@ import (
%token <b> BOOL %token <b> BOOL
%token <f> FLOAT %token <f> FLOAT
%token <num> NUMBER %token <num> NUMBER
%token <str> COMMA COMMAEND IDENTIFIER EQUAL NEWLINE STRING MINUS %token <str> COMMA IDENTIFIER EQUAL NEWLINE STRING MINUS
%token <str> LEFTBRACE RIGHTBRACE LEFTBRACKET RIGHTBRACKET PERIOD %token <str> LEFTBRACE RIGHTBRACE LEFTBRACKET RIGHTBRACKET PERIOD
%token <str> EPLUS EMINUS %token <str> EPLUS EMINUS
%token <str> NULL
%% %%
...@@ -96,6 +97,13 @@ objectitem: ...@@ -96,6 +97,13 @@ objectitem:
Value: $3, Value: $3,
} }
} }
| objectkey EQUAL NULL
{
$$ = &Object{
Key: $1,
Type: ValueTypeNil,
}
}
| objectkey EQUAL STRING | objectkey EQUAL STRING
{ {
$$ = &Object{ $$ = &Object{
...@@ -152,6 +160,10 @@ list: ...@@ -152,6 +160,10 @@ list:
{ {
$$ = $2 $$ = $2
} }
| LEFTBRACKET listitems COMMA RIGHTBRACKET
{
$$ = $2
}
| LEFTBRACKET RIGHTBRACKET | LEFTBRACKET RIGHTBRACKET
{ {
$$ = nil $$ = nil
...@@ -166,10 +178,6 @@ listitems: ...@@ -166,10 +178,6 @@ listitems:
{ {
$$ = append($1, $3) $$ = append($1, $3)
} }
| listitems COMMAEND
{
$$ = $1
}
listitem: listitem:
number number
...@@ -183,6 +191,20 @@ listitem: ...@@ -183,6 +191,20 @@ listitem:
Value: $1, Value: $1,
} }
} }
| BOOL
{
$$ = &Object{
Type: ValueTypeBool,
Value: $1,
}
}
| NULL
{
$$ = &Object{
Type: ValueTypeNil,
}
}
number: number:
int int
......
...@@ -59,6 +59,14 @@ func TestParse(t *testing.T) { ...@@ -59,6 +59,14 @@ func TestParse(t *testing.T) {
"types.hcl", "types.hcl",
false, false,
}, },
{
"array_comment.hcl",
false,
},
{
"null.hcl",
false,
},
} }
for _, tc := range cases { for _, tc := range cases {
......
foo = null
bar = [1, null, 3]
baz {
foo = null
}
...@@ -5,3 +5,4 @@ foo = -12 ...@@ -5,3 +5,4 @@ foo = -12
bar = 3.14159 bar = 3.14159
foo = true foo = true
bar = false bar = false
baz = null
...@@ -3,7 +3,7 @@ package json ...@@ -3,7 +3,7 @@ package json
import ( import (
"sync" "sync"
"github.com/hashicorp/hcl/hcl" "github.com/yudai/hcl/hcl"
"github.com/hashicorp/go-multierror" "github.com/hashicorp/go-multierror"
) )
......
...@@ -3,8 +3,8 @@ package hcl ...@@ -3,8 +3,8 @@ package hcl
import ( import (
"fmt" "fmt"
"github.com/hashicorp/hcl/hcl" "github.com/yudai/hcl/hcl"
"github.com/hashicorp/hcl/json" "github.com/yudai/hcl/json"
) )
// Parse parses the given input and returns the root object. // Parse parses the given input and returns the root object.
......
...@@ -23,8 +23,8 @@ import ( ...@@ -23,8 +23,8 @@ import (
"github.com/braintree/manners" "github.com/braintree/manners"
"github.com/elazarl/go-bindata-assetfs" "github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/hashicorp/hcl"
"github.com/kr/pty" "github.com/kr/pty"
"github.com/yudai/hcl"
"github.com/yudai/umutex" "github.com/yudai/umutex"
) )
...@@ -64,7 +64,8 @@ type Options struct { ...@@ -64,7 +64,8 @@ type Options struct {
ReconnectTime int `hcl:"reconnect_time"` ReconnectTime int `hcl:"reconnect_time"`
Once bool `hcl:"once"` Once bool `hcl:"once"`
PermitArguments bool `hcl:"permit_arguments"` PermitArguments bool `hcl:"permit_arguments"`
Preferences map[string]interface{} `hcl:"preferences"` Preferences HtermPrefernces `hcl:"preferences"`
RawPreferences map[string]interface{} `hcl:"preferences"`
} }
var Version = "0.0.11" var Version = "0.0.11"
...@@ -87,7 +88,7 @@ var DefaultOptions = Options{ ...@@ -87,7 +88,7 @@ var DefaultOptions = Options{
EnableReconnect: false, EnableReconnect: false,
ReconnectTime: 10, ReconnectTime: 10,
Once: false, Once: false,
Preferences: make(map[string]interface{}), Preferences: HtermPrefernces{},
} }
func New(command []string, options *Options) (*App, error) { func New(command []string, options *Options) (*App, error) {
......
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
"syscall" "syscall"
"unsafe" "unsafe"
"github.com/fatih/structs"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
...@@ -128,11 +129,20 @@ func (context *clientContext) sendInitialize() error { ...@@ -128,11 +129,20 @@ func (context *clientContext) sendInitialize() error {
return err return err
} }
prefStruct := structs.New(context.app.options.Preferences)
prefMap := prefStruct.Map()
htermPrefs := make(map[string]interface{}) htermPrefs := make(map[string]interface{})
for key, value := range context.app.options.Preferences { for key, value := range prefMap {
htermPrefs[strings.Replace(key, "_", "-", -1)] = value rawKey := prefStruct.Field(key).Tag("hcl")
if _, ok := context.app.options.RawPreferences[rawKey]; ok {
htermPrefs[strings.Replace(rawKey, "_", "-", -1)] = value
} }
prefs, _ := json.Marshal(htermPrefs) }
prefs, err := json.Marshal(htermPrefs)
if err != nil {
return err
}
if err := context.write(append([]byte{SetPreferences}, prefs...)); err != nil { if err := context.write(append([]byte{SetPreferences}, prefs...)); err != nil {
return err return err
} }
......
package app
type HtermPrefernces struct {
AltGrMode *string `hcl:"alt_gr_mode"`
AltBackspaceIsMetaBackspace bool `hcl:"alt_backspace_is_meta_backspace"`
AltIsMeta bool `hcl:"alt_is_meta"`
AltSendsWhat string `hcl:"alt_sends_what"`
AudibleBellSound string `hcl:"audible_bell_sound"`
DesktopNotificationBell bool `hcl:"desktop_notification_bell"`
BackgroundColor string `hcl:"background_color"`
BackgroundImage string `hcl:"background_image"`
BackgroundSize string `hcl:"background_size"`
BackgroundPosition string `hcl:"background_position"`
BackspaceSendsBackspace bool `hcl:"backspace_sends_backspace"`
CharacterMapOverrides map[string]map[string]string `hcl:"character_map_overrides"`
CloseOnExit bool `hcl:"close_on_exit"`
CursorBlink bool `hcl:"cursor_blink"`
CursorBlinkCycle [2]int `hcl:"cursor_blink_cycle"`
CursorColor string `hcl:"cursor_color"`
ColorPaletteOverrides []*string `hcl:"color_palette_overrides"`
CopyOnSelect bool `hcl:"copy_on_select"`
UseDefaultWindowCopy bool `hcl:"use_default_window_copy"`
ClearSelectionAfterCopy bool `hcl:"clear_selection_after_copy"`
CtrlPlusMinusZeroZoom bool `hcl:"ctrl_plus_minus_zero_zoom"`
CtrlCCopy bool `hcl:"ctrl_c_copy"`
CtrlVPaste bool `hcl:"ctrl_v_paste"`
EastAsianAmbiguousAsTwoColumn bool `hcl:"east_asian_ambiguous_as_two_column"`
Enable8BitControl *bool `hcl:"enable_8_bit_control"`
EnableBold *bool `hcl:"enable_bold"`
EnableBoldAsBright bool `hcl:"enable_bold_as_bright"`
EnableClipboardNotice bool `hcl:"enable_clipboard_notice"`
EnableClipboardWrite bool `hcl:"enable_clipboard_write"`
EnableDec12 bool `hcl:"enable_dec12"`
Environment map[string]string `hcl:"environment"`
FontFamily string `hcl:"font_family"`
FontSize int `hcl:"font_size"`
FontSmoothing string `hcl:"font_smoothing"`
ForegroundColor string `hcl:"foreground_color"`
HomeKeysScroll bool `hcl:"home_keys_scroll"`
Keybindings map[string]string `hcl:"keybindings"`
MaxStringSequence int `hcl:"max_string_sequence"`
MediaKeysAreFkeys bool `hcl:"media_keys_are_fkeys"`
MetaSendsEscape bool `hcl:"meta_sends_escape"`
MousePasteButton *int `hcl:"mouse_paste_button"`
PageKeysScroll bool `hcl:"page_keys_scroll"`
PassAltNumber *bool `hcl:"pass_alt_number"`
PassCtrlNumber *bool `hcl:"pass_ctrl_number"`
PassMetaNumber *bool `hcl:"pass_meta_number"`
PassMetaV bool `hcl:"pass_meta_v"`
ReceiveEncoding string `hcl:"receive_encoding"`
ScrollOnKeystroke bool `hcl:"scroll_on_keystroke"`
ScrollOnOutput bool `hcl:"scroll_on_output"`
ScrollbarVisible bool `hcl:"scrollbar_visible"`
ScrollWheelMoveMultiplier int `hcl:"scroll_wheel_move_multiplier"`
SendEncoding string `hcl:"send_encoding"`
ShiftInsertPaste bool `hcl:"shift_insert_paste"`
UserCss string `hcl:"user_css"`
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!