Skip to content

Commit 6666bb4

Browse files
committed
Merge branch 'feature/append-mode' into develop
2 parents 2be4908 + 4e20f27 commit 6666bb4

File tree

13 files changed

+225
-10
lines changed

13 files changed

+225
-10
lines changed

src/common/default_configs.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { LocaleName } from '@root/common/i18n'
22
import { FolderModeType } from './data.d'
33

4+
export type WriteModeType = null | 'overwrite' | 'append'
45
export type ThemeType = 'light' | 'dark' | 'system'
56
export type ProtocolType = 'http' | 'https'
67
export type DefaultLocaleType = LocaleName | undefined
@@ -11,6 +12,7 @@ const configs = {
1112
left_panel_width: 270,
1213

1314
// preferences
15+
write_mode: null as WriteModeType,
1416
history_limit: 50,
1517
locale: undefined as DefaultLocaleType,
1618
theme: 'light' as ThemeType,

src/common/events.ts

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default {
2323
show_about: 'show_about',
2424
show_history: 'show_history',
2525
show_preferences: 'show_preferences',
26+
show_set_write_mode: 'show_set_write_mode',
2627
show_source: 'show_source',
2728
show_sudo_password_input: 'show_sudo_password_input',
2829
system_hosts_updated: 'system_hosts_updated',

src/common/i18n/languages/en.ts

+9
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
export default {
77
_app_name: 'SwitchHosts',
8+
_key: 'en',
89
_name: 'English',
910
about: 'About',
1011
acknowledgement: 'Acknowledgement',
1112
advanced: 'Advanced',
1213
all: 'All',
14+
append: 'Append',
1315
auto_refresh: 'Auto refresh',
1416
btn_cancel: 'Cancel',
1517
btn_ok: 'OK',
@@ -108,6 +110,7 @@ export default {
108110
next: 'Next',
109111
no_access_to_hosts: 'No permission to write to the Hosts file.',
110112
no_record: 'No record',
113+
overwrite: 'Overwrite',
111114
password: 'Password',
112115
paste: 'Paste',
113116
port: 'Port',
@@ -175,6 +178,12 @@ export default {
175178
where_is_my_data: 'Where is my data stored?',
176179
where_is_my_hosts: 'Where is my hosts file?',
177180
window: 'Window',
181+
write_mode: 'Write mode',
182+
write_mode_append_help:
183+
'Append the new records to the end of the system hosts file.',
184+
write_mode_overwrite_help:
185+
'Overwrite the system hosts file with the new records.',
186+
write_mode_set: 'Set the write mode',
178187
your_data_is: 'Your data files are stored in:',
179188
your_hosts_file_is: 'Your hosts file is located at:',
180189
zoom: 'Zoom',

src/common/i18n/languages/fr.ts

+9
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
export default {
77
_app_name: 'SwitchHosts',
8+
_key: 'fr',
89
_name: 'Français',
910
about: 'À propos',
1011
acknowledgement: 'Remerciements',
1112
advanced: 'Avancé',
1213
all: 'Tout',
14+
append: 'Ajouter',
1315
auto_refresh: 'Rafraîchissement automatique',
1416
btn_cancel: 'Annuler',
1517
btn_ok: 'OK',
@@ -108,6 +110,7 @@ export default {
108110
next: 'Suivant',
109111
no_access_to_hosts: 'Aucune autorisation pour écrire dans le fichier hosts.',
110112
no_record: 'Aucun enregistrement',
113+
overwrite: 'Écraser',
111114
password: 'Mot de passe',
112115
paste: 'Coller',
113116
port: 'Port',
@@ -176,6 +179,12 @@ export default {
176179
where_is_my_data: 'Où sont stockées mes données ?',
177180
where_is_my_hosts: 'Où est mon fichier hosts ?',
178181
window: 'Fenêtre',
182+
write_mode: "Mode d'écriture",
183+
write_mode_append_help:
184+
"Ajoutez les nouveaux enregistrements à la fin du fichier d'hôtes système.",
185+
write_mode_overwrite_help:
186+
"Écrasez le fichier d'hôtes système avec les nouveaux enregistrements.",
187+
write_mode_set: "Définir le mode d'écriture",
179188
your_data_is: 'Les fichiers contenant vos données sont stockés ici :',
180189
your_hosts_file_is: 'Votre fichier hosts est situé ici :',
181190
zoom: 'Zoom',

src/common/i18n/languages/zh.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import { LanguageDict } from '@root/common/types'
77

88
const lang: LanguageDict = {
99
_app_name: 'SwitchHosts',
10+
_key: 'zh',
1011
_name: '中文',
1112
about: '关于',
1213
acknowledgement: '特别致谢',
1314
advanced: '高级',
1415
all: '全部',
16+
append: '追加',
1517
auto_refresh: '自动刷新',
1618
btn_cancel: '取消',
1719
btn_ok: '确定',
@@ -56,6 +58,7 @@ const lang: LanguageDict = {
5658
help: 'Help',
5759
hide: '隐藏',
5860
hide_at_launch: '启动时隐藏',
61+
hide_dock_icon: '隐藏任务栏(Dock)图标',
5962
hide_history: '隐藏历史记录',
6063
hide_others: '隐藏其他',
6164
homepage: '主页',
@@ -92,7 +95,6 @@ const lang: LanguageDict = {
9295
migrate_confirm:
9396
'SwitchHosts v4.0 使用了新的数据存储格式,是否迁移旧数据为新格式?',
9497
migrate_data: '迁移数据',
95-
hide_dock_icon: '隐藏任务栏(Dock)图标',
9698
minimize: '最小化',
9799
minute: '分钟',
98100
minutes: '分钟',
@@ -106,6 +108,7 @@ const lang: LanguageDict = {
106108
next: '下一个',
107109
no_access_to_hosts: '没有写入 Hosts 文件的权限。',
108110
no_record: '没有记录',
111+
overwrite: '覆盖',
109112
password: '密码',
110113
paste: '粘贴',
111114
port: '端口',
@@ -171,6 +174,10 @@ const lang: LanguageDict = {
171174
where_is_my_data: '我的数据存储在哪里?',
172175
where_is_my_hosts: '我的 hosts 文件在哪里?',
173176
window: 'Window',
177+
write_mode: '写入模式',
178+
write_mode_append_help: '新记录将追加到现有系统 hosts 文件末尾。',
179+
write_mode_overwrite_help: '新记录将覆盖现有系统 hosts 文件。',
180+
write_mode_set: '设置写入模式',
174181
your_data_is: '你的数据在:',
175182
your_hosts_file_is: '你的 hosts 文件在:',
176183
zoom: '缩放',

src/main/actions/hosts/setSystemHosts.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ interface IWriteResult {
3333
new_content?: string
3434
}
3535

36+
const CONTENT_START = '# --- SWITCHHOSTS_CONTENT_START ---'
37+
3638
let sudo_pswd: string = ''
3739

3840
const checkAccess = async (fn: string): Promise<boolean> => {
@@ -160,7 +162,7 @@ const write = async (
160162

161163
try {
162164
await fs.promises.writeFile(sys_hosts_path, content, 'utf-8')
163-
} catch (e) {
165+
} catch (e: any) {
164166
console.error(e)
165167
let code = 'fail'
166168
if (e.code === 'EPERM' || e.message.include('operation not permitted')) {
@@ -177,10 +179,31 @@ const write = async (
177179
return { success: true, old_content, new_content: content }
178180
}
179181

182+
const makeAppendContent = async (content: string): Promise<string> => {
183+
const sys_hosts_path = await getPathOfSystemHosts()
184+
const old_content = await fs.promises.readFile(sys_hosts_path, 'utf-8')
185+
186+
let index = old_content.indexOf(CONTENT_START)
187+
let new_content =
188+
index > -1 ? old_content.substring(0, index).trimEnd() : old_content
189+
190+
if (!content) {
191+
return new_content + '\n'
192+
}
193+
194+
return `${new_content}\n\n${CONTENT_START}\n\n${content}`
195+
}
196+
180197
const setSystemHosts = async (
181198
content: string,
182199
options?: IHostsWriteOptions,
183200
): Promise<IWriteResult> => {
201+
let write_mode = await configGet('write_mode')
202+
console.log(`write_mode: ${write_mode}`)
203+
if (write_mode === 'append') {
204+
content = await makeAppendContent(content)
205+
}
206+
184207
let result = await write(content, options)
185208
let { success, old_content } = result
186209

src/renderer/components/List/index.tsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ const List = (props: Props) => {
5757
}, [hosts_data])
5858

5959
const onToggleItem = async (id: string, on: boolean) => {
60+
console.log(`writeMode: ${configs?.write_mode}`)
6061
console.log(`toggle hosts #${id} as ${on ? 'on' : 'off'}`)
62+
63+
if (!configs?.write_mode) {
64+
agent.broadcast(events.show_set_write_mode, { id, on })
65+
return
66+
}
67+
6168
const new_list = setOnStateOfItem(
6269
hosts_data.list,
6370
id,
@@ -134,7 +141,7 @@ const List = (props: Props) => {
134141
}
135142

136143
if (!is_tray) {
137-
useOnBroadcast(events.toggle_item, onToggleItem, [hosts_data])
144+
useOnBroadcast(events.toggle_item, onToggleItem, [hosts_data, configs])
138145
useOnBroadcast(events.write_hosts_to_system, writeHostsToSystem, [
139146
hosts_data,
140147
])

src/renderer/components/Pref/General.tsx

+40-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
RadioGroup,
1717
Select,
1818
VStack,
19-
Stack
19+
Stack,
2020
} from '@chakra-ui/react'
2121
import { agent } from '@renderer/core/agent'
2222
import { http_api_port } from '@root/common/constants'
@@ -67,8 +67,38 @@ const General = (props: IProps) => {
6767
</HStack>
6868
</FormControl>
6969

70+
<FormControl>
71+
<HStack alignItems={'flex-start'}>
72+
<FormLabel w={label_width}>{lang.write_mode}</FormLabel>
73+
<VStack align="left">
74+
<RadioGroup
75+
value={data.write_mode || ''}
76+
onChange={(v) =>
77+
onChange({
78+
write_mode: v as ConfigsType['write_mode'],
79+
})
80+
}
81+
>
82+
<HStack spacing={10}>
83+
<Radio value="append">
84+
<Box>{lang.append}</Box>
85+
</Radio>
86+
<Radio value="overwrite">
87+
<Box>{lang.overwrite}</Box>
88+
</Radio>
89+
</HStack>
90+
</RadioGroup>
91+
<FormHelperText maxW={'350px'}>
92+
{data.write_mode === 'append' && lang.write_mode_append_help}
93+
{data.write_mode === 'overwrite' &&
94+
lang.write_mode_overwrite_help}
95+
</FormHelperText>
96+
</VStack>
97+
</HStack>
98+
</FormControl>
99+
70100
<FormControl pb={6}>
71-
<HStack>
101+
<HStack alignItems={'flex-start'}>
72102
<FormLabel w={label_width}>{lang.choice_mode}</FormLabel>
73103
<VStack align="left">
74104
<RadioGroup
@@ -90,7 +120,9 @@ const General = (props: IProps) => {
90120
</Radio>
91121
</HStack>
92122
</RadioGroup>
93-
<FormHelperText>{lang.choice_mode_desc}</FormHelperText>
123+
<FormHelperText maxW={'350px'}>
124+
{lang.choice_mode_desc}
125+
</FormHelperText>
94126
</VStack>
95127
</HStack>
96128
</FormControl>
@@ -163,9 +195,11 @@ const General = (props: IProps) => {
163195
</FormHelperText>
164196
<Stack pl={6} mt={1} spacing={1}>
165197
<Checkbox
166-
isDisabled={!data.http_api_on}
167-
isChecked={data.http_api_only_local}
168-
onChange={(e) => onChange({ http_api_only_local: e.target.checked })}
198+
isDisabled={!data.http_api_on}
199+
isChecked={data.http_api_only_local}
200+
onChange={(e) =>
201+
onChange({ http_api_only_local: e.target.checked })
202+
}
169203
>
170204
{lang.http_api_only_local}
171205
</Checkbox>

src/renderer/components/Pref/styles.less

+9
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,12 @@
44
text-decoration: underline;
55
color: inherit;
66
}
7+
8+
:global {
9+
label {
10+
span.chakra-radio__control {
11+
width: 16px;
12+
height: 16px;
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@import "../styles/common";
2+
3+
.root {
4+
}
5+
6+
.label {
7+
margin: 10px 0 20px 0;
8+
}

0 commit comments

Comments
 (0)